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.

141 lines
3.9 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. IsLeader bool
  13. collectionMap map[string]*Collection
  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, seq sequence.Sequencer, 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.collectionMap = make(map[string]*Collection)
  29. t.pulse = int64(pulse)
  30. t.volumeSizeLimit = volumeSizeLimit
  31. t.sequence = seq
  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(collection string, vid storage.VolumeId) []*DataNode {
  49. //maybe an issue if lots of collections?
  50. if collection == "" {
  51. for _, c := range t.collectionMap {
  52. if list := c.Lookup(vid); list != nil {
  53. return list
  54. }
  55. }
  56. } else {
  57. if c, ok := t.collectionMap[collection]; ok {
  58. return c.Lookup(vid)
  59. }
  60. }
  61. return nil
  62. }
  63. func (t *Topology) RandomlyReserveOneVolume(dataCenter string) (bool, *DataNode, *storage.VolumeId) {
  64. if t.FreeSpace() <= 0 {
  65. glog.V(0).Infoln("Topology does not have free space left!")
  66. return false, nil, nil
  67. }
  68. vid := t.NextVolumeId()
  69. ret, node := t.ReserveOneVolume(rand.Intn(t.FreeSpace()), vid, dataCenter)
  70. return ret, node, &vid
  71. }
  72. func (t *Topology) NextVolumeId() storage.VolumeId {
  73. vid := t.GetMaxVolumeId()
  74. return vid.Next()
  75. }
  76. func (t *Topology) PickForWrite(collectionName string, repType storage.ReplicationType, count int, dataCenter string) (string, int, *DataNode, error) {
  77. vid, count, datanodes, err := t.GetVolumeLayout(collectionName, repType).PickForWrite(count, dataCenter)
  78. if err != nil || datanodes.Length() == 0 {
  79. return "", 0, nil, errors.New("No writable volumes avalable!")
  80. }
  81. fileId, count := t.sequence.NextFileId(count)
  82. return storage.NewFileId(*vid, fileId, rand.Uint32()).String(), count, datanodes.Head(), nil
  83. }
  84. func (t *Topology) GetVolumeLayout(collectionName string, repType storage.ReplicationType) *VolumeLayout {
  85. _, ok := t.collectionMap[collectionName]
  86. if !ok {
  87. t.collectionMap[collectionName] = NewCollection(collectionName, t.volumeSizeLimit)
  88. }
  89. return t.collectionMap[collectionName].GetOrCreateVolumeLayout(repType)
  90. }
  91. func (t *Topology) RegisterVolumeLayout(v *storage.VolumeInfo, dn *DataNode) {
  92. t.GetVolumeLayout(v.Collection, 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. }