filer_delete_entry.go 5.1 KB

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