filer_deletion.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. )
  8. func (f *Filer) loopProcessingDeletion() {
  9. lookupFunc := func(vids []string) (map[string]operation.LookupResult, error) {
  10. m := make(map[string]operation.LookupResult)
  11. for _, vid := range vids {
  12. locs, _ := f.MasterClient.GetVidLocations(vid)
  13. var locations []operation.Location
  14. for _, loc := range locs {
  15. locations = append(locations, operation.Location{
  16. Url: loc.Url,
  17. PublicUrl: loc.PublicUrl,
  18. })
  19. }
  20. m[vid] = operation.LookupResult{
  21. VolumeId: vid,
  22. Locations: locations,
  23. }
  24. }
  25. return m, nil
  26. }
  27. var deletionCount int
  28. for {
  29. deletionCount = 0
  30. f.fileIdDeletionQueue.Consume(func(fileIds []string) {
  31. deletionCount = len(fileIds)
  32. _, err := operation.DeleteFilesWithLookupVolumeId(f.GrpcDialOption, fileIds, lookupFunc)
  33. if err != nil {
  34. glog.V(0).Infof("deleting fileIds len=%d error: %v", deletionCount, err)
  35. } else {
  36. glog.V(1).Infof("deleting fileIds len=%d", deletionCount)
  37. }
  38. })
  39. if deletionCount == 0 {
  40. time.Sleep(1123 * time.Millisecond)
  41. }
  42. }
  43. }
  44. func (f *Filer) DeleteChunks(chunks []*filer_pb.FileChunk) {
  45. for _, chunk := range chunks {
  46. f.fileIdDeletionQueue.EnQueue(chunk.GetFileIdString())
  47. }
  48. }
  49. // DeleteFileByFileId direct delete by file id.
  50. // Only used when the fileId is not being managed by snapshots.
  51. func (f *Filer) DeleteFileByFileId(fileId string) {
  52. f.fileIdDeletionQueue.EnQueue(fileId)
  53. }
  54. func (f *Filer) deleteChunksIfNotNew(oldEntry, newEntry *Entry) {
  55. if oldEntry == nil {
  56. return
  57. }
  58. if newEntry == nil {
  59. f.DeleteChunks(oldEntry.Chunks)
  60. }
  61. var toDelete []*filer_pb.FileChunk
  62. newChunkIds := make(map[string]bool)
  63. for _, newChunk := range newEntry.Chunks {
  64. newChunkIds[newChunk.GetFileIdString()] = true
  65. }
  66. for _, oldChunk := range oldEntry.Chunks {
  67. if _, found := newChunkIds[oldChunk.GetFileIdString()]; !found {
  68. toDelete = append(toDelete, oldChunk)
  69. }
  70. }
  71. f.DeleteChunks(toDelete)
  72. }