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.

163 lines
5.3 KiB

6 years ago
6 years ago
6 years ago
  1. package topology
  2. import (
  3. "context"
  4. "time"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. "github.com/chrislusf/seaweedfs/weed/operation"
  7. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  8. "github.com/chrislusf/seaweedfs/weed/storage"
  9. )
  10. func batchVacuumVolumeCheck(vl *VolumeLayout, vid storage.VolumeId, locationlist *VolumeLocationList, garbageThreshold float64) bool {
  11. ch := make(chan bool, locationlist.Length())
  12. for index, dn := range locationlist.list {
  13. go func(index int, url string, vid storage.VolumeId) {
  14. err := operation.WithVolumeServerClient(url, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  15. ctx, cancel := context.WithTimeout(context.Background(), time.Duration(5*time.Second))
  16. defer cancel()
  17. resp, err := volumeServerClient.VacuumVolumeCheck(ctx, &volume_server_pb.VacuumVolumeCheckRequest{
  18. VolumdId: uint32(vid),
  19. })
  20. if err != nil {
  21. ch <- false
  22. return err
  23. }
  24. isNeeded := resp.GarbageRatio > garbageThreshold
  25. ch <- isNeeded
  26. return nil
  27. })
  28. if err != nil {
  29. glog.V(0).Infof("Checking vacuuming %d on %s: %v", vid, url, err)
  30. }
  31. }(index, dn.Url(), vid)
  32. }
  33. isCheckSuccess := true
  34. for _ = range locationlist.list {
  35. select {
  36. case canVacuum := <-ch:
  37. isCheckSuccess = isCheckSuccess && canVacuum
  38. case <-time.After(30 * time.Minute):
  39. isCheckSuccess = false
  40. break
  41. }
  42. }
  43. return isCheckSuccess
  44. }
  45. func batchVacuumVolumeCompact(vl *VolumeLayout, vid storage.VolumeId, locationlist *VolumeLocationList, preallocate int64) bool {
  46. vl.removeFromWritable(vid)
  47. ch := make(chan bool, locationlist.Length())
  48. for index, dn := range locationlist.list {
  49. go func(index int, url string, vid storage.VolumeId) {
  50. glog.V(0).Infoln(index, "Start vacuuming", vid, "on", url)
  51. err := operation.WithVolumeServerClient(url, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  52. _, err := volumeServerClient.VacuumVolumeCompact(context.Background(), &volume_server_pb.VacuumVolumeCompactRequest{
  53. VolumdId: uint32(vid),
  54. })
  55. return err
  56. })
  57. if err != nil {
  58. glog.Errorf("Error when vacuuming %d on %s: %v", vid, url, err)
  59. ch <- false
  60. } else {
  61. glog.V(0).Infof("Complete vacuuming %d on %s", vid, url)
  62. ch <- true
  63. }
  64. }(index, dn.Url(), vid)
  65. }
  66. isVacuumSuccess := true
  67. for _ = range locationlist.list {
  68. select {
  69. case canCommit := <-ch:
  70. isVacuumSuccess = isVacuumSuccess && canCommit
  71. case <-time.After(30 * time.Minute):
  72. isVacuumSuccess = false
  73. break
  74. }
  75. }
  76. return isVacuumSuccess
  77. }
  78. func batchVacuumVolumeCommit(vl *VolumeLayout, vid storage.VolumeId, locationlist *VolumeLocationList) bool {
  79. isCommitSuccess := true
  80. for _, dn := range locationlist.list {
  81. glog.V(0).Infoln("Start Committing vacuum", vid, "on", dn.Url())
  82. err := operation.WithVolumeServerClient(dn.Url(), func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  83. _, err := volumeServerClient.VacuumVolumeCommit(context.Background(), &volume_server_pb.VacuumVolumeCommitRequest{
  84. VolumdId: uint32(vid),
  85. })
  86. return err
  87. })
  88. if err != nil {
  89. glog.Errorf("Error when committing vacuum %d on %s: %v", vid, dn.Url(), err)
  90. isCommitSuccess = false
  91. } else {
  92. glog.V(0).Infof("Complete Committing vacuum %d on %s", vid, dn.Url())
  93. }
  94. if isCommitSuccess {
  95. vl.SetVolumeAvailable(dn, vid)
  96. }
  97. }
  98. return isCommitSuccess
  99. }
  100. func batchVacuumVolumeCleanup(vl *VolumeLayout, vid storage.VolumeId, locationlist *VolumeLocationList) {
  101. for _, dn := range locationlist.list {
  102. glog.V(0).Infoln("Start cleaning up", vid, "on", dn.Url())
  103. err := operation.WithVolumeServerClient(dn.Url(), func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  104. _, err := volumeServerClient.VacuumVolumeCleanup(context.Background(), &volume_server_pb.VacuumVolumeCleanupRequest{
  105. VolumdId: uint32(vid),
  106. })
  107. return err
  108. })
  109. if err != nil {
  110. glog.Errorf("Error when cleaning up vacuum %d on %s: %v", vid, dn.Url(), err)
  111. } else {
  112. glog.V(0).Infof("Complete cleaning up vacuum %d on %s", vid, dn.Url())
  113. }
  114. }
  115. }
  116. func (t *Topology) Vacuum(garbageThreshold float64, preallocate int64) int {
  117. glog.V(1).Infof("Start vacuum on demand with threshold: %f", garbageThreshold)
  118. for _, col := range t.collectionMap.Items() {
  119. c := col.(*Collection)
  120. for _, vl := range c.storageType2VolumeLayout.Items() {
  121. if vl != nil {
  122. volumeLayout := vl.(*VolumeLayout)
  123. vacuumOneVolumeLayout(volumeLayout, c, garbageThreshold, preallocate)
  124. }
  125. }
  126. }
  127. return 0
  128. }
  129. func vacuumOneVolumeLayout(volumeLayout *VolumeLayout, c *Collection, garbageThreshold float64, preallocate int64) {
  130. volumeLayout.accessLock.RLock()
  131. tmpMap := make(map[storage.VolumeId]*VolumeLocationList)
  132. for vid, locationList := range volumeLayout.vid2location {
  133. tmpMap[vid] = locationList
  134. }
  135. volumeLayout.accessLock.RUnlock()
  136. for vid, locationList := range tmpMap {
  137. volumeLayout.accessLock.RLock()
  138. isReadOnly, hasValue := volumeLayout.readonlyVolumes[vid]
  139. volumeLayout.accessLock.RUnlock()
  140. if hasValue && isReadOnly {
  141. continue
  142. }
  143. glog.V(2).Infof("check vacuum on collection:%s volume:%d", c.Name, vid)
  144. if batchVacuumVolumeCheck(volumeLayout, vid, locationList, garbageThreshold) {
  145. if batchVacuumVolumeCompact(volumeLayout, vid, locationList, preallocate) {
  146. batchVacuumVolumeCommit(volumeLayout, vid, locationList)
  147. } else {
  148. batchVacuumVolumeCleanup(volumeLayout, vid, locationList)
  149. }
  150. }
  151. }
  152. }