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.

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