filer_deletion.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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) doDeleteFileIds(fileIds []string) {
  65. lookupFunc := LookupByMasterClientFn(f.MasterClient)
  66. DeletionBatchSize := 100000 // roughly 20 bytes cost per file id.
  67. for len(fileIds) > 0 {
  68. var toDeleteFileIds []string
  69. if len(fileIds) > DeletionBatchSize {
  70. toDeleteFileIds = fileIds[:DeletionBatchSize]
  71. fileIds = fileIds[DeletionBatchSize:]
  72. } else {
  73. toDeleteFileIds = fileIds
  74. fileIds = fileIds[:0]
  75. }
  76. deletionCount := len(toDeleteFileIds)
  77. _, err := operation.DeleteFilesWithLookupVolumeId(f.GrpcDialOption, toDeleteFileIds, lookupFunc)
  78. if err != nil {
  79. if !strings.Contains(err.Error(), storage.ErrorDeleted.Error()) {
  80. glog.V(0).Infof("deleting fileIds len=%d error: %v", deletionCount, err)
  81. }
  82. }
  83. }
  84. }
  85. func (f *Filer) DirectDeleteChunks(chunks []*filer_pb.FileChunk) {
  86. var fileIdsToDelete []string
  87. for _, chunk := range chunks {
  88. if !chunk.IsChunkManifest {
  89. fileIdsToDelete = append(fileIdsToDelete, chunk.GetFileIdString())
  90. continue
  91. }
  92. dataChunks, manifestResolveErr := ResolveOneChunkManifest(f.MasterClient.LookupFileId, chunk)
  93. if manifestResolveErr != nil {
  94. glog.V(0).Infof("failed to resolve manifest %s: %v", chunk.FileId, manifestResolveErr)
  95. }
  96. for _, dChunk := range dataChunks {
  97. fileIdsToDelete = append(fileIdsToDelete, dChunk.GetFileIdString())
  98. }
  99. fileIdsToDelete = append(fileIdsToDelete, chunk.GetFileIdString())
  100. }
  101. f.doDeleteFileIds(fileIdsToDelete)
  102. }
  103. func (f *Filer) DeleteUncommittedChunks(chunks []*filer_pb.FileChunk) {
  104. f.doDeleteChunks(chunks)
  105. }
  106. func (f *Filer) DeleteChunks(fullpath util.FullPath, chunks []*filer_pb.FileChunk) {
  107. rule := f.FilerConf.MatchStorageRule(string(fullpath))
  108. if rule.DisableChunkDeletion {
  109. return
  110. }
  111. f.doDeleteChunks(chunks)
  112. }
  113. func (f *Filer) doDeleteChunks(chunks []*filer_pb.FileChunk) {
  114. for _, chunk := range chunks {
  115. if !chunk.IsChunkManifest {
  116. f.fileIdDeletionQueue.EnQueue(chunk.GetFileIdString())
  117. continue
  118. }
  119. dataChunks, manifestResolveErr := ResolveOneChunkManifest(f.MasterClient.LookupFileId, chunk)
  120. if manifestResolveErr != nil {
  121. glog.V(0).Infof("failed to resolve manifest %s: %v", chunk.FileId, manifestResolveErr)
  122. }
  123. for _, dChunk := range dataChunks {
  124. f.fileIdDeletionQueue.EnQueue(dChunk.GetFileIdString())
  125. }
  126. f.fileIdDeletionQueue.EnQueue(chunk.GetFileIdString())
  127. }
  128. }
  129. func (f *Filer) DeleteChunksNotRecursive(chunks []*filer_pb.FileChunk) {
  130. for _, chunk := range chunks {
  131. f.fileIdDeletionQueue.EnQueue(chunk.GetFileIdString())
  132. }
  133. }
  134. func (f *Filer) deleteChunksIfNotNew(oldEntry, newEntry *Entry) {
  135. var oldChunks, newChunks []*filer_pb.FileChunk
  136. if oldEntry != nil {
  137. oldChunks = oldEntry.GetChunks()
  138. }
  139. if newEntry != nil {
  140. newChunks = newEntry.GetChunks()
  141. }
  142. toDelete, err := MinusChunks(f.MasterClient.GetLookupFileIdFunction(), oldChunks, newChunks)
  143. if err != nil {
  144. glog.Errorf("Failed to resolve old entry chunks when delete old entry chunks. new: %s, old: %s", newChunks, oldChunks)
  145. return
  146. }
  147. f.DeleteChunksNotRecursive(toDelete)
  148. }