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.

173 lines
4.6 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) FindCollection(collectionName string) (*Collection, bool) {
  96. c, hasCollection := t.collectionMap.Find(collectionName)
  97. if !hasCollection {
  98. return nil, false
  99. }
  100. return c.(*Collection), hasCollection
  101. }
  102. func (t *Topology) DeleteCollection(collectionName string) {
  103. t.collectionMap.Delete(collectionName)
  104. }
  105. func (t *Topology) RegisterVolumeLayout(v storage.VolumeInfo, dn *DataNode) {
  106. t.GetVolumeLayout(v.Collection, v.ReplicaPlacement, v.Ttl).RegisterVolume(&v, dn)
  107. }
  108. func (t *Topology) UnRegisterVolumeLayout(v storage.VolumeInfo, dn *DataNode) {
  109. glog.Infof("removing volume info:%+v", v)
  110. volumeLayout := t.GetVolumeLayout(v.Collection, v.ReplicaPlacement, v.Ttl)
  111. volumeLayout.UnRegisterVolume(&v, dn)
  112. if volumeLayout.isEmpty() {
  113. t.DeleteCollection(v.Collection)
  114. }
  115. }
  116. func (t *Topology) GetOrCreateDataCenter(dcName string) *DataCenter {
  117. for _, c := range t.Children() {
  118. dc := c.(*DataCenter)
  119. if string(dc.Id()) == dcName {
  120. return dc
  121. }
  122. }
  123. dc := NewDataCenter(dcName)
  124. t.LinkChildNode(dc)
  125. return dc
  126. }
  127. func (t *Topology) SyncDataNodeRegistration(volumes []*master_pb.VolumeInformationMessage, dn *DataNode) (newVolumes, deletedVolumes []storage.VolumeInfo) {
  128. var volumeInfos []storage.VolumeInfo
  129. for _, v := range volumes {
  130. if vi, err := storage.NewVolumeInfo(v); err == nil {
  131. volumeInfos = append(volumeInfos, vi)
  132. } else {
  133. glog.V(0).Infof("Fail to convert joined volume information: %v", err)
  134. }
  135. }
  136. newVolumes, deletedVolumes = dn.UpdateVolumes(volumeInfos)
  137. for _, v := range volumeInfos {
  138. t.RegisterVolumeLayout(v, dn)
  139. }
  140. for _, v := range deletedVolumes {
  141. t.UnRegisterVolumeLayout(v, dn)
  142. }
  143. return
  144. }