topology_vacuum.go 6.2 KB

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