topology.go 7.8 KB

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