filer_deletion.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package filer
  2. import (
  3. "strings"
  4. "time"
  5. "github.com/chrislusf/seaweedfs/weed/util/log"
  6. "github.com/chrislusf/seaweedfs/weed/operation"
  7. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  8. "github.com/chrislusf/seaweedfs/weed/wdclient"
  9. )
  10. func LookupByMasterClientFn(masterClient *wdclient.MasterClient) func(vids []string) (map[string]operation.LookupResult, error) {
  11. return func(vids []string) (map[string]operation.LookupResult, error) {
  12. m := make(map[string]operation.LookupResult)
  13. for _, vid := range vids {
  14. locs, _ := masterClient.GetVidLocations(vid)
  15. var locations []operation.Location
  16. for _, loc := range locs {
  17. locations = append(locations, operation.Location{
  18. Url: loc.Url,
  19. PublicUrl: loc.PublicUrl,
  20. })
  21. }
  22. m[vid] = operation.LookupResult{
  23. VolumeId: vid,
  24. Locations: locations,
  25. }
  26. }
  27. return m, nil
  28. }
  29. }
  30. func (f *Filer) loopProcessingDeletion() {
  31. lookupFunc := LookupByMasterClientFn(f.MasterClient)
  32. DeletionBatchSize := 100000 // roughly 20 bytes cost per file id.
  33. var deletionCount int
  34. for {
  35. deletionCount = 0
  36. f.fileIdDeletionQueue.Consume(func(fileIds []string) {
  37. for len(fileIds) > 0 {
  38. var toDeleteFileIds []string
  39. if len(fileIds) > DeletionBatchSize {
  40. toDeleteFileIds = fileIds[:DeletionBatchSize]
  41. fileIds = fileIds[DeletionBatchSize:]
  42. } else {
  43. toDeleteFileIds = fileIds
  44. fileIds = fileIds[:0]
  45. }
  46. deletionCount = len(toDeleteFileIds)
  47. _, err := operation.DeleteFilesWithLookupVolumeId(f.GrpcDialOption, toDeleteFileIds, lookupFunc)
  48. if err != nil {
  49. if !strings.Contains(err.Error(), "already deleted") {
  50. log.Infof("deleting fileIds len=%d error: %v", deletionCount, err)
  51. }
  52. } else {
  53. log.Debugf("deleting fileIds len=%d", deletionCount)
  54. }
  55. }
  56. })
  57. if deletionCount == 0 {
  58. time.Sleep(1123 * time.Millisecond)
  59. }
  60. }
  61. }
  62. func (f *Filer) DeleteChunks(chunks []*filer_pb.FileChunk) {
  63. for _, chunk := range chunks {
  64. if !chunk.IsChunkManifest {
  65. f.fileIdDeletionQueue.EnQueue(chunk.GetFileIdString())
  66. continue
  67. }
  68. dataChunks, manifestResolveErr := ResolveOneChunkManifest(f.MasterClient.LookupFileId, chunk)
  69. if manifestResolveErr != nil {
  70. log.Infof("failed to resolve manifest %s: %v", chunk.FileId, manifestResolveErr)
  71. }
  72. for _, dChunk := range dataChunks {
  73. f.fileIdDeletionQueue.EnQueue(dChunk.GetFileIdString())
  74. }
  75. f.fileIdDeletionQueue.EnQueue(chunk.GetFileIdString())
  76. }
  77. }
  78. func (f *Filer) deleteChunksIfNotNew(oldEntry, newEntry *Entry) {
  79. if oldEntry == nil {
  80. return
  81. }
  82. if newEntry == nil {
  83. f.DeleteChunks(oldEntry.Chunks)
  84. }
  85. var toDelete []*filer_pb.FileChunk
  86. newChunkIds := make(map[string]bool)
  87. for _, newChunk := range newEntry.Chunks {
  88. newChunkIds[newChunk.GetFileIdString()] = true
  89. }
  90. for _, oldChunk := range oldEntry.Chunks {
  91. if _, found := newChunkIds[oldChunk.GetFileIdString()]; !found {
  92. toDelete = append(toDelete, oldChunk)
  93. }
  94. }
  95. f.DeleteChunks(toDelete)
  96. }