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.

188 lines
6.3 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
  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. for _, dn := range locationlist.list {
  91. glog.V(0).Infoln("Start Committing vacuum", vid, "on", dn.Url())
  92. err := operation.WithVolumeServerClient(dn.Url(), grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  93. _, err := volumeServerClient.VacuumVolumeCommit(context.Background(), &volume_server_pb.VacuumVolumeCommitRequest{
  94. VolumeId: uint32(vid),
  95. })
  96. return err
  97. })
  98. if err != nil {
  99. glog.Errorf("Error when committing vacuum %d on %s: %v", vid, dn.Url(), err)
  100. isCommitSuccess = false
  101. } else {
  102. glog.V(0).Infof("Complete Committing vacuum %d on %s", vid, dn.Url())
  103. }
  104. }
  105. if isCommitSuccess {
  106. for _, dn := range locationlist.list {
  107. vl.SetVolumeAvailable(dn, vid)
  108. }
  109. }
  110. return isCommitSuccess
  111. }
  112. func batchVacuumVolumeCleanup(grpcDialOption grpc.DialOption, vl *VolumeLayout, vid needle.VolumeId, locationlist *VolumeLocationList) {
  113. for _, dn := range locationlist.list {
  114. glog.V(0).Infoln("Start cleaning up", vid, "on", dn.Url())
  115. err := operation.WithVolumeServerClient(dn.Url(), grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  116. _, err := volumeServerClient.VacuumVolumeCleanup(context.Background(), &volume_server_pb.VacuumVolumeCleanupRequest{
  117. VolumeId: uint32(vid),
  118. })
  119. return err
  120. })
  121. if err != nil {
  122. glog.Errorf("Error when cleaning up vacuum %d on %s: %v", vid, dn.Url(), err)
  123. } else {
  124. glog.V(0).Infof("Complete cleaning up vacuum %d on %s", vid, dn.Url())
  125. }
  126. }
  127. }
  128. func (t *Topology) Vacuum(grpcDialOption grpc.DialOption, garbageThreshold float64, preallocate int64) int {
  129. // if there is vacuum going on, return immediately
  130. swapped := atomic.CompareAndSwapInt64(&t.vacuumLockCounter, 0, 1)
  131. if !swapped {
  132. return 0
  133. }
  134. defer atomic.StoreInt64(&t.vacuumLockCounter, 0)
  135. // now only one vacuum process going on
  136. glog.V(1).Infof("Start vacuum on demand with threshold: %f", garbageThreshold)
  137. for _, col := range t.collectionMap.Items() {
  138. c := col.(*Collection)
  139. for _, vl := range c.storageType2VolumeLayout.Items() {
  140. if vl != nil {
  141. volumeLayout := vl.(*VolumeLayout)
  142. vacuumOneVolumeLayout(grpcDialOption, volumeLayout, c, garbageThreshold, preallocate)
  143. }
  144. }
  145. }
  146. return 0
  147. }
  148. func vacuumOneVolumeLayout(grpcDialOption grpc.DialOption, volumeLayout *VolumeLayout, c *Collection, garbageThreshold float64, preallocate int64) {
  149. volumeLayout.accessLock.RLock()
  150. tmpMap := make(map[needle.VolumeId]*VolumeLocationList)
  151. for vid, locationList := range volumeLayout.vid2location {
  152. tmpMap[vid] = locationList
  153. }
  154. volumeLayout.accessLock.RUnlock()
  155. for vid, locationList := range tmpMap {
  156. volumeLayout.accessLock.RLock()
  157. isReadOnly, hasValue := volumeLayout.readonlyVolumes[vid]
  158. volumeLayout.accessLock.RUnlock()
  159. if hasValue && isReadOnly {
  160. continue
  161. }
  162. glog.V(2).Infof("check vacuum on collection:%s volume:%d", c.Name, vid)
  163. if vacuumLocationList, needVacuum := batchVacuumVolumeCheck(
  164. grpcDialOption, volumeLayout, vid, locationList, garbageThreshold); needVacuum {
  165. if batchVacuumVolumeCompact(grpcDialOption, volumeLayout, vid, vacuumLocationList, preallocate) {
  166. batchVacuumVolumeCommit(grpcDialOption, volumeLayout, vid, vacuumLocationList)
  167. } else {
  168. batchVacuumVolumeCleanup(grpcDialOption, volumeLayout, vid, vacuumLocationList)
  169. }
  170. }
  171. }
  172. }