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.

151 lines
4.1 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, dataCenter string) (*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. if dataCenter == "" {
  54. vid := vl.writables[rand.Intn(len_writers)]
  55. locationList := vl.vid2location[vid]
  56. if locationList != nil {
  57. return &vid, count, locationList, nil
  58. }
  59. return nil, 0, nil, errors.New("Strangely vid " + vid.String() + " is on no machine!")
  60. } else {
  61. var vid storage.VolumeId
  62. var locationList *VolumeLocationList
  63. counter := 0
  64. for _, v := range vl.writables {
  65. volumeLocationList := vl.vid2location[v]
  66. for _, dn := range volumeLocationList.list {
  67. if dn.GetDataCenter().Id() == NodeId(dataCenter) {
  68. counter++
  69. if rand.Intn(counter) < 1 {
  70. vid, locationList = v, volumeLocationList
  71. }
  72. }
  73. }
  74. }
  75. return &vid, count, locationList, nil
  76. }
  77. return nil, 0, nil, errors.New("Strangely This Should Never Have Happened!")
  78. }
  79. func (vl *VolumeLayout) GetActiveVolumeCount(dataCenter string) int {
  80. if dataCenter == "" {
  81. return len(vl.writables)
  82. }
  83. counter := 0
  84. for _, v := range vl.writables {
  85. for _, dn := range vl.vid2location[v].list {
  86. if dn.GetDataCenter().Id() == NodeId(dataCenter) {
  87. counter++
  88. }
  89. }
  90. }
  91. return counter
  92. }
  93. func (vl *VolumeLayout) removeFromWritable(vid storage.VolumeId) bool {
  94. for i, v := range vl.writables {
  95. if v == vid {
  96. fmt.Println("Volume", vid, "becomes unwritable")
  97. vl.writables = append(vl.writables[:i], vl.writables[i+1:]...)
  98. return true
  99. }
  100. }
  101. return false
  102. }
  103. func (vl *VolumeLayout) setVolumeWritable(vid storage.VolumeId) bool {
  104. for _, v := range vl.writables {
  105. if v == vid {
  106. return false
  107. }
  108. }
  109. fmt.Println("Volume", vid, "becomes writable")
  110. vl.writables = append(vl.writables, vid)
  111. return true
  112. }
  113. func (vl *VolumeLayout) SetVolumeUnavailable(dn *DataNode, vid storage.VolumeId) bool {
  114. if vl.vid2location[vid].Remove(dn) {
  115. if vl.vid2location[vid].Length() < vl.repType.GetCopyCount() {
  116. fmt.Println("Volume", vid, "has", vl.vid2location[vid].Length(), "replica, less than required", vl.repType.GetCopyCount())
  117. return vl.removeFromWritable(vid)
  118. }
  119. }
  120. return false
  121. }
  122. func (vl *VolumeLayout) SetVolumeAvailable(dn *DataNode, vid storage.VolumeId) bool {
  123. if vl.vid2location[vid].Add(dn) {
  124. if vl.vid2location[vid].Length() >= vl.repType.GetCopyCount() {
  125. return vl.setVolumeWritable(vid)
  126. }
  127. }
  128. return false
  129. }
  130. func (vl *VolumeLayout) SetVolumeCapacityFull(vid storage.VolumeId) bool {
  131. return vl.removeFromWritable(vid)
  132. }
  133. func (vl *VolumeLayout) ToMap() interface{} {
  134. m := make(map[string]interface{})
  135. m["replication"] = vl.repType.String()
  136. m["writables"] = vl.writables
  137. //m["locations"] = vl.vid2location
  138. return m
  139. }