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.

199 lines
5.5 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. return nil, 0, nil, errors.New("Strangely This Should Never Have Happened!")
  101. }
  102. func (vl *VolumeLayout) GetActiveVolumeCount(option *VolumeGrowOption) int {
  103. if option.DataCenter == "" {
  104. return len(vl.writables)
  105. }
  106. counter := 0
  107. for _, v := range vl.writables {
  108. for _, dn := range vl.vid2location[v].list {
  109. if dn.GetDataCenter().Id() == NodeId(option.DataCenter) {
  110. if option.Rack != "" && dn.GetRack().Id() != NodeId(option.Rack) {
  111. continue
  112. }
  113. if option.DataNode != "" && dn.Id() != NodeId(option.DataNode) {
  114. continue
  115. }
  116. counter++
  117. }
  118. }
  119. }
  120. return counter
  121. }
  122. func (vl *VolumeLayout) removeFromWritable(vid storage.VolumeId) bool {
  123. toDeleteIndex := -1
  124. for k, id := range vl.writables {
  125. if id == vid {
  126. toDeleteIndex = k
  127. break
  128. }
  129. }
  130. if toDeleteIndex >= 0 {
  131. glog.V(0).Infoln("Volume", vid, "becomes unwritable")
  132. vl.writables = append(vl.writables[0:toDeleteIndex], vl.writables[toDeleteIndex+1:]...)
  133. return true
  134. }
  135. return false
  136. }
  137. func (vl *VolumeLayout) setVolumeWritable(vid storage.VolumeId) bool {
  138. for _, v := range vl.writables {
  139. if v == vid {
  140. return false
  141. }
  142. }
  143. glog.V(0).Infoln("Volume", vid, "becomes writable")
  144. vl.writables = append(vl.writables, vid)
  145. return true
  146. }
  147. func (vl *VolumeLayout) SetVolumeUnavailable(dn *DataNode, vid storage.VolumeId) bool {
  148. vl.accessLock.Lock()
  149. defer vl.accessLock.Unlock()
  150. if location, ok := vl.vid2location[vid]; ok {
  151. if location.Remove(dn) {
  152. if location.Length() < vl.rp.GetCopyCount() {
  153. glog.V(0).Infoln("Volume", vid, "has", location.Length(), "replica, less than required", vl.rp.GetCopyCount())
  154. return vl.removeFromWritable(vid)
  155. }
  156. }
  157. }
  158. return false
  159. }
  160. func (vl *VolumeLayout) SetVolumeAvailable(dn *DataNode, vid storage.VolumeId) bool {
  161. vl.accessLock.Lock()
  162. defer vl.accessLock.Unlock()
  163. vl.vid2location[vid].Set(dn)
  164. if vl.vid2location[vid].Length() >= vl.rp.GetCopyCount() {
  165. return vl.setVolumeWritable(vid)
  166. }
  167. return false
  168. }
  169. func (vl *VolumeLayout) SetVolumeCapacityFull(vid storage.VolumeId) bool {
  170. vl.accessLock.Lock()
  171. defer vl.accessLock.Unlock()
  172. // glog.V(0).Infoln("Volume", vid, "reaches full capacity.")
  173. return vl.removeFromWritable(vid)
  174. }
  175. func (vl *VolumeLayout) ToMap() map[string]interface{} {
  176. m := make(map[string]interface{})
  177. m["replication"] = vl.rp.String()
  178. m["writables"] = vl.writables
  179. //m["locations"] = vl.vid2location
  180. return m
  181. }