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.

157 lines
4.6 KiB

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