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.

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