topology.go 6.7 KB

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