volume_layout.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. package topology
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/storage/types"
  6. "math/rand"
  7. "sync"
  8. "time"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/storage"
  11. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  12. "github.com/chrislusf/seaweedfs/weed/storage/super_block"
  13. )
  14. type copyState int
  15. const (
  16. noCopies copyState = 0 + iota
  17. insufficientCopies
  18. enoughCopies
  19. )
  20. type volumeState string
  21. const (
  22. readOnlyState volumeState = "ReadOnly"
  23. oversizedState = "Oversized"
  24. crowdedState = "Crowded"
  25. )
  26. type stateIndicator func(copyState) bool
  27. func ExistCopies() stateIndicator {
  28. return func(state copyState) bool { return state != noCopies }
  29. }
  30. func NoCopies() stateIndicator {
  31. return func(state copyState) bool { return state == noCopies }
  32. }
  33. type volumesBinaryState struct {
  34. rp *super_block.ReplicaPlacement
  35. name volumeState // the name for volume state (eg. "Readonly", "Oversized")
  36. indicator stateIndicator // indicate whether the volumes should be marked as `name`
  37. copyMap map[needle.VolumeId]*VolumeLocationList
  38. }
  39. func NewVolumesBinaryState(name volumeState, rp *super_block.ReplicaPlacement, indicator stateIndicator) *volumesBinaryState {
  40. return &volumesBinaryState{
  41. rp: rp,
  42. name: name,
  43. indicator: indicator,
  44. copyMap: make(map[needle.VolumeId]*VolumeLocationList),
  45. }
  46. }
  47. func (v *volumesBinaryState) Dump() (res []uint32) {
  48. for vid, list := range v.copyMap {
  49. if v.indicator(v.copyState(list)) {
  50. res = append(res, uint32(vid))
  51. }
  52. }
  53. return
  54. }
  55. func (v *volumesBinaryState) IsTrue(vid needle.VolumeId) bool {
  56. list, _ := v.copyMap[vid]
  57. return v.indicator(v.copyState(list))
  58. }
  59. func (v *volumesBinaryState) Add(vid needle.VolumeId, dn *DataNode) {
  60. list, _ := v.copyMap[vid]
  61. if list != nil {
  62. list.Set(dn)
  63. return
  64. }
  65. list = NewVolumeLocationList()
  66. list.Set(dn)
  67. v.copyMap[vid] = list
  68. }
  69. func (v *volumesBinaryState) Remove(vid needle.VolumeId, dn *DataNode) {
  70. list, _ := v.copyMap[vid]
  71. if list != nil {
  72. list.Remove(dn)
  73. if list.Length() == 0 {
  74. delete(v.copyMap, vid)
  75. }
  76. }
  77. }
  78. func (v *volumesBinaryState) copyState(list *VolumeLocationList) copyState {
  79. if list == nil {
  80. return noCopies
  81. }
  82. if list.Length() < v.rp.GetCopyCount() {
  83. return insufficientCopies
  84. }
  85. return enoughCopies
  86. }
  87. // mapping from volume to its locations, inverted from server to volume
  88. type VolumeLayout struct {
  89. rp *super_block.ReplicaPlacement
  90. ttl *needle.TTL
  91. diskType types.DiskType
  92. vid2location map[needle.VolumeId]*VolumeLocationList
  93. writables []needle.VolumeId // transient array of writable volume id
  94. crowded map[needle.VolumeId]struct{}
  95. readonlyVolumes *volumesBinaryState // readonly volumes
  96. oversizedVolumes *volumesBinaryState // oversized volumes
  97. volumeSizeLimit uint64
  98. replicationAsMin bool
  99. accessLock sync.RWMutex
  100. }
  101. type VolumeLayoutStats struct {
  102. TotalSize uint64
  103. UsedSize uint64
  104. FileCount uint64
  105. }
  106. func NewVolumeLayout(rp *super_block.ReplicaPlacement, ttl *needle.TTL, diskType types.DiskType, volumeSizeLimit uint64, replicationAsMin bool) *VolumeLayout {
  107. return &VolumeLayout{
  108. rp: rp,
  109. ttl: ttl,
  110. diskType: diskType,
  111. vid2location: make(map[needle.VolumeId]*VolumeLocationList),
  112. writables: *new([]needle.VolumeId),
  113. crowded: make(map[needle.VolumeId]struct{}),
  114. readonlyVolumes: NewVolumesBinaryState(readOnlyState, rp, ExistCopies()),
  115. oversizedVolumes: NewVolumesBinaryState(oversizedState, rp, ExistCopies()),
  116. volumeSizeLimit: volumeSizeLimit,
  117. replicationAsMin: replicationAsMin,
  118. }
  119. }
  120. func (vl *VolumeLayout) String() string {
  121. vl.accessLock.RLock()
  122. defer vl.accessLock.RUnlock()
  123. return fmt.Sprintf("rp:%v, ttl:%v, vid2location:%v, writables:%v, volumeSizeLimit:%v", vl.rp, vl.ttl, vl.vid2location, vl.writables, vl.volumeSizeLimit)
  124. }
  125. func (vl *VolumeLayout) RegisterVolume(v *storage.VolumeInfo, dn *DataNode) {
  126. vl.accessLock.Lock()
  127. defer vl.accessLock.Unlock()
  128. defer vl.rememberOversizedVolume(v, dn)
  129. if _, ok := vl.vid2location[v.Id]; !ok {
  130. vl.vid2location[v.Id] = NewVolumeLocationList()
  131. }
  132. vl.vid2location[v.Id].Set(dn)
  133. // 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())
  134. for _, dn := range vl.vid2location[v.Id].list {
  135. if vInfo, err := dn.GetVolumesById(v.Id); err == nil {
  136. if vInfo.ReadOnly {
  137. glog.V(1).Infof("vid %d removed from writable", v.Id)
  138. vl.removeFromWritable(v.Id)
  139. vl.readonlyVolumes.Add(v.Id, dn)
  140. return
  141. } else {
  142. vl.readonlyVolumes.Remove(v.Id, dn)
  143. }
  144. } else {
  145. glog.V(1).Infof("vid %d removed from writable", v.Id)
  146. vl.removeFromWritable(v.Id)
  147. vl.readonlyVolumes.Remove(v.Id, dn)
  148. return
  149. }
  150. }
  151. }
  152. func (vl *VolumeLayout) rememberOversizedVolume(v *storage.VolumeInfo, dn *DataNode) {
  153. if vl.isOversized(v) {
  154. vl.oversizedVolumes.Add(v.Id, dn)
  155. } else {
  156. vl.oversizedVolumes.Remove(v.Id, dn)
  157. }
  158. }
  159. func (vl *VolumeLayout) UnRegisterVolume(v *storage.VolumeInfo, dn *DataNode) {
  160. vl.accessLock.Lock()
  161. defer vl.accessLock.Unlock()
  162. // remove from vid2location map
  163. location, ok := vl.vid2location[v.Id]
  164. if !ok {
  165. return
  166. }
  167. if location.Remove(dn) {
  168. vl.readonlyVolumes.Remove(v.Id, dn)
  169. vl.oversizedVolumes.Remove(v.Id, dn)
  170. vl.ensureCorrectWritables(v.Id)
  171. if location.Length() == 0 {
  172. delete(vl.vid2location, v.Id)
  173. }
  174. }
  175. }
  176. func (vl *VolumeLayout) EnsureCorrectWritables(v *storage.VolumeInfo) {
  177. vl.accessLock.Lock()
  178. defer vl.accessLock.Unlock()
  179. vl.ensureCorrectWritables(v.Id)
  180. }
  181. func (vl *VolumeLayout) ensureCorrectWritables(vid needle.VolumeId) {
  182. if vl.enoughCopies(vid) && vl.isAllWritable(vid) {
  183. if !vl.oversizedVolumes.IsTrue(vid) {
  184. vl.setVolumeWritable(vid)
  185. }
  186. } else {
  187. vl.removeFromWritable(vid)
  188. }
  189. }
  190. func (vl *VolumeLayout) isAllWritable(vid needle.VolumeId) bool {
  191. for _, dn := range vl.vid2location[vid].list {
  192. if v, getError := dn.GetVolumesById(vid); getError == nil {
  193. if v.ReadOnly {
  194. return false
  195. }
  196. }
  197. }
  198. return true
  199. }
  200. func (vl *VolumeLayout) isOversized(v *storage.VolumeInfo) bool {
  201. return uint64(v.Size) >= vl.volumeSizeLimit
  202. }
  203. func (vl *VolumeLayout) isWritable(v *storage.VolumeInfo) bool {
  204. return !vl.isOversized(v) &&
  205. v.Version == needle.CurrentVersion &&
  206. !v.ReadOnly
  207. }
  208. func (vl *VolumeLayout) isEmpty() bool {
  209. vl.accessLock.RLock()
  210. defer vl.accessLock.RUnlock()
  211. return len(vl.vid2location) == 0
  212. }
  213. func (vl *VolumeLayout) Lookup(vid needle.VolumeId) []*DataNode {
  214. vl.accessLock.RLock()
  215. defer vl.accessLock.RUnlock()
  216. if location := vl.vid2location[vid]; location != nil {
  217. return location.list
  218. }
  219. return nil
  220. }
  221. func (vl *VolumeLayout) ListVolumeServers() (nodes []*DataNode) {
  222. vl.accessLock.RLock()
  223. defer vl.accessLock.RUnlock()
  224. for _, location := range vl.vid2location {
  225. nodes = append(nodes, location.list...)
  226. }
  227. return
  228. }
  229. func (vl *VolumeLayout) PickForWrite(count uint64, option *VolumeGrowOption) (*needle.VolumeId, uint64, *VolumeLocationList, error) {
  230. vl.accessLock.RLock()
  231. defer vl.accessLock.RUnlock()
  232. lenWriters := len(vl.writables)
  233. if lenWriters <= 0 {
  234. //glog.V(0).Infoln("No more writable volumes!")
  235. return nil, 0, nil, errors.New("No more writable volumes!")
  236. }
  237. if option.DataCenter == "" {
  238. vid := vl.writables[rand.Intn(lenWriters)]
  239. locationList := vl.vid2location[vid]
  240. if locationList != nil {
  241. return &vid, count, locationList, nil
  242. }
  243. return nil, 0, nil, errors.New("Strangely vid " + vid.String() + " is on no machine!")
  244. }
  245. var vid needle.VolumeId
  246. var locationList *VolumeLocationList
  247. counter := 0
  248. for _, v := range vl.writables {
  249. volumeLocationList := vl.vid2location[v]
  250. for _, dn := range volumeLocationList.list {
  251. if dn.GetDataCenter().Id() == NodeId(option.DataCenter) {
  252. if option.Rack != "" && dn.GetRack().Id() != NodeId(option.Rack) {
  253. continue
  254. }
  255. if option.DataNode != "" && dn.Id() != NodeId(option.DataNode) {
  256. continue
  257. }
  258. counter++
  259. if rand.Intn(counter) < 1 {
  260. vid, locationList = v, volumeLocationList
  261. }
  262. }
  263. }
  264. }
  265. return &vid, count, locationList, nil
  266. }
  267. func (vl *VolumeLayout) GetActiveVolumeCount(option *VolumeGrowOption) (active, crowded int) {
  268. vl.accessLock.RLock()
  269. defer vl.accessLock.RUnlock()
  270. if option.DataCenter == "" {
  271. return len(vl.writables), len(vl.crowded)
  272. }
  273. for _, v := range vl.writables {
  274. for _, dn := range vl.vid2location[v].list {
  275. if dn.GetDataCenter().Id() == NodeId(option.DataCenter) {
  276. if option.Rack != "" && dn.GetRack().Id() != NodeId(option.Rack) {
  277. continue
  278. }
  279. if option.DataNode != "" && dn.Id() != NodeId(option.DataNode) {
  280. continue
  281. }
  282. active++
  283. info, _ := dn.GetVolumesById(v)
  284. if float64(info.Size) > float64(vl.volumeSizeLimit)*option.Threshold() {
  285. crowded++
  286. }
  287. }
  288. }
  289. }
  290. return
  291. }
  292. func (vl *VolumeLayout) removeFromWritable(vid needle.VolumeId) bool {
  293. toDeleteIndex := -1
  294. for k, id := range vl.writables {
  295. if id == vid {
  296. toDeleteIndex = k
  297. break
  298. }
  299. }
  300. if toDeleteIndex >= 0 {
  301. glog.V(0).Infoln("Volume", vid, "becomes unwritable")
  302. vl.writables = append(vl.writables[0:toDeleteIndex], vl.writables[toDeleteIndex+1:]...)
  303. vl.removeFromCrowded(vid)
  304. return true
  305. }
  306. return false
  307. }
  308. func (vl *VolumeLayout) setVolumeWritable(vid needle.VolumeId) bool {
  309. for _, v := range vl.writables {
  310. if v == vid {
  311. return false
  312. }
  313. }
  314. glog.V(0).Infoln("Volume", vid, "becomes writable")
  315. vl.writables = append(vl.writables, vid)
  316. return true
  317. }
  318. func (vl *VolumeLayout) SetVolumeUnavailable(dn *DataNode, vid needle.VolumeId) bool {
  319. vl.accessLock.Lock()
  320. defer vl.accessLock.Unlock()
  321. if location, ok := vl.vid2location[vid]; ok {
  322. if location.Remove(dn) {
  323. vl.readonlyVolumes.Remove(vid, dn)
  324. vl.oversizedVolumes.Remove(vid, dn)
  325. if location.Length() < vl.rp.GetCopyCount() {
  326. glog.V(0).Infoln("Volume", vid, "has", location.Length(), "replica, less than required", vl.rp.GetCopyCount())
  327. return vl.removeFromWritable(vid)
  328. }
  329. }
  330. }
  331. return false
  332. }
  333. func (vl *VolumeLayout) SetVolumeAvailable(dn *DataNode, vid needle.VolumeId, isReadOnly bool) bool {
  334. vl.accessLock.Lock()
  335. defer vl.accessLock.Unlock()
  336. vInfo, err := dn.GetVolumesById(vid)
  337. if err != nil {
  338. return false
  339. }
  340. vl.vid2location[vid].Set(dn)
  341. if vInfo.ReadOnly || isReadOnly {
  342. return false
  343. }
  344. if vl.enoughCopies(vid) {
  345. return vl.setVolumeWritable(vid)
  346. }
  347. return false
  348. }
  349. func (vl *VolumeLayout) enoughCopies(vid needle.VolumeId) bool {
  350. locations := vl.vid2location[vid].Length()
  351. desired := vl.rp.GetCopyCount()
  352. return locations == desired || (vl.replicationAsMin && locations > desired)
  353. }
  354. func (vl *VolumeLayout) SetVolumeCapacityFull(vid needle.VolumeId) bool {
  355. vl.accessLock.Lock()
  356. defer vl.accessLock.Unlock()
  357. // glog.V(0).Infoln("Volume", vid, "reaches full capacity.")
  358. return vl.removeFromWritable(vid)
  359. }
  360. func (vl *VolumeLayout) removeFromCrowded(vid needle.VolumeId) {
  361. delete(vl.crowded, vid)
  362. }
  363. func (vl *VolumeLayout) setVolumeCrowded(vid needle.VolumeId) {
  364. if _, ok := vl.crowded[vid]; !ok {
  365. vl.crowded[vid] = struct{}{}
  366. glog.V(0).Infoln("Volume", vid, "becomes crowded")
  367. }
  368. }
  369. func (vl *VolumeLayout) SetVolumeCrowded(vid needle.VolumeId) {
  370. // since delete is guarded by accessLock.Lock(),
  371. // and is always called in sequential order,
  372. // RLock() should be safe enough
  373. vl.accessLock.RLock()
  374. defer vl.accessLock.RUnlock()
  375. for _, v := range vl.writables {
  376. if v == vid {
  377. vl.setVolumeCrowded(vid)
  378. break
  379. }
  380. }
  381. }
  382. func (vl *VolumeLayout) ToMap() map[string]interface{} {
  383. m := make(map[string]interface{})
  384. m["replication"] = vl.rp.String()
  385. m["ttl"] = vl.ttl.String()
  386. m["writables"] = vl.writables
  387. //m["locations"] = vl.vid2location
  388. return m
  389. }
  390. func (vl *VolumeLayout) Stats() *VolumeLayoutStats {
  391. vl.accessLock.RLock()
  392. defer vl.accessLock.RUnlock()
  393. ret := &VolumeLayoutStats{}
  394. freshThreshold := time.Now().Unix() - 60
  395. for vid, vll := range vl.vid2location {
  396. size, fileCount := vll.Stats(vid, freshThreshold)
  397. ret.FileCount += uint64(fileCount)
  398. ret.UsedSize += size
  399. if vl.readonlyVolumes.IsTrue(vid) {
  400. ret.TotalSize += size
  401. } else {
  402. ret.TotalSize += vl.volumeSizeLimit * uint64(vll.Length())
  403. }
  404. }
  405. return ret
  406. }