Skip to content

Commit

Permalink
- don't check multiple shard from inverted index keys in cache
Browse files Browse the repository at this point in the history
- minor renames
  • Loading branch information
walldiss committed Sep 13, 2023
1 parent 718a80a commit c92b0ab
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
12 changes: 5 additions & 7 deletions share/eds/blockstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,13 @@ func (bs *blockstore) getReadOnlyBlockstore(ctx context.Context, cid cid.Cid) (*
return nil, fmt.Errorf("failed to find shards containing multihash: %w", err)
}

// a share can exist in multiple EDSes, check cache to contain any of accessors containing shard
for _, k := range keys {
if accessor, err := bs.store.cache.Get(k); err == nil {
return blockstoreCloser(accessor)
}
// check if cache contains any of accessors
shardKey := keys[0]
if accessor, err := bs.store.cache.Get(shardKey); err == nil {
return blockstoreCloser(accessor)
}

// a share can exist in multiple EDSes, so just take the first one.
shardKey := keys[0]
// load accessor to the cache and use it as blockstoreCloser
accessor, err := bs.store.cache.GetOrLoad(ctx, shardKey, bs.store.getAccessor)
if err != nil {
return nil, fmt.Errorf("failed to get accessor for shard %s: %w", shardKey, err)
Expand Down
17 changes: 10 additions & 7 deletions share/eds/cache/accessor_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,27 +114,30 @@ func (bc *AccessorCache) GetOrLoad(
lk.Lock()
defer lk.Unlock()

if accessor, err := bc.get(key); err == nil {
return newCloser(accessor)
abs, err := bc.get(key)
if err == nil {
bc.metrics.observeGet(true)
return newCloser(abs)
}

provider, err := loader(ctx, key)
// accessor not found in cache, so load new one using loader
accessor, err := loader(ctx, key)
if err != nil {
return nil, fmt.Errorf("unable to load accessor: %w", err)
}

abs := &accessorWithBlockstore{
shardAccessor: provider,
abs = &accessorWithBlockstore{
shardAccessor: accessor,
}

// Create a new accessor first to increment the reference count in it, so it cannot get evicted from the inner lru cache
// before it is used.
accessor, err := newCloser(abs)
ac, err := newCloser(abs)
if err != nil {
return nil, err
}
bc.cache.Add(key, abs)
return accessor, nil
return ac, nil
}

// Remove removes the Accessor for a given key from the cache.
Expand Down

0 comments on commit c92b0ab

Please sign in to comment.