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.

214 lines
5.9 KiB

  1. package topology
  2. import (
  3. "errors"
  4. "fmt"
  5. "math/rand"
  6. "sync"
  7. "github.com/chrislusf/seaweedfs/go/glog"
  8. "github.com/chrislusf/seaweedfs/go/storage"
  9. )
  10. // mapping from volume to its locations, inverted from server to volume
  11. type VolumeLayout struct {
  12. rp *storage.ReplicaPlacement
  13. ttl *storage.TTL
  14. vid2location map[storage.VolumeId]*VolumeLocationList
  15. writables []storage.VolumeId // transient array of writable volume id
  16. volumeSizeLimit uint64
  17. accessLock sync.Mutex
  18. }
  19. func NewVolumeLayout(rp *storage.ReplicaPlacement, ttl *storage.TTL, volumeSizeLimit uint64) *VolumeLayout {
  20. return &VolumeLayout{
  21. rp: rp,
  22. ttl: ttl,
  23. vid2location: make(map[storage.VolumeId]*VolumeLocationList),
  24. writables: *new([]storage.VolumeId),
  25. volumeSizeLimit: volumeSizeLimit,
  26. }
  27. }
  28. func (vl *VolumeLayout) String() string {
  29. return fmt.Sprintf("rp:%v, ttl:%v, vid2location:%v, writables:%v, volumeSizeLimit:%v", vl.rp, vl.ttl, vl.vid2location, vl.writables, vl.volumeSizeLimit)
  30. }
  31. func (vl *VolumeLayout) RegisterVolume(v *storage.VolumeInfo, dn *DataNode) {
  32. vl.accessLock.Lock()
  33. defer vl.accessLock.Unlock()
  34. if _, ok := vl.vid2location[v.Id]; !ok {
  35. vl.vid2location[v.Id] = NewVolumeLocationList()
  36. }
  37. vl.vid2location[v.Id].Set(dn)
  38. glog.V(4).Infoln("volume", v.Id, "added to dn", dn, "len", vl.vid2location[v.Id].Length(), "copy", v.ReplicaPlacement.GetCopyCount())
  39. if vl.vid2location[v.Id].Length() == vl.rp.GetCopyCount() && vl.isWritable(v) {
  40. vl.AddToWritable(v.Id)
  41. } else {
  42. vl.removeFromWritable(v.Id)
  43. }
  44. }
  45. func (vl *VolumeLayout) UnRegisterVolume(v *storage.VolumeInfo, dn *DataNode) {
  46. vl.accessLock.Lock()
  47. defer vl.accessLock.Unlock()
  48. vl.removeFromWritable(v.Id)
  49. delete(vl.vid2location, v.Id)
  50. }
  51. func (vl *VolumeLayout) AddToWritable(vid storage.VolumeId) {
  52. for _, id := range vl.writables {
  53. if vid == id {
  54. return
  55. }
  56. }
  57. vl.writables = append(vl.writables, vid)
  58. }
  59. func (vl *VolumeLayout) isWritable(v *storage.VolumeInfo) bool {
  60. return uint64(v.Size) < vl.volumeSizeLimit &&
  61. v.Version == storage.CurrentVersion &&
  62. !v.ReadOnly
  63. }
  64. func (vl *VolumeLayout) Lookup(vid storage.VolumeId) []*DataNode {
  65. if location := vl.vid2location[vid]; location != nil {
  66. return location.list
  67. }
  68. return nil
  69. }
  70. func (vl *VolumeLayout) ListVolumeServers() (nodes []*DataNode) {
  71. for _, location := range vl.vid2location {
  72. nodes = append(nodes, location.list...)
  73. }
  74. return
  75. }
  76. func (vl *VolumeLayout) PickForWrite(count uint64, option *VolumeGrowOption) (*storage.VolumeId, uint64, *VolumeLocationList, error) {
  77. len_writers := len(vl.writables)
  78. if len_writers <= 0 {
  79. glog.V(0).Infoln("No more writable volumes!")
  80. return nil, 0, nil, errors.New("No more writable volumes!")
  81. }
  82. if option.DataCenter == "" {
  83. vid := vl.writables[rand.Intn(len_writers)]
  84. locationList := vl.vid2location[vid]
  85. if locationList != nil {
  86. return &vid, count, locationList, nil
  87. }
  88. return nil, 0, nil, errors.New("Strangely vid " + vid.String() + " is on no machine!")
  89. }
  90. var vid storage.VolumeId
  91. var locationList *VolumeLocationList
  92. counter := 0
  93. for _, v := range vl.writables {
  94. volumeLocationList := vl.vid2location[v]
  95. for _, dn := range volumeLocationList.list {
  96. if dn.GetDataCenter().Id() == NodeId(option.DataCenter) {
  97. if option.Rack != "" && dn.GetRack().Id() != NodeId(option.Rack) {
  98. continue
  99. }
  100. if option.DataNode != "" && dn.Id() != NodeId(option.DataNode) {
  101. continue
  102. }
  103. counter++
  104. if rand.Intn(counter) < 1 {
  105. vid, locationList = v, volumeLocationList
  106. }
  107. }
  108. }
  109. }
  110. return &vid, count, locationList, nil
  111. }
  112. func (vl *VolumeLayout) GetActiveVolumeCount(option *VolumeGrowOption) int {
  113. if option.DataCenter == "" {
  114. return len(vl.writables)
  115. }
  116. counter := 0
  117. for _, v := range vl.writables {
  118. for _, dn := range vl.vid2location[v].list {
  119. if dn.GetDataCenter().Id() == NodeId(option.DataCenter) {
  120. if option.Rack != "" && dn.GetRack().Id() != NodeId(option.Rack) {
  121. continue
  122. }
  123. if option.DataNode != "" && dn.Id() != NodeId(option.DataNode) {
  124. continue
  125. }
  126. counter++
  127. }
  128. }
  129. }
  130. return counter
  131. }
  132. func (vl *VolumeLayout) removeFromWritable(vid storage.VolumeId) bool {
  133. toDeleteIndex := -1
  134. for k, id := range vl.writables {
  135. if id == vid {
  136. toDeleteIndex = k
  137. break
  138. }
  139. }
  140. if toDeleteIndex >= 0 {
  141. glog.V(0).Infoln("Volume", vid, "becomes unwritable")
  142. vl.writables = append(vl.writables[0:toDeleteIndex], vl.writables[toDeleteIndex+1:]...)
  143. return true
  144. }
  145. return false
  146. }
  147. func (vl *VolumeLayout) setVolumeWritable(vid storage.VolumeId) bool {
  148. for _, v := range vl.writables {
  149. if v == vid {
  150. return false
  151. }
  152. }
  153. glog.V(0).Infoln("Volume", vid, "becomes writable")
  154. vl.writables = append(vl.writables, vid)
  155. return true
  156. }
  157. func (vl *VolumeLayout) SetVolumeUnavailable(dn *DataNode, vid storage.VolumeId) bool {
  158. vl.accessLock.Lock()
  159. defer vl.accessLock.Unlock()
  160. if location, ok := vl.vid2location[vid]; ok {
  161. if location.Remove(dn) {
  162. if location.Length() < vl.rp.GetCopyCount() {
  163. glog.V(0).Infoln("Volume", vid, "has", location.Length(), "replica, less than required", vl.rp.GetCopyCount())
  164. return vl.removeFromWritable(vid)
  165. }
  166. }
  167. }
  168. return false
  169. }
  170. func (vl *VolumeLayout) SetVolumeAvailable(dn *DataNode, vid storage.VolumeId) bool {
  171. vl.accessLock.Lock()
  172. defer vl.accessLock.Unlock()
  173. vl.vid2location[vid].Set(dn)
  174. if vl.vid2location[vid].Length() >= vl.rp.GetCopyCount() {
  175. return vl.setVolumeWritable(vid)
  176. }
  177. return false
  178. }
  179. func (vl *VolumeLayout) SetVolumeCapacityFull(vid storage.VolumeId) bool {
  180. vl.accessLock.Lock()
  181. defer vl.accessLock.Unlock()
  182. // glog.V(0).Infoln("Volume", vid, "reaches full capacity.")
  183. return vl.removeFromWritable(vid)
  184. }
  185. func (vl *VolumeLayout) ToMap() map[string]interface{} {
  186. m := make(map[string]interface{})
  187. m["replication"] = vl.rp.String()
  188. m["ttl"] = vl.ttl.String()
  189. m["writables"] = vl.writables
  190. //m["locations"] = vl.vid2location
  191. return m
  192. }