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.

121 lines
3.4 KiB

  1. package topology
  2. import (
  3. "code.google.com/p/weed-fs/go/storage"
  4. "errors"
  5. "fmt"
  6. "math/rand"
  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 &&
  38. v.Version == storage.CurrentVersion &&
  39. !v.ReadOnly
  40. }
  41. func (vl *VolumeLayout) Lookup(vid storage.VolumeId) []*DataNode {
  42. if location := vl.vid2location[vid]; location != nil {
  43. return location.list
  44. }
  45. return nil
  46. }
  47. func (vl *VolumeLayout) PickForWrite(count int) (*storage.VolumeId, int, *VolumeLocationList, error) {
  48. len_writers := len(vl.writables)
  49. if len_writers <= 0 {
  50. fmt.Println("No more writable volumes!")
  51. return nil, 0, nil, errors.New("No more writable volumes!")
  52. }
  53. vid := vl.writables[rand.Intn(len_writers)]
  54. locationList := vl.vid2location[vid]
  55. if locationList != nil {
  56. return &vid, count, locationList, nil
  57. }
  58. return nil, 0, nil, errors.New("Strangely vid " + vid.String() + " is on no machine!")
  59. }
  60. func (vl *VolumeLayout) GetActiveVolumeCount() int {
  61. return len(vl.writables)
  62. }
  63. func (vl *VolumeLayout) removeFromWritable(vid storage.VolumeId) bool {
  64. for i, v := range vl.writables {
  65. if v == vid {
  66. fmt.Println("Volume", vid, "becomes unwritable")
  67. vl.writables = append(vl.writables[:i], vl.writables[i+1:]...)
  68. return true
  69. }
  70. }
  71. return false
  72. }
  73. func (vl *VolumeLayout) setVolumeWritable(vid storage.VolumeId) bool {
  74. for _, v := range vl.writables {
  75. if v == vid {
  76. return false
  77. }
  78. }
  79. fmt.Println("Volume", vid, "becomes writable")
  80. vl.writables = append(vl.writables, vid)
  81. return true
  82. }
  83. func (vl *VolumeLayout) SetVolumeUnavailable(dn *DataNode, vid storage.VolumeId) bool {
  84. if vl.vid2location[vid].Remove(dn) {
  85. if vl.vid2location[vid].Length() < vl.repType.GetCopyCount() {
  86. fmt.Println("Volume", vid, "has", vl.vid2location[vid].Length(), "replica, less than required", vl.repType.GetCopyCount())
  87. return vl.removeFromWritable(vid)
  88. }
  89. }
  90. return false
  91. }
  92. func (vl *VolumeLayout) SetVolumeAvailable(dn *DataNode, vid storage.VolumeId) bool {
  93. if vl.vid2location[vid].Add(dn) {
  94. if vl.vid2location[vid].Length() >= vl.repType.GetCopyCount() {
  95. return vl.setVolumeWritable(vid)
  96. }
  97. }
  98. return false
  99. }
  100. func (vl *VolumeLayout) SetVolumeCapacityFull(vid storage.VolumeId) bool {
  101. return vl.removeFromWritable(vid)
  102. }
  103. func (vl *VolumeLayout) ToMap() interface{} {
  104. m := make(map[string]interface{})
  105. m["replication"] = vl.repType.String()
  106. m["writables"] = vl.writables
  107. //m["locations"] = vl.vid2location
  108. return m
  109. }