topology.go 3.8 KB

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