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.

259 lines
7.1 KiB

6 years ago
6 years ago
6 years ago
10 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. "time"
  8. "github.com/chrislusf/raft"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  11. "github.com/chrislusf/seaweedfs/weed/sequence"
  12. "github.com/chrislusf/seaweedfs/weed/storage"
  13. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  14. "github.com/chrislusf/seaweedfs/weed/storage/super_block"
  15. "github.com/chrislusf/seaweedfs/weed/util"
  16. )
  17. type Topology struct {
  18. vacuumLockCounter int64
  19. NodeImpl
  20. collectionMap *util.ConcurrentReadMap
  21. ecShardMap map[needle.VolumeId]*EcShardLocations
  22. ecShardMapLock sync.RWMutex
  23. pulse int64
  24. volumeSizeLimit uint64
  25. replicationAsMin bool
  26. Sequence sequence.Sequencer
  27. chanFullVolumes chan storage.VolumeInfo
  28. Configuration *Configuration
  29. RaftServer raft.Server
  30. }
  31. func NewTopology(id string, seq sequence.Sequencer, volumeSizeLimit uint64, pulse int, replicationAsMin bool) *Topology {
  32. t := &Topology{}
  33. t.id = NodeId(id)
  34. t.nodeType = "Topology"
  35. t.NodeImpl.value = t
  36. t.children = make(map[NodeId]Node)
  37. t.collectionMap = util.NewConcurrentReadMap()
  38. t.ecShardMap = make(map[needle.VolumeId]*EcShardLocations)
  39. t.pulse = int64(pulse)
  40. t.volumeSizeLimit = volumeSizeLimit
  41. t.replicationAsMin = replicationAsMin
  42. t.Sequence = seq
  43. t.chanFullVolumes = make(chan storage.VolumeInfo)
  44. t.Configuration = &Configuration{}
  45. return t
  46. }
  47. func (t *Topology) IsLeader() bool {
  48. if t.RaftServer != nil {
  49. if t.RaftServer.State() == raft.Leader {
  50. return true
  51. }
  52. }
  53. return false
  54. }
  55. func (t *Topology) Leader() (string, error) {
  56. l := ""
  57. count := 3
  58. for count > 0 {
  59. if t.RaftServer != nil {
  60. l = t.RaftServer.Leader()
  61. } else {
  62. return "", errors.New("Raft Server not ready yet!")
  63. }
  64. if l != "" {
  65. break
  66. } else {
  67. time.Sleep(time.Duration(5-count) * time.Second)
  68. }
  69. count -= 1
  70. }
  71. return l, nil
  72. }
  73. func (t *Topology) Lookup(collection string, vid needle.VolumeId) (dataNodes []*DataNode) {
  74. //maybe an issue if lots of collections?
  75. if collection == "" {
  76. for _, c := range t.collectionMap.Items() {
  77. if list := c.(*Collection).Lookup(vid); list != nil {
  78. return list
  79. }
  80. }
  81. } else {
  82. if c, ok := t.collectionMap.Find(collection); ok {
  83. return c.(*Collection).Lookup(vid)
  84. }
  85. }
  86. if locations, found := t.LookupEcShards(vid); found {
  87. for _, loc := range locations.Locations {
  88. dataNodes = append(dataNodes, loc...)
  89. }
  90. return dataNodes
  91. }
  92. return nil
  93. }
  94. func (t *Topology) NextVolumeId() (needle.VolumeId, error) {
  95. vid := t.GetMaxVolumeId()
  96. next := vid.Next()
  97. if _, err := t.RaftServer.Do(NewMaxVolumeIdCommand(next)); err != nil {
  98. return 0, err
  99. }
  100. return next, nil
  101. }
  102. func (t *Topology) HasWritableVolume(option *VolumeGrowOption) bool {
  103. vl := t.GetVolumeLayout(option.Collection, option.ReplicaPlacement, option.Ttl)
  104. return vl.GetActiveVolumeCount(option) > 0
  105. }
  106. func (t *Topology) PickForWrite(count uint64, option *VolumeGrowOption) (string, uint64, *DataNode, error) {
  107. vid, count, datanodes, err := t.GetVolumeLayout(option.Collection, option.ReplicaPlacement, option.Ttl).PickForWrite(count, option)
  108. if err != nil {
  109. return "", 0, nil, fmt.Errorf("failed to find writable volumes for collection:%s replication:%s ttl:%s error: %v", option.Collection, option.ReplicaPlacement.String(), option.Ttl.String(), err)
  110. }
  111. if datanodes.Length() == 0 {
  112. return "", 0, nil, fmt.Errorf("no writable volumes available for collection:%s replication:%s ttl:%s", option.Collection, option.ReplicaPlacement.String(), option.Ttl.String())
  113. }
  114. fileId := t.Sequence.NextFileId(count)
  115. return needle.NewFileId(*vid, fileId, rand.Uint32()).String(), count, datanodes.Head(), nil
  116. }
  117. func (t *Topology) GetVolumeLayout(collectionName string, rp *super_block.ReplicaPlacement, ttl *needle.TTL) *VolumeLayout {
  118. return t.collectionMap.Get(collectionName, func() interface{} {
  119. return NewCollection(collectionName, t.volumeSizeLimit, t.replicationAsMin)
  120. }).(*Collection).GetOrCreateVolumeLayout(rp, ttl)
  121. }
  122. func (t *Topology) ListCollections(includeNormalVolumes, includeEcVolumes bool) (ret []string) {
  123. mapOfCollections := make(map[string]bool)
  124. for _, c := range t.collectionMap.Items() {
  125. mapOfCollections[c.(*Collection).Name] = true
  126. }
  127. if includeEcVolumes {
  128. t.ecShardMapLock.RLock()
  129. for _, ecVolumeLocation := range t.ecShardMap {
  130. mapOfCollections[ecVolumeLocation.Collection] = true
  131. }
  132. t.ecShardMapLock.RUnlock()
  133. }
  134. for k := range mapOfCollections {
  135. ret = append(ret, k)
  136. }
  137. return ret
  138. }
  139. func (t *Topology) FindCollection(collectionName string) (*Collection, bool) {
  140. c, hasCollection := t.collectionMap.Find(collectionName)
  141. if !hasCollection {
  142. return nil, false
  143. }
  144. return c.(*Collection), hasCollection
  145. }
  146. func (t *Topology) DeleteCollection(collectionName string) {
  147. t.collectionMap.Delete(collectionName)
  148. }
  149. func (t *Topology) RegisterVolumeLayout(v storage.VolumeInfo, dn *DataNode) {
  150. t.GetVolumeLayout(v.Collection, v.ReplicaPlacement, v.Ttl).RegisterVolume(&v, dn)
  151. }
  152. func (t *Topology) UnRegisterVolumeLayout(v storage.VolumeInfo, dn *DataNode) {
  153. glog.Infof("removing volume info:%+v", v)
  154. volumeLayout := t.GetVolumeLayout(v.Collection, v.ReplicaPlacement, v.Ttl)
  155. volumeLayout.UnRegisterVolume(&v, dn)
  156. if volumeLayout.isEmpty() {
  157. t.DeleteCollection(v.Collection)
  158. }
  159. }
  160. func (t *Topology) GetOrCreateDataCenter(dcName string) *DataCenter {
  161. for _, c := range t.Children() {
  162. dc := c.(*DataCenter)
  163. if string(dc.Id()) == dcName {
  164. return dc
  165. }
  166. }
  167. dc := NewDataCenter(dcName)
  168. t.LinkChildNode(dc)
  169. return dc
  170. }
  171. func (t *Topology) SyncDataNodeRegistration(volumes []*master_pb.VolumeInformationMessage, dn *DataNode) (newVolumes, deletedVolumes []storage.VolumeInfo) {
  172. // convert into in memory struct storage.VolumeInfo
  173. var volumeInfos []storage.VolumeInfo
  174. for _, v := range volumes {
  175. if vi, err := storage.NewVolumeInfo(v); err == nil {
  176. volumeInfos = append(volumeInfos, vi)
  177. } else {
  178. glog.V(0).Infof("Fail to convert joined volume information: %v", err)
  179. }
  180. }
  181. // find out the delta volumes
  182. var changedVolumes []storage.VolumeInfo
  183. newVolumes, deletedVolumes, changedVolumes = dn.UpdateVolumes(volumeInfos)
  184. for _, v := range newVolumes {
  185. t.RegisterVolumeLayout(v, dn)
  186. }
  187. for _, v := range deletedVolumes {
  188. t.UnRegisterVolumeLayout(v, dn)
  189. }
  190. for _, v := range changedVolumes {
  191. vl := t.GetVolumeLayout(v.Collection, v.ReplicaPlacement, v.Ttl)
  192. vl.ensureCorrectWritables(&v)
  193. }
  194. return
  195. }
  196. func (t *Topology) IncrementalSyncDataNodeRegistration(newVolumes, deletedVolumes []*master_pb.VolumeShortInformationMessage, dn *DataNode) {
  197. var newVis, oldVis []storage.VolumeInfo
  198. for _, v := range newVolumes {
  199. vi, err := storage.NewVolumeInfoFromShort(v)
  200. if err != nil {
  201. glog.V(0).Infof("NewVolumeInfoFromShort %v: %v", v, err)
  202. continue
  203. }
  204. newVis = append(newVis, vi)
  205. }
  206. for _, v := range deletedVolumes {
  207. vi, err := storage.NewVolumeInfoFromShort(v)
  208. if err != nil {
  209. glog.V(0).Infof("NewVolumeInfoFromShort %v: %v", v, err)
  210. continue
  211. }
  212. oldVis = append(oldVis, vi)
  213. }
  214. dn.DeltaUpdateVolumes(newVis, oldVis)
  215. for _, vi := range newVis {
  216. t.RegisterVolumeLayout(vi, dn)
  217. }
  218. for _, vi := range oldVis {
  219. t.UnRegisterVolumeLayout(vi, dn)
  220. }
  221. return
  222. }