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.

151 lines
4.2 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. "io/ioutil"
  7. "log"
  8. "math/rand"
  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, error) {
  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. err := t.loadConfiguration(confFile)
  36. return t, err
  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. return e
  43. } else {
  44. log.Println("Using default configurations.")
  45. }
  46. return nil
  47. }
  48. func (t *Topology) Lookup(vid storage.VolumeId) []*DataNode {
  49. for _, vl := range t.replicaType2VolumeLayout {
  50. if vl != nil {
  51. if list := vl.Lookup(vid); list != nil {
  52. return list
  53. }
  54. }
  55. }
  56. return nil
  57. }
  58. func (t *Topology) RandomlyReserveOneVolume() (bool, *DataNode, *storage.VolumeId) {
  59. if t.FreeSpace() <= 0 {
  60. return false, nil, nil
  61. }
  62. vid := t.NextVolumeId()
  63. ret, node := t.ReserveOneVolume(rand.Intn(t.FreeSpace()), vid)
  64. return ret, node, &vid
  65. }
  66. func (t *Topology) RandomlyReserveOneVolumeExcept(except []Node) (bool, *DataNode, *storage.VolumeId) {
  67. freeSpace := t.FreeSpace()
  68. for _, node := range except {
  69. freeSpace -= node.FreeSpace()
  70. }
  71. if freeSpace <= 0 {
  72. return false, nil, nil
  73. }
  74. vid := t.NextVolumeId()
  75. ret, node := t.ReserveOneVolume(rand.Intn(freeSpace), vid)
  76. return ret, node, &vid
  77. }
  78. func (t *Topology) NextVolumeId() storage.VolumeId {
  79. vid := t.GetMaxVolumeId()
  80. return vid.Next()
  81. }
  82. func (t *Topology) PickForWrite(repType storage.ReplicationType, count int) (string, int, *DataNode, error) {
  83. replicationTypeIndex := repType.GetReplicationLevelIndex()
  84. if t.replicaType2VolumeLayout[replicationTypeIndex] == nil {
  85. t.replicaType2VolumeLayout[replicationTypeIndex] = NewVolumeLayout(repType, t.volumeSizeLimit, t.pulse)
  86. }
  87. vid, count, datanodes, err := t.replicaType2VolumeLayout[replicationTypeIndex].PickForWrite(count)
  88. if err != nil || datanodes.Length() == 0 {
  89. return "", 0, nil, errors.New("No writable volumes avalable!")
  90. }
  91. fileId, count := t.sequence.NextFileId(count)
  92. return storage.NewFileId(*vid, fileId, rand.Uint32()).String(), count, datanodes.Head(), nil
  93. }
  94. func (t *Topology) GetVolumeLayout(repType storage.ReplicationType) *VolumeLayout {
  95. replicationTypeIndex := repType.GetReplicationLevelIndex()
  96. if t.replicaType2VolumeLayout[replicationTypeIndex] == nil {
  97. t.replicaType2VolumeLayout[replicationTypeIndex] = NewVolumeLayout(repType, t.volumeSizeLimit, t.pulse)
  98. }
  99. return t.replicaType2VolumeLayout[replicationTypeIndex]
  100. }
  101. func (t *Topology) RegisterVolumeLayout(v *storage.VolumeInfo, dn *DataNode) {
  102. t.GetVolumeLayout(v.RepType).RegisterVolume(v, dn)
  103. }
  104. func (t *Topology) RegisterVolumes(init bool, volumeInfos []storage.VolumeInfo, ip string, port int, publicUrl string, maxVolumeCount int) {
  105. dcName, rackName := t.configuration.Locate(ip)
  106. dc := t.GetOrCreateDataCenter(dcName)
  107. rack := dc.GetOrCreateRack(rackName)
  108. dn := rack.FindDataNode(ip, port)
  109. if init && dn != nil {
  110. t.UnRegisterDataNode(dn)
  111. }
  112. dn = rack.GetOrCreateDataNode(ip, port, publicUrl, maxVolumeCount)
  113. for _, v := range volumeInfos {
  114. dn.AddOrUpdateVolume(v)
  115. t.RegisterVolumeLayout(&v, dn)
  116. }
  117. }
  118. func (t *Topology) GetOrCreateDataCenter(dcName string) *DataCenter {
  119. for _, c := range t.Children() {
  120. dc := c.(*DataCenter)
  121. if string(dc.Id()) == dcName {
  122. return dc
  123. }
  124. }
  125. dc := NewDataCenter(dcName)
  126. t.LinkChildNode(dc)
  127. return dc
  128. }