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.

180 lines
4.8 KiB

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