topology_vacuum.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package topology
  2. import (
  3. "context"
  4. "sync/atomic"
  5. "time"
  6. "google.golang.org/grpc"
  7. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  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(volumeServerClient volume_server_pb.VolumeServerClient) error {
  19. resp, err := volumeServerClient.VacuumVolumeCheck(context.Background(), &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(volumeServerClient volume_server_pb.VolumeServerClient) error {
  62. _, err := volumeServerClient.VacuumVolumeCompact(context.Background(), &volume_server_pb.VacuumVolumeCompactRequest{
  63. VolumeId: uint32(vid),
  64. Preallocate: preallocate,
  65. })
  66. return err
  67. })
  68. if err != nil {
  69. glog.Errorf("Error when vacuuming %d on %s: %v", vid, url, err)
  70. ch <- false
  71. } else {
  72. glog.V(0).Infof("Complete vacuuming %d on %s", vid, url)
  73. ch <- true
  74. }
  75. }(index, dn.Url(), vid)
  76. }
  77. isVacuumSuccess := true
  78. for range locationlist.list {
  79. select {
  80. case canCommit := <-ch:
  81. isVacuumSuccess = isVacuumSuccess && canCommit
  82. case <-time.After(30 * time.Minute):
  83. return false
  84. }
  85. }
  86. return isVacuumSuccess
  87. }
  88. func batchVacuumVolumeCommit(grpcDialOption grpc.DialOption, vl *VolumeLayout, vid needle.VolumeId, locationlist *VolumeLocationList) bool {
  89. isCommitSuccess := true
  90. isReadOnly := false
  91. for _, dn := range locationlist.list {
  92. glog.V(0).Infoln("Start Committing vacuum", vid, "on", dn.Url())
  93. err := operation.WithVolumeServerClient(dn.Url(), grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  94. resp, err := volumeServerClient.VacuumVolumeCommit(context.Background(), &volume_server_pb.VacuumVolumeCommitRequest{
  95. VolumeId: uint32(vid),
  96. })
  97. if resp.IsReadOnly {
  98. isReadOnly = true
  99. }
  100. return err
  101. })
  102. if err != nil {
  103. glog.Errorf("Error when committing vacuum %d on %s: %v", vid, dn.Url(), err)
  104. isCommitSuccess = false
  105. } else {
  106. glog.V(0).Infof("Complete Committing vacuum %d on %s", vid, dn.Url())
  107. }
  108. }
  109. if isCommitSuccess {
  110. for _, dn := range locationlist.list {
  111. vl.SetVolumeAvailable(dn, vid, isReadOnly)
  112. }
  113. }
  114. return isCommitSuccess
  115. }
  116. func batchVacuumVolumeCleanup(grpcDialOption grpc.DialOption, vl *VolumeLayout, vid needle.VolumeId, locationlist *VolumeLocationList) {
  117. for _, dn := range locationlist.list {
  118. glog.V(0).Infoln("Start cleaning up", vid, "on", dn.Url())
  119. err := operation.WithVolumeServerClient(dn.Url(), grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  120. _, err := volumeServerClient.VacuumVolumeCleanup(context.Background(), &volume_server_pb.VacuumVolumeCleanupRequest{
  121. VolumeId: uint32(vid),
  122. })
  123. return err
  124. })
  125. if err != nil {
  126. glog.Errorf("Error when cleaning up vacuum %d on %s: %v", vid, dn.Url(), err)
  127. } else {
  128. glog.V(0).Infof("Complete cleaning up vacuum %d on %s", vid, dn.Url())
  129. }
  130. }
  131. }
  132. func (t *Topology) Vacuum(grpcDialOption grpc.DialOption, garbageThreshold float64, preallocate int64) int {
  133. // if there is vacuum going on, return immediately
  134. swapped := atomic.CompareAndSwapInt64(&t.vacuumLockCounter, 0, 1)
  135. if !swapped {
  136. return 0
  137. }
  138. defer atomic.StoreInt64(&t.vacuumLockCounter, 0)
  139. // now only one vacuum process going on
  140. glog.V(1).Infof("Start vacuum on demand with threshold: %f", garbageThreshold)
  141. for _, col := range t.collectionMap.Items() {
  142. c := col.(*Collection)
  143. for _, vl := range c.storageType2VolumeLayout.Items() {
  144. if vl != nil {
  145. volumeLayout := vl.(*VolumeLayout)
  146. vacuumOneVolumeLayout(grpcDialOption, volumeLayout, c, garbageThreshold, preallocate)
  147. }
  148. }
  149. }
  150. return 0
  151. }
  152. func vacuumOneVolumeLayout(grpcDialOption grpc.DialOption, volumeLayout *VolumeLayout, c *Collection, garbageThreshold float64, preallocate int64) {
  153. volumeLayout.accessLock.RLock()
  154. tmpMap := make(map[needle.VolumeId]*VolumeLocationList)
  155. for vid, locationList := range volumeLayout.vid2location {
  156. tmpMap[vid] = locationList
  157. }
  158. volumeLayout.accessLock.RUnlock()
  159. for vid, locationList := range tmpMap {
  160. volumeLayout.accessLock.RLock()
  161. isReadOnly, hasValue := volumeLayout.readonlyVolumes[vid]
  162. volumeLayout.accessLock.RUnlock()
  163. if hasValue && isReadOnly {
  164. continue
  165. }
  166. glog.V(2).Infof("check vacuum on collection:%s volume:%d", c.Name, vid)
  167. if vacuumLocationList, needVacuum := batchVacuumVolumeCheck(
  168. grpcDialOption, volumeLayout, vid, locationList, garbageThreshold); needVacuum {
  169. if batchVacuumVolumeCompact(grpcDialOption, volumeLayout, vid, vacuumLocationList, preallocate) {
  170. batchVacuumVolumeCommit(grpcDialOption, volumeLayout, vid, vacuumLocationList)
  171. } else {
  172. batchVacuumVolumeCleanup(grpcDialOption, volumeLayout, vid, vacuumLocationList)
  173. }
  174. }
  175. }
  176. }