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.

150 lines
4.3 KiB

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