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.

116 lines
3.3 KiB

  1. package topology
  2. import (
  3. "errors"
  4. "fmt"
  5. "math/rand"
  6. "code.google.com/p/weed-fs/weed/storage"
  7. )
  8. type VolumeLayout struct {
  9. repType storage.ReplicationType
  10. vid2location map[storage.VolumeId]*VolumeLocationList
  11. writables []storage.VolumeId // transient array of writable volume id
  12. pulse int64
  13. volumeSizeLimit uint64
  14. }
  15. func NewVolumeLayout(repType storage.ReplicationType, volumeSizeLimit uint64, pulse int64) *VolumeLayout {
  16. return &VolumeLayout{
  17. repType: repType,
  18. vid2location: make(map[storage.VolumeId]*VolumeLocationList),
  19. writables: *new([]storage.VolumeId),
  20. pulse: pulse,
  21. volumeSizeLimit: volumeSizeLimit,
  22. }
  23. }
  24. func (vl *VolumeLayout) RegisterVolume(v *storage.VolumeInfo, dn *DataNode) {
  25. if _, ok := vl.vid2location[v.Id]; !ok {
  26. vl.vid2location[v.Id] = NewVolumeLocationList()
  27. }
  28. if vl.vid2location[v.Id].Add(dn) {
  29. if len(vl.vid2location[v.Id].list) == v.RepType.GetCopyCount() {
  30. if vl.isWritable(v) {
  31. vl.writables = append(vl.writables, v.Id)
  32. }
  33. }
  34. }
  35. }
  36. func (vl *VolumeLayout) isWritable(v *storage.VolumeInfo) bool {
  37. return uint64(v.Size) < vl.volumeSizeLimit && v.Version == storage.CurrentVersion
  38. }
  39. func (vl *VolumeLayout) Lookup(vid storage.VolumeId) []*DataNode {
  40. return vl.vid2location[vid].list
  41. }
  42. func (vl *VolumeLayout) PickForWrite(count int) (*storage.VolumeId, int, *VolumeLocationList, error) {
  43. len_writers := len(vl.writables)
  44. if len_writers <= 0 {
  45. fmt.Println("No more writable volumes!")
  46. return nil, 0, nil, errors.New("No more writable volumes!")
  47. }
  48. vid := vl.writables[rand.Intn(len_writers)]
  49. locationList := vl.vid2location[vid]
  50. if locationList != nil {
  51. return &vid, count, locationList, nil
  52. }
  53. return nil, 0, nil, errors.New("Strangely vid " + vid.String() + " is on no machine!")
  54. }
  55. func (vl *VolumeLayout) GetActiveVolumeCount() int {
  56. return len(vl.writables)
  57. }
  58. func (vl *VolumeLayout) removeFromWritable(vid storage.VolumeId) bool {
  59. for i, v := range vl.writables {
  60. if v == vid {
  61. fmt.Println("Volume", vid, "becomes unwritable")
  62. vl.writables = append(vl.writables[:i], vl.writables[i+1:]...)
  63. return true
  64. }
  65. }
  66. return false
  67. }
  68. func (vl *VolumeLayout) setVolumeWritable(vid storage.VolumeId) bool {
  69. for _, v := range vl.writables {
  70. if v == vid {
  71. return false
  72. }
  73. }
  74. fmt.Println("Volume", vid, "becomes writable")
  75. vl.writables = append(vl.writables, vid)
  76. return true
  77. }
  78. func (vl *VolumeLayout) SetVolumeUnavailable(dn *DataNode, vid storage.VolumeId) bool {
  79. if vl.vid2location[vid].Remove(dn) {
  80. if vl.vid2location[vid].Length() < vl.repType.GetCopyCount() {
  81. fmt.Println("Volume", vid, "has", vl.vid2location[vid].Length(), "replica, less than required", vl.repType.GetCopyCount())
  82. return vl.removeFromWritable(vid)
  83. }
  84. }
  85. return false
  86. }
  87. func (vl *VolumeLayout) SetVolumeAvailable(dn *DataNode, vid storage.VolumeId) bool {
  88. if vl.vid2location[vid].Add(dn) {
  89. if vl.vid2location[vid].Length() >= vl.repType.GetCopyCount() {
  90. return vl.setVolumeWritable(vid)
  91. }
  92. }
  93. return false
  94. }
  95. func (vl *VolumeLayout) SetVolumeCapacityFull(vid storage.VolumeId) bool {
  96. return vl.removeFromWritable(vid)
  97. }
  98. func (vl *VolumeLayout) ToMap() interface{} {
  99. m := make(map[string]interface{})
  100. m["replication"] = vl.repType.String()
  101. m["writables"] = vl.writables
  102. //m["locations"] = vl.vid2location
  103. return m
  104. }