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.

161 lines
4.2 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. return t.RaftServer == nil || t.Leader() == t.RaftServer.Name()
  41. }
  42. func (t *Topology) Leader() string {
  43. l := ""
  44. if t.RaftServer != nil {
  45. l = t.RaftServer.Leader()
  46. }
  47. if l == "" {
  48. // We are a single node cluster, we are the leader
  49. return t.RaftServer.Name()
  50. }
  51. return l
  52. }
  53. func (t *Topology) loadConfiguration(configurationFile string) error {
  54. b, e := ioutil.ReadFile(configurationFile)
  55. if e == nil {
  56. t.configuration, e = NewConfiguration(b)
  57. return e
  58. } else {
  59. glog.V(0).Infoln("Using default configurations.")
  60. }
  61. return nil
  62. }
  63. func (t *Topology) Lookup(collection string, vid storage.VolumeId) []*DataNode {
  64. //maybe an issue if lots of collections?
  65. if collection == "" {
  66. for _, c := range t.collectionMap {
  67. if list := c.Lookup(vid); list != nil {
  68. return list
  69. }
  70. }
  71. } else {
  72. if c, ok := t.collectionMap[collection]; ok {
  73. return c.Lookup(vid)
  74. }
  75. }
  76. return nil
  77. }
  78. func (t *Topology) NextVolumeId() storage.VolumeId {
  79. vid := t.GetMaxVolumeId()
  80. next := vid.Next()
  81. go t.RaftServer.Do(NewMaxVolumeIdCommand(next))
  82. return next
  83. }
  84. func (t *Topology) PickForWrite(collectionName string, rp *storage.ReplicaPlacement, count int, dataCenter string) (string, int, *DataNode, error) {
  85. vid, count, datanodes, err := t.GetVolumeLayout(collectionName, rp).PickForWrite(count, dataCenter)
  86. if err != nil || datanodes.Length() == 0 {
  87. return "", 0, nil, errors.New("No writable volumes avalable!")
  88. }
  89. fileId, count := t.sequence.NextFileId(count)
  90. return storage.NewFileId(*vid, fileId, rand.Uint32()).String(), count, datanodes.Head(), nil
  91. }
  92. func (t *Topology) GetVolumeLayout(collectionName string, rp *storage.ReplicaPlacement) *VolumeLayout {
  93. _, ok := t.collectionMap[collectionName]
  94. if !ok {
  95. t.collectionMap[collectionName] = NewCollection(collectionName, t.volumeSizeLimit)
  96. }
  97. return t.collectionMap[collectionName].GetOrCreateVolumeLayout(rp)
  98. }
  99. func (t *Topology) GetCollection(collectionName string) (collection *Collection, ok bool) {
  100. collection, ok = t.collectionMap[collectionName]
  101. return
  102. }
  103. func (t *Topology) DeleteCollection(collectionName string) {
  104. delete(t.collectionMap, collectionName)
  105. }
  106. func (t *Topology) RegisterVolumeLayout(v storage.VolumeInfo, dn *DataNode) {
  107. t.GetVolumeLayout(v.Collection, v.ReplicaPlacement).RegisterVolume(&v, dn)
  108. }
  109. func (t *Topology) RegisterVolumes(init bool, volumeInfos []storage.VolumeInfo, ip string, port int, publicUrl string, maxVolumeCount int, dcName string, rackName string) {
  110. dcName, rackName = t.configuration.Locate(ip, dcName, rackName)
  111. dc := t.GetOrCreateDataCenter(dcName)
  112. rack := dc.GetOrCreateRack(rackName)
  113. dn := rack.FindDataNode(ip, port)
  114. if init && dn != nil {
  115. t.UnRegisterDataNode(dn)
  116. }
  117. dn = rack.GetOrCreateDataNode(ip, port, publicUrl, maxVolumeCount)
  118. dn.UpdateVolumes(volumeInfos)
  119. for _, v := range volumeInfos {
  120. t.RegisterVolumeLayout(v, dn)
  121. }
  122. }
  123. func (t *Topology) GetOrCreateDataCenter(dcName string) *DataCenter {
  124. for _, c := range t.Children() {
  125. dc := c.(*DataCenter)
  126. if string(dc.Id()) == dcName {
  127. return dc
  128. }
  129. }
  130. dc := NewDataCenter(dcName)
  131. t.LinkChildNode(dc)
  132. return dc
  133. }