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.

297 lines
7.8 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 {
  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(3).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(3).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)
  86. }
  87. func (vl *VolumeLayout) addToWritable(vid needle.VolumeId) {
  88. for _, id := range vl.writables {
  89. if vid == id {
  90. return
  91. }
  92. }
  93. vl.writables = append(vl.writables, vid)
  94. }
  95. func (vl *VolumeLayout) isOversized(v *storage.VolumeInfo) bool {
  96. return uint64(v.Size) >= vl.volumeSizeLimit
  97. }
  98. func (vl *VolumeLayout) isWritable(v *storage.VolumeInfo) bool {
  99. return !vl.isOversized(v) &&
  100. v.Version == needle.CurrentVersion &&
  101. !v.ReadOnly
  102. }
  103. func (vl *VolumeLayout) isEmpty() bool {
  104. vl.accessLock.RLock()
  105. defer vl.accessLock.RUnlock()
  106. return len(vl.vid2location) == 0
  107. }
  108. func (vl *VolumeLayout) Lookup(vid needle.VolumeId) []*DataNode {
  109. vl.accessLock.RLock()
  110. defer vl.accessLock.RUnlock()
  111. if location := vl.vid2location[vid]; location != nil {
  112. return location.list
  113. }
  114. return nil
  115. }
  116. func (vl *VolumeLayout) ListVolumeServers() (nodes []*DataNode) {
  117. vl.accessLock.RLock()
  118. defer vl.accessLock.RUnlock()
  119. for _, location := range vl.vid2location {
  120. nodes = append(nodes, location.list...)
  121. }
  122. return
  123. }
  124. func (vl *VolumeLayout) PickForWrite(count uint64, option *VolumeGrowOption) (*needle.VolumeId, uint64, *VolumeLocationList, error) {
  125. vl.accessLock.RLock()
  126. defer vl.accessLock.RUnlock()
  127. lenWriters := len(vl.writables)
  128. if lenWriters <= 0 {
  129. glog.V(0).Infoln("No more writable volumes!")
  130. return nil, 0, nil, errors.New("No more writable volumes!")
  131. }
  132. if option.DataCenter == "" {
  133. vid := vl.writables[rand.Intn(lenWriters)]
  134. locationList := vl.vid2location[vid]
  135. if locationList != nil {
  136. return &vid, count, locationList, nil
  137. }
  138. return nil, 0, nil, errors.New("Strangely vid " + vid.String() + " is on no machine!")
  139. }
  140. var vid needle.VolumeId
  141. var locationList *VolumeLocationList
  142. counter := 0
  143. for _, v := range vl.writables {
  144. volumeLocationList := vl.vid2location[v]
  145. for _, dn := range volumeLocationList.list {
  146. if dn.GetDataCenter().Id() == NodeId(option.DataCenter) {
  147. if option.Rack != "" && dn.GetRack().Id() != NodeId(option.Rack) {
  148. continue
  149. }
  150. if option.DataNode != "" && dn.Id() != NodeId(option.DataNode) {
  151. continue
  152. }
  153. counter++
  154. if rand.Intn(counter) < 1 {
  155. vid, locationList = v, volumeLocationList
  156. }
  157. }
  158. }
  159. }
  160. return &vid, count, locationList, nil
  161. }
  162. func (vl *VolumeLayout) GetActiveVolumeCount(option *VolumeGrowOption) int {
  163. vl.accessLock.RLock()
  164. defer vl.accessLock.RUnlock()
  165. if option.DataCenter == "" {
  166. return len(vl.writables)
  167. }
  168. counter := 0
  169. for _, v := range vl.writables {
  170. for _, dn := range vl.vid2location[v].list {
  171. if dn.GetDataCenter().Id() == NodeId(option.DataCenter) {
  172. if option.Rack != "" && dn.GetRack().Id() != NodeId(option.Rack) {
  173. continue
  174. }
  175. if option.DataNode != "" && dn.Id() != NodeId(option.DataNode) {
  176. continue
  177. }
  178. counter++
  179. }
  180. }
  181. }
  182. return counter
  183. }
  184. func (vl *VolumeLayout) removeFromWritable(vid needle.VolumeId) bool {
  185. toDeleteIndex := -1
  186. for k, id := range vl.writables {
  187. if id == vid {
  188. toDeleteIndex = k
  189. break
  190. }
  191. }
  192. if toDeleteIndex >= 0 {
  193. glog.V(0).Infoln("Volume", vid, "becomes unwritable")
  194. vl.writables = append(vl.writables[0:toDeleteIndex], vl.writables[toDeleteIndex+1:]...)
  195. return true
  196. }
  197. return false
  198. }
  199. func (vl *VolumeLayout) setVolumeWritable(vid needle.VolumeId) bool {
  200. for _, v := range vl.writables {
  201. if v == vid {
  202. return false
  203. }
  204. }
  205. glog.V(0).Infoln("Volume", vid, "becomes writable")
  206. vl.writables = append(vl.writables, vid)
  207. return true
  208. }
  209. func (vl *VolumeLayout) SetVolumeUnavailable(dn *DataNode, vid needle.VolumeId) bool {
  210. vl.accessLock.Lock()
  211. defer vl.accessLock.Unlock()
  212. if location, ok := vl.vid2location[vid]; ok {
  213. if location.Remove(dn) {
  214. if location.Length() < vl.rp.GetCopyCount() {
  215. glog.V(0).Infoln("Volume", vid, "has", location.Length(), "replica, less than required", vl.rp.GetCopyCount())
  216. return vl.removeFromWritable(vid)
  217. }
  218. }
  219. }
  220. return false
  221. }
  222. func (vl *VolumeLayout) SetVolumeAvailable(dn *DataNode, vid needle.VolumeId) bool {
  223. vl.accessLock.Lock()
  224. defer vl.accessLock.Unlock()
  225. vl.vid2location[vid].Set(dn)
  226. if vl.vid2location[vid].Length() >= vl.rp.GetCopyCount() {
  227. return vl.setVolumeWritable(vid)
  228. }
  229. return false
  230. }
  231. func (vl *VolumeLayout) SetVolumeCapacityFull(vid needle.VolumeId) bool {
  232. vl.accessLock.Lock()
  233. defer vl.accessLock.Unlock()
  234. // glog.V(0).Infoln("Volume", vid, "reaches full capacity.")
  235. return vl.removeFromWritable(vid)
  236. }
  237. func (vl *VolumeLayout) ToMap() map[string]interface{} {
  238. m := make(map[string]interface{})
  239. m["replication"] = vl.rp.String()
  240. m["ttl"] = vl.ttl.String()
  241. m["writables"] = vl.writables
  242. //m["locations"] = vl.vid2location
  243. return m
  244. }
  245. func (vl *VolumeLayout) Stats() *VolumeLayoutStats {
  246. vl.accessLock.RLock()
  247. defer vl.accessLock.RUnlock()
  248. ret := &VolumeLayoutStats{}
  249. freshThreshold := time.Now().Unix() - 60
  250. for vid, vll := range vl.vid2location {
  251. size, fileCount := vll.Stats(vid, freshThreshold)
  252. ret.FileCount += uint64(fileCount)
  253. ret.UsedSize += size
  254. if vl.readonlyVolumes[vid] {
  255. ret.TotalSize += size
  256. } else {
  257. ret.TotalSize += vl.volumeSizeLimit
  258. }
  259. }
  260. return ret
  261. }