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.

267 lines
7.3 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) isEmpty() bool {
  97. vl.accessLock.RLock()
  98. defer vl.accessLock.RUnlock()
  99. return len(vl.vid2location) == 0
  100. }
  101. func (vl *VolumeLayout) Lookup(vid storage.VolumeId) []*DataNode {
  102. vl.accessLock.RLock()
  103. defer vl.accessLock.RUnlock()
  104. if location := vl.vid2location[vid]; location != nil {
  105. return location.list
  106. }
  107. return nil
  108. }
  109. func (vl *VolumeLayout) ListVolumeServers() (nodes []*DataNode) {
  110. vl.accessLock.RLock()
  111. defer vl.accessLock.RUnlock()
  112. for _, location := range vl.vid2location {
  113. nodes = append(nodes, location.list...)
  114. }
  115. return
  116. }
  117. func (vl *VolumeLayout) PickForWrite(count uint64, option *VolumeGrowOption) (*storage.VolumeId, uint64, *VolumeLocationList, error) {
  118. vl.accessLock.RLock()
  119. defer vl.accessLock.RUnlock()
  120. len_writers := len(vl.writables)
  121. if len_writers <= 0 {
  122. glog.V(0).Infoln("No more writable volumes!")
  123. return nil, 0, nil, errors.New("No more writable volumes!")
  124. }
  125. if option.DataCenter == "" {
  126. vid := vl.writables[rand.Intn(len_writers)]
  127. locationList := vl.vid2location[vid]
  128. if locationList != nil {
  129. return &vid, count, locationList, nil
  130. }
  131. return nil, 0, nil, errors.New("Strangely vid " + vid.String() + " is on no machine!")
  132. }
  133. var vid storage.VolumeId
  134. var locationList *VolumeLocationList
  135. counter := 0
  136. for _, v := range vl.writables {
  137. volumeLocationList := vl.vid2location[v]
  138. for _, dn := range volumeLocationList.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. if rand.Intn(counter) < 1 {
  148. vid, locationList = v, volumeLocationList
  149. }
  150. }
  151. }
  152. }
  153. return &vid, count, locationList, nil
  154. }
  155. func (vl *VolumeLayout) GetActiveVolumeCount(option *VolumeGrowOption) int {
  156. vl.accessLock.RLock()
  157. defer vl.accessLock.RUnlock()
  158. if option.DataCenter == "" {
  159. return len(vl.writables)
  160. }
  161. counter := 0
  162. for _, v := range vl.writables {
  163. for _, dn := range vl.vid2location[v].list {
  164. if dn.GetDataCenter().Id() == NodeId(option.DataCenter) {
  165. if option.Rack != "" && dn.GetRack().Id() != NodeId(option.Rack) {
  166. continue
  167. }
  168. if option.DataNode != "" && dn.Id() != NodeId(option.DataNode) {
  169. continue
  170. }
  171. counter++
  172. }
  173. }
  174. }
  175. return counter
  176. }
  177. func (vl *VolumeLayout) removeFromWritable(vid storage.VolumeId) bool {
  178. toDeleteIndex := -1
  179. for k, id := range vl.writables {
  180. if id == vid {
  181. toDeleteIndex = k
  182. break
  183. }
  184. }
  185. if toDeleteIndex >= 0 {
  186. glog.V(0).Infoln("Volume", vid, "becomes unwritable")
  187. vl.writables = append(vl.writables[0:toDeleteIndex], vl.writables[toDeleteIndex+1:]...)
  188. return true
  189. }
  190. return false
  191. }
  192. func (vl *VolumeLayout) setVolumeWritable(vid storage.VolumeId) bool {
  193. for _, v := range vl.writables {
  194. if v == vid {
  195. return false
  196. }
  197. }
  198. glog.V(0).Infoln("Volume", vid, "becomes writable")
  199. vl.writables = append(vl.writables, vid)
  200. return true
  201. }
  202. func (vl *VolumeLayout) SetVolumeUnavailable(dn *DataNode, vid storage.VolumeId) bool {
  203. vl.accessLock.Lock()
  204. defer vl.accessLock.Unlock()
  205. if location, ok := vl.vid2location[vid]; ok {
  206. if location.Remove(dn) {
  207. if location.Length() < vl.rp.GetCopyCount() {
  208. glog.V(0).Infoln("Volume", vid, "has", location.Length(), "replica, less than required", vl.rp.GetCopyCount())
  209. return vl.removeFromWritable(vid)
  210. }
  211. }
  212. }
  213. return false
  214. }
  215. func (vl *VolumeLayout) SetVolumeAvailable(dn *DataNode, vid storage.VolumeId) bool {
  216. vl.accessLock.Lock()
  217. defer vl.accessLock.Unlock()
  218. vl.vid2location[vid].Set(dn)
  219. if vl.vid2location[vid].Length() >= vl.rp.GetCopyCount() {
  220. return vl.setVolumeWritable(vid)
  221. }
  222. return false
  223. }
  224. func (vl *VolumeLayout) SetVolumeCapacityFull(vid storage.VolumeId) bool {
  225. vl.accessLock.Lock()
  226. defer vl.accessLock.Unlock()
  227. // glog.V(0).Infoln("Volume", vid, "reaches full capacity.")
  228. return vl.removeFromWritable(vid)
  229. }
  230. func (vl *VolumeLayout) ToMap() map[string]interface{} {
  231. m := make(map[string]interface{})
  232. m["replication"] = vl.rp.String()
  233. m["ttl"] = vl.ttl.String()
  234. m["writables"] = vl.writables
  235. //m["locations"] = vl.vid2location
  236. return m
  237. }