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.

155 lines
4.5 KiB

  1. package topology
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "net/url"
  6. "time"
  7. "github.com/chrislusf/seaweedfs/go/glog"
  8. "github.com/chrislusf/seaweedfs/go/storage"
  9. "github.com/chrislusf/seaweedfs/go/util"
  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 _, col := range t.collectionMap.Items {
  81. c := col.(*Collection)
  82. for _, vl := range c.storageType2VolumeLayout.Items {
  83. if vl != nil {
  84. volumeLayout := vl.(*VolumeLayout)
  85. for vid, locationlist := range volumeLayout.vid2location {
  86. if batchVacuumVolumeCheck(volumeLayout, vid, locationlist, garbageThreshold) {
  87. if batchVacuumVolumeCompact(volumeLayout, vid, locationlist) {
  88. batchVacuumVolumeCommit(volumeLayout, vid, locationlist)
  89. }
  90. }
  91. }
  92. }
  93. }
  94. }
  95. return 0
  96. }
  97. type VacuumVolumeResult struct {
  98. Result bool
  99. Error string
  100. }
  101. func vacuumVolume_Check(urlLocation string, vid storage.VolumeId, garbageThreshold string) (error, bool) {
  102. values := make(url.Values)
  103. values.Add("volume", vid.String())
  104. values.Add("garbageThreshold", garbageThreshold)
  105. jsonBlob, err := util.Post("http://"+urlLocation+"/admin/vacuum/check", values)
  106. if err != nil {
  107. glog.V(0).Infoln("parameters:", values)
  108. return err, false
  109. }
  110. var ret VacuumVolumeResult
  111. if err := json.Unmarshal(jsonBlob, &ret); err != nil {
  112. return err, false
  113. }
  114. if ret.Error != "" {
  115. return errors.New(ret.Error), false
  116. }
  117. return nil, ret.Result
  118. }
  119. func vacuumVolume_Compact(urlLocation string, vid storage.VolumeId) error {
  120. values := make(url.Values)
  121. values.Add("volume", vid.String())
  122. jsonBlob, err := util.Post("http://"+urlLocation+"/admin/vacuum/compact", values)
  123. if err != nil {
  124. return err
  125. }
  126. var ret VacuumVolumeResult
  127. if err := json.Unmarshal(jsonBlob, &ret); err != nil {
  128. return err
  129. }
  130. if ret.Error != "" {
  131. return errors.New(ret.Error)
  132. }
  133. return nil
  134. }
  135. func vacuumVolume_Commit(urlLocation string, vid storage.VolumeId) error {
  136. values := make(url.Values)
  137. values.Add("volume", vid.String())
  138. jsonBlob, err := util.Post("http://"+urlLocation+"/admin/vacuum/commit", values)
  139. if err != nil {
  140. return err
  141. }
  142. var ret VacuumVolumeResult
  143. if err := json.Unmarshal(jsonBlob, &ret); err != nil {
  144. return err
  145. }
  146. if ret.Error != "" {
  147. return errors.New(ret.Error)
  148. }
  149. return nil
  150. }