From df03d9f16a95e6bb3114d0c17223fbfbf757bb64 Mon Sep 17 00:00:00 2001 From: Hannah Howard Date: Mon, 14 Jun 2021 19:19:10 -0700 Subject: [PATCH] Remove file on storage termination (#219) * feat(tasks): remove file on storage termination recover hard drive space once a storage deal terminates by deleting the generated file * fix(integration tests): attempt to fix integration tests --- integration_tests/01_storage_retrieval_ok.sh | 4 ++-- integration_tests/02_controller_daemon.sh | 2 +- tasks/storage_deal.go | 25 ++++++++++++++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/integration_tests/01_storage_retrieval_ok.sh b/integration_tests/01_storage_retrieval_ok.sh index c24efa90..b0c7007b 100755 --- a/integration_tests/01_storage_retrieval_ok.sh +++ b/integration_tests/01_storage_retrieval_ok.sh @@ -5,7 +5,7 @@ source "$my_dir/header.sh" export DEALBOT_MINER_ADDRESS=t01000 -dealbot storage-deal +dealbot storage-deal 2>&1 | tee dealbot.log returnValue=$? if [[ $returnValue -ne 0 ]]; then @@ -13,7 +13,7 @@ if [[ $returnValue -ne 0 ]]; then exit 1 fi -CID=$(lotus client local | tail -1 | awk '{print $2}') +CID=$(cat dealbot.log | grep datacid | sed 's/.*datacid": "//' | sed 's/"}//') dealbot retrieval-deal --cid=$CID diff --git a/integration_tests/02_controller_daemon.sh b/integration_tests/02_controller_daemon.sh index 6c6c6543..f85819d8 100755 --- a/integration_tests/02_controller_daemon.sh +++ b/integration_tests/02_controller_daemon.sh @@ -76,7 +76,7 @@ if ! grep -q 'INFO.*controller.*storage task.*"miner": "t01000",.*"size": 1024,. exit 1 fi -CID=$(lotus client local | tail -1 | awk '{print $2}') +CID=$(cat dealbot-daemon.log | grep datacid | sed 's/.*datacid": "//' | sed 's/"}//') # Also queue a retrieval task of the data we just stored. curl --header "Content-Type: application/json" \ diff --git a/tasks/storage_deal.go b/tasks/storage_deal.go index fe98c6bd..64b41549 100644 --- a/tasks/storage_deal.go +++ b/tasks/storage_deal.go @@ -54,6 +54,10 @@ func MakeStorageDeal(ctx context.Context, config NodeConfig, node api.FullNode, task: task, } + defer func() { + _ = de.cleanupDeal() + }() + defaultTimeout := stageTimeouts[defaultStorageStageTimeoutName] getStageCtx := func(stage string) (context.Context, context.CancelFunc) { timeout, ok := stageTimeouts[stage] @@ -251,6 +255,7 @@ func (de *storageDealExecutor) importFile(l logFunc) (err error) { } func (de *storageDealExecutor) executeAndMonitorDeal(ctx context.Context, updateStage UpdateStage, stageTimeouts map[string]time.Duration) error { + stage := "ProposeDeal" dealStage := CommonStages[stage]() err := updateStage(ctx, stage, dealStage) @@ -379,6 +384,26 @@ func (de *storageDealExecutor) executeAndMonitorDeal(ctx context.Context, update return nil } +func (de *storageDealExecutor) cleanupDeal() error { + if de.importRes != nil { + // clear out the lotus import store for CIDs in this deal + err := de.node.ClientRemoveImport(de.ctx, de.importRes.ImportID) + if err != nil { + return err + } + de.importRes = nil + } + if de.fileName != "" { + // as a last step no matter what ended up happening with the deal, delete the generated file off to reclaim disk space + err := os.Remove(filepath.Join(de.config.DataDir, de.fileName)) + if err != nil { + return err + } + de.fileName = "" + } + return nil +} + func mktime(t time.Time) _Time { return _Time{x: t.UnixNano()} }