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.

181 lines
4.9 KiB

  1. package topology
  2. import (
  3. "code.google.com/p/weed-fs/go/glog"
  4. "code.google.com/p/weed-fs/go/operation"
  5. "code.google.com/p/weed-fs/go/sequence"
  6. "code.google.com/p/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)
  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).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) *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)
  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).RegisterVolume(&v, dn)
  118. }
  119. func (t *Topology) RegisterVolumes(joinMessage *operation.JoinMessage) {
  120. t.Sequence.SetMax(*joinMessage.MaxFileKey)
  121. dcName, rackName := t.configuration.Locate(*joinMessage.Ip, *joinMessage.DataCenter, *joinMessage.Rack)
  122. dc := t.GetOrCreateDataCenter(dcName)
  123. rack := dc.GetOrCreateRack(rackName)
  124. dn := rack.FindDataNode(*joinMessage.Ip, int(*joinMessage.Port))
  125. if *joinMessage.IsInit && dn != nil {
  126. t.UnRegisterDataNode(dn)
  127. }
  128. dn = rack.GetOrCreateDataNode(*joinMessage.Ip, int(*joinMessage.Port), *joinMessage.PublicUrl, int(*joinMessage.MaxVolumeCount))
  129. var volumeInfos []storage.VolumeInfo
  130. for _, v := range joinMessage.Volumes {
  131. if vi, err := storage.NewVolumeInfo(v); err == nil {
  132. volumeInfos = append(volumeInfos, vi)
  133. } else {
  134. glog.V(0).Infoln("Fail to convert joined volume information:", err.Error())
  135. }
  136. }
  137. dn.UpdateVolumes(volumeInfos)
  138. for _, v := range volumeInfos {
  139. t.RegisterVolumeLayout(v, dn)
  140. }
  141. }
  142. func (t *Topology) GetOrCreateDataCenter(dcName string) *DataCenter {
  143. for _, c := range t.Children() {
  144. dc := c.(*DataCenter)
  145. if string(dc.Id()) == dcName {
  146. return dc
  147. }
  148. }
  149. dc := NewDataCenter(dcName)
  150. t.LinkChildNode(dc)
  151. return dc
  152. }