Skip to content

Commit

Permalink
Remove Permissions From Fetcher (#1839)
Browse files Browse the repository at this point in the history
  • Loading branch information
RodrigoVillar authored Dec 17, 2024
1 parent 63908f6 commit 5a4c4eb
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 25 deletions.
2 changes: 1 addition & 1 deletion chain/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ func (p *Processor) executeTxs(

// Prefetch state keys from disk
txID := tx.GetID()
if err := f.Fetch(ctx, txID, stateKeys); err != nil {
if err := f.Fetch(ctx, txID, stateKeys.WithoutPermissions()); err != nil {
return nil, nil, err
}
e.Run(stateKeys, func() error {
Expand Down
16 changes: 8 additions & 8 deletions internal/fetcher/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type Fetcher struct {
type tx struct {
blockers int
waiter chan struct{}
keys state.Keys
keys []string
}

type task struct {
Expand Down Expand Up @@ -140,18 +140,18 @@ func (f *Fetcher) handleErr(err error) {
// Fetch can be called concurrently.
//
// Invariant: Don't call [Fetch] afer calling [Stop] or [Wait]
func (f *Fetcher) Fetch(ctx context.Context, txID ids.ID, stateKeys state.Keys) error {
func (f *Fetcher) Fetch(ctx context.Context, txID ids.ID, keys []string) error {
f.l.Lock()
if f.err != nil {
f.l.Unlock()
return f.err
}
var (
tx = &tx{keys: stateKeys}
tasks = make([]*task, 0, len(stateKeys))
tx = &tx{keys: keys}
tasks = make([]*task, 0, len(keys))
blockers = 0
)
for k := range stateKeys {
for _, k := range keys {
d, ok := f.keys[k]
if !ok {
f.keys[k] = &key{blocked: []ids.ID{txID}}
Expand Down Expand Up @@ -216,10 +216,10 @@ func (f *Fetcher) Get(txID ids.ID) (map[string][]byte, error) {
f.l.RLock()
defer f.l.RUnlock()
var (
stateKeys = tx.keys
storage = make(map[string][]byte, len(stateKeys))
keys = tx.keys
storage = make(map[string][]byte, len(keys))
)
for k := range stateKeys {
for _, k := range keys {
if v := f.keys[k].cache; v != nil {
if v.exists {
storage[k] = v.v
Expand Down
28 changes: 12 additions & 16 deletions internal/fetcher/fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,14 @@ func TestFetchDifferentKeys(t *testing.T) {
wg.Add(numTxs)

for i := 0; i < numTxs; i++ {
stateKeys := make(state.Keys, (i + 1))
keys := make([]string, (i + 1))
for k := 0; k < i+1; k++ {
// Generate different read keys
stateKeys.Add(ids.GenerateTestID().String(), state.Read)
keys = append(keys, ids.GenerateTestID().String())
}
txID := ids.GenerateTestID()
// Since these are all different keys, we will
// fetch each key from disk
require.NoError(f.Fetch(ctx, txID, stateKeys))
require.NoError(f.Fetch(ctx, txID, keys))
go func() {
defer wg.Done()
// Get keys from cache
Expand Down Expand Up @@ -98,15 +97,14 @@ func TestFetchSameKeys(t *testing.T) {
wg.Add(numTxs)

for i := 0; i < numTxs; i++ {
stateKeys := make(state.Keys, (i + 1))
keys := make([]string, (i + 1))
for k := 0; k < i+1; k++ {
// Generate the same keys
stateKeys.Add(keyBase+strconv.Itoa(k), state.Read)
keys = append(keys, keyBase+strconv.Itoa(k))
}
txID := ids.GenerateTestID()
// We are fetching the same keys, so we should
// be getting subsequent requests from cache
require.NoError(f.Fetch(ctx, txID, stateKeys))
require.NoError(f.Fetch(ctx, txID, keys))
go func() {
defer wg.Done()
storage, err := f.Get(txID)
Expand Down Expand Up @@ -136,18 +134,17 @@ func TestFetchSameKeysSlow(t *testing.T) {
)
wg.Add(numTxs)
for i := 0; i < numTxs; i++ {
stateKeys := make(state.Keys, (i + 1))
keys := make([]string, (i + 1))
for k := 0; k < i+1; k++ {
// Generate the same keys
stateKeys.Add(keyBase+strconv.Itoa(k), state.Read)
keys = append(keys, keyBase+strconv.Itoa(k))
}
txID := ids.GenerateTestID()

// Empty chan to mimic timing out
delay := make(chan struct{})

// Fetch the key
require.NoError(f.Fetch(ctx, txID, stateKeys))
require.NoError(f.Fetch(ctx, txID, keys))
go func() {
defer wg.Done()
// Get the keys from cache
Expand Down Expand Up @@ -183,13 +180,12 @@ func TestFetcherStop(t *testing.T) {
)
wg.Add(numTxs)
for i := 0; i < numTxs; i++ {
stateKeys := make(state.Keys, (i + 1))
keys := make([]string, (i + 1))
for k := 0; k < i+1; k++ {
// Generate the same keys
stateKeys.Add(keyBase+strconv.Itoa(k), state.Read)
keys = append(keys, keyBase+strconv.Itoa(k))
}
txID := ids.GenerateTestID()
err := f.Fetch(ctx, txID, stateKeys)
err := f.Fetch(ctx, txID, keys)
if err != nil {
// Some [Fetch] may return an error.
// This happens after we called [Stop]
Expand Down
9 changes: 9 additions & 0 deletions state/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ func (k Keys) ChunkSizes() ([]uint16, bool) {
return chunks, true
}

// WithoutPermissions returns the keys of k as a slice with permissions removed
func (k Keys) WithoutPermissions() []string {
ks := make([]string, len(k))
for key := range k {
ks = append(ks, key)
}
return ks
}

type keysJSON map[string]Permissions

// MarshalJSON marshals Keys as readable JSON.
Expand Down

0 comments on commit 5a4c4eb

Please sign in to comment.