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.

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