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.

243 lines
6.7 KiB

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