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.

152 lines
4.4 KiB

  1. package topology
  2. import (
  3. "github.com/chrislusf/weed-fs/go/glog"
  4. "github.com/chrislusf/weed-fs/go/storage"
  5. "github.com/chrislusf/weed-fs/go/util"
  6. "encoding/json"
  7. "errors"
  8. "net/url"
  9. "time"
  10. )
  11. func batchVacuumVolumeCheck(vl *VolumeLayout, vid storage.VolumeId, locationlist *VolumeLocationList, garbageThreshold string) bool {
  12. ch := make(chan bool, locationlist.Length())
  13. for index, dn := range locationlist.list {
  14. go func(index int, url string, vid storage.VolumeId) {
  15. //glog.V(0).Infoln(index, "Check vacuuming", vid, "on", dn.Url())
  16. if e, ret := vacuumVolume_Check(url, vid, garbageThreshold); e != nil {
  17. //glog.V(0).Infoln(index, "Error when checking vacuuming", vid, "on", url, e)
  18. ch <- false
  19. } else {
  20. //glog.V(0).Infoln(index, "Checked vacuuming", vid, "on", url, "needVacuum", ret)
  21. ch <- ret
  22. }
  23. }(index, dn.Url(), vid)
  24. }
  25. isCheckSuccess := true
  26. for _ = range locationlist.list {
  27. select {
  28. case canVacuum := <-ch:
  29. isCheckSuccess = isCheckSuccess && canVacuum
  30. case <-time.After(30 * time.Minute):
  31. isCheckSuccess = false
  32. break
  33. }
  34. }
  35. return isCheckSuccess
  36. }
  37. func batchVacuumVolumeCompact(vl *VolumeLayout, vid storage.VolumeId, locationlist *VolumeLocationList) bool {
  38. vl.removeFromWritable(vid)
  39. ch := make(chan bool, locationlist.Length())
  40. for index, dn := range locationlist.list {
  41. go func(index int, url string, vid storage.VolumeId) {
  42. glog.V(0).Infoln(index, "Start vacuuming", vid, "on", url)
  43. if e := vacuumVolume_Compact(url, vid); e != nil {
  44. glog.V(0).Infoln(index, "Error when vacuuming", vid, "on", url, e)
  45. ch <- false
  46. } else {
  47. glog.V(0).Infoln(index, "Complete vacuuming", vid, "on", url)
  48. ch <- true
  49. }
  50. }(index, dn.Url(), vid)
  51. }
  52. isVacuumSuccess := true
  53. for _ = range locationlist.list {
  54. select {
  55. case _ = <-ch:
  56. case <-time.After(30 * time.Minute):
  57. isVacuumSuccess = false
  58. break
  59. }
  60. }
  61. return isVacuumSuccess
  62. }
  63. func batchVacuumVolumeCommit(vl *VolumeLayout, vid storage.VolumeId, locationlist *VolumeLocationList) bool {
  64. isCommitSuccess := true
  65. for _, dn := range locationlist.list {
  66. glog.V(0).Infoln("Start Commiting vacuum", vid, "on", dn.Url())
  67. if e := vacuumVolume_Commit(dn.Url(), vid); e != nil {
  68. glog.V(0).Infoln("Error when committing vacuum", vid, "on", dn.Url(), e)
  69. isCommitSuccess = false
  70. } else {
  71. glog.V(0).Infoln("Complete Commiting vacuum", vid, "on", dn.Url())
  72. }
  73. if isCommitSuccess {
  74. vl.SetVolumeAvailable(dn, vid)
  75. }
  76. }
  77. return isCommitSuccess
  78. }
  79. func (t *Topology) Vacuum(garbageThreshold string) int {
  80. for _, c := range t.collectionMap {
  81. for _, vl := range c.storageType2VolumeLayout {
  82. if vl != nil {
  83. for vid, locationlist := range vl.vid2location {
  84. if batchVacuumVolumeCheck(vl, vid, locationlist, garbageThreshold) {
  85. if batchVacuumVolumeCompact(vl, vid, locationlist) {
  86. batchVacuumVolumeCommit(vl, vid, locationlist)
  87. }
  88. }
  89. }
  90. }
  91. }
  92. }
  93. return 0
  94. }
  95. type VacuumVolumeResult struct {
  96. Result bool
  97. Error string
  98. }
  99. func vacuumVolume_Check(urlLocation string, vid storage.VolumeId, garbageThreshold string) (error, bool) {
  100. values := make(url.Values)
  101. values.Add("volume", vid.String())
  102. values.Add("garbageThreshold", garbageThreshold)
  103. jsonBlob, err := util.Post("http://"+urlLocation+"/admin/vacuum_volume_check", values)
  104. if err != nil {
  105. glog.V(0).Infoln("parameters:", values)
  106. return err, false
  107. }
  108. var ret VacuumVolumeResult
  109. if err := json.Unmarshal(jsonBlob, &ret); err != nil {
  110. return err, false
  111. }
  112. if ret.Error != "" {
  113. return errors.New(ret.Error), false
  114. }
  115. return nil, ret.Result
  116. }
  117. func vacuumVolume_Compact(urlLocation string, vid storage.VolumeId) error {
  118. values := make(url.Values)
  119. values.Add("volume", vid.String())
  120. jsonBlob, err := util.Post("http://"+urlLocation+"/admin/vacuum_volume_compact", values)
  121. if err != nil {
  122. return err
  123. }
  124. var ret VacuumVolumeResult
  125. if err := json.Unmarshal(jsonBlob, &ret); err != nil {
  126. return err
  127. }
  128. if ret.Error != "" {
  129. return errors.New(ret.Error)
  130. }
  131. return nil
  132. }
  133. func vacuumVolume_Commit(urlLocation string, vid storage.VolumeId) error {
  134. values := make(url.Values)
  135. values.Add("volume", vid.String())
  136. jsonBlob, err := util.Post("http://"+urlLocation+"/admin/vacuum_volume_commit", values)
  137. if err != nil {
  138. return err
  139. }
  140. var ret VacuumVolumeResult
  141. if err := json.Unmarshal(jsonBlob, &ret); err != nil {
  142. return err
  143. }
  144. if ret.Error != "" {
  145. return errors.New(ret.Error)
  146. }
  147. return nil
  148. }