You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.7 KiB

6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
  1. package filer2
  2. import (
  3. "strings"
  4. "time"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  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. glog.V(0).Infof("deleting fileIds len=%d error: %v", deletionCount, err)
  51. }
  52. } else {
  53. glog.V(1).Infof("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. 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. }