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.

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