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.

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