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.

215 lines
5.9 KiB

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