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.

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