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.

155 lines
4.1 KiB

10 years ago
10 years ago
  1. package topology
  2. import (
  3. "errors"
  4. "io/ioutil"
  5. "math/rand"
  6. "github.com/chrislusf/raft"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "github.com/chrislusf/seaweedfs/weed/sequence"
  9. "github.com/chrislusf/seaweedfs/weed/storage"
  10. "github.com/chrislusf/seaweedfs/weed/util"
  11. )
  12. type Topology struct {
  13. NodeImpl
  14. collectionMap *util.ConcurrentReadMap
  15. pulse int64
  16. volumeSizeLimit uint64
  17. Sequence sequence.Sequencer
  18. chanFullVolumes chan storage.VolumeInfo
  19. Configuration *Configuration
  20. RaftServer raft.Server
  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 = util.NewConcurrentReadMap()
  29. t.pulse = int64(pulse)
  30. t.volumeSizeLimit = volumeSizeLimit
  31. t.Sequence = seq
  32. t.chanFullVolumes = make(chan storage.VolumeInfo)
  33. err := t.loadConfiguration(confFile)
  34. return t, err
  35. }
  36. func (t *Topology) IsLeader() bool {
  37. if leader, e := t.Leader(); e == nil {
  38. return leader == t.RaftServer.Name()
  39. }
  40. return false
  41. }
  42. func (t *Topology) Leader() (string, error) {
  43. l := ""
  44. if t.RaftServer != nil {
  45. l = t.RaftServer.Leader()
  46. } else {
  47. return "", errors.New("Raft Server not ready yet!")
  48. }
  49. if l == "" {
  50. // We are a single node cluster, we are the leader
  51. return t.RaftServer.Name(), errors.New("Raft Server not initialized!")
  52. }
  53. return l, nil
  54. }
  55. func (t *Topology) loadConfiguration(configurationFile string) error {
  56. b, e := ioutil.ReadFile(configurationFile)
  57. if e == nil {
  58. t.Configuration, e = NewConfiguration(b)
  59. return e
  60. }
  61. glog.V(0).Infoln("Using default configurations.")
  62. return nil
  63. }
  64. func (t *Topology) Lookup(collection string, vid storage.VolumeId) []*DataNode {
  65. //maybe an issue if lots of collections?
  66. if collection == "" {
  67. for _, c := range t.collectionMap.Items() {
  68. if list := c.(*Collection).Lookup(vid); list != nil {
  69. return list
  70. }
  71. }
  72. } else {
  73. if c, ok := t.collectionMap.Find(collection); ok {
  74. return c.(*Collection).Lookup(vid)
  75. }
  76. }
  77. return nil
  78. }
  79. func (t *Topology) NextVolumeId() storage.VolumeId {
  80. vid := t.GetMaxVolumeId()
  81. next := vid.Next()
  82. go t.RaftServer.Do(NewMaxVolumeIdCommand(next))
  83. return next
  84. }
  85. func (t *Topology) HasWritableVolume(option *VolumeGrowOption) bool {
  86. vl := t.GetVolumeLayout(option.Collection, option.ReplicaPlacement, option.Ttl)
  87. return vl.GetActiveVolumeCount(option) > 0
  88. }
  89. func (t *Topology) PickForWrite(count uint64, option *VolumeGrowOption) (string, uint64, *DataNode, error) {
  90. vid, count, datanodes, err := t.GetVolumeLayout(option.Collection, option.ReplicaPlacement, option.Ttl).PickForWrite(count, option)
  91. if err != nil || datanodes.Length() == 0 {
  92. return "", 0, nil, errors.New("No writable volumes available!")
  93. }
  94. fileId, count := t.Sequence.NextFileId(count)
  95. return storage.NewFileId(*vid, fileId, rand.Uint32()).String(), count, datanodes.Head(), nil
  96. }
  97. func (t *Topology) GetVolumeLayout(collectionName string, rp *storage.ReplicaPlacement, ttl *storage.TTL) *VolumeLayout {
  98. return t.collectionMap.Get(collectionName, func() interface{} {
  99. return NewCollection(collectionName, t.volumeSizeLimit)
  100. }).(*Collection).GetOrCreateVolumeLayout(rp, ttl)
  101. }
  102. func (t *Topology) FindCollection(collectionName string) (*Collection, bool) {
  103. c, hasCollection := t.collectionMap.Find(collectionName)
  104. return c.(*Collection), hasCollection
  105. }
  106. func (t *Topology) DeleteCollection(collectionName string) {
  107. t.collectionMap.Delete(collectionName)
  108. }
  109. func (t *Topology) RegisterVolumeLayout(v storage.VolumeInfo, dn *DataNode) {
  110. t.GetVolumeLayout(v.Collection, v.ReplicaPlacement, v.Ttl).RegisterVolume(&v, dn)
  111. }
  112. func (t *Topology) UnRegisterVolumeLayout(v storage.VolumeInfo, dn *DataNode) {
  113. glog.Infof("removing volume info:%+v", v)
  114. t.GetVolumeLayout(v.Collection, v.ReplicaPlacement, v.Ttl).UnRegisterVolume(&v, dn)
  115. }
  116. func (t *Topology) GetOrCreateDataCenter(dcName string) *DataCenter {
  117. for _, c := range t.Children() {
  118. dc := c.(*DataCenter)
  119. if string(dc.Id()) == dcName {
  120. return dc
  121. }
  122. }
  123. dc := NewDataCenter(dcName)
  124. t.LinkChildNode(dc)
  125. return dc
  126. }