From d956dd5cd538077da5061ed61ba9853cf1da70f7 Mon Sep 17 00:00:00 2001 From: Junpei Kawamoto Date: Mon, 19 Apr 2021 15:14:11 -0600 Subject: [PATCH 1/2] Remove TODOs that implemented with #169 --- renter/renterutil/strategies.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/renter/renterutil/strategies.go b/renter/renterutil/strategies.go index 23ea322..f659add 100644 --- a/renter/renterutil/strategies.go +++ b/renter/renterutil/strategies.go @@ -1303,7 +1303,6 @@ func (ssd SerialSectorDeleter) DeleteSectors(ctx context.Context, db MetaDB, sec if err != nil { return err } - // TODO: no-op if roots already deleted // TODO: respect ctx err = h.DeleteSectors(roots) ssd.Hosts.release(hostKey) @@ -1330,7 +1329,6 @@ func (psd ParallelSectorDeleter) DeleteSectors(ctx context.Context, db MetaDB, s if err != nil { return &HostError{hostKey, err} } - // TODO: no-op if roots already deleted // TODO: respect ctx err = h.DeleteSectors(roots) psd.Hosts.release(hostKey) From a8855da61c2d4b6061e4cd74a980a0f6076a163f Mon Sep 17 00:00:00 2001 From: Junpei Kawamoto Date: Mon, 19 Apr 2021 15:32:47 -0600 Subject: [PATCH 2/2] DeleteSectors supports context-based cancellation --- renter/renterutil/strategies.go | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/renter/renterutil/strategies.go b/renter/renterutil/strategies.go index f659add..33de5b1 100644 --- a/renter/renterutil/strategies.go +++ b/renter/renterutil/strategies.go @@ -99,6 +99,24 @@ func downloadCtx(ctx context.Context, sess *proto.Session, key renter.KeySeed, s return } +func deleteCtx(ctx context.Context, sess *proto.Session, roots []crypto.Hash) error { + if ctx.Err() != nil { + return ctx.Err() + } + done := make(chan error) + go func() { + done <- sess.DeleteSectors(roots) + }() + select { + case <-ctx.Done(): + sess.Interrupt() + <-done // wait for goroutine to exit + return ctx.Err() + case err := <-done: + return err + } +} + // A ChunkUploader uploads shards, associating them with a given chunk. type ChunkUploader interface { UploadChunk(ctx context.Context, db MetaDB, c DBChunk, key renter.KeySeed, shards [][]byte) error @@ -1299,12 +1317,11 @@ type SerialSectorDeleter struct { // DeleteSectors implements SectorDeleter. func (ssd SerialSectorDeleter) DeleteSectors(ctx context.Context, db MetaDB, sectors map[hostdb.HostPublicKey][]crypto.Hash) error { for hostKey, roots := range sectors { - h, err := ssd.Hosts.acquire(hostKey) + h, err := acquireCtx(ctx, ssd.Hosts, hostKey, true) if err != nil { return err } - // TODO: respect ctx - err = h.DeleteSectors(roots) + err = deleteCtx(ctx, h, roots) ssd.Hosts.release(hostKey) if err != nil && !errors.Is(err, contractmanager.ErrSectorNotFound) { return err @@ -1325,12 +1342,11 @@ func (psd ParallelSectorDeleter) DeleteSectors(ctx context.Context, db MetaDB, s for hostKey, roots := range sectors { go func(hostKey hostdb.HostPublicKey, roots []crypto.Hash) { errCh <- func() *HostError { - h, err := psd.Hosts.acquire(hostKey) + h, err := acquireCtx(ctx, psd.Hosts, hostKey, true) if err != nil { return &HostError{hostKey, err} } - // TODO: respect ctx - err = h.DeleteSectors(roots) + err = deleteCtx(ctx, h, roots) psd.Hosts.release(hostKey) if err != nil && !errors.Is(err, contractmanager.ErrSectorNotFound) { return &HostError{hostKey, err}