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
5.2 KiB

5 months ago
5 months ago
5 months ago
4 years ago
  1. package filer
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/glog"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  7. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  8. "github.com/seaweedfs/seaweedfs/weed/util"
  9. )
  10. const (
  11. MsgFailDelNonEmptyFolder = "fail to delete non-empty folder"
  12. )
  13. type OnChunksFunc func([]*filer_pb.FileChunk) error
  14. type OnHardLinkIdsFunc func([]HardLinkId) error
  15. func (f *Filer) DeleteEntryMetaAndData(ctx context.Context, p util.FullPath, isRecursive, ignoreRecursiveError, shouldDeleteChunks, isFromOtherCluster bool, signatures []int32, ifNotModifiedAfter int64) (err error) {
  16. if p == "/" {
  17. return nil
  18. }
  19. entry, findErr := f.FindEntry(ctx, p)
  20. if findErr != nil {
  21. return findErr
  22. }
  23. if ifNotModifiedAfter > 0 && entry.Attr.Mtime.Unix() > ifNotModifiedAfter {
  24. return nil
  25. }
  26. isDeleteCollection := f.isBucket(entry)
  27. if entry.IsDirectory() {
  28. // delete the folder children, not including the folder itself
  29. err = f.doBatchDeleteFolderMetaAndData(ctx, entry, isRecursive, ignoreRecursiveError, shouldDeleteChunks && !isDeleteCollection, isDeleteCollection, isFromOtherCluster, signatures, func(hardLinkIds []HardLinkId) error {
  30. // A case not handled:
  31. // what if the chunk is in a different collection?
  32. if shouldDeleteChunks {
  33. f.maybeDeleteHardLinks(hardLinkIds)
  34. }
  35. return nil
  36. })
  37. if err != nil {
  38. glog.V(2).Infof("delete directory %s: %v", p, err)
  39. return fmt.Errorf("delete directory %s: %v", p, err)
  40. }
  41. }
  42. // delete the file or folder
  43. err = f.doDeleteEntryMetaAndData(ctx, entry, shouldDeleteChunks, isFromOtherCluster, signatures)
  44. if err != nil {
  45. return fmt.Errorf("delete file %s: %v", p, err)
  46. }
  47. if shouldDeleteChunks && !isDeleteCollection {
  48. f.DeleteChunks(p, entry.GetChunks())
  49. }
  50. if isDeleteCollection {
  51. collectionName := entry.Name()
  52. f.DoDeleteCollection(collectionName)
  53. }
  54. return nil
  55. }
  56. func (f *Filer) doBatchDeleteFolderMetaAndData(ctx context.Context, entry *Entry, isRecursive, ignoreRecursiveError, shouldDeleteChunks, isDeletingBucket, isFromOtherCluster bool, signatures []int32, onHardLinkIdsFn OnHardLinkIdsFunc) (err error) {
  57. //collect all the chunks of this layer and delete them together at the end
  58. var chunksToDelete []*filer_pb.FileChunk
  59. lastFileName := ""
  60. includeLastFile := false
  61. if !isDeletingBucket || !f.Store.CanDropWholeBucket() {
  62. for {
  63. entries, _, err := f.ListDirectoryEntries(ctx, entry.FullPath, lastFileName, includeLastFile, PaginationSize, "", "", "")
  64. if err != nil {
  65. glog.Errorf("list folder %s: %v", entry.FullPath, err)
  66. return fmt.Errorf("list folder %s: %v", entry.FullPath, err)
  67. }
  68. if lastFileName == "" && !isRecursive && len(entries) > 0 {
  69. // only for first iteration in the loop
  70. glog.V(2).Infof("deleting a folder %s has children: %+v ...", entry.FullPath, entries[0].Name())
  71. return fmt.Errorf("%s: %s", MsgFailDelNonEmptyFolder, entry.FullPath)
  72. }
  73. for _, sub := range entries {
  74. lastFileName = sub.Name()
  75. if sub.IsDirectory() {
  76. subIsDeletingBucket := f.isBucket(sub)
  77. err = f.doBatchDeleteFolderMetaAndData(ctx, sub, isRecursive, ignoreRecursiveError, shouldDeleteChunks, subIsDeletingBucket, false, nil, onHardLinkIdsFn)
  78. } else {
  79. f.NotifyUpdateEvent(ctx, sub, nil, shouldDeleteChunks, isFromOtherCluster, nil)
  80. if len(sub.HardLinkId) != 0 {
  81. // hard link chunk data are deleted separately
  82. err = onHardLinkIdsFn([]HardLinkId{sub.HardLinkId})
  83. } else {
  84. if shouldDeleteChunks {
  85. chunksToDelete = append(chunksToDelete, sub.GetChunks()...)
  86. }
  87. }
  88. }
  89. if err != nil && !ignoreRecursiveError {
  90. return err
  91. }
  92. }
  93. if len(entries) < PaginationSize {
  94. break
  95. }
  96. }
  97. }
  98. glog.V(3).Infof("deleting directory %v delete chunks: %v", entry.FullPath, shouldDeleteChunks)
  99. if storeDeletionErr := f.Store.DeleteFolderChildren(ctx, entry.FullPath); storeDeletionErr != nil {
  100. return fmt.Errorf("filer store delete: %v", storeDeletionErr)
  101. }
  102. f.NotifyUpdateEvent(ctx, entry, nil, shouldDeleteChunks, isFromOtherCluster, signatures)
  103. f.DeleteChunks(entry.FullPath, chunksToDelete)
  104. return nil
  105. }
  106. func (f *Filer) doDeleteEntryMetaAndData(ctx context.Context, entry *Entry, shouldDeleteChunks bool, isFromOtherCluster bool, signatures []int32) (err error) {
  107. glog.V(3).Infof("deleting entry %v, delete chunks: %v", entry.FullPath, shouldDeleteChunks)
  108. if storeDeletionErr := f.Store.DeleteOneEntry(ctx, entry); storeDeletionErr != nil {
  109. return fmt.Errorf("filer store delete: %v", storeDeletionErr)
  110. }
  111. if !entry.IsDirectory() {
  112. f.NotifyUpdateEvent(ctx, entry, nil, shouldDeleteChunks, isFromOtherCluster, signatures)
  113. }
  114. return nil
  115. }
  116. func (f *Filer) DoDeleteCollection(collectionName string) (err error) {
  117. return f.MasterClient.WithClient(false, func(client master_pb.SeaweedClient) error {
  118. _, err := client.CollectionDelete(context.Background(), &master_pb.CollectionDeleteRequest{
  119. Name: collectionName,
  120. })
  121. if err != nil {
  122. glog.Infof("delete collection %s: %v", collectionName, err)
  123. }
  124. return err
  125. })
  126. }
  127. func (f *Filer) maybeDeleteHardLinks(hardLinkIds []HardLinkId) {
  128. for _, hardLinkId := range hardLinkIds {
  129. if err := f.Store.DeleteHardLink(context.Background(), hardLinkId); err != nil {
  130. glog.Errorf("delete hard link id %d : %v", hardLinkId, err)
  131. }
  132. }
  133. }