filer_deletion.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package filer2
  2. import (
  3. "time"
  4. "github.com/chrislusf/seaweedfs/weed/glog"
  5. "github.com/chrislusf/seaweedfs/weed/operation"
  6. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  7. "github.com/chrislusf/seaweedfs/weed/wdclient"
  8. )
  9. func LookupByMasterClientFn(masterClient *wdclient.MasterClient) func(vids []string) (map[string]operation.LookupResult, error) {
  10. return func(vids []string) (map[string]operation.LookupResult, error) {
  11. m := make(map[string]operation.LookupResult)
  12. for _, vid := range vids {
  13. locs, _ := masterClient.GetVidLocations(vid)
  14. var locations []operation.Location
  15. for _, loc := range locs {
  16. locations = append(locations, operation.Location{
  17. Url: loc.Url,
  18. PublicUrl: loc.PublicUrl,
  19. })
  20. }
  21. m[vid] = operation.LookupResult{
  22. VolumeId: vid,
  23. Locations: locations,
  24. }
  25. }
  26. return m, nil
  27. }
  28. }
  29. func (f *Filer) loopProcessingDeletion() {
  30. lookupFunc := LookupByMasterClientFn(f.MasterClient)
  31. DeletionBatchSize := 100000 // roughly 20 bytes cost per file id.
  32. var deletionCount int
  33. for {
  34. deletionCount = 0
  35. f.fileIdDeletionQueue.Consume(func(fileIds []string) {
  36. for len(fileIds) > 0 {
  37. var toDeleteFileIds []string
  38. if len(fileIds) > DeletionBatchSize {
  39. toDeleteFileIds = fileIds[:DeletionBatchSize]
  40. fileIds = fileIds[DeletionBatchSize:]
  41. } else {
  42. toDeleteFileIds = fileIds
  43. fileIds = fileIds[:0]
  44. }
  45. deletionCount = len(toDeleteFileIds)
  46. deleteResults, err := operation.DeleteFilesWithLookupVolumeId(f.GrpcDialOption, toDeleteFileIds, lookupFunc)
  47. if err != nil {
  48. glog.V(0).Infof("deleting fileIds len=%d error: %v", deletionCount, err)
  49. } else {
  50. glog.V(1).Infof("deleting fileIds len=%d", deletionCount)
  51. }
  52. if len(deleteResults) != deletionCount {
  53. glog.V(0).Infof("delete %d fileIds actual %d", deletionCount, len(deleteResults))
  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. f.fileIdDeletionQueue.EnQueue(chunk.GetFileIdString())
  65. }
  66. }
  67. // DeleteFileByFileId direct delete by file id.
  68. // Only used when the fileId is not being managed by snapshots.
  69. func (f *Filer) DeleteFileByFileId(fileId string) {
  70. f.fileIdDeletionQueue.EnQueue(fileId)
  71. }
  72. func (f *Filer) deleteChunksIfNotNew(oldEntry, newEntry *Entry) {
  73. if oldEntry == nil {
  74. return
  75. }
  76. if newEntry == nil {
  77. f.DeleteChunks(oldEntry.Chunks)
  78. }
  79. var toDelete []*filer_pb.FileChunk
  80. newChunkIds := make(map[string]bool)
  81. for _, newChunk := range newEntry.Chunks {
  82. newChunkIds[newChunk.GetFileIdString()] = true
  83. }
  84. for _, oldChunk := range oldEntry.Chunks {
  85. if _, found := newChunkIds[oldChunk.GetFileIdString()]; !found {
  86. toDelete = append(toDelete, oldChunk)
  87. }
  88. }
  89. f.DeleteChunks(toDelete)
  90. }