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.

184 lines
5.5 KiB

3 years ago
5 years ago
5 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package filer
  2. import (
  3. "github.com/seaweedfs/seaweedfs/weed/storage"
  4. "math"
  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) DeleteChunks(chunks []*filer_pb.FileChunk) {
  104. for _, chunk := range chunks {
  105. if !chunk.IsChunkManifest {
  106. f.fileIdDeletionQueue.EnQueue(chunk.GetFileIdString())
  107. continue
  108. }
  109. dataChunks, manifestResolveErr := ResolveOneChunkManifest(f.MasterClient.LookupFileId, chunk)
  110. if manifestResolveErr != nil {
  111. glog.V(0).Infof("failed to resolve manifest %s: %v", chunk.FileId, manifestResolveErr)
  112. }
  113. for _, dChunk := range dataChunks {
  114. f.fileIdDeletionQueue.EnQueue(dChunk.GetFileIdString())
  115. }
  116. f.fileIdDeletionQueue.EnQueue(chunk.GetFileIdString())
  117. }
  118. }
  119. func (f *Filer) DeleteChunksNotRecursive(chunks []*filer_pb.FileChunk) {
  120. for _, chunk := range chunks {
  121. f.fileIdDeletionQueue.EnQueue(chunk.GetFileIdString())
  122. }
  123. }
  124. func (f *Filer) deleteChunksIfNotNew(oldEntry, newEntry *Entry) {
  125. if oldEntry == nil {
  126. return
  127. }
  128. if newEntry == nil {
  129. f.DeleteChunks(oldEntry.GetChunks())
  130. return
  131. }
  132. var toDelete []*filer_pb.FileChunk
  133. newChunkIds := make(map[string]bool)
  134. newDataChunks, newManifestChunks, err := ResolveChunkManifest(f.MasterClient.GetLookupFileIdFunction(),
  135. newEntry.GetChunks(), 0, math.MaxInt64)
  136. if err != nil {
  137. glog.Errorf("Failed to resolve new entry chunks when delete old entry chunks. new: %s, old: %s",
  138. newEntry.GetChunks(), oldEntry.Chunks)
  139. return
  140. }
  141. for _, newChunk := range newDataChunks {
  142. newChunkIds[newChunk.GetFileIdString()] = true
  143. }
  144. for _, newChunk := range newManifestChunks {
  145. newChunkIds[newChunk.GetFileIdString()] = true
  146. }
  147. oldDataChunks, oldManifestChunks, err := ResolveChunkManifest(f.MasterClient.GetLookupFileIdFunction(),
  148. oldEntry.GetChunks(), 0, math.MaxInt64)
  149. if err != nil {
  150. glog.Errorf("Failed to resolve old entry chunks when delete old entry chunks. new: %s, old: %s",
  151. newEntry.GetChunks(), oldEntry.GetChunks())
  152. return
  153. }
  154. for _, oldChunk := range oldDataChunks {
  155. if _, found := newChunkIds[oldChunk.GetFileIdString()]; !found {
  156. toDelete = append(toDelete, oldChunk)
  157. }
  158. }
  159. for _, oldChunk := range oldManifestChunks {
  160. if _, found := newChunkIds[oldChunk.GetFileIdString()]; !found {
  161. toDelete = append(toDelete, oldChunk)
  162. }
  163. }
  164. f.DeleteChunksNotRecursive(toDelete)
  165. }