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.

176 lines
5.9 KiB

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