topology.go 6.8 KB

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