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.

296 lines
7.8 KiB

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