topology.go 7.1 KB

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