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.

152 lines
4.3 KiB

  1. package topology
  2. import (
  3. "code.google.com/p/weed-fs/go/directory"
  4. "code.google.com/p/weed-fs/go/sequence"
  5. "code.google.com/p/weed-fs/go/storage"
  6. "errors"
  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() (bool, *DataNode, *storage.VolumeId) {
  60. if t.FreeSpace() <= 0 {
  61. return false, nil, nil
  62. }
  63. vid := t.NextVolumeId()
  64. ret, node := t.ReserveOneVolume(rand.Intn(t.FreeSpace()), vid)
  65. return ret, node, &vid
  66. }
  67. func (t *Topology) RandomlyReserveOneVolumeExcept(except []Node) (bool, *DataNode, *storage.VolumeId) {
  68. freeSpace := t.FreeSpace()
  69. for _, node := range except {
  70. freeSpace -= node.FreeSpace()
  71. }
  72. if freeSpace <= 0 {
  73. return false, nil, nil
  74. }
  75. vid := t.NextVolumeId()
  76. ret, node := t.ReserveOneVolume(rand.Intn(freeSpace), vid)
  77. return ret, node, &vid
  78. }
  79. func (t *Topology) NextVolumeId() storage.VolumeId {
  80. vid := t.GetMaxVolumeId()
  81. return vid.Next()
  82. }
  83. func (t *Topology) PickForWrite(repType storage.ReplicationType, count int) (string, int, *DataNode, error) {
  84. replicationTypeIndex := repType.GetReplicationLevelIndex()
  85. if t.replicaType2VolumeLayout[replicationTypeIndex] == nil {
  86. t.replicaType2VolumeLayout[replicationTypeIndex] = NewVolumeLayout(repType, t.volumeSizeLimit, t.pulse)
  87. }
  88. vid, count, datanodes, err := t.replicaType2VolumeLayout[replicationTypeIndex].PickForWrite(count)
  89. if err != nil || datanodes.Length() == 0 {
  90. return "", 0, nil, errors.New("No writable volumes avalable!")
  91. }
  92. fileId, count := t.sequence.NextFileId(count)
  93. return directory.NewFileId(*vid, fileId, rand.Uint32()).String(), count, datanodes.Head(), nil
  94. }
  95. func (t *Topology) GetVolumeLayout(repType storage.ReplicationType) *VolumeLayout {
  96. replicationTypeIndex := repType.GetReplicationLevelIndex()
  97. if t.replicaType2VolumeLayout[replicationTypeIndex] == nil {
  98. t.replicaType2VolumeLayout[replicationTypeIndex] = NewVolumeLayout(repType, t.volumeSizeLimit, t.pulse)
  99. }
  100. return t.replicaType2VolumeLayout[replicationTypeIndex]
  101. }
  102. func (t *Topology) RegisterVolumeLayout(v *storage.VolumeInfo, dn *DataNode) {
  103. t.GetVolumeLayout(v.RepType).RegisterVolume(v, dn)
  104. }
  105. func (t *Topology) RegisterVolumes(init bool, volumeInfos []storage.VolumeInfo, ip string, port int, publicUrl string, maxVolumeCount int) {
  106. dcName, rackName := t.configuration.Locate(ip)
  107. dc := t.GetOrCreateDataCenter(dcName)
  108. rack := dc.GetOrCreateRack(rackName)
  109. dn := rack.FindDataNode(ip, port)
  110. if init && dn != nil {
  111. t.UnRegisterDataNode(dn)
  112. }
  113. dn = rack.GetOrCreateDataNode(ip, port, publicUrl, maxVolumeCount)
  114. for _, v := range volumeInfos {
  115. dn.AddOrUpdateVolume(v)
  116. t.RegisterVolumeLayout(&v, dn)
  117. }
  118. }
  119. func (t *Topology) GetOrCreateDataCenter(dcName string) *DataCenter {
  120. for _, c := range t.Children() {
  121. dc := c.(*DataCenter)
  122. if string(dc.Id()) == dcName {
  123. return dc
  124. }
  125. }
  126. dc := NewDataCenter(dcName)
  127. t.LinkChildNode(dc)
  128. return dc
  129. }