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.

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