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.

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