filer_deletion.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. ticker := time.NewTicker(5 * time.Second)
  10. lookupFunc := func(vids []string) (map[string]operation.LookupResult, error) {
  11. m := make(map[string]operation.LookupResult)
  12. for _, vid := range vids {
  13. locs, _ := f.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. var fileIds []string
  29. for {
  30. select {
  31. case fid := <-f.fileIdDeletionChan:
  32. fileIds = append(fileIds, fid)
  33. if len(fileIds) >= 4096 {
  34. glog.V(1).Infof("deleting fileIds len=%d", len(fileIds))
  35. operation.DeleteFilesWithLookupVolumeId(f.GrpcDialOption, fileIds, lookupFunc)
  36. fileIds = fileIds[:0]
  37. }
  38. case <-ticker.C:
  39. if len(fileIds) > 0 {
  40. glog.V(1).Infof("timed deletion fileIds len=%d", len(fileIds))
  41. operation.DeleteFilesWithLookupVolumeId(f.GrpcDialOption, fileIds, lookupFunc)
  42. fileIds = fileIds[:0]
  43. }
  44. }
  45. }
  46. }
  47. func (f *Filer) DeleteChunks(chunks []*filer_pb.FileChunk) {
  48. for _, chunk := range chunks {
  49. f.fileIdDeletionChan <- chunk.GetFileIdString()
  50. }
  51. }
  52. // DeleteFileByFileId direct delete by file id.
  53. // Only used when the fileId is not being managed by snapshots.
  54. func (f *Filer) DeleteFileByFileId(fileId string) {
  55. f.fileIdDeletionChan <- fileId
  56. }
  57. func (f *Filer) deleteChunksIfNotNew(oldEntry, newEntry *Entry) {
  58. if oldEntry == nil {
  59. return
  60. }
  61. if newEntry == nil {
  62. f.DeleteChunks(oldEntry.Chunks)
  63. }
  64. var toDelete []*filer_pb.FileChunk
  65. newChunkIds := make(map[string]bool)
  66. for _, newChunk := range newEntry.Chunks {
  67. newChunkIds[newChunk.GetFileIdString()] = true
  68. }
  69. for _, oldChunk := range oldEntry.Chunks {
  70. if _, found := newChunkIds[oldChunk.GetFileIdString()]; !found {
  71. toDelete = append(toDelete, oldChunk)
  72. }
  73. }
  74. f.DeleteChunks(toDelete)
  75. }