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.

139 lines
3.8 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. collectionMap map[string]*Collection
  13. pulse int64
  14. volumeSizeLimit uint64
  15. sequence sequence.Sequencer
  16. chanDeadDataNodes chan *DataNode
  17. chanRecoveredDataNodes chan *DataNode
  18. chanFullVolumes chan storage.VolumeInfo
  19. configuration *Configuration
  20. }
  21. func NewTopology(id string, confFile string, seq sequence.Sequencer, volumeSizeLimit uint64, pulse int) (*Topology, error) {
  22. t := &Topology{}
  23. t.id = NodeId(id)
  24. t.nodeType = "Topology"
  25. t.NodeImpl.value = t
  26. t.children = make(map[NodeId]Node)
  27. t.collectionMap = make(map[string]*Collection)
  28. t.pulse = int64(pulse)
  29. t.volumeSizeLimit = volumeSizeLimit
  30. t.sequence = seq
  31. t.chanDeadDataNodes = make(chan *DataNode)
  32. t.chanRecoveredDataNodes = make(chan *DataNode)
  33. t.chanFullVolumes = make(chan storage.VolumeInfo)
  34. err := t.loadConfiguration(confFile)
  35. return t, err
  36. }
  37. func (t *Topology) loadConfiguration(configurationFile string) error {
  38. b, e := ioutil.ReadFile(configurationFile)
  39. if e == nil {
  40. t.configuration, e = NewConfiguration(b)
  41. return e
  42. } else {
  43. glog.V(0).Infoln("Using default configurations.")
  44. }
  45. return nil
  46. }
  47. func (t *Topology) Lookup(collection string, vid storage.VolumeId) []*DataNode {
  48. //maybe an issue if lots of collections?
  49. if collection == "" {
  50. for _, c := range t.collectionMap {
  51. if list := c.Lookup(vid); list != nil {
  52. return list
  53. }
  54. }
  55. } else {
  56. if c, ok := t.collectionMap[collection]; ok {
  57. return c.Lookup(vid)
  58. }
  59. }
  60. return nil
  61. }
  62. func (t *Topology) RandomlyReserveOneVolume(dataCenter string) (bool, *DataNode, *storage.VolumeId) {
  63. if t.FreeSpace() <= 0 {
  64. glog.V(0).Infoln("Topology does not have free space left!")
  65. return false, nil, nil
  66. }
  67. vid := t.NextVolumeId()
  68. ret, node := t.ReserveOneVolume(rand.Intn(t.FreeSpace()), vid, dataCenter)
  69. return ret, node, &vid
  70. }
  71. func (t *Topology) NextVolumeId() storage.VolumeId {
  72. vid := t.GetMaxVolumeId()
  73. return vid.Next()
  74. }
  75. func (t *Topology) PickForWrite(collectionName string, repType storage.ReplicationType, count int, dataCenter string) (string, int, *DataNode, error) {
  76. vid, count, datanodes, err := t.GetVolumeLayout(collectionName, repType).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(collectionName string, repType storage.ReplicationType) *VolumeLayout {
  84. _, ok := t.collectionMap[collectionName]
  85. if !ok {
  86. t.collectionMap[collectionName] = NewCollection(collectionName, t.volumeSizeLimit)
  87. }
  88. return t.collectionMap[collectionName].GetOrCreateVolumeLayout(repType)
  89. }
  90. func (t *Topology) RegisterVolumeLayout(v *storage.VolumeInfo, dn *DataNode) {
  91. t.GetVolumeLayout(v.Collection, v.RepType).RegisterVolume(v, dn)
  92. }
  93. func (t *Topology) RegisterVolumes(init bool, volumeInfos []storage.VolumeInfo, ip string, port int, publicUrl string, maxVolumeCount int, dcName string, rackName string) {
  94. dcName, rackName = t.configuration.Locate(ip, dcName, rackName)
  95. dc := t.GetOrCreateDataCenter(dcName)
  96. rack := dc.GetOrCreateRack(rackName)
  97. dn := rack.FindDataNode(ip, port)
  98. if init && dn != nil {
  99. t.UnRegisterDataNode(dn)
  100. }
  101. dn = rack.GetOrCreateDataNode(ip, port, publicUrl, maxVolumeCount)
  102. for _, v := range volumeInfos {
  103. dn.AddOrUpdateVolume(v)
  104. t.RegisterVolumeLayout(&v, dn)
  105. }
  106. }
  107. func (t *Topology) GetOrCreateDataCenter(dcName string) *DataCenter {
  108. for _, c := range t.Children() {
  109. dc := c.(*DataCenter)
  110. if string(dc.Id()) == dcName {
  111. return dc
  112. }
  113. }
  114. dc := NewDataCenter(dcName)
  115. t.LinkChildNode(dc)
  116. return dc
  117. }