From e5ebb7b8f8b3695f674b4d44864986dcb5524872 Mon Sep 17 00:00:00 2001 From: Manish Sethi Date: Tue, 10 Nov 2020 14:42:24 -0500 Subject: [PATCH] Remove the temp folder on error during snapshot generation (#2104) Signed-off-by: manish --- core/ledger/kvledger/snapshot.go | 10 ++++++---- core/ledger/kvledger/snapshot_test.go | 12 ++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/core/ledger/kvledger/snapshot.go b/core/ledger/kvledger/snapshot.go index dfc537fa251..a01ea5dcdf6 100644 --- a/core/ledger/kvledger/snapshot.go +++ b/core/ledger/kvledger/snapshot.go @@ -105,6 +105,8 @@ func (l *kvLedger) generateSnapshot() error { if err != nil { return errors.Wrapf(err, "error while creating temp dir [%s]", snapshotTempDir) } + defer os.RemoveAll(snapshotTempDir) + newHashFunc := func() (hash.Hash, error) { return l.hashProvider.GetHash(snapshotHashOpts) } @@ -113,19 +115,19 @@ func (l *kvLedger) generateSnapshot() error { if err != nil { return err } - logger.Debugw("Snapshot generation - exported TxIDs from blockstore", "channelID", l.ledgerID) + logger.Debugw("Exported TxIDs from blockstore", "channelID", l.ledgerID) configsHistoryExportSummary, err := l.configHistoryRetriever.ExportConfigHistory(snapshotTempDir, newHashFunc) if err != nil { return err } - logger.Debugw("Snapshot generation - exported collection config history", "channelID", l.ledgerID) + logger.Debugw("Exported collection config history", "channelID", l.ledgerID) stateDBExportSummary, err := l.txmgr.ExportPubStateAndPvtStateHashes(snapshotTempDir, newHashFunc) if err != nil { return err } - logger.Debugw("Snapshot generation - exported public state and private state hashes", "channelID", l.ledgerID) + logger.Debugw("Exported public state and private state hashes", "channelID", l.ledgerID) if err := l.generateSnapshotMetadataFiles( snapshotTempDir, txIDsExportSummary, @@ -133,7 +135,7 @@ func (l *kvLedger) generateSnapshot() error { ); err != nil { return err } - logger.Debugw("Snapshot generation - generated metadata files", "channelID", l.ledgerID) + logger.Debugw("Generated metadata files", "channelID", l.ledgerID) if err := fileutil.SyncDir(snapshotTempDir); err != nil { return err diff --git a/core/ledger/kvledger/snapshot_test.go b/core/ledger/kvledger/snapshot_test.go index 77eb53f0bc2..6b1b60dacf9 100644 --- a/core/ledger/kvledger/snapshot_test.go +++ b/core/ledger/kvledger/snapshot_test.go @@ -36,6 +36,7 @@ import ( "github.com/hyperledger/fabric/core/ledger/kvledger/msgs" "github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb/statecouchdb" "github.com/hyperledger/fabric/core/ledger/mock" + "github.com/hyperledger/fabric/internal/fileutil" "github.com/hyperledger/fabric/protoutil" "github.com/stretchr/testify/require" ) @@ -604,6 +605,17 @@ func TestGenerateSnapshotErrors(t *testing.T) { err := kvlgr.generateSnapshot() require.Contains(t, err.Error(), "error while renaming dir") }) + + t.Run("deletes the temp folder upon error", func(t *testing.T) { + closeAndReopenLedgerProvider() + provider.blkStoreProvider.Close() // close the blockstore provider to trigger an error + err := kvlgr.generateSnapshot() + require.Error(t, err) + + empty, err := fileutil.DirEmpty(SnapshotsTempDirPath(conf.SnapshotsConfig.RootDir)) + require.NoError(t, err) + require.True(t, empty) + }) } func testCreateLedgerFromSnapshotErrorPaths(t *testing.T, originalSnapshotDir string) {