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.

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