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.

188 lines
5.2 KiB

  1. package topology
  2. import (
  3. "github.com/chrislusf/weed-fs/go/glog"
  4. "github.com/chrislusf/weed-fs/go/operation"
  5. "github.com/chrislusf/weed-fs/go/sequence"
  6. "github.com/chrislusf/weed-fs/go/storage"
  7. "errors"
  8. "github.com/goraft/raft"
  9. "io/ioutil"
  10. "math/rand"
  11. )
  12. type Topology struct {
  13. NodeImpl
  14. collectionMap map[string]*Collection
  15. pulse int64
  16. volumeSizeLimit uint64
  17. Sequence sequence.Sequencer
  18. chanDeadDataNodes chan *DataNode
  19. chanRecoveredDataNodes chan *DataNode
  20. chanFullVolumes chan storage.VolumeInfo
  21. configuration *Configuration
  22. RaftServer raft.Server
  23. }
  24. func NewTopology(id string, confFile string, seq sequence.Sequencer, volumeSizeLimit uint64, pulse int) (*Topology, error) {
  25. t := &Topology{}
  26. t.id = NodeId(id)
  27. t.nodeType = "Topology"
  28. t.NodeImpl.value = t
  29. t.children = make(map[NodeId]Node)
  30. t.collectionMap = make(map[string]*Collection)
  31. t.pulse = int64(pulse)
  32. t.volumeSizeLimit = volumeSizeLimit
  33. t.Sequence = seq
  34. t.chanDeadDataNodes = make(chan *DataNode)
  35. t.chanRecoveredDataNodes = make(chan *DataNode)
  36. t.chanFullVolumes = make(chan storage.VolumeInfo)
  37. err := t.loadConfiguration(confFile)
  38. return t, err
  39. }
  40. func (t *Topology) IsLeader() bool {
  41. if leader, e := t.Leader(); e == nil {
  42. return leader == t.RaftServer.Name()
  43. }
  44. return false
  45. }
  46. func (t *Topology) Leader() (string, error) {
  47. l := ""
  48. if t.RaftServer != nil {
  49. l = t.RaftServer.Leader()
  50. } else {
  51. return "", errors.New("Raft Server not ready yet!")
  52. }
  53. if l == "" {
  54. // We are a single node cluster, we are the leader
  55. return t.RaftServer.Name(), errors.New("Raft Server not initialized!")
  56. }
  57. return l, nil
  58. }
  59. func (t *Topology) loadConfiguration(configurationFile string) error {
  60. b, e := ioutil.ReadFile(configurationFile)
  61. if e == nil {
  62. t.configuration, e = NewConfiguration(b)
  63. return e
  64. } else {
  65. glog.V(0).Infoln("Using default configurations.")
  66. }
  67. return nil
  68. }
  69. func (t *Topology) Lookup(collection string, vid storage.VolumeId) []*DataNode {
  70. //maybe an issue if lots of collections?
  71. if collection == "" {
  72. for _, c := range t.collectionMap {
  73. if list := c.Lookup(vid); list != nil {
  74. return list
  75. }
  76. }
  77. } else {
  78. if c, ok := t.collectionMap[collection]; ok {
  79. return c.Lookup(vid)
  80. }
  81. }
  82. return nil
  83. }
  84. func (t *Topology) NextVolumeId() storage.VolumeId {
  85. vid := t.GetMaxVolumeId()
  86. next := vid.Next()
  87. go t.RaftServer.Do(NewMaxVolumeIdCommand(next))
  88. return next
  89. }
  90. func (t *Topology) HasWriableVolume(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 int, option *VolumeGrowOption) (string, int, *DataNode, error) {
  95. vid, count, datanodes, err := t.GetVolumeLayout(option.Collection, option.ReplicaPlacement, option.Ttl).PickForWrite(count, option)
  96. if err != nil || datanodes.Length() == 0 {
  97. return "", 0, nil, errors.New("No writable volumes avalable!")
  98. }
  99. fileId, count := t.Sequence.NextFileId(count)
  100. return storage.NewFileId(*vid, fileId, rand.Uint32()).String(), count, datanodes.Head(), nil
  101. }
  102. func (t *Topology) GetVolumeLayout(collectionName string, rp *storage.ReplicaPlacement, ttl *storage.TTL) *VolumeLayout {
  103. _, ok := t.collectionMap[collectionName]
  104. if !ok {
  105. t.collectionMap[collectionName] = NewCollection(collectionName, t.volumeSizeLimit)
  106. }
  107. return t.collectionMap[collectionName].GetOrCreateVolumeLayout(rp, ttl)
  108. }
  109. func (t *Topology) GetCollection(collectionName string) (collection *Collection, ok bool) {
  110. collection, ok = t.collectionMap[collectionName]
  111. return
  112. }
  113. func (t *Topology) DeleteCollection(collectionName string) {
  114. delete(t.collectionMap, collectionName)
  115. }
  116. func (t *Topology) RegisterVolumeLayout(v storage.VolumeInfo, dn *DataNode) {
  117. t.GetVolumeLayout(v.Collection, v.ReplicaPlacement, v.Ttl).RegisterVolume(&v, dn)
  118. }
  119. func (t *Topology) UnRegisterVolumeLayout(v storage.VolumeInfo, dn *DataNode) {
  120. glog.Infof("removing volume info:%+v", v)
  121. t.GetVolumeLayout(v.Collection, v.ReplicaPlacement, v.Ttl).UnRegisterVolume(&v, dn)
  122. }
  123. func (t *Topology) ProcessJoinMessage(joinMessage *operation.JoinMessage) {
  124. t.Sequence.SetMax(*joinMessage.MaxFileKey)
  125. dcName, rackName := t.configuration.Locate(*joinMessage.Ip, *joinMessage.DataCenter, *joinMessage.Rack)
  126. dc := t.GetOrCreateDataCenter(dcName)
  127. rack := dc.GetOrCreateRack(rackName)
  128. dn := rack.FindDataNode(*joinMessage.Ip, int(*joinMessage.Port))
  129. if *joinMessage.IsInit && dn != nil {
  130. t.UnRegisterDataNode(dn)
  131. }
  132. dn = rack.GetOrCreateDataNode(*joinMessage.Ip, int(*joinMessage.Port), *joinMessage.PublicUrl, int(*joinMessage.MaxVolumeCount))
  133. var volumeInfos []storage.VolumeInfo
  134. for _, v := range joinMessage.Volumes {
  135. if vi, err := storage.NewVolumeInfo(v); err == nil {
  136. volumeInfos = append(volumeInfos, vi)
  137. } else {
  138. glog.V(0).Infoln("Fail to convert joined volume information:", err.Error())
  139. }
  140. }
  141. deletedVolumes := dn.UpdateVolumes(volumeInfos)
  142. for _, v := range volumeInfos {
  143. t.RegisterVolumeLayout(v, dn)
  144. }
  145. for _, v := range deletedVolumes {
  146. t.UnRegisterVolumeLayout(v, dn)
  147. }
  148. }
  149. func (t *Topology) GetOrCreateDataCenter(dcName string) *DataCenter {
  150. for _, c := range t.Children() {
  151. dc := c.(*DataCenter)
  152. if string(dc.Id()) == dcName {
  153. return dc
  154. }
  155. }
  156. dc := NewDataCenter(dcName)
  157. t.LinkChildNode(dc)
  158. return dc
  159. }