Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DeleteSectors supports context-based cancellation #170

Merged
merged 2 commits into from
Apr 20, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions renter/renterutil/strategies.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1299,13 +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: no-op if roots already deleted
// 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
Expand All @@ -1326,13 +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: no-op if roots already deleted
// 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}
Expand Down