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.

164 lines
5.7 KiB

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