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.

155 lines
4.6 KiB

5 years ago
5 years 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. "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(), storage.ErrorDeleted.Error()) {
  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(), storage.ErrorDeleted.Error()) {
  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 fileIdsToDelete []string
  86. for _, chunk := range chunks {
  87. if !chunk.IsChunkManifest {
  88. fileIdsToDelete = append(fileIdsToDelete, 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. fileIdsToDelete = append(fileIdsToDelete, dChunk.GetFileIdString())
  97. }
  98. fileIdsToDelete = append(fileIdsToDelete, chunk.GetFileIdString())
  99. }
  100. f.doDeleteFileIds(fileIdsToDelete)
  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. var oldChunks, newChunks []*filer_pb.FileChunk
  125. if oldEntry != nil {
  126. oldChunks = oldEntry.GetChunks()
  127. }
  128. if newEntry != nil {
  129. newChunks = newEntry.GetChunks()
  130. }
  131. toDelete, err := MinusChunks(f.MasterClient.GetLookupFileIdFunction(), oldChunks, newChunks)
  132. if err != nil {
  133. glog.Errorf("Failed to resolve old entry chunks when delete old entry chunks. new: %s, old: %s",
  134. newChunks, oldChunks)
  135. return
  136. }
  137. f.DeleteChunksNotRecursive(toDelete)
  138. }