From 91c51f4807aa3458a0ebd03c6cc92d578e01028b Mon Sep 17 00:00:00 2001 From: Jarek Kowalski Date: Sat, 23 Jun 2018 13:05:40 -0700 Subject: [PATCH] refactored 'block gc' subcommand --- block/block_manager.go | 37 ++++++++++++++++++++++++++++++ cli/command_block_gc.go | 50 +++++++---------------------------------- 2 files changed, 45 insertions(+), 42 deletions(-) diff --git a/block/block_manager.go b/block/block_manager.go index 6c721fc35..d95af8a4f 100644 --- a/block/block_manager.go +++ b/block/block_manager.go @@ -863,6 +863,43 @@ func (bm *Manager) BlockInfo(ctx context.Context, blockID string) (Info, error) return bm.packedBlockInfoLocked(blockID) } +// FindUnreferencedStorageFiles returns the list of unreferenced storage blocks. +func (bm *Manager) FindUnreferencedStorageFiles(ctx context.Context) ([]storage.BlockMetadata, error) { + infos, err := bm.ListBlockInfos("", false) + if err != nil { + return nil, fmt.Errorf("unable to list index blocks: %v", err) + } + + usedPackBlocks := findPackBlocksInUse(infos) + ch := bm.st.ListBlocks(ctx, PackBlockPrefix) + + var unused []storage.BlockMetadata + for bi := range ch { + if bi.Error != nil { + return nil, fmt.Errorf("error listing storage blocks: %v", bi.Error) + } + + u := usedPackBlocks[bi.BlockID] + if u > 0 { + log.Printf("pack %v, in use by %v blocks", bi.BlockID, u) + continue + } + + unused = append(unused, bi) + } + return unused, nil +} + +func findPackBlocksInUse(infos []Info) map[string]int { + packUsage := map[string]int{} + + for _, bi := range infos { + packUsage[bi.PackFile]++ + } + + return packUsage +} + func (bm *Manager) packedBlockInfoLocked(blockID string) (Info, error) { bm.assertLocked() diff --git a/cli/command_block_gc.go b/cli/command_block_gc.go index 27cc44117..dd92157f5 100644 --- a/cli/command_block_gc.go +++ b/cli/command_block_gc.go @@ -5,9 +5,7 @@ "fmt" "os" - "github.com/kopia/kopia/block" "github.com/kopia/kopia/repo" - "github.com/rs/zerolog/log" ) var ( @@ -16,43 +14,21 @@ ) func runBlockGarbageCollectAction(ctx context.Context, rep *repo.Repository) error { - infos, err := rep.Blocks.ListBlockInfos("", false) + unused, err := rep.Blocks.FindUnreferencedStorageFiles(ctx) if err != nil { - return fmt.Errorf("unable to list index blocks: %v", err) + return fmt.Errorf("error looking for unreferenced storage files: %v", err) } - usedPackBlocks := findPackBlocksInUse(infos) - ch := rep.Storage.ListBlocks(ctx, block.PackBlockPrefix) - - var unused []string - var totalBytes int64 - allPackBlocks := 0 - for bi := range ch { - if bi.Error != nil { - return fmt.Errorf("error listing storage blocks: %v", bi.Error) - } - - allPackBlocks++ - - u := usedPackBlocks[bi.BlockID] - if u > 0 { - log.Printf("pack %v, in use by %v blocks", bi.BlockID, u) - continue - } - - totalBytes += bi.Length - unused = append(unused, bi.BlockID) - } - fmt.Fprintf(os.Stderr, "Found %v/%v pack blocks in use.\n", len(usedPackBlocks), allPackBlocks) - if len(unused) == 0 { fmt.Fprintf(os.Stderr, "No unused blocks found.\n") return nil } if *blockGarbageCollectCommandDelete != "yes" { + var totalBytes int64 for _, u := range unused { - fmt.Fprintf(os.Stderr, "unused %v\n", u) + fmt.Fprintf(os.Stderr, "unused %v (%v bytes)\n", u.BlockID, u.Length) + totalBytes += u.Length } fmt.Fprintf(os.Stderr, "Would delete %v unused blocks (%v bytes), pass '--delete=yes' to actually delete.\n", len(unused), totalBytes) @@ -60,25 +36,15 @@ func runBlockGarbageCollectAction(ctx context.Context, rep *repo.Repository) err } for _, u := range unused { - fmt.Fprintf(os.Stderr, "Deleting unused block %q...\n", u) - if err := rep.Storage.DeleteBlock(ctx, u); err != nil { - return fmt.Errorf("unable to delete block %q: %v", u, err) + fmt.Fprintf(os.Stderr, "Deleting unused block %q (%v bytes)...\n", u.BlockID, u.Length) + if err := rep.Storage.DeleteBlock(ctx, u.BlockID); err != nil { + return fmt.Errorf("unable to delete block %q: %v", u.BlockID, err) } } return nil } -func findPackBlocksInUse(infos []block.Info) map[string]int { - packUsage := map[string]int{} - - for _, bi := range infos { - packUsage[bi.PackFile]++ - } - - return packUsage -} - func init() { blockGarbageCollectCommand.Action(repositoryAction(runBlockGarbageCollectAction)) }