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.

123 lines
3.5 KiB

5 years ago
5 years ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
  1. package filer
  2. import (
  3. "github.com/seaweedfs/seaweedfs/weed/storage"
  4. "github.com/seaweedfs/seaweedfs/weed/util"
  5. "strings"
  6. "time"
  7. "github.com/seaweedfs/seaweedfs/weed/glog"
  8. "github.com/seaweedfs/seaweedfs/weed/operation"
  9. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  10. "github.com/seaweedfs/seaweedfs/weed/wdclient"
  11. )
  12. func LookupByMasterClientFn(masterClient *wdclient.MasterClient) func(vids []string) (map[string]*operation.LookupResult, error) {
  13. return func(vids []string) (map[string]*operation.LookupResult, error) {
  14. m := make(map[string]*operation.LookupResult)
  15. for _, vid := range vids {
  16. locs, _ := masterClient.GetVidLocations(vid)
  17. var locations []operation.Location
  18. for _, loc := range locs {
  19. locations = append(locations, operation.Location{
  20. Url: loc.Url,
  21. PublicUrl: loc.PublicUrl,
  22. })
  23. }
  24. m[vid] = &operation.LookupResult{
  25. VolumeOrFileId: vid,
  26. Locations: locations,
  27. }
  28. }
  29. return m, nil
  30. }
  31. }
  32. func (f *Filer) loopProcessingDeletion() {
  33. lookupFunc := LookupByMasterClientFn(f.MasterClient)
  34. DeletionBatchSize := 100000 // roughly 20 bytes cost per file id.
  35. var deletionCount int
  36. for {
  37. deletionCount = 0
  38. f.fileIdDeletionQueue.Consume(func(fileIds []string) {
  39. for len(fileIds) > 0 {
  40. var toDeleteFileIds []string
  41. if len(fileIds) > DeletionBatchSize {
  42. toDeleteFileIds = fileIds[:DeletionBatchSize]
  43. fileIds = fileIds[DeletionBatchSize:]
  44. } else {
  45. toDeleteFileIds = fileIds
  46. fileIds = fileIds[:0]
  47. }
  48. deletionCount = len(toDeleteFileIds)
  49. _, err := operation.DeleteFilesWithLookupVolumeId(f.GrpcDialOption, toDeleteFileIds, lookupFunc)
  50. if err != nil {
  51. if !strings.Contains(err.Error(), storage.ErrorDeleted.Error()) {
  52. glog.V(0).Infof("deleting fileIds len=%d error: %v", deletionCount, err)
  53. }
  54. } else {
  55. glog.V(2).Infof("deleting fileIds %+v", toDeleteFileIds)
  56. }
  57. }
  58. })
  59. if deletionCount == 0 {
  60. time.Sleep(1123 * time.Millisecond)
  61. }
  62. }
  63. }
  64. func (f *Filer) DeleteUncommittedChunks(chunks []*filer_pb.FileChunk) {
  65. f.doDeleteChunks(chunks)
  66. }
  67. func (f *Filer) DeleteChunks(fullpath util.FullPath, chunks []*filer_pb.FileChunk) {
  68. rule := f.FilerConf.MatchStorageRule(string(fullpath))
  69. if rule.DisableChunkDeletion {
  70. return
  71. }
  72. f.doDeleteChunks(chunks)
  73. }
  74. func (f *Filer) doDeleteChunks(chunks []*filer_pb.FileChunk) {
  75. for _, chunk := range chunks {
  76. if !chunk.IsChunkManifest {
  77. f.fileIdDeletionQueue.EnQueue(chunk.GetFileIdString())
  78. continue
  79. }
  80. dataChunks, manifestResolveErr := ResolveOneChunkManifest(f.MasterClient.LookupFileId, chunk)
  81. if manifestResolveErr != nil {
  82. glog.V(0).Infof("failed to resolve manifest %s: %v", chunk.FileId, manifestResolveErr)
  83. }
  84. for _, dChunk := range dataChunks {
  85. f.fileIdDeletionQueue.EnQueue(dChunk.GetFileIdString())
  86. }
  87. f.fileIdDeletionQueue.EnQueue(chunk.GetFileIdString())
  88. }
  89. }
  90. func (f *Filer) DeleteChunksNotRecursive(chunks []*filer_pb.FileChunk) {
  91. for _, chunk := range chunks {
  92. f.fileIdDeletionQueue.EnQueue(chunk.GetFileIdString())
  93. }
  94. }
  95. func (f *Filer) deleteChunksIfNotNew(oldEntry, newEntry *Entry) {
  96. var oldChunks, newChunks []*filer_pb.FileChunk
  97. if oldEntry != nil {
  98. oldChunks = oldEntry.GetChunks()
  99. }
  100. if newEntry != nil {
  101. newChunks = newEntry.GetChunks()
  102. }
  103. toDelete, err := MinusChunks(f.MasterClient.GetLookupFileIdFunction(), oldChunks, newChunks)
  104. if err != nil {
  105. glog.Errorf("Failed to resolve old entry chunks when delete old entry chunks. new: %s, old: %s", newChunks, oldChunks)
  106. return
  107. }
  108. f.DeleteChunksNotRecursive(toDelete)
  109. }