volume_layout.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. package topology
  2. import (
  3. "errors"
  4. "fmt"
  5. "math/rand"
  6. "sync"
  7. "time"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/storage"
  10. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  11. "github.com/chrislusf/seaweedfs/weed/storage/super_block"
  12. )
  13. // mapping from volume to its locations, inverted from server to volume
  14. type VolumeLayout struct {
  15. rp *super_block.ReplicaPlacement
  16. ttl *needle.TTL
  17. vid2location map[needle.VolumeId]*VolumeLocationList
  18. writables []needle.VolumeId // transient array of writable volume id
  19. readonlyVolumes map[needle.VolumeId]bool // transient set of readonly volumes
  20. oversizedVolumes map[needle.VolumeId]bool // set of oversized volumes
  21. volumeSizeLimit uint64
  22. accessLock sync.RWMutex
  23. }
  24. type VolumeLayoutStats struct {
  25. TotalSize uint64
  26. UsedSize uint64
  27. FileCount uint64
  28. }
  29. func NewVolumeLayout(rp *super_block.ReplicaPlacement, ttl *needle.TTL, volumeSizeLimit uint64) *VolumeLayout {
  30. return &VolumeLayout{
  31. rp: rp,
  32. ttl: ttl,
  33. vid2location: make(map[needle.VolumeId]*VolumeLocationList),
  34. writables: *new([]needle.VolumeId),
  35. readonlyVolumes: make(map[needle.VolumeId]bool),
  36. oversizedVolumes: make(map[needle.VolumeId]bool),
  37. volumeSizeLimit: volumeSizeLimit,
  38. }
  39. }
  40. func (vl *VolumeLayout) String() string {
  41. return fmt.Sprintf("rp:%v, ttl:%v, vid2location:%v, writables:%v, volumeSizeLimit:%v", vl.rp, vl.ttl, vl.vid2location, vl.writables, vl.volumeSizeLimit)
  42. }
  43. func (vl *VolumeLayout) RegisterVolume(v *storage.VolumeInfo, dn *DataNode) {
  44. vl.accessLock.Lock()
  45. defer vl.accessLock.Unlock()
  46. if _, ok := vl.vid2location[v.Id]; !ok {
  47. vl.vid2location[v.Id] = NewVolumeLocationList()
  48. }
  49. vl.vid2location[v.Id].Set(dn)
  50. // glog.V(4).Infof("volume %d added to %s len %d copy %d", v.Id, dn.Id(), vl.vid2location[v.Id].Length(), v.ReplicaPlacement.GetCopyCount())
  51. for _, dn := range vl.vid2location[v.Id].list {
  52. if vInfo, err := dn.GetVolumesById(v.Id); err == nil {
  53. if vInfo.ReadOnly {
  54. glog.V(1).Infof("vid %d removed from writable", v.Id)
  55. vl.removeFromWritable(v.Id)
  56. vl.readonlyVolumes[v.Id] = true
  57. return
  58. } else {
  59. delete(vl.readonlyVolumes, v.Id)
  60. }
  61. } else {
  62. glog.V(1).Infof("vid %d removed from writable", v.Id)
  63. vl.removeFromWritable(v.Id)
  64. delete(vl.readonlyVolumes, v.Id)
  65. return
  66. }
  67. }
  68. vl.rememberOversizedVolume(v)
  69. vl.ensureCorrectWritables(v)
  70. }
  71. func (vl *VolumeLayout) rememberOversizedVolume(v *storage.VolumeInfo) {
  72. if vl.isOversized(v) {
  73. vl.oversizedVolumes[v.Id] = true
  74. }
  75. }
  76. func (vl *VolumeLayout) UnRegisterVolume(v *storage.VolumeInfo, dn *DataNode) {
  77. vl.accessLock.Lock()
  78. defer vl.accessLock.Unlock()
  79. // remove from vid2location map
  80. location, ok := vl.vid2location[v.Id]
  81. if !ok {
  82. return
  83. }
  84. if location.Remove(dn) {
  85. vl.ensureCorrectWritables(v)
  86. if location.Length() == 0 {
  87. delete(vl.vid2location, v.Id)
  88. }
  89. }
  90. }
  91. func (vl *VolumeLayout) ensureCorrectWritables(v *storage.VolumeInfo) {
  92. if vl.vid2location[v.Id].Length() == vl.rp.GetCopyCount() && vl.isWritable(v) {
  93. if _, ok := vl.oversizedVolumes[v.Id]; !ok {
  94. vl.addToWritable(v.Id)
  95. }
  96. } else {
  97. vl.removeFromWritable(v.Id)
  98. }
  99. }
  100. func (vl *VolumeLayout) addToWritable(vid needle.VolumeId) {
  101. for _, id := range vl.writables {
  102. if vid == id {
  103. return
  104. }
  105. }
  106. vl.writables = append(vl.writables, vid)
  107. }
  108. func (vl *VolumeLayout) isOversized(v *storage.VolumeInfo) bool {
  109. return uint64(v.Size) >= vl.volumeSizeLimit
  110. }
  111. func (vl *VolumeLayout) isWritable(v *storage.VolumeInfo) bool {
  112. return !vl.isOversized(v) &&
  113. v.Version == needle.CurrentVersion &&
  114. !v.ReadOnly
  115. }
  116. func (vl *VolumeLayout) isEmpty() bool {
  117. vl.accessLock.RLock()
  118. defer vl.accessLock.RUnlock()
  119. return len(vl.vid2location) == 0
  120. }
  121. func (vl *VolumeLayout) Lookup(vid needle.VolumeId) []*DataNode {
  122. vl.accessLock.RLock()
  123. defer vl.accessLock.RUnlock()
  124. if location := vl.vid2location[vid]; location != nil {
  125. return location.list
  126. }
  127. return nil
  128. }
  129. func (vl *VolumeLayout) ListVolumeServers() (nodes []*DataNode) {
  130. vl.accessLock.RLock()
  131. defer vl.accessLock.RUnlock()
  132. for _, location := range vl.vid2location {
  133. nodes = append(nodes, location.list...)
  134. }
  135. return
  136. }
  137. func (vl *VolumeLayout) PickForWrite(count uint64, option *VolumeGrowOption) (*needle.VolumeId, uint64, *VolumeLocationList, error) {
  138. vl.accessLock.RLock()
  139. defer vl.accessLock.RUnlock()
  140. lenWriters := len(vl.writables)
  141. if lenWriters <= 0 {
  142. glog.V(0).Infoln("No more writable volumes!")
  143. return nil, 0, nil, errors.New("No more writable volumes!")
  144. }
  145. if option.DataCenter == "" {
  146. vid := vl.writables[rand.Intn(lenWriters)]
  147. locationList := vl.vid2location[vid]
  148. if locationList != nil {
  149. return &vid, count, locationList, nil
  150. }
  151. return nil, 0, nil, errors.New("Strangely vid " + vid.String() + " is on no machine!")
  152. }
  153. var vid needle.VolumeId
  154. var locationList *VolumeLocationList
  155. counter := 0
  156. for _, v := range vl.writables {
  157. volumeLocationList := vl.vid2location[v]
  158. for _, dn := range volumeLocationList.list {
  159. if dn.GetDataCenter().Id() == NodeId(option.DataCenter) {
  160. if option.Rack != "" && dn.GetRack().Id() != NodeId(option.Rack) {
  161. continue
  162. }
  163. if option.DataNode != "" && dn.Id() != NodeId(option.DataNode) {
  164. continue
  165. }
  166. counter++
  167. if rand.Intn(counter) < 1 {
  168. vid, locationList = v, volumeLocationList
  169. }
  170. }
  171. }
  172. }
  173. return &vid, count, locationList, nil
  174. }
  175. func (vl *VolumeLayout) GetActiveVolumeCount(option *VolumeGrowOption) int {
  176. vl.accessLock.RLock()
  177. defer vl.accessLock.RUnlock()
  178. if option.DataCenter == "" {
  179. return len(vl.writables)
  180. }
  181. counter := 0
  182. for _, v := range vl.writables {
  183. for _, dn := range vl.vid2location[v].list {
  184. if dn.GetDataCenter().Id() == NodeId(option.DataCenter) {
  185. if option.Rack != "" && dn.GetRack().Id() != NodeId(option.Rack) {
  186. continue
  187. }
  188. if option.DataNode != "" && dn.Id() != NodeId(option.DataNode) {
  189. continue
  190. }
  191. counter++
  192. }
  193. }
  194. }
  195. return counter
  196. }
  197. func (vl *VolumeLayout) removeFromWritable(vid needle.VolumeId) bool {
  198. toDeleteIndex := -1
  199. for k, id := range vl.writables {
  200. if id == vid {
  201. toDeleteIndex = k
  202. break
  203. }
  204. }
  205. if toDeleteIndex >= 0 {
  206. glog.V(0).Infoln("Volume", vid, "becomes unwritable")
  207. vl.writables = append(vl.writables[0:toDeleteIndex], vl.writables[toDeleteIndex+1:]...)
  208. return true
  209. }
  210. return false
  211. }
  212. func (vl *VolumeLayout) setVolumeWritable(vid needle.VolumeId) bool {
  213. for _, v := range vl.writables {
  214. if v == vid {
  215. return false
  216. }
  217. }
  218. glog.V(0).Infoln("Volume", vid, "becomes writable")
  219. vl.writables = append(vl.writables, vid)
  220. return true
  221. }
  222. func (vl *VolumeLayout) SetVolumeUnavailable(dn *DataNode, vid needle.VolumeId) bool {
  223. vl.accessLock.Lock()
  224. defer vl.accessLock.Unlock()
  225. if location, ok := vl.vid2location[vid]; ok {
  226. if location.Remove(dn) {
  227. if location.Length() < vl.rp.GetCopyCount() {
  228. glog.V(0).Infoln("Volume", vid, "has", location.Length(), "replica, less than required", vl.rp.GetCopyCount())
  229. return vl.removeFromWritable(vid)
  230. }
  231. }
  232. }
  233. return false
  234. }
  235. func (vl *VolumeLayout) SetVolumeAvailable(dn *DataNode, vid needle.VolumeId) bool {
  236. vl.accessLock.Lock()
  237. defer vl.accessLock.Unlock()
  238. vl.vid2location[vid].Set(dn)
  239. if vl.vid2location[vid].Length() == vl.rp.GetCopyCount() {
  240. return vl.setVolumeWritable(vid)
  241. }
  242. return false
  243. }
  244. func (vl *VolumeLayout) SetVolumeCapacityFull(vid needle.VolumeId) bool {
  245. vl.accessLock.Lock()
  246. defer vl.accessLock.Unlock()
  247. // glog.V(0).Infoln("Volume", vid, "reaches full capacity.")
  248. return vl.removeFromWritable(vid)
  249. }
  250. func (vl *VolumeLayout) ToMap() map[string]interface{} {
  251. m := make(map[string]interface{})
  252. m["replication"] = vl.rp.String()
  253. m["ttl"] = vl.ttl.String()
  254. m["writables"] = vl.writables
  255. //m["locations"] = vl.vid2location
  256. return m
  257. }
  258. func (vl *VolumeLayout) Stats() *VolumeLayoutStats {
  259. vl.accessLock.RLock()
  260. defer vl.accessLock.RUnlock()
  261. ret := &VolumeLayoutStats{}
  262. freshThreshold := time.Now().Unix() - 60
  263. for vid, vll := range vl.vid2location {
  264. size, fileCount := vll.Stats(vid, freshThreshold)
  265. ret.FileCount += uint64(fileCount)
  266. ret.UsedSize += size
  267. if vl.readonlyVolumes[vid] {
  268. ret.TotalSize += size
  269. } else {
  270. ret.TotalSize += vl.volumeSizeLimit
  271. }
  272. }
  273. return ret
  274. }