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.

166 lines
4.4 KiB

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