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.

225 lines
7.3 KiB

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