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.

189 lines
5.3 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/operation"
  9. "github.com/chrislusf/seaweedfs/weed/sequence"
  10. "github.com/chrislusf/seaweedfs/weed/storage"
  11. "github.com/chrislusf/seaweedfs/weed/util"
  12. )
  13. type Topology struct {
  14. NodeImpl
  15. collectionMap *util.ConcurrentReadMap
  16. pulse int64
  17. volumeSizeLimit uint64
  18. Sequence sequence.Sequencer
  19. chanDeadDataNodes chan *DataNode
  20. chanRecoveredDataNodes chan *DataNode
  21. chanFullVolumes chan storage.VolumeInfo
  22. configuration *Configuration
  23. RaftServer raft.Server
  24. }
  25. func NewTopology(id string, confFile string, seq sequence.Sequencer, volumeSizeLimit uint64, pulse int) (*Topology, error) {
  26. t := &Topology{}
  27. t.id = NodeId(id)
  28. t.nodeType = "Topology"
  29. t.NodeImpl.value = t
  30. t.children = make(map[NodeId]Node)
  31. t.collectionMap = util.NewConcurrentReadMap()
  32. t.pulse = int64(pulse)
  33. t.volumeSizeLimit = volumeSizeLimit
  34. t.Sequence = seq
  35. t.chanDeadDataNodes = make(chan *DataNode)
  36. t.chanRecoveredDataNodes = make(chan *DataNode)
  37. t.chanFullVolumes = make(chan storage.VolumeInfo)
  38. err := t.loadConfiguration(confFile)
  39. return t, err
  40. }
  41. func (t *Topology) IsLeader() bool {
  42. if leader, e := t.Leader(); e == nil {
  43. return leader == t.RaftServer.Name()
  44. }
  45. return false
  46. }
  47. func (t *Topology) Leader() (string, error) {
  48. l := ""
  49. if t.RaftServer != nil {
  50. l = t.RaftServer.Leader()
  51. } else {
  52. return "", errors.New("Raft Server not ready yet!")
  53. }
  54. if l == "" {
  55. // We are a single node cluster, we are the leader
  56. return t.RaftServer.Name(), errors.New("Raft Server not initialized!")
  57. }
  58. return l, nil
  59. }
  60. func (t *Topology) loadConfiguration(configurationFile string) error {
  61. b, e := ioutil.ReadFile(configurationFile)
  62. if e == nil {
  63. t.configuration, e = NewConfiguration(b)
  64. return e
  65. }
  66. glog.V(0).Infoln("Using default configurations.")
  67. return nil
  68. }
  69. func (t *Topology) Lookup(collection string, vid storage.VolumeId) []*DataNode {
  70. //maybe an issue if lots of collections?
  71. if collection == "" {
  72. for _, c := range t.collectionMap.Items() {
  73. if list := c.(*Collection).Lookup(vid); list != nil {
  74. return list
  75. }
  76. }
  77. } else {
  78. if c, ok := t.collectionMap.Find(collection); ok {
  79. return c.(*Collection).Lookup(vid)
  80. }
  81. }
  82. return nil
  83. }
  84. func (t *Topology) NextVolumeId() storage.VolumeId {
  85. vid := t.GetMaxVolumeId()
  86. next := vid.Next()
  87. go t.RaftServer.Do(NewMaxVolumeIdCommand(next))
  88. return next
  89. }
  90. func (t *Topology) HasWritableVolume(option *VolumeGrowOption) bool {
  91. vl := t.GetVolumeLayout(option.Collection, option.ReplicaPlacement, option.Ttl)
  92. return vl.GetActiveVolumeCount(option) > 0
  93. }
  94. func (t *Topology) PickForWrite(count uint64, option *VolumeGrowOption) (string, uint64, *DataNode, error) {
  95. vid, count, datanodes, err := t.GetVolumeLayout(option.Collection, option.ReplicaPlacement, option.Ttl).PickForWrite(count, option)
  96. if err != nil || datanodes.Length() == 0 {
  97. return "", 0, nil, errors.New("No writable volumes available!")
  98. }
  99. fileId, count := t.Sequence.NextFileId(count)
  100. return storage.NewFileId(*vid, fileId, rand.Uint32()).String(), count, datanodes.Head(), nil
  101. }
  102. func (t *Topology) GetVolumeLayout(collectionName string, rp *storage.ReplicaPlacement, ttl *storage.TTL) *VolumeLayout {
  103. return t.collectionMap.Get(collectionName, func() interface{} {
  104. return NewCollection(collectionName, t.volumeSizeLimit)
  105. }).(*Collection).GetOrCreateVolumeLayout(rp, ttl)
  106. }
  107. func (t *Topology) FindCollection(collectionName string) (*Collection, bool) {
  108. c, hasCollection := t.collectionMap.Find(collectionName)
  109. return c.(*Collection), hasCollection
  110. }
  111. func (t *Topology) DeleteCollection(collectionName string) {
  112. t.collectionMap.Delete(collectionName)
  113. }
  114. func (t *Topology) RegisterVolumeLayout(v storage.VolumeInfo, dn *DataNode) {
  115. t.GetVolumeLayout(v.Collection, v.ReplicaPlacement, v.Ttl).RegisterVolume(&v, dn)
  116. }
  117. func (t *Topology) UnRegisterVolumeLayout(v storage.VolumeInfo, dn *DataNode) {
  118. glog.Infof("removing volume info:%+v", v)
  119. t.GetVolumeLayout(v.Collection, v.ReplicaPlacement, v.Ttl).UnRegisterVolume(&v, dn)
  120. }
  121. func (t *Topology) ProcessJoinMessage(joinMessage *operation.JoinMessage) {
  122. t.Sequence.SetMax(*joinMessage.MaxFileKey)
  123. dcName, rackName := t.configuration.Locate(*joinMessage.Ip, *joinMessage.DataCenter, *joinMessage.Rack)
  124. dc := t.GetOrCreateDataCenter(dcName)
  125. rack := dc.GetOrCreateRack(rackName)
  126. dn := rack.FindDataNode(*joinMessage.Ip, int(*joinMessage.Port))
  127. if *joinMessage.IsInit && dn != nil {
  128. t.UnRegisterDataNode(dn)
  129. }
  130. dn = rack.GetOrCreateDataNode(*joinMessage.Ip,
  131. int(*joinMessage.Port), *joinMessage.PublicUrl,
  132. int(*joinMessage.MaxVolumeCount))
  133. var volumeInfos []storage.VolumeInfo
  134. for _, v := range joinMessage.Volumes {
  135. if vi, err := storage.NewVolumeInfo(v); err == nil {
  136. volumeInfos = append(volumeInfos, vi)
  137. } else {
  138. glog.V(0).Infoln("Fail to convert joined volume information:", err.Error())
  139. }
  140. }
  141. deletedVolumes := dn.UpdateVolumes(volumeInfos)
  142. for _, v := range volumeInfos {
  143. t.RegisterVolumeLayout(v, dn)
  144. }
  145. for _, v := range deletedVolumes {
  146. t.UnRegisterVolumeLayout(v, dn)
  147. }
  148. }
  149. func (t *Topology) GetOrCreateDataCenter(dcName string) *DataCenter {
  150. for _, c := range t.Children() {
  151. dc := c.(*DataCenter)
  152. if string(dc.Id()) == dcName {
  153. return dc
  154. }
  155. }
  156. dc := NewDataCenter(dcName)
  157. t.LinkChildNode(dc)
  158. return dc
  159. }