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.

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