You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

258 lines
7.1 KiB

6 years ago
6 years ago
6 years ago
10 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
7 years ago
  1. package topology
  2. import (
  3. "errors"
  4. "fmt"
  5. "math/rand"
  6. "sync"
  7. "github.com/chrislusf/raft"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  10. "github.com/chrislusf/seaweedfs/weed/sequence"
  11. "github.com/chrislusf/seaweedfs/weed/storage"
  12. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  13. "github.com/chrislusf/seaweedfs/weed/storage/super_block"
  14. "github.com/chrislusf/seaweedfs/weed/util"
  15. )
  16. type Topology struct {
  17. vacuumLockCounter int64
  18. NodeImpl
  19. collectionMap *util.ConcurrentReadMap
  20. ecShardMap map[needle.VolumeId]*EcShardLocations
  21. ecShardMapLock sync.RWMutex
  22. pulse int64
  23. volumeSizeLimit uint64
  24. replicationAsMin bool
  25. Sequence sequence.Sequencer
  26. chanFullVolumes chan storage.VolumeInfo
  27. Configuration *Configuration
  28. RaftServer raft.Server
  29. }
  30. func NewTopology(id string, seq sequence.Sequencer, volumeSizeLimit uint64, pulse int, replicationAsMin bool) *Topology {
  31. t := &Topology{}
  32. t.id = NodeId(id)
  33. t.nodeType = "Topology"
  34. t.NodeImpl.value = t
  35. t.children = make(map[NodeId]Node)
  36. t.collectionMap = util.NewConcurrentReadMap()
  37. t.ecShardMap = make(map[needle.VolumeId]*EcShardLocations)
  38. t.pulse = int64(pulse)
  39. t.volumeSizeLimit = volumeSizeLimit
  40. t.replicationAsMin = replicationAsMin
  41. t.Sequence = seq
  42. t.chanFullVolumes = make(chan storage.VolumeInfo)
  43. t.Configuration = &Configuration{}
  44. return t
  45. }
  46. func (t *Topology) IsLeader() bool {
  47. if t.RaftServer != nil {
  48. if t.RaftServer.State() == raft.Leader {
  49. return true
  50. }
  51. if t.RaftServer.Leader() == "" {
  52. return true
  53. }
  54. }
  55. return false
  56. }
  57. func (t *Topology) Leader() (string, error) {
  58. l := ""
  59. if t.RaftServer != nil {
  60. l = t.RaftServer.Leader()
  61. } else {
  62. return "", errors.New("Raft Server not ready yet!")
  63. }
  64. if l == "" {
  65. // We are a single node cluster, we are the leader
  66. return t.RaftServer.Name(), nil
  67. }
  68. return l, nil
  69. }
  70. func (t *Topology) Lookup(collection string, vid needle.VolumeId) (dataNodes []*DataNode) {
  71. //maybe an issue if lots of collections?
  72. if collection == "" {
  73. for _, c := range t.collectionMap.Items() {
  74. if list := c.(*Collection).Lookup(vid); list != nil {
  75. return list
  76. }
  77. }
  78. } else {
  79. if c, ok := t.collectionMap.Find(collection); ok {
  80. return c.(*Collection).Lookup(vid)
  81. }
  82. }
  83. if locations, found := t.LookupEcShards(vid); found {
  84. for _, loc := range locations.Locations {
  85. dataNodes = append(dataNodes, loc...)
  86. }
  87. return dataNodes
  88. }
  89. return nil
  90. }
  91. func (t *Topology) NextVolumeId() (needle.VolumeId, error) {
  92. vid := t.GetMaxVolumeId()
  93. next := vid.Next()
  94. if _, err := t.RaftServer.Do(NewMaxVolumeIdCommand(next)); err != nil {
  95. return 0, err
  96. }
  97. return next, nil
  98. }
  99. func (t *Topology) HasWritableVolume(option *VolumeGrowOption) bool {
  100. vl := t.GetVolumeLayout(option.Collection, option.ReplicaPlacement, option.Ttl)
  101. return vl.GetActiveVolumeCount(option) > 0
  102. }
  103. func (t *Topology) PickForWrite(count uint64, option *VolumeGrowOption) (string, uint64, *DataNode, error) {
  104. vid, count, datanodes, err := t.GetVolumeLayout(option.Collection, option.ReplicaPlacement, option.Ttl).PickForWrite(count, option)
  105. if err != nil {
  106. return "", 0, nil, fmt.Errorf("failed to find writable volumes for collection:%s replication:%s ttl:%s error: %v", option.Collection, option.ReplicaPlacement.String(), option.Ttl.String(), err)
  107. }
  108. if datanodes.Length() == 0 {
  109. return "", 0, nil, fmt.Errorf("no writable volumes available for collection:%s replication:%s ttl:%s", option.Collection, option.ReplicaPlacement.String(), option.Ttl.String())
  110. }
  111. fileId := t.Sequence.NextFileId(count)
  112. return needle.NewFileId(*vid, fileId, rand.Uint32()).String(), count, datanodes.Head(), nil
  113. }
  114. func (t *Topology) GetVolumeLayout(collectionName string, rp *super_block.ReplicaPlacement, ttl *needle.TTL) *VolumeLayout {
  115. return t.collectionMap.Get(collectionName, func() interface{} {
  116. return NewCollection(collectionName, t.volumeSizeLimit, t.replicationAsMin)
  117. }).(*Collection).GetOrCreateVolumeLayout(rp, ttl)
  118. }
  119. func (t *Topology) ListCollections(includeNormalVolumes, includeEcVolumes bool) (ret []string) {
  120. mapOfCollections := make(map[string]bool)
  121. for _, c := range t.collectionMap.Items() {
  122. mapOfCollections[c.(*Collection).Name] = true
  123. }
  124. if includeEcVolumes {
  125. t.ecShardMapLock.RLock()
  126. for _, ecVolumeLocation := range t.ecShardMap {
  127. mapOfCollections[ecVolumeLocation.Collection] = true
  128. }
  129. t.ecShardMapLock.RUnlock()
  130. }
  131. for k := range mapOfCollections {
  132. ret = append(ret, k)
  133. }
  134. return ret
  135. }
  136. func (t *Topology) FindCollection(collectionName string) (*Collection, bool) {
  137. c, hasCollection := t.collectionMap.Find(collectionName)
  138. if !hasCollection {
  139. return nil, false
  140. }
  141. return c.(*Collection), hasCollection
  142. }
  143. func (t *Topology) DeleteCollection(collectionName string) {
  144. t.collectionMap.Delete(collectionName)
  145. }
  146. func (t *Topology) RegisterVolumeLayout(v storage.VolumeInfo, dn *DataNode) {
  147. t.GetVolumeLayout(v.Collection, v.ReplicaPlacement, v.Ttl).RegisterVolume(&v, dn)
  148. }
  149. func (t *Topology) UnRegisterVolumeLayout(v storage.VolumeInfo, dn *DataNode) {
  150. glog.Infof("removing volume info:%+v", v)
  151. volumeLayout := t.GetVolumeLayout(v.Collection, v.ReplicaPlacement, v.Ttl)
  152. volumeLayout.UnRegisterVolume(&v, dn)
  153. if volumeLayout.isEmpty() {
  154. t.DeleteCollection(v.Collection)
  155. }
  156. }
  157. func (t *Topology) GetOrCreateDataCenter(dcName string) *DataCenter {
  158. for _, c := range t.Children() {
  159. dc := c.(*DataCenter)
  160. if string(dc.Id()) == dcName {
  161. return dc
  162. }
  163. }
  164. dc := NewDataCenter(dcName)
  165. t.LinkChildNode(dc)
  166. return dc
  167. }
  168. func (t *Topology) SyncDataNodeRegistration(volumes []*master_pb.VolumeInformationMessage, dn *DataNode) (newVolumes, deletedVolumes []storage.VolumeInfo) {
  169. // convert into in memory struct storage.VolumeInfo
  170. var volumeInfos []storage.VolumeInfo
  171. for _, v := range volumes {
  172. if vi, err := storage.NewVolumeInfo(v); err == nil {
  173. volumeInfos = append(volumeInfos, vi)
  174. } else {
  175. glog.V(0).Infof("Fail to convert joined volume information: %v", err)
  176. }
  177. }
  178. // find out the delta volumes
  179. var changedVolumes []storage.VolumeInfo
  180. newVolumes, deletedVolumes, changedVolumes = dn.UpdateVolumes(volumeInfos)
  181. for _, v := range newVolumes {
  182. t.RegisterVolumeLayout(v, dn)
  183. }
  184. for _, v := range deletedVolumes {
  185. t.UnRegisterVolumeLayout(v, dn)
  186. }
  187. for _, v := range changedVolumes {
  188. vl := t.GetVolumeLayout(v.Collection, v.ReplicaPlacement, v.Ttl)
  189. vl.ensureCorrectWritables(&v)
  190. }
  191. return
  192. }
  193. func (t *Topology) IncrementalSyncDataNodeRegistration(newVolumes, deletedVolumes []*master_pb.VolumeShortInformationMessage, dn *DataNode) {
  194. var newVis, oldVis []storage.VolumeInfo
  195. for _, v := range newVolumes {
  196. vi, err := storage.NewVolumeInfoFromShort(v)
  197. if err != nil {
  198. glog.V(0).Infof("NewVolumeInfoFromShort %v: %v", v, err)
  199. continue
  200. }
  201. newVis = append(newVis, vi)
  202. }
  203. for _, v := range deletedVolumes {
  204. vi, err := storage.NewVolumeInfoFromShort(v)
  205. if err != nil {
  206. glog.V(0).Infof("NewVolumeInfoFromShort %v: %v", v, err)
  207. continue
  208. }
  209. oldVis = append(oldVis, vi)
  210. }
  211. dn.DeltaUpdateVolumes(newVis, oldVis)
  212. for _, vi := range newVis {
  213. t.RegisterVolumeLayout(vi, dn)
  214. }
  215. for _, vi := range oldVis {
  216. t.UnRegisterVolumeLayout(vi, dn)
  217. }
  218. return
  219. }