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.

233 lines
6.4 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 []string) {
  105. mapOfCollections := make(map[string]bool)
  106. for _, c := range t.collectionMap.Items() {
  107. mapOfCollections[c.(*Collection).Name] = true
  108. }
  109. t.ecShardMapLock.RLock()
  110. for _, ecVolumeLocation := range t.ecShardMap {
  111. mapOfCollections[ecVolumeLocation.Collection] = true
  112. }
  113. t.ecShardMapLock.RUnlock()
  114. for k, _ := range mapOfCollections {
  115. ret = append(ret, k)
  116. }
  117. return ret
  118. }
  119. func (t *Topology) FindCollection(collectionName string) (*Collection, bool) {
  120. c, hasCollection := t.collectionMap.Find(collectionName)
  121. if !hasCollection {
  122. return nil, false
  123. }
  124. return c.(*Collection), hasCollection
  125. }
  126. func (t *Topology) DeleteCollection(collectionName string) {
  127. t.collectionMap.Delete(collectionName)
  128. }
  129. func (t *Topology) RegisterVolumeLayout(v storage.VolumeInfo, dn *DataNode) {
  130. t.GetVolumeLayout(v.Collection, v.ReplicaPlacement, v.Ttl).RegisterVolume(&v, dn)
  131. }
  132. func (t *Topology) UnRegisterVolumeLayout(v storage.VolumeInfo, dn *DataNode) {
  133. glog.Infof("removing volume info:%+v", v)
  134. volumeLayout := t.GetVolumeLayout(v.Collection, v.ReplicaPlacement, v.Ttl)
  135. volumeLayout.UnRegisterVolume(&v, dn)
  136. if volumeLayout.isEmpty() {
  137. t.DeleteCollection(v.Collection)
  138. }
  139. }
  140. func (t *Topology) GetOrCreateDataCenter(dcName string) *DataCenter {
  141. for _, c := range t.Children() {
  142. dc := c.(*DataCenter)
  143. if string(dc.Id()) == dcName {
  144. return dc
  145. }
  146. }
  147. dc := NewDataCenter(dcName)
  148. t.LinkChildNode(dc)
  149. return dc
  150. }
  151. func (t *Topology) SyncDataNodeRegistration(volumes []*master_pb.VolumeInformationMessage, dn *DataNode) (newVolumes, deletedVolumes []storage.VolumeInfo) {
  152. // convert into in memory struct storage.VolumeInfo
  153. var volumeInfos []storage.VolumeInfo
  154. for _, v := range volumes {
  155. if vi, err := storage.NewVolumeInfo(v); err == nil {
  156. volumeInfos = append(volumeInfos, vi)
  157. } else {
  158. glog.V(0).Infof("Fail to convert joined volume information: %v", err)
  159. }
  160. }
  161. // find out the delta volumes
  162. newVolumes, deletedVolumes = dn.UpdateVolumes(volumeInfos)
  163. for _, v := range newVolumes {
  164. t.RegisterVolumeLayout(v, dn)
  165. }
  166. for _, v := range deletedVolumes {
  167. t.UnRegisterVolumeLayout(v, dn)
  168. }
  169. return
  170. }
  171. func (t *Topology) IncrementalSyncDataNodeRegistration(newVolumes, deletedVolumes []*master_pb.VolumeShortInformationMessage, dn *DataNode) {
  172. var newVis, oldVis []storage.VolumeInfo
  173. for _, v := range newVolumes {
  174. vi, err := storage.NewVolumeInfoFromShort(v)
  175. if err != nil {
  176. glog.V(0).Infof("NewVolumeInfoFromShort %v: %v", v, err)
  177. continue
  178. }
  179. newVis = append(newVis, vi)
  180. }
  181. for _, v := range deletedVolumes {
  182. vi, err := storage.NewVolumeInfoFromShort(v)
  183. if err != nil {
  184. glog.V(0).Infof("NewVolumeInfoFromShort %v: %v", v, err)
  185. continue
  186. }
  187. oldVis = append(oldVis, vi)
  188. }
  189. dn.DeltaUpdateVolumes(newVis, oldVis)
  190. for _, vi := range newVis {
  191. t.RegisterVolumeLayout(vi, dn)
  192. }
  193. for _, vi := range oldVis {
  194. t.UnRegisterVolumeLayout(vi, dn)
  195. }
  196. return
  197. }