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.

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