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.

183 lines
5.4 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. "math"
  4. "strings"
  5. "time"
  6. "github.com/seaweedfs/seaweedfs/weed/glog"
  7. "github.com/seaweedfs/seaweedfs/weed/operation"
  8. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  9. "github.com/seaweedfs/seaweedfs/weed/wdclient"
  10. )
  11. func LookupByMasterClientFn(masterClient *wdclient.MasterClient) func(vids []string) (map[string]*operation.LookupResult, error) {
  12. return func(vids []string) (map[string]*operation.LookupResult, error) {
  13. m := make(map[string]*operation.LookupResult)
  14. for _, vid := range vids {
  15. locs, _ := masterClient.GetVidLocations(vid)
  16. var locations []operation.Location
  17. for _, loc := range locs {
  18. locations = append(locations, operation.Location{
  19. Url: loc.Url,
  20. PublicUrl: loc.PublicUrl,
  21. })
  22. }
  23. m[vid] = &operation.LookupResult{
  24. VolumeOrFileId: vid,
  25. Locations: locations,
  26. }
  27. }
  28. return m, nil
  29. }
  30. }
  31. func (f *Filer) loopProcessingDeletion() {
  32. lookupFunc := LookupByMasterClientFn(f.MasterClient)
  33. DeletionBatchSize := 100000 // roughly 20 bytes cost per file id.
  34. var deletionCount int
  35. for {
  36. deletionCount = 0
  37. f.fileIdDeletionQueue.Consume(func(fileIds []string) {
  38. for len(fileIds) > 0 {
  39. var toDeleteFileIds []string
  40. if len(fileIds) > DeletionBatchSize {
  41. toDeleteFileIds = fileIds[:DeletionBatchSize]
  42. fileIds = fileIds[DeletionBatchSize:]
  43. } else {
  44. toDeleteFileIds = fileIds
  45. fileIds = fileIds[:0]
  46. }
  47. deletionCount = len(toDeleteFileIds)
  48. _, err := operation.DeleteFilesWithLookupVolumeId(f.GrpcDialOption, toDeleteFileIds, lookupFunc)
  49. if err != nil {
  50. if !strings.Contains(err.Error(), "already deleted") {
  51. glog.V(0).Infof("deleting fileIds len=%d error: %v", deletionCount, err)
  52. }
  53. } else {
  54. glog.V(2).Infof("deleting fileIds %+v", toDeleteFileIds)
  55. }
  56. }
  57. })
  58. if deletionCount == 0 {
  59. time.Sleep(1123 * time.Millisecond)
  60. }
  61. }
  62. }
  63. func (f *Filer) doDeleteFileIds(fileIds []string) {
  64. lookupFunc := LookupByMasterClientFn(f.MasterClient)
  65. DeletionBatchSize := 100000 // roughly 20 bytes cost per file id.
  66. for len(fileIds) > 0 {
  67. var toDeleteFileIds []string
  68. if len(fileIds) > DeletionBatchSize {
  69. toDeleteFileIds = fileIds[:DeletionBatchSize]
  70. fileIds = fileIds[DeletionBatchSize:]
  71. } else {
  72. toDeleteFileIds = fileIds
  73. fileIds = fileIds[:0]
  74. }
  75. deletionCount := len(toDeleteFileIds)
  76. _, err := operation.DeleteFilesWithLookupVolumeId(f.GrpcDialOption, toDeleteFileIds, lookupFunc)
  77. if err != nil {
  78. if !strings.Contains(err.Error(), "already deleted") {
  79. glog.V(0).Infof("deleting fileIds len=%d error: %v", deletionCount, err)
  80. }
  81. }
  82. }
  83. }
  84. func (f *Filer) DirectDeleteChunks(chunks []*filer_pb.FileChunk) {
  85. var fildIdsToDelete []string
  86. for _, chunk := range chunks {
  87. if !chunk.IsChunkManifest {
  88. fildIdsToDelete = append(fildIdsToDelete, chunk.GetFileIdString())
  89. continue
  90. }
  91. dataChunks, manifestResolveErr := ResolveOneChunkManifest(f.MasterClient.LookupFileId, chunk)
  92. if manifestResolveErr != nil {
  93. glog.V(0).Infof("failed to resolve manifest %s: %v", chunk.FileId, manifestResolveErr)
  94. }
  95. for _, dChunk := range dataChunks {
  96. fildIdsToDelete = append(fildIdsToDelete, dChunk.GetFileIdString())
  97. }
  98. fildIdsToDelete = append(fildIdsToDelete, chunk.GetFileIdString())
  99. }
  100. f.doDeleteFileIds(fildIdsToDelete)
  101. }
  102. func (f *Filer) DeleteChunks(chunks []*filer_pb.FileChunk) {
  103. for _, chunk := range chunks {
  104. if !chunk.IsChunkManifest {
  105. f.fileIdDeletionQueue.EnQueue(chunk.GetFileIdString())
  106. continue
  107. }
  108. dataChunks, manifestResolveErr := ResolveOneChunkManifest(f.MasterClient.LookupFileId, chunk)
  109. if manifestResolveErr != nil {
  110. glog.V(0).Infof("failed to resolve manifest %s: %v", chunk.FileId, manifestResolveErr)
  111. }
  112. for _, dChunk := range dataChunks {
  113. f.fileIdDeletionQueue.EnQueue(dChunk.GetFileIdString())
  114. }
  115. f.fileIdDeletionQueue.EnQueue(chunk.GetFileIdString())
  116. }
  117. }
  118. func (f *Filer) DeleteChunksNotRecursive(chunks []*filer_pb.FileChunk) {
  119. for _, chunk := range chunks {
  120. f.fileIdDeletionQueue.EnQueue(chunk.GetFileIdString())
  121. }
  122. }
  123. func (f *Filer) deleteChunksIfNotNew(oldEntry, newEntry *Entry) {
  124. if oldEntry == nil {
  125. return
  126. }
  127. if newEntry == nil {
  128. f.DeleteChunks(oldEntry.Chunks)
  129. return
  130. }
  131. var toDelete []*filer_pb.FileChunk
  132. newChunkIds := make(map[string]bool)
  133. newDataChunks, newManifestChunks, err := ResolveChunkManifest(f.MasterClient.GetLookupFileIdFunction(),
  134. newEntry.Chunks, 0, math.MaxInt64)
  135. if err != nil {
  136. glog.Errorf("Failed to resolve new entry chunks when delete old entry chunks. new: %s, old: %s",
  137. newEntry.Chunks, oldEntry.Chunks)
  138. return
  139. }
  140. for _, newChunk := range newDataChunks {
  141. newChunkIds[newChunk.GetFileIdString()] = true
  142. }
  143. for _, newChunk := range newManifestChunks {
  144. newChunkIds[newChunk.GetFileIdString()] = true
  145. }
  146. oldDataChunks, oldManifestChunks, err := ResolveChunkManifest(f.MasterClient.GetLookupFileIdFunction(),
  147. oldEntry.Chunks, 0, math.MaxInt64)
  148. if err != nil {
  149. glog.Errorf("Failed to resolve old entry chunks when delete old entry chunks. new: %s, old: %s",
  150. newEntry.Chunks, oldEntry.Chunks)
  151. return
  152. }
  153. for _, oldChunk := range oldDataChunks {
  154. if _, found := newChunkIds[oldChunk.GetFileIdString()]; !found {
  155. toDelete = append(toDelete, oldChunk)
  156. }
  157. }
  158. for _, oldChunk := range oldManifestChunks {
  159. if _, found := newChunkIds[oldChunk.GetFileIdString()]; !found {
  160. toDelete = append(toDelete, oldChunk)
  161. }
  162. }
  163. f.DeleteChunksNotRecursive(toDelete)
  164. }