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.

298 lines
7.9 KiB

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