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.

141 lines
4.1 KiB

  1. package topology
  2. import (
  3. "code.google.com/p/weed-fs/go/sequence"
  4. "code.google.com/p/weed-fs/go/storage"
  5. "errors"
  6. "fmt"
  7. "io/ioutil"
  8. "log"
  9. "math/rand"
  10. )
  11. type Topology struct {
  12. NodeImpl
  13. //transient vid~servers mapping for each replication type
  14. replicaType2VolumeLayout []*VolumeLayout
  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. }
  23. func NewTopology(id string, confFile string, dirname string, sequenceFilename string, 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.replicaType2VolumeLayout = make([]*VolumeLayout, storage.LengthRelicationType)
  30. t.pulse = int64(pulse)
  31. t.volumeSizeLimit = volumeSizeLimit
  32. t.sequence = sequence.NewSequencer(dirname, sequenceFilename)
  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) loadConfiguration(configurationFile string) error {
  40. b, e := ioutil.ReadFile(configurationFile)
  41. if e == nil {
  42. t.configuration, e = NewConfiguration(b)
  43. return e
  44. } else {
  45. log.Println("Using default configurations.")
  46. }
  47. return nil
  48. }
  49. func (t *Topology) Lookup(vid storage.VolumeId) []*DataNode {
  50. for _, vl := range t.replicaType2VolumeLayout {
  51. if vl != nil {
  52. if list := vl.Lookup(vid); list != nil {
  53. return list
  54. }
  55. }
  56. }
  57. return nil
  58. }
  59. func (t *Topology) RandomlyReserveOneVolume(dataCenter string) (bool, *DataNode, *storage.VolumeId) {
  60. if t.FreeSpace() <= 0 {
  61. fmt.Println("Topology does not have free space left!")
  62. return false, nil, nil
  63. }
  64. vid := t.NextVolumeId()
  65. ret, node := t.ReserveOneVolume(rand.Intn(t.FreeSpace()), vid, dataCenter)
  66. return ret, node, &vid
  67. }
  68. func (t *Topology) NextVolumeId() storage.VolumeId {
  69. vid := t.GetMaxVolumeId()
  70. return vid.Next()
  71. }
  72. func (t *Topology) PickForWrite(repType storage.ReplicationType, count int, dataCenter string) (string, int, *DataNode, error) {
  73. replicationTypeIndex := repType.GetReplicationLevelIndex()
  74. if t.replicaType2VolumeLayout[replicationTypeIndex] == nil {
  75. t.replicaType2VolumeLayout[replicationTypeIndex] = NewVolumeLayout(repType, t.volumeSizeLimit, t.pulse)
  76. }
  77. vid, count, datanodes, err := t.replicaType2VolumeLayout[replicationTypeIndex].PickForWrite(count, dataCenter)
  78. if err != nil || datanodes.Length() == 0 {
  79. return "", 0, nil, errors.New("No writable volumes avalable!")
  80. }
  81. fileId, count := t.sequence.NextFileId(count)
  82. return storage.NewFileId(*vid, fileId, rand.Uint32()).String(), count, datanodes.Head(), nil
  83. }
  84. func (t *Topology) GetVolumeLayout(repType storage.ReplicationType) *VolumeLayout {
  85. replicationTypeIndex := repType.GetReplicationLevelIndex()
  86. if t.replicaType2VolumeLayout[replicationTypeIndex] == nil {
  87. fmt.Println("adding replication type", repType)
  88. t.replicaType2VolumeLayout[replicationTypeIndex] = NewVolumeLayout(repType, t.volumeSizeLimit, t.pulse)
  89. }
  90. return t.replicaType2VolumeLayout[replicationTypeIndex]
  91. }
  92. func (t *Topology) RegisterVolumeLayout(v *storage.VolumeInfo, dn *DataNode) {
  93. t.GetVolumeLayout(v.RepType).RegisterVolume(v, dn)
  94. }
  95. func (t *Topology) RegisterVolumes(init bool, volumeInfos []storage.VolumeInfo, ip string, port int, publicUrl string, maxVolumeCount int, dcName string, rackName string) {
  96. dcName, rackName = t.configuration.Locate(ip, dcName, rackName)
  97. dc := t.GetOrCreateDataCenter(dcName)
  98. rack := dc.GetOrCreateRack(rackName)
  99. dn := rack.FindDataNode(ip, port)
  100. if init && dn != nil {
  101. t.UnRegisterDataNode(dn)
  102. }
  103. dn = rack.GetOrCreateDataNode(ip, port, publicUrl, maxVolumeCount)
  104. for _, v := range volumeInfos {
  105. dn.AddOrUpdateVolume(v)
  106. t.RegisterVolumeLayout(&v, dn)
  107. }
  108. }
  109. func (t *Topology) GetOrCreateDataCenter(dcName string) *DataCenter {
  110. for _, c := range t.Children() {
  111. dc := c.(*DataCenter)
  112. if string(dc.Id()) == dcName {
  113. return dc
  114. }
  115. }
  116. dc := NewDataCenter(dcName)
  117. t.LinkChildNode(dc)
  118. return dc
  119. }