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.

162 lines
4.6 KiB

4 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
  1. package filer
  2. import (
  3. "context"
  4. "strings"
  5. "time"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. "github.com/chrislusf/seaweedfs/weed/operation"
  8. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  9. "github.com/chrislusf/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. VolumeId: 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(1).Infof("deleting fileIds len=%d", deletionCount)
  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) deleteChunksIfNotNew(oldEntry, newEntry *Entry) {
  119. if oldEntry == nil {
  120. return
  121. }
  122. if newEntry == nil {
  123. f.DeleteChunks(oldEntry.Chunks)
  124. }
  125. var toDelete []*filer_pb.FileChunk
  126. newChunkIds := make(map[string]bool)
  127. for _, newChunk := range newEntry.Chunks {
  128. newChunkIds[newChunk.GetFileIdString()] = true
  129. }
  130. for _, oldChunk := range oldEntry.Chunks {
  131. if _, found := newChunkIds[oldChunk.GetFileIdString()]; !found {
  132. toDelete = append(toDelete, oldChunk)
  133. }
  134. }
  135. f.DeleteChunks(toDelete)
  136. }
  137. func (f *Filer) maybeDeleteHardLinks(hardLinkIds []HardLinkId) {
  138. for _, hardLinkId := range hardLinkIds {
  139. if err := f.Store.DeleteHardLink(context.Background(), hardLinkId); err != nil {
  140. glog.Errorf("delete hard link id %d : %v", hardLinkId, err)
  141. }
  142. }
  143. }