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
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. 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) NextVolumeId() storage.VolumeId {
  64. vid := t.GetMaxVolumeId()
  65. return vid.Next()
  66. }
  67. func (t *Topology) PickForWrite(collectionName string, rp *storage.ReplicaPlacement, count int, dataCenter string) (string, int, *DataNode, error) {
  68. vid, count, datanodes, err := t.GetVolumeLayout(collectionName, rp).PickForWrite(count, dataCenter)
  69. if err != nil || datanodes.Length() == 0 {
  70. return "", 0, nil, errors.New("No writable volumes avalable!")
  71. }
  72. fileId, count := t.sequence.NextFileId(count)
  73. return storage.NewFileId(*vid, fileId, rand.Uint32()).String(), count, datanodes.Head(), nil
  74. }
  75. func (t *Topology) GetVolumeLayout(collectionName string, rp *storage.ReplicaPlacement) *VolumeLayout {
  76. _, ok := t.collectionMap[collectionName]
  77. if !ok {
  78. t.collectionMap[collectionName] = NewCollection(collectionName, t.volumeSizeLimit)
  79. }
  80. return t.collectionMap[collectionName].GetOrCreateVolumeLayout(rp)
  81. }
  82. func (t *Topology) GetCollection(collectionName string) (collection *Collection, ok bool) {
  83. collection, ok = t.collectionMap[collectionName]
  84. return
  85. }
  86. func (t *Topology) DeleteCollection(collectionName string) {
  87. delete(t.collectionMap, collectionName)
  88. }
  89. func (t *Topology) RegisterVolumeLayout(v *storage.VolumeInfo, dn *DataNode) {
  90. t.GetVolumeLayout(v.Collection, v.ReplicaPlacement).RegisterVolume(v, dn)
  91. }
  92. func (t *Topology) RegisterVolumes(init bool, volumeInfos []storage.VolumeInfo, ip string, port int, publicUrl string, maxVolumeCount int, dcName string, rackName string) {
  93. dcName, rackName = t.configuration.Locate(ip, dcName, rackName)
  94. dc := t.GetOrCreateDataCenter(dcName)
  95. rack := dc.GetOrCreateRack(rackName)
  96. dn := rack.FindDataNode(ip, port)
  97. if init && dn != nil {
  98. t.UnRegisterDataNode(dn)
  99. }
  100. dn = rack.GetOrCreateDataNode(ip, port, publicUrl, maxVolumeCount)
  101. dn.UpdateVolumes(volumeInfos)
  102. for _, v := range volumeInfos {
  103. t.RegisterVolumeLayout(&v, dn)
  104. }
  105. }
  106. func (t *Topology) GetOrCreateDataCenter(dcName string) *DataCenter {
  107. for _, c := range t.Children() {
  108. dc := c.(*DataCenter)
  109. if string(dc.Id()) == dcName {
  110. return dc
  111. }
  112. }
  113. dc := NewDataCenter(dcName)
  114. t.LinkChildNode(dc)
  115. return dc
  116. }