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.

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