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.

199 lines
6.7 KiB

6 years ago
6 years ago
4 years ago
6 years ago
6 years ago
6 years ago
  1. package topology
  2. import (
  3. "context"
  4. "github.com/chrislusf/seaweedfs/weed/pb"
  5. "sync/atomic"
  6. "time"
  7. "google.golang.org/grpc"
  8. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/operation"
  11. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  12. )
  13. func (t *Topology) batchVacuumVolumeCheck(grpcDialOption grpc.DialOption, vid needle.VolumeId,
  14. locationlist *VolumeLocationList, garbageThreshold float64) (*VolumeLocationList, bool) {
  15. ch := make(chan int, locationlist.Length())
  16. errCount := int32(0)
  17. for index, dn := range locationlist.list {
  18. go func(index int, url pb.ServerAddress, vid needle.VolumeId) {
  19. err := operation.WithVolumeServerClient(url, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  20. resp, err := volumeServerClient.VacuumVolumeCheck(context.Background(), &volume_server_pb.VacuumVolumeCheckRequest{
  21. VolumeId: uint32(vid),
  22. })
  23. if err != nil {
  24. atomic.AddInt32(&errCount, 1)
  25. ch <- -1
  26. return err
  27. }
  28. if resp.GarbageRatio >= garbageThreshold {
  29. ch <- index
  30. } else {
  31. ch <- -1
  32. }
  33. return nil
  34. })
  35. if err != nil {
  36. glog.V(0).Infof("Checking vacuuming %d on %s: %v", vid, url, err)
  37. }
  38. }(index, dn.ServerAddress(), vid)
  39. }
  40. vacuumLocationList := NewVolumeLocationList()
  41. waitTimeout := time.NewTimer(time.Minute * time.Duration(t.volumeSizeLimit/1024/1024/1000+1))
  42. defer waitTimeout.Stop()
  43. for range locationlist.list {
  44. select {
  45. case index := <-ch:
  46. if index != -1 {
  47. vacuumLocationList.list = append(vacuumLocationList.list, locationlist.list[index])
  48. }
  49. case <-waitTimeout.C:
  50. return vacuumLocationList, false
  51. }
  52. }
  53. return vacuumLocationList, errCount == 0 && len(vacuumLocationList.list) > 0
  54. }
  55. func (t *Topology) batchVacuumVolumeCompact(grpcDialOption grpc.DialOption, vl *VolumeLayout, vid needle.VolumeId,
  56. locationlist *VolumeLocationList, preallocate int64) bool {
  57. vl.accessLock.Lock()
  58. vl.removeFromWritable(vid)
  59. vl.accessLock.Unlock()
  60. ch := make(chan bool, locationlist.Length())
  61. for index, dn := range locationlist.list {
  62. go func(index int, url pb.ServerAddress, vid needle.VolumeId) {
  63. glog.V(0).Infoln(index, "Start vacuuming", vid, "on", url)
  64. err := operation.WithVolumeServerClient(url, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  65. _, err := volumeServerClient.VacuumVolumeCompact(context.Background(), &volume_server_pb.VacuumVolumeCompactRequest{
  66. VolumeId: uint32(vid),
  67. Preallocate: preallocate,
  68. })
  69. return err
  70. })
  71. if err != nil {
  72. glog.Errorf("Error when vacuuming %d on %s: %v", vid, url, err)
  73. ch <- false
  74. } else {
  75. glog.V(0).Infof("Complete vacuuming %d on %s", vid, url)
  76. ch <- true
  77. }
  78. }(index, dn.ServerAddress(), vid)
  79. }
  80. isVacuumSuccess := true
  81. waitTimeout := time.NewTimer(3 * time.Minute * time.Duration(t.volumeSizeLimit/1024/1024/1000+1))
  82. defer waitTimeout.Stop()
  83. for range locationlist.list {
  84. select {
  85. case canCommit := <-ch:
  86. isVacuumSuccess = isVacuumSuccess && canCommit
  87. case <-waitTimeout.C:
  88. return false
  89. }
  90. }
  91. return isVacuumSuccess
  92. }
  93. func (t *Topology) batchVacuumVolumeCommit(grpcDialOption grpc.DialOption, vl *VolumeLayout, vid needle.VolumeId, locationlist *VolumeLocationList) bool {
  94. isCommitSuccess := true
  95. isReadOnly := false
  96. for _, dn := range locationlist.list {
  97. glog.V(0).Infoln("Start Committing vacuum", vid, "on", dn.Url())
  98. err := operation.WithVolumeServerClient(dn.ServerAddress(), grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  99. resp, err := volumeServerClient.VacuumVolumeCommit(context.Background(), &volume_server_pb.VacuumVolumeCommitRequest{
  100. VolumeId: uint32(vid),
  101. })
  102. if resp != nil && resp.IsReadOnly {
  103. isReadOnly = true
  104. }
  105. return err
  106. })
  107. if err != nil {
  108. glog.Errorf("Error when committing vacuum %d on %s: %v", vid, dn.Url(), err)
  109. isCommitSuccess = false
  110. } else {
  111. glog.V(0).Infof("Complete Committing vacuum %d on %s", vid, dn.Url())
  112. }
  113. }
  114. if isCommitSuccess {
  115. for _, dn := range locationlist.list {
  116. vl.SetVolumeAvailable(dn, vid, isReadOnly)
  117. }
  118. }
  119. return isCommitSuccess
  120. }
  121. func (t *Topology) batchVacuumVolumeCleanup(grpcDialOption grpc.DialOption, vl *VolumeLayout, vid needle.VolumeId, locationlist *VolumeLocationList) {
  122. for _, dn := range locationlist.list {
  123. glog.V(0).Infoln("Start cleaning up", vid, "on", dn.Url())
  124. err := operation.WithVolumeServerClient(dn.ServerAddress(), grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  125. _, err := volumeServerClient.VacuumVolumeCleanup(context.Background(), &volume_server_pb.VacuumVolumeCleanupRequest{
  126. VolumeId: uint32(vid),
  127. })
  128. return err
  129. })
  130. if err != nil {
  131. glog.Errorf("Error when cleaning up vacuum %d on %s: %v", vid, dn.Url(), err)
  132. } else {
  133. glog.V(0).Infof("Complete cleaning up vacuum %d on %s", vid, dn.Url())
  134. }
  135. }
  136. }
  137. func (t *Topology) Vacuum(grpcDialOption grpc.DialOption, garbageThreshold float64, preallocate int64) {
  138. // if there is vacuum going on, return immediately
  139. swapped := atomic.CompareAndSwapInt64(&t.vacuumLockCounter, 0, 1)
  140. if !swapped {
  141. return
  142. }
  143. defer atomic.StoreInt64(&t.vacuumLockCounter, 0)
  144. // now only one vacuum process going on
  145. glog.V(1).Infof("Start vacuum on demand with threshold: %f", garbageThreshold)
  146. for _, col := range t.collectionMap.Items() {
  147. c := col.(*Collection)
  148. for _, vl := range c.storageType2VolumeLayout.Items() {
  149. if vl != nil {
  150. volumeLayout := vl.(*VolumeLayout)
  151. t.vacuumOneVolumeLayout(grpcDialOption, volumeLayout, c, garbageThreshold, preallocate)
  152. }
  153. }
  154. }
  155. }
  156. func (t *Topology) vacuumOneVolumeLayout(grpcDialOption grpc.DialOption, volumeLayout *VolumeLayout, c *Collection, garbageThreshold float64, preallocate int64) {
  157. volumeLayout.accessLock.RLock()
  158. tmpMap := make(map[needle.VolumeId]*VolumeLocationList)
  159. for vid, locationList := range volumeLayout.vid2location {
  160. tmpMap[vid] = locationList.Copy()
  161. }
  162. volumeLayout.accessLock.RUnlock()
  163. for vid, locationList := range tmpMap {
  164. volumeLayout.accessLock.RLock()
  165. isReadOnly := volumeLayout.readonlyVolumes.IsTrue(vid)
  166. volumeLayout.accessLock.RUnlock()
  167. if isReadOnly {
  168. continue
  169. }
  170. glog.V(2).Infof("check vacuum on collection:%s volume:%d", c.Name, vid)
  171. if vacuumLocationList, needVacuum := t.batchVacuumVolumeCheck(grpcDialOption, vid, locationList, garbageThreshold); needVacuum {
  172. if t.batchVacuumVolumeCompact(grpcDialOption, volumeLayout, vid, vacuumLocationList, preallocate) {
  173. t.batchVacuumVolumeCommit(grpcDialOption, volumeLayout, vid, vacuumLocationList)
  174. } else {
  175. t.batchVacuumVolumeCleanup(grpcDialOption, volumeLayout, vid, vacuumLocationList)
  176. }
  177. }
  178. }
  179. }