Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ The following emojis are used to highlight certain changes:

### Added

- `provider`: Add ability to clear provide queue [#978](https://github.com/ipfs/boxo/pull/978)

### Changed

### Removed
Expand Down
77 changes: 77 additions & 0 deletions provider/internal/queue/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
dequeue chan cid.Cid
ds datastore.Batching
enqueue chan cid.Cid
clear chan chan<- int
}

// New creates a queue for cids.
Expand All @@ -69,6 +70,7 @@
dequeue: make(chan cid.Cid),
ds: namespace.Wrap(ds, datastore.NewKey("/queue")),
enqueue: make(chan cid.Cid),
clear: make(chan chan<- int),
}

go q.worker(ctx)
Expand Down Expand Up @@ -112,6 +114,14 @@
return q.dequeue
}

// Clear clears all queued records from memory and the datastore. Returns the
// number of CIDs removed from the queue.
func (q *Queue) Clear() int {
rsp := make(chan int)
q.clear <- rsp
return <-rsp
}

func makeCidString(c cid.Cid) string {
data := c.Bytes()
if len(data) > 4 {
Expand Down Expand Up @@ -249,6 +259,26 @@

case <-ctx.Done():
return

case rsp := <-q.clear:
var rmMemCount int
if c != cid.Undef {
rmMemCount = 1
}
c = cid.Undef
k = datastore.Key{}
idle = false
rmMemCount += inBuf.Len()
inBuf.Clear()
dedupCache.Purge()
rmDSCount, err := q.clearDatastore(ctx)
if err != nil {
log.Errorw("provider queue: cannot clear datastore", "err", err)

Check warning on line 276 in provider/internal/queue/queue.go

View check run for this annotation

Codecov / codecov/patch

provider/internal/queue/queue.go#L276

Added line #L276 was not covered by tests
} else {
dsEmpty = true
}
log.Infow("cleared provider queue", "fromMemory", rmMemCount, "fromDatastore", rmDSCount)
rsp <- rmMemCount + rmDSCount
}

if commit {
Expand All @@ -267,6 +297,52 @@
}
}

func (q *Queue) clearDatastore(ctx context.Context) (int, error) {
qry := query.Query{
KeysOnly: true,
}
results, err := q.ds.Query(ctx, qry)
if err != nil {
return 0, fmt.Errorf("cannot query datastore: %w", err)
}

Check warning on line 307 in provider/internal/queue/queue.go

View check run for this annotation

Codecov / codecov/patch

provider/internal/queue/queue.go#L306-L307

Added lines #L306 - L307 were not covered by tests
defer results.Close()

batch, err := q.ds.Batch(ctx)
if err != nil {
return 0, fmt.Errorf("cannot create datastore batch: %w", err)
}

Check warning on line 313 in provider/internal/queue/queue.go

View check run for this annotation

Codecov / codecov/patch

provider/internal/queue/queue.go#L312-L313

Added lines #L312 - L313 were not covered by tests

var rmCount, writeCount int
for result := range results.Next() {
if ctx.Err() != nil {
return 0, ctx.Err()
}

Check warning on line 319 in provider/internal/queue/queue.go

View check run for this annotation

Codecov / codecov/patch

provider/internal/queue/queue.go#L318-L319

Added lines #L318 - L319 were not covered by tests
if writeCount >= batchSize {
writeCount = 0
if err = batch.Commit(ctx); err != nil {
return 0, fmt.Errorf("cannot commit datastore updates: %w", err)
}

Check warning on line 324 in provider/internal/queue/queue.go

View check run for this annotation

Codecov / codecov/patch

provider/internal/queue/queue.go#L321-L324

Added lines #L321 - L324 were not covered by tests
}
if result.Error != nil {
return 0, fmt.Errorf("cannot read query result from datastore: %w", result.Error)
}

Check warning on line 328 in provider/internal/queue/queue.go

View check run for this annotation

Codecov / codecov/patch

provider/internal/queue/queue.go#L327-L328

Added lines #L327 - L328 were not covered by tests
if err = batch.Delete(ctx, datastore.NewKey(result.Key)); err != nil {
return 0, fmt.Errorf("cannot delete key from datastore: %w", err)
}

Check warning on line 331 in provider/internal/queue/queue.go

View check run for this annotation

Codecov / codecov/patch

provider/internal/queue/queue.go#L330-L331

Added lines #L330 - L331 were not covered by tests
rmCount++
writeCount++
}

if err = batch.Commit(ctx); err != nil {
return 0, fmt.Errorf("cannot commit datastore updated: %w", err)
}

Check warning on line 338 in provider/internal/queue/queue.go

View check run for this annotation

Codecov / codecov/patch

provider/internal/queue/queue.go#L337-L338

Added lines #L337 - L338 were not covered by tests
if err = q.ds.Sync(ctx, datastore.NewKey("")); err != nil {
return 0, fmt.Errorf("cannot sync datastore: %w", err)
}

Check warning on line 341 in provider/internal/queue/queue.go

View check run for this annotation

Codecov / codecov/patch

provider/internal/queue/queue.go#L340-L341

Added lines #L340 - L341 were not covered by tests

return rmCount, nil
}

func (q *Queue) getQueueHead(ctx context.Context) (*query.Entry, error) {
qry := query.Query{
Orders: []query.Order{query.OrderByKey{}},
Expand Down Expand Up @@ -301,6 +377,7 @@
}
counter++
}

cids.Clear()

if err = b.Commit(ctx); err != nil {
Expand Down
45 changes: 45 additions & 0 deletions provider/internal/queue/queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,48 @@ func TestDeduplicateCids(t *testing.T) {

assertOrdered(cids, queue, t)
}

func TestClear(t *testing.T) {
const cidCount = 25

ds := sync.MutexWrap(datastore.NewMapDatastore())
queue := New(ds)
defer queue.Close()

for _, c := range random.Cids(cidCount) {
queue.Enqueue(c)
}

// Cause queued entried to be saved in datastore.
err := queue.Close()
if err != nil {
t.Fatal(err)
}

queue = New(ds)
defer queue.Close()

for _, c := range random.Cids(cidCount) {
queue.Enqueue(c)
}

rmCount := queue.Clear()
t.Log("Cleared", rmCount, "entries from provider queue")
if rmCount != 2*cidCount {
t.Fatalf("expected %d cleared, got %d", 2*cidCount, rmCount)
}

if err = queue.Close(); err != nil {
t.Fatal(err)
}

// Ensure no data when creating new queue.
queue = New(ds)
defer queue.Close()

select {
case <-queue.Dequeue():
t.Fatal("dequeue should not return")
case <-time.After(10 * time.Millisecond):
}
}
4 changes: 4 additions & 0 deletions provider/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
return &noopProvider{}
}

func (op *noopProvider) Clear() int {
return 0

Check warning on line 19 in provider/noop.go

View check run for this annotation

Codecov / codecov/patch

provider/noop.go#L18-L19

Added lines #L18 - L19 were not covered by tests
}

func (op *noopProvider) Close() error {
return nil
}
Expand Down
3 changes: 3 additions & 0 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ type Reprovider interface {
// System defines the interface for interacting with the value
// provider system
type System interface {
// Clear removes all entries from the provide queue. Returns the number of
// CIDs removed from the queue.
Clear() int
Close() error
Stat() (ReproviderStats, error)
Provider
Expand Down
6 changes: 6 additions & 0 deletions provider/reprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,12 @@
return time.Unix(0, tns), nil
}

// Clear removes all entries from the provide queue. Returns the number of CIDs
// removed from the queue.
func (s *reprovider) Clear() int {
return s.q.Clear()

Check warning on line 405 in provider/reprovider.go

View check run for this annotation

Codecov / codecov/patch

provider/reprovider.go#L404-L405

Added lines #L404 - L405 were not covered by tests
}

func (s *reprovider) Close() error {
s.close()
err := s.q.Close()
Expand Down