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.

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