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.

140 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. "io/ioutil"
  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. glog.V(0).Infoln("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(dataCenter string) (bool, *DataNode, *storage.VolumeId) {
  59. if t.FreeSpace() <= 0 {
  60. glog.V(0).Infoln("Topology does not have free space left!")
  61. return false, nil, nil
  62. }
  63. vid := t.NextVolumeId()
  64. ret, node := t.ReserveOneVolume(rand.Intn(t.FreeSpace()), vid, dataCenter)
  65. return ret, node, &vid
  66. }
  67. func (t *Topology) NextVolumeId() storage.VolumeId {
  68. vid := t.GetMaxVolumeId()
  69. return vid.Next()
  70. }
  71. func (t *Topology) PickForWrite(repType storage.ReplicationType, count int, dataCenter string) (string, int, *DataNode, error) {
  72. replicationTypeIndex := repType.GetReplicationLevelIndex()
  73. if t.replicaType2VolumeLayout[replicationTypeIndex] == nil {
  74. t.replicaType2VolumeLayout[replicationTypeIndex] = NewVolumeLayout(repType, t.volumeSizeLimit, t.pulse)
  75. }
  76. vid, count, datanodes, err := t.replicaType2VolumeLayout[replicationTypeIndex].PickForWrite(count, dataCenter)
  77. if err != nil || datanodes.Length() == 0 {
  78. return "", 0, nil, errors.New("No writable volumes avalable!")
  79. }
  80. fileId, count := t.sequence.NextFileId(count)
  81. return storage.NewFileId(*vid, fileId, rand.Uint32()).String(), count, datanodes.Head(), nil
  82. }
  83. func (t *Topology) GetVolumeLayout(repType storage.ReplicationType) *VolumeLayout {
  84. replicationTypeIndex := repType.GetReplicationLevelIndex()
  85. if t.replicaType2VolumeLayout[replicationTypeIndex] == nil {
  86. glog.V(0).Infoln("adding replication type", repType)
  87. t.replicaType2VolumeLayout[replicationTypeIndex] = NewVolumeLayout(repType, t.volumeSizeLimit, t.pulse)
  88. }
  89. return t.replicaType2VolumeLayout[replicationTypeIndex]
  90. }
  91. func (t *Topology) RegisterVolumeLayout(v *storage.VolumeInfo, dn *DataNode) {
  92. t.GetVolumeLayout(v.RepType).RegisterVolume(v, dn)
  93. }
  94. func (t *Topology) RegisterVolumes(init bool, volumeInfos []storage.VolumeInfo, ip string, port int, publicUrl string, maxVolumeCount int, dcName string, rackName string) {
  95. dcName, rackName = t.configuration.Locate(ip, dcName, rackName)
  96. dc := t.GetOrCreateDataCenter(dcName)
  97. rack := dc.GetOrCreateRack(rackName)
  98. dn := rack.FindDataNode(ip, port)
  99. if init && dn != nil {
  100. t.UnRegisterDataNode(dn)
  101. }
  102. dn = rack.GetOrCreateDataNode(ip, port, publicUrl, maxVolumeCount)
  103. for _, v := range volumeInfos {
  104. dn.AddOrUpdateVolume(v)
  105. t.RegisterVolumeLayout(&v, dn)
  106. }
  107. }
  108. func (t *Topology) GetOrCreateDataCenter(dcName string) *DataCenter {
  109. for _, c := range t.Children() {
  110. dc := c.(*DataCenter)
  111. if string(dc.Id()) == dcName {
  112. return dc
  113. }
  114. }
  115. dc := NewDataCenter(dcName)
  116. t.LinkChildNode(dc)
  117. return dc
  118. }