filer_deletion.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package filer
  2. import (
  3. "context"
  4. "strings"
  5. "time"
  6. "github.com/seaweedfs/seaweedfs/weed/storage"
  7. "github.com/seaweedfs/seaweedfs/weed/util"
  8. "github.com/seaweedfs/seaweedfs/weed/glog"
  9. "github.com/seaweedfs/seaweedfs/weed/operation"
  10. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  11. "github.com/seaweedfs/seaweedfs/weed/wdclient"
  12. )
  13. func LookupByMasterClientFn(masterClient *wdclient.MasterClient) func(vids []string) (map[string]*operation.LookupResult, error) {
  14. return func(vids []string) (map[string]*operation.LookupResult, error) {
  15. m := make(map[string]*operation.LookupResult)
  16. for _, vid := range vids {
  17. locs, _ := masterClient.GetVidLocations(vid)
  18. var locations []operation.Location
  19. for _, loc := range locs {
  20. locations = append(locations, operation.Location{
  21. Url: loc.Url,
  22. PublicUrl: loc.PublicUrl,
  23. GrpcPort: loc.GrpcPort,
  24. })
  25. }
  26. m[vid] = &operation.LookupResult{
  27. VolumeOrFileId: vid,
  28. Locations: locations,
  29. }
  30. }
  31. return m, nil
  32. }
  33. }
  34. func (f *Filer) loopProcessingDeletion() {
  35. lookupFunc := LookupByMasterClientFn(f.MasterClient)
  36. DeletionBatchSize := 100000 // roughly 20 bytes cost per file id.
  37. var deletionCount int
  38. for {
  39. deletionCount = 0
  40. f.fileIdDeletionQueue.Consume(func(fileIds []string) {
  41. for len(fileIds) > 0 {
  42. var toDeleteFileIds []string
  43. if len(fileIds) > DeletionBatchSize {
  44. toDeleteFileIds = fileIds[:DeletionBatchSize]
  45. fileIds = fileIds[DeletionBatchSize:]
  46. } else {
  47. toDeleteFileIds = fileIds
  48. fileIds = fileIds[:0]
  49. }
  50. deletionCount = len(toDeleteFileIds)
  51. _, err := operation.DeleteFileIdsWithLookupVolumeId(f.GrpcDialOption, toDeleteFileIds, lookupFunc)
  52. if err != nil {
  53. if !strings.Contains(err.Error(), storage.ErrorDeleted.Error()) {
  54. glog.V(0).Infof("deleting fileIds len=%d error: %v", deletionCount, err)
  55. }
  56. } else {
  57. glog.V(2).Infof("deleting fileIds %+v", toDeleteFileIds)
  58. }
  59. }
  60. })
  61. if deletionCount == 0 {
  62. time.Sleep(1123 * time.Millisecond)
  63. }
  64. }
  65. }
  66. func (f *Filer) DeleteUncommittedChunks(ctx context.Context, chunks []*filer_pb.FileChunk) {
  67. f.doDeleteChunks(ctx, chunks)
  68. }
  69. func (f *Filer) DeleteChunks(ctx context.Context, fullpath util.FullPath, chunks []*filer_pb.FileChunk) {
  70. rule := f.FilerConf.MatchStorageRule(string(fullpath))
  71. if rule.DisableChunkDeletion {
  72. return
  73. }
  74. f.doDeleteChunks(ctx, chunks)
  75. }
  76. func (f *Filer) doDeleteChunks(ctx context.Context, chunks []*filer_pb.FileChunk) {
  77. for _, chunk := range chunks {
  78. if !chunk.IsChunkManifest {
  79. f.fileIdDeletionQueue.EnQueue(chunk.GetFileIdString())
  80. continue
  81. }
  82. dataChunks, manifestResolveErr := ResolveOneChunkManifest(ctx, f.MasterClient.LookupFileId, chunk)
  83. if manifestResolveErr != nil {
  84. glog.V(0).InfofCtx(ctx, "failed to resolve manifest %s: %v", chunk.FileId, manifestResolveErr)
  85. }
  86. for _, dChunk := range dataChunks {
  87. f.fileIdDeletionQueue.EnQueue(dChunk.GetFileIdString())
  88. }
  89. f.fileIdDeletionQueue.EnQueue(chunk.GetFileIdString())
  90. }
  91. }
  92. func (f *Filer) DeleteChunksNotRecursive(chunks []*filer_pb.FileChunk) {
  93. for _, chunk := range chunks {
  94. f.fileIdDeletionQueue.EnQueue(chunk.GetFileIdString())
  95. }
  96. }
  97. func (f *Filer) deleteChunksIfNotNew(ctx context.Context, oldEntry, newEntry *Entry) {
  98. var oldChunks, newChunks []*filer_pb.FileChunk
  99. if oldEntry != nil {
  100. oldChunks = oldEntry.GetChunks()
  101. }
  102. if newEntry != nil {
  103. newChunks = newEntry.GetChunks()
  104. }
  105. toDelete, err := MinusChunks(ctx, f.MasterClient.GetLookupFileIdFunction(), oldChunks, newChunks)
  106. if err != nil {
  107. glog.ErrorfCtx(ctx, "Failed to resolve old entry chunks when delete old entry chunks. new: %s, old: %s", newChunks, oldChunks)
  108. return
  109. }
  110. f.DeleteChunksNotRecursive(toDelete)
  111. }