Skip to content

Commit

Permalink
Remove the temp folder on error during snapshot generation (#2104)
Browse files Browse the repository at this point in the history
Signed-off-by: manish <manish.sethi@gmail.com>
  • Loading branch information
manish-sethi authored Nov 10, 2020
1 parent 941f0af commit e5ebb7b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
10 changes: 6 additions & 4 deletions core/ledger/kvledger/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -113,27 +115,27 @@ 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,
configsHistoryExportSummary, stateDBExportSummary,
); 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
Expand Down
12 changes: 12 additions & 0 deletions core/ledger/kvledger/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit e5ebb7b

Please sign in to comment.