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.

159 lines
4.7 KiB

5 years ago
5 years ago
8 months ago
8 months ago
8 months ago
8 months ago
8 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) DeleteUncommittedChunks(chunks []*filer_pb.FileChunk) {
  103. f.DeleteChunks(chunks)
  104. }
  105. func (f *Filer) DeleteChunks(chunks []*filer_pb.FileChunk) {
  106. for _, chunk := range chunks {
  107. if !chunk.IsChunkManifest {
  108. f.fileIdDeletionQueue.EnQueue(chunk.GetFileIdString())
  109. continue
  110. }
  111. dataChunks, manifestResolveErr := ResolveOneChunkManifest(f.MasterClient.LookupFileId, chunk)
  112. if manifestResolveErr != nil {
  113. glog.V(0).Infof("failed to resolve manifest %s: %v", chunk.FileId, manifestResolveErr)
  114. }
  115. for _, dChunk := range dataChunks {
  116. f.fileIdDeletionQueue.EnQueue(dChunk.GetFileIdString())
  117. }
  118. f.fileIdDeletionQueue.EnQueue(chunk.GetFileIdString())
  119. }
  120. }
  121. func (f *Filer) DeleteChunksNotRecursive(chunks []*filer_pb.FileChunk) {
  122. for _, chunk := range chunks {
  123. f.fileIdDeletionQueue.EnQueue(chunk.GetFileIdString())
  124. }
  125. }
  126. func (f *Filer) deleteChunksIfNotNew(oldEntry, newEntry *Entry) {
  127. var oldChunks, newChunks []*filer_pb.FileChunk
  128. if oldEntry != nil {
  129. oldChunks = oldEntry.GetChunks()
  130. }
  131. if newEntry != nil {
  132. newChunks = newEntry.GetChunks()
  133. }
  134. toDelete, err := MinusChunks(f.MasterClient.GetLookupFileIdFunction(), oldChunks, newChunks)
  135. if err != nil {
  136. glog.Errorf("Failed to resolve old entry chunks when delete old entry chunks. new: %s, old: %s",
  137. newChunks, oldChunks)
  138. return
  139. }
  140. f.DeleteChunksNotRecursive(toDelete)
  141. }