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.

205 lines
5.5 KiB

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