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.

226 lines
6.1 KiB

  1. package topology
  2. import (
  3. "errors"
  4. "fmt"
  5. "math/rand"
  6. "sync"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "github.com/chrislusf/seaweedfs/weed/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.RWMutex
  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.Id(), "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. vl.accessLock.RLock()
  66. defer vl.accessLock.RUnlock()
  67. if location := vl.vid2location[vid]; location != nil {
  68. return location.list
  69. }
  70. return nil
  71. }
  72. func (vl *VolumeLayout) ListVolumeServers() (nodes []*DataNode) {
  73. vl.accessLock.RLock()
  74. defer vl.accessLock.RUnlock()
  75. for _, location := range vl.vid2location {
  76. nodes = append(nodes, location.list...)
  77. }
  78. return
  79. }
  80. func (vl *VolumeLayout) PickForWrite(count uint64, option *VolumeGrowOption) (*storage.VolumeId, uint64, *VolumeLocationList, error) {
  81. vl.accessLock.RLock()
  82. defer vl.accessLock.RUnlock()
  83. len_writers := len(vl.writables)
  84. if len_writers <= 0 {
  85. glog.V(0).Infoln("No more writable volumes!")
  86. return nil, 0, nil, errors.New("No more writable volumes!")
  87. }
  88. if option.DataCenter == "" {
  89. vid := vl.writables[rand.Intn(len_writers)]
  90. locationList := vl.vid2location[vid]
  91. if locationList != nil {
  92. return &vid, count, locationList, nil
  93. }
  94. return nil, 0, nil, errors.New("Strangely vid " + vid.String() + " is on no machine!")
  95. }
  96. var vid storage.VolumeId
  97. var locationList *VolumeLocationList
  98. counter := 0
  99. for _, v := range vl.writables {
  100. volumeLocationList := vl.vid2location[v]
  101. for _, dn := range volumeLocationList.list {
  102. if dn.GetDataCenter().Id() == NodeId(option.DataCenter) {
  103. if option.Rack != "" && dn.GetRack().Id() != NodeId(option.Rack) {
  104. continue
  105. }
  106. if option.DataNode != "" && dn.Id() != NodeId(option.DataNode) {
  107. continue
  108. }
  109. counter++
  110. if rand.Intn(counter) < 1 {
  111. vid, locationList = v, volumeLocationList
  112. }
  113. }
  114. }
  115. }
  116. return &vid, count, locationList, nil
  117. }
  118. func (vl *VolumeLayout) GetActiveVolumeCount(option *VolumeGrowOption) int {
  119. vl.accessLock.RLock()
  120. defer vl.accessLock.RUnlock()
  121. if option.DataCenter == "" {
  122. return len(vl.writables)
  123. }
  124. counter := 0
  125. for _, v := range vl.writables {
  126. for _, dn := range vl.vid2location[v].list {
  127. if dn.GetDataCenter().Id() == NodeId(option.DataCenter) {
  128. if option.Rack != "" && dn.GetRack().Id() != NodeId(option.Rack) {
  129. continue
  130. }
  131. if option.DataNode != "" && dn.Id() != NodeId(option.DataNode) {
  132. continue
  133. }
  134. counter++
  135. }
  136. }
  137. }
  138. return counter
  139. }
  140. func (vl *VolumeLayout) removeFromWritable(vid storage.VolumeId) bool {
  141. toDeleteIndex := -1
  142. for k, id := range vl.writables {
  143. if id == vid {
  144. toDeleteIndex = k
  145. break
  146. }
  147. }
  148. if toDeleteIndex >= 0 {
  149. glog.V(0).Infoln("Volume", vid, "becomes unwritable")
  150. vl.writables = append(vl.writables[0:toDeleteIndex], vl.writables[toDeleteIndex+1:]...)
  151. return true
  152. }
  153. return false
  154. }
  155. func (vl *VolumeLayout) setVolumeWritable(vid storage.VolumeId) bool {
  156. for _, v := range vl.writables {
  157. if v == vid {
  158. return false
  159. }
  160. }
  161. glog.V(0).Infoln("Volume", vid, "becomes writable")
  162. vl.writables = append(vl.writables, vid)
  163. return true
  164. }
  165. func (vl *VolumeLayout) SetVolumeUnavailable(dn *DataNode, vid storage.VolumeId) bool {
  166. vl.accessLock.Lock()
  167. defer vl.accessLock.Unlock()
  168. if location, ok := vl.vid2location[vid]; ok {
  169. if location.Remove(dn) {
  170. if location.Length() < vl.rp.GetCopyCount() {
  171. glog.V(0).Infoln("Volume", vid, "has", location.Length(), "replica, less than required", vl.rp.GetCopyCount())
  172. return vl.removeFromWritable(vid)
  173. }
  174. }
  175. }
  176. return false
  177. }
  178. func (vl *VolumeLayout) SetVolumeAvailable(dn *DataNode, vid storage.VolumeId) bool {
  179. vl.accessLock.Lock()
  180. defer vl.accessLock.Unlock()
  181. vl.vid2location[vid].Set(dn)
  182. if vl.vid2location[vid].Length() >= vl.rp.GetCopyCount() {
  183. return vl.setVolumeWritable(vid)
  184. }
  185. return false
  186. }
  187. func (vl *VolumeLayout) SetVolumeCapacityFull(vid storage.VolumeId) bool {
  188. vl.accessLock.Lock()
  189. defer vl.accessLock.Unlock()
  190. // glog.V(0).Infoln("Volume", vid, "reaches full capacity.")
  191. return vl.removeFromWritable(vid)
  192. }
  193. func (vl *VolumeLayout) ToMap() map[string]interface{} {
  194. m := make(map[string]interface{})
  195. m["replication"] = vl.rp.String()
  196. m["ttl"] = vl.ttl.String()
  197. m["writables"] = vl.writables
  198. //m["locations"] = vl.vid2location
  199. return m
  200. }