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.

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