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.

172 lines
4.5 KiB

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