Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ The following emojis are used to highlight certain changes:
- upgrade to `go-libp2p` [v0.41.1](https://github.com/libp2p/go-libp2p/releases/tag/v0.41.1)
- `bitswap/network`: Add a new `requests_in_flight` metric gauge that measures how many bitswap streams are being written or read at a given time.
- improve speed of data onboarding by batching/bufering provider queue writes [#888](https://github.com/ipfs/boxo/pull/888)
- `provider/queue` deduplicates CIDs [#910](https://github.com/ipfs/boxo/pull/910)

### Removed

Expand Down
9 changes: 7 additions & 2 deletions provider/internal/queue/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/gammazero/deque"
lru "github.com/hashicorp/golang-lru/v2"
cid "github.com/ipfs/go-cid"
datastore "github.com/ipfs/go-datastore"
namespace "github.com/ipfs/go-datastore/namespace"
Expand Down Expand Up @@ -127,12 +128,13 @@ func (q *Queue) worker(ctx context.Context) {
var (
c cid.Cid
counter uint64
k datastore.Key = datastore.Key{}
inBuf deque.Deque[cid.Cid]
)

const baseCap = 1024
inBuf.SetBaseCap(baseCap)
k := datastore.Key{}
dedupCache, _ := lru.New[cid.Cid, struct{}](baseCap)
Comment thread
gammazero marked this conversation as resolved.
Outdated

defer func() {
if c != cid.Undef {
Expand Down Expand Up @@ -206,6 +208,9 @@ func (q *Queue) worker(ctx context.Context) {
if !ok {
return
}
if found, _ := dedupCache.ContainsOrAdd(toQueue, struct{}{}); found {
continue
}
idle = false

if c == cid.Undef {
Expand Down Expand Up @@ -283,7 +288,7 @@ func (q *Queue) commitInput(ctx context.Context, counter uint64, cids *deque.Deq

cstr := makeCidString(cids.Front())
n := cids.Len()
for i := 0; i < n; i++ {
for i := range n {
Comment thread
guillaumemichel marked this conversation as resolved.
Outdated
c := cids.At(i)
key := datastore.NewKey(fmt.Sprintf("%020d/%s", counter, cstr))
if err = b.Put(ctx, key, c.Bytes()); err != nil {
Expand Down
18 changes: 18 additions & 0 deletions provider/internal/queue/queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,21 @@ func TestInitializationWithManyCids(t *testing.T) {

assertOrdered(cids, queue, t)
}

func TestDeduplicateCids(t *testing.T) {
ds := sync.MutexWrap(datastore.NewMapDatastore())
queue := New(ds)
defer queue.Close()

cids := random.Cids(5)
queue.Enqueue(cids[0])
queue.Enqueue(cids[0])
queue.Enqueue(cids[1])
queue.Enqueue(cids[2])
queue.Enqueue(cids[1])
queue.Enqueue(cids[3])
queue.Enqueue(cids[0])
queue.Enqueue(cids[4])

assertOrdered(cids, queue, t)
}