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.

171 lines
4.6 KiB

  1. package topology
  2. import (
  3. "code.google.com/p/weed-fs/go/glog"
  4. "code.google.com/p/weed-fs/go/storage"
  5. "errors"
  6. "math/rand"
  7. "sync"
  8. )
  9. type VolumeLayout struct {
  10. repType storage.ReplicationType
  11. vid2location map[storage.VolumeId]*VolumeLocationList
  12. writables []storage.VolumeId // transient array of writable volume id
  13. volumeSizeLimit uint64
  14. accessLock sync.Mutex
  15. }
  16. func NewVolumeLayout(repType storage.ReplicationType, volumeSizeLimit uint64) *VolumeLayout {
  17. return &VolumeLayout{
  18. repType: repType,
  19. vid2location: make(map[storage.VolumeId]*VolumeLocationList),
  20. writables: *new([]storage.VolumeId),
  21. volumeSizeLimit: volumeSizeLimit,
  22. }
  23. }
  24. func (vl *VolumeLayout) RegisterVolume(v *storage.VolumeInfo, dn *DataNode) {
  25. vl.accessLock.Lock()
  26. defer vl.accessLock.Unlock()
  27. if _, ok := vl.vid2location[v.Id]; !ok {
  28. vl.vid2location[v.Id] = NewVolumeLocationList()
  29. }
  30. if vl.vid2location[v.Id].Add(dn) {
  31. if len(vl.vid2location[v.Id].list) == v.RepType.GetCopyCount() {
  32. if vl.isWritable(v) {
  33. vl.writables = append(vl.writables, v.Id)
  34. } else {
  35. vl.removeFromWritable(v.Id)
  36. }
  37. }
  38. }
  39. }
  40. func (vl *VolumeLayout) isWritable(v *storage.VolumeInfo) bool {
  41. return uint64(v.Size) < vl.volumeSizeLimit &&
  42. v.Version == storage.CurrentVersion &&
  43. !v.ReadOnly
  44. }
  45. func (vl *VolumeLayout) Lookup(vid storage.VolumeId) []*DataNode {
  46. if location := vl.vid2location[vid]; location != nil {
  47. return location.list
  48. }
  49. return nil
  50. }
  51. func (vl *VolumeLayout) PickForWrite(count int, dataCenter string) (*storage.VolumeId, int, *VolumeLocationList, error) {
  52. len_writers := len(vl.writables)
  53. if len_writers <= 0 {
  54. glog.V(0).Infoln("No more writable volumes!")
  55. return nil, 0, nil, errors.New("No more writable volumes!")
  56. }
  57. if dataCenter == "" {
  58. vid := vl.writables[rand.Intn(len_writers)]
  59. locationList := vl.vid2location[vid]
  60. if locationList != nil {
  61. return &vid, count, locationList, nil
  62. }
  63. return nil, 0, nil, errors.New("Strangely vid " + vid.String() + " is on no machine!")
  64. } else {
  65. var vid storage.VolumeId
  66. var locationList *VolumeLocationList
  67. counter := 0
  68. for _, v := range vl.writables {
  69. volumeLocationList := vl.vid2location[v]
  70. for _, dn := range volumeLocationList.list {
  71. if dn.GetDataCenter().Id() == NodeId(dataCenter) {
  72. counter++
  73. if rand.Intn(counter) < 1 {
  74. vid, locationList = v, volumeLocationList
  75. }
  76. }
  77. }
  78. }
  79. return &vid, count, locationList, nil
  80. }
  81. return nil, 0, nil, errors.New("Strangely This Should Never Have Happened!")
  82. }
  83. func (vl *VolumeLayout) GetActiveVolumeCount(dataCenter string) int {
  84. if dataCenter == "" {
  85. return len(vl.writables)
  86. }
  87. counter := 0
  88. for _, v := range vl.writables {
  89. for _, dn := range vl.vid2location[v].list {
  90. if dn.GetDataCenter().Id() == NodeId(dataCenter) {
  91. counter++
  92. }
  93. }
  94. }
  95. return counter
  96. }
  97. func (vl *VolumeLayout) removeFromWritable(vid storage.VolumeId) bool {
  98. toDeleteIndex := -1
  99. for k, id := range vl.writables {
  100. if id == vid {
  101. toDeleteIndex = k
  102. break
  103. }
  104. }
  105. if toDeleteIndex >= 0 {
  106. glog.V(0).Infoln("Volume", vid, "becomes unwritable")
  107. vl.writables = append(vl.writables[0:toDeleteIndex], vl.writables[toDeleteIndex+1:]...)
  108. return true
  109. }
  110. return false
  111. }
  112. func (vl *VolumeLayout) setVolumeWritable(vid storage.VolumeId) bool {
  113. for _, v := range vl.writables {
  114. if v == vid {
  115. return false
  116. }
  117. }
  118. glog.V(0).Infoln("Volume", vid, "becomes writable")
  119. vl.writables = append(vl.writables, vid)
  120. return true
  121. }
  122. func (vl *VolumeLayout) SetVolumeUnavailable(dn *DataNode, vid storage.VolumeId) bool {
  123. vl.accessLock.Lock()
  124. defer vl.accessLock.Unlock()
  125. if vl.vid2location[vid].Remove(dn) {
  126. if vl.vid2location[vid].Length() < vl.repType.GetCopyCount() {
  127. glog.V(0).Infoln("Volume", vid, "has", vl.vid2location[vid].Length(), "replica, less than required", vl.repType.GetCopyCount())
  128. return vl.removeFromWritable(vid)
  129. }
  130. }
  131. return false
  132. }
  133. func (vl *VolumeLayout) SetVolumeAvailable(dn *DataNode, vid storage.VolumeId) bool {
  134. vl.accessLock.Lock()
  135. defer vl.accessLock.Unlock()
  136. if vl.vid2location[vid].Add(dn) {
  137. if vl.vid2location[vid].Length() >= vl.repType.GetCopyCount() {
  138. return vl.setVolumeWritable(vid)
  139. }
  140. }
  141. return false
  142. }
  143. func (vl *VolumeLayout) SetVolumeCapacityFull(vid storage.VolumeId) bool {
  144. vl.accessLock.Lock()
  145. defer vl.accessLock.Unlock()
  146. // glog.V(0).Infoln("Volume", vid, "reaches full capacity.")
  147. return vl.removeFromWritable(vid)
  148. }
  149. func (vl *VolumeLayout) ToMap() map[string]interface{} {
  150. m := make(map[string]interface{})
  151. m["replication"] = vl.repType.String()
  152. m["writables"] = vl.writables
  153. //m["locations"] = vl.vid2location
  154. return m
  155. }