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.

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