topology_vacuum.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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, locationlist *VolumeLocationList, garbageThreshold float64) bool {
  13. ch := make(chan bool, locationlist.Length())
  14. for index, dn := range locationlist.list {
  15. go func(index int, url string, vid needle.VolumeId) {
  16. err := operation.WithVolumeServerClient(url, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  17. resp, err := volumeServerClient.VacuumVolumeCheck(context.Background(), &volume_server_pb.VacuumVolumeCheckRequest{
  18. VolumeId: 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. return false
  40. }
  41. }
  42. return isCheckSuccess
  43. }
  44. func batchVacuumVolumeCompact(grpcDialOption grpc.DialOption, vl *VolumeLayout, vid needle.VolumeId, locationlist *VolumeLocationList, preallocate int64) bool {
  45. vl.accessLock.Lock()
  46. vl.removeFromWritable(vid)
  47. vl.accessLock.Unlock()
  48. ch := make(chan bool, locationlist.Length())
  49. for index, dn := range locationlist.list {
  50. go func(index int, url string, vid needle.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. VolumeId: 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. return false
  74. }
  75. }
  76. return isVacuumSuccess
  77. }
  78. func batchVacuumVolumeCommit(grpcDialOption grpc.DialOption, vl *VolumeLayout, vid needle.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(), grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  83. _, err := volumeServerClient.VacuumVolumeCommit(context.Background(), &volume_server_pb.VacuumVolumeCommitRequest{
  84. VolumeId: 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(grpcDialOption grpc.DialOption, vl *VolumeLayout, vid needle.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(), grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  104. _, err := volumeServerClient.VacuumVolumeCleanup(context.Background(), &volume_server_pb.VacuumVolumeCleanupRequest{
  105. VolumeId: 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(grpcDialOption grpc.DialOption, garbageThreshold float64, preallocate int64) int {
  117. // if there is vacuum going on, return immediately
  118. swapped := atomic.CompareAndSwapInt64(&t.vacuumLockCounter, 0, 1)
  119. if !swapped {
  120. return 0
  121. }
  122. defer atomic.StoreInt64(&t.vacuumLockCounter, 0)
  123. // now only one vacuum process going on
  124. glog.V(1).Infof("Start vacuum on demand with threshold: %f", garbageThreshold)
  125. for _, col := range t.collectionMap.Items() {
  126. c := col.(*Collection)
  127. for _, vl := range c.storageType2VolumeLayout.Items() {
  128. if vl != nil {
  129. volumeLayout := vl.(*VolumeLayout)
  130. vacuumOneVolumeLayout(grpcDialOption, volumeLayout, c, garbageThreshold, preallocate)
  131. }
  132. }
  133. }
  134. return 0
  135. }
  136. func vacuumOneVolumeLayout(grpcDialOption grpc.DialOption, volumeLayout *VolumeLayout, c *Collection, garbageThreshold float64, preallocate int64) {
  137. volumeLayout.accessLock.RLock()
  138. tmpMap := make(map[needle.VolumeId]*VolumeLocationList)
  139. for vid, locationList := range volumeLayout.vid2location {
  140. tmpMap[vid] = locationList
  141. }
  142. volumeLayout.accessLock.RUnlock()
  143. for vid, locationList := range tmpMap {
  144. volumeLayout.accessLock.RLock()
  145. isReadOnly, hasValue := volumeLayout.readonlyVolumes[vid]
  146. volumeLayout.accessLock.RUnlock()
  147. if hasValue && isReadOnly {
  148. continue
  149. }
  150. glog.V(2).Infof("check vacuum on collection:%s volume:%d", c.Name, vid)
  151. if batchVacuumVolumeCheck(grpcDialOption, volumeLayout, vid, locationList, garbageThreshold) {
  152. if batchVacuumVolumeCompact(grpcDialOption, volumeLayout, vid, locationList, preallocate) {
  153. batchVacuumVolumeCommit(grpcDialOption, volumeLayout, vid, locationList)
  154. } else {
  155. batchVacuumVolumeCleanup(grpcDialOption, volumeLayout, vid, locationList)
  156. }
  157. }
  158. }
  159. }