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.

170 lines
5.6 KiB

6 years ago
  1. package topology
  2. import (
  3. "context"
  4. "time"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. "github.com/chrislusf/seaweedfs/weed/operation"
  7. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  8. "github.com/chrislusf/seaweedfs/weed/storage"
  9. )
  10. func batchVacuumVolumeCheck(vl *VolumeLayout, vid storage.VolumeId, locationlist *VolumeLocationList, garbageThreshold float64) bool {
  11. ch := make(chan bool, locationlist.Length())
  12. for index, dn := range locationlist.list {
  13. go func(index int, url string, vid storage.VolumeId) {
  14. err := operation.WithVolumeServerClient(url, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  15. ctx, cancel := context.WithTimeout(context.Background(), time.Duration(5*time.Second))
  16. defer cancel()
  17. resp, err := volumeServerClient.VacuumVolumeCheck(ctx, &volume_server_pb.VacuumVolumeCheckRequest{
  18. VolumdId: 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(vl *VolumeLayout, vid storage.VolumeId, locationlist *VolumeLocationList, preallocate int64) bool {
  46. vl.removeFromWritable(vid)
  47. ch := make(chan bool, locationlist.Length())
  48. for index, dn := range locationlist.list {
  49. go func(index int, url string, vid storage.VolumeId) {
  50. // TODO: set timeout according to actual compact duration
  51. glog.V(0).Infoln(index, "Start vacuuming", vid, "on", url)
  52. err := operation.WithVolumeServerClient(url, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  53. _, err := volumeServerClient.VacuumVolumeCompact(context.Background(), &volume_server_pb.VacuumVolumeCompactRequest{
  54. VolumdId: 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. isVacuumSuccess = false
  74. break
  75. }
  76. }
  77. return isVacuumSuccess
  78. }
  79. func batchVacuumVolumeCommit(vl *VolumeLayout, vid storage.VolumeId, locationlist *VolumeLocationList) bool {
  80. isCommitSuccess := true
  81. for _, dn := range locationlist.list {
  82. glog.V(0).Infoln("Start Commiting vacuum", vid, "on", dn.Url())
  83. err := operation.WithVolumeServerClient(dn.Url(), func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  84. ctx, cancel := context.WithTimeout(context.Background(), time.Duration(5*time.Second))
  85. defer cancel()
  86. _, err := volumeServerClient.VacuumVolumeCommit(ctx, &volume_server_pb.VacuumVolumeCommitRequest{
  87. VolumdId: uint32(vid),
  88. })
  89. return err
  90. })
  91. if err != nil {
  92. glog.Errorf("Error when committing vacuum %d on %s: %v", vid, dn.Url(), err)
  93. isCommitSuccess = false
  94. } else {
  95. glog.V(0).Infof("Complete Commiting vacuum %d on %s", vid, dn.Url())
  96. }
  97. if isCommitSuccess {
  98. vl.SetVolumeAvailable(dn, vid)
  99. }
  100. }
  101. return isCommitSuccess
  102. }
  103. func batchVacuumVolumeCleanup(vl *VolumeLayout, vid storage.VolumeId, locationlist *VolumeLocationList) {
  104. for _, dn := range locationlist.list {
  105. glog.V(0).Infoln("Start cleaning up", vid, "on", dn.Url())
  106. err := operation.WithVolumeServerClient(dn.Url(), func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  107. ctx, cancel := context.WithTimeout(context.Background(), time.Duration(5*time.Second))
  108. defer cancel()
  109. _, err := volumeServerClient.VacuumVolumeCleanup(ctx, &volume_server_pb.VacuumVolumeCleanupRequest{
  110. VolumdId: uint32(vid),
  111. })
  112. return err
  113. })
  114. if err != nil {
  115. glog.Errorf("Error when cleaning up vacuum %d on %s: %v", vid, dn.Url(), err)
  116. } else {
  117. glog.V(0).Infof("Complete cleaning up vacuum %d on %s", vid, dn.Url())
  118. }
  119. }
  120. }
  121. func (t *Topology) Vacuum(garbageThreshold float64, preallocate int64) int {
  122. glog.V(1).Infof("Start vacuum on demand with threshold: %f", garbageThreshold)
  123. for _, col := range t.collectionMap.Items() {
  124. c := col.(*Collection)
  125. for _, vl := range c.storageType2VolumeLayout.Items() {
  126. if vl != nil {
  127. volumeLayout := vl.(*VolumeLayout)
  128. vacuumOneVolumeLayout(volumeLayout, c, garbageThreshold, preallocate)
  129. }
  130. }
  131. }
  132. return 0
  133. }
  134. func vacuumOneVolumeLayout(volumeLayout *VolumeLayout, c *Collection, garbageThreshold float64, preallocate int64) {
  135. volumeLayout.accessLock.RLock()
  136. tmpMap := make(map[storage.VolumeId]*VolumeLocationList)
  137. for vid, locationlist := range volumeLayout.vid2location {
  138. tmpMap[vid] = locationlist
  139. }
  140. volumeLayout.accessLock.RUnlock()
  141. for vid, locationlist := range tmpMap {
  142. volumeLayout.accessLock.RLock()
  143. isReadOnly, hasValue := volumeLayout.readonlyVolumes[vid]
  144. volumeLayout.accessLock.RUnlock()
  145. if hasValue && isReadOnly {
  146. continue
  147. }
  148. glog.V(2).Infof("check vacuum on collection:%s volume:%d", c.Name, vid)
  149. if batchVacuumVolumeCheck(volumeLayout, vid, locationlist, garbageThreshold) {
  150. if batchVacuumVolumeCompact(volumeLayout, vid, locationlist, preallocate) {
  151. batchVacuumVolumeCommit(volumeLayout, vid, locationlist)
  152. } else {
  153. batchVacuumVolumeCleanup(volumeLayout, vid, locationlist)
  154. }
  155. }
  156. }
  157. }