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.

171 lines
4.6 KiB

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 leader, e := t.Leader(); e == nil {
  38. return leader == t.RaftServer.Name()
  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 {
  71. vid := t.GetMaxVolumeId()
  72. next := vid.Next()
  73. go t.RaftServer.Do(NewMaxVolumeIdCommand(next))
  74. return next
  75. }
  76. func (t *Topology) HasWritableVolume(option *VolumeGrowOption) bool {
  77. vl := t.GetVolumeLayout(option.Collection, option.ReplicaPlacement, option.Ttl)
  78. return vl.GetActiveVolumeCount(option) > 0
  79. }
  80. func (t *Topology) PickForWrite(count uint64, option *VolumeGrowOption) (string, uint64, *DataNode, error) {
  81. vid, count, datanodes, err := t.GetVolumeLayout(option.Collection, option.ReplicaPlacement, option.Ttl).PickForWrite(count, option)
  82. if err != nil || datanodes.Length() == 0 {
  83. return "", 0, nil, errors.New("No writable volumes available!")
  84. }
  85. fileId, count := t.Sequence.NextFileId(count)
  86. return storage.NewFileId(*vid, fileId, rand.Uint32()).String(), count, datanodes.Head(), nil
  87. }
  88. func (t *Topology) GetVolumeLayout(collectionName string, rp *storage.ReplicaPlacement, ttl *storage.TTL) *VolumeLayout {
  89. return t.collectionMap.Get(collectionName, func() interface{} {
  90. return NewCollection(collectionName, t.volumeSizeLimit)
  91. }).(*Collection).GetOrCreateVolumeLayout(rp, ttl)
  92. }
  93. func (t *Topology) FindCollection(collectionName string) (*Collection, bool) {
  94. c, hasCollection := t.collectionMap.Find(collectionName)
  95. if !hasCollection {
  96. return nil, false
  97. }
  98. return c.(*Collection), hasCollection
  99. }
  100. func (t *Topology) DeleteCollection(collectionName string) {
  101. t.collectionMap.Delete(collectionName)
  102. }
  103. func (t *Topology) RegisterVolumeLayout(v storage.VolumeInfo, dn *DataNode) {
  104. t.GetVolumeLayout(v.Collection, v.ReplicaPlacement, v.Ttl).RegisterVolume(&v, dn)
  105. }
  106. func (t *Topology) UnRegisterVolumeLayout(v storage.VolumeInfo, dn *DataNode) {
  107. glog.Infof("removing volume info:%+v", v)
  108. volumeLayout := t.GetVolumeLayout(v.Collection, v.ReplicaPlacement, v.Ttl)
  109. volumeLayout.UnRegisterVolume(&v, dn)
  110. if volumeLayout.isEmpty() {
  111. t.DeleteCollection(v.Collection)
  112. }
  113. }
  114. func (t *Topology) GetOrCreateDataCenter(dcName string) *DataCenter {
  115. for _, c := range t.Children() {
  116. dc := c.(*DataCenter)
  117. if string(dc.Id()) == dcName {
  118. return dc
  119. }
  120. }
  121. dc := NewDataCenter(dcName)
  122. t.LinkChildNode(dc)
  123. return dc
  124. }
  125. func (t *Topology) SyncDataNodeRegistration(volumes []*master_pb.VolumeInformationMessage, dn *DataNode) (newVolumes, deletedVolumes []storage.VolumeInfo) {
  126. var volumeInfos []storage.VolumeInfo
  127. for _, v := range volumes {
  128. if vi, err := storage.NewVolumeInfo(v); err == nil {
  129. volumeInfos = append(volumeInfos, vi)
  130. } else {
  131. glog.V(0).Infof("Fail to convert joined volume information: %v", err)
  132. }
  133. }
  134. newVolumes, deletedVolumes = dn.UpdateVolumes(volumeInfos)
  135. for _, v := range volumeInfos {
  136. t.RegisterVolumeLayout(v, dn)
  137. }
  138. for _, v := range deletedVolumes {
  139. t.UnRegisterVolumeLayout(v, dn)
  140. }
  141. return
  142. }