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.

148 lines
4.2 KiB

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