Skip to content

Commit

Permalink
Revert removing events for cachekv (#396)
Browse files Browse the repository at this point in the history
## Describe your changes and provide context
This PR reverts the change in
#353 and
#391 until we have OCC
fully enabled.

## Testing performed to validate your change
Unit test coverage
  • Loading branch information
yzang2019 authored and codchen committed Feb 6, 2024
1 parent 9f6b732 commit 3bfa2ca
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 15 deletions.
23 changes: 15 additions & 8 deletions store/cachekv/mergeiterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ import (
//
// TODO: Optimize by memoizing.
type cacheMergeIterator struct {
parent types.Iterator
cache types.Iterator
ascending bool
storeKey sdktypes.StoreKey
parent types.Iterator
cache types.Iterator
ascending bool
storeKey sdktypes.StoreKey
eventManager *sdktypes.EventManager
}

var _ types.Iterator = (*cacheMergeIterator)(nil)
Expand All @@ -28,12 +29,14 @@ func NewCacheMergeIterator(
parent, cache types.Iterator,
ascending bool,
storeKey sdktypes.StoreKey,
eventManager *sdktypes.EventManager,
) *cacheMergeIterator {
iter := &cacheMergeIterator{
parent: parent,
cache: cache,
ascending: ascending,
storeKey: storeKey,
parent: parent,
cache: cache,
ascending: ascending,
storeKey: storeKey,
eventManager: eventManager,
}

return iter
Expand Down Expand Up @@ -135,12 +138,14 @@ func (iter *cacheMergeIterator) Value() []byte {
// If parent is invalid, get the cache value.
if !iter.parent.Valid() {
value := iter.cache.Value()
iter.eventManager.EmitResourceAccessReadEvent("iterator", iter.storeKey, iter.cache.Key(), value)
return value
}

// If cache is invalid, get the parent value.
if !iter.cache.Valid() {
value := iter.parent.Value()
iter.eventManager.EmitResourceAccessReadEvent("iterator", iter.storeKey, iter.parent.Key(), value)
return value
}

Expand All @@ -151,9 +156,11 @@ func (iter *cacheMergeIterator) Value() []byte {
switch cmp {
case -1: // parent < cache
value := iter.parent.Value()
iter.eventManager.EmitResourceAccessReadEvent("iterator", iter.storeKey, keyP, value)
return value
case 0, 1: // parent >= cache
value := iter.cache.Value()
iter.eventManager.EmitResourceAccessReadEvent("iterator", iter.storeKey, keyC, value)
return value
default:
panic("invalid comparison result")
Expand Down
28 changes: 22 additions & 6 deletions store/cachekv/mergeiterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import (
"github.com/cosmos/cosmos-sdk/store/cachekv"
"github.com/cosmos/cosmos-sdk/store/dbadapter"
"github.com/cosmos/cosmos-sdk/store/types"
sdktypes "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
dbm "github.com/tendermint/tm-db"
)

func TestMangerIterator(t *testing.T) {
// initiate mock kvstore
mem := dbadapter.Store{DB: dbm.NewMemDB()}
eventManager := sdktypes.NewEventManager()
kvstore := cachekv.NewStore(mem, types.NewKVStoreKey("CacheKvTest"), types.DefaultCacheSizeLimit)
value := randSlice(defaultValueSizeBz)
startKey := randSlice(32)
Expand All @@ -27,13 +29,27 @@ func TestMangerIterator(t *testing.T) {
cache := kvstore.Iterator(nil, nil)
for ; cache.Valid(); cache.Next() {
}
iter := cachekv.NewCacheMergeIterator(parent, cache, true, types.NewKVStoreKey("CacheKvTest"))
iter := cachekv.NewCacheMergeIterator(parent, cache, true, types.NewKVStoreKey("CacheKvTest"), eventManager)

// get the next value and it should not be nil
nextValue := iter.Value()
require.NotNil(t, nextValue)
// get the next value
iter.Value()

// assert the resource access is still emitted correctly when the cache store is unavailable
require.Equal(t, "access_type", string(eventManager.Events()[0].Attributes[0].Key))
require.Equal(t, "read", string(eventManager.Events()[0].Attributes[0].Value))
require.Equal(t, "store_key", string(eventManager.Events()[0].Attributes[1].Key))
require.Equal(t, "CacheKvTest", string(eventManager.Events()[0].Attributes[1].Value))

// assert event emission when cache is available
cache = kvstore.Iterator(keys[1], keys[2])
iter = cachekv.NewCacheMergeIterator(parent, cache, true, types.NewKVStoreKey("CacheKvTest"), eventManager)

// get the next value
nextValue = iter.Value()
require.NotNil(t, nextValue)
iter.Value()

// assert the resource access is still emitted correctly when the cache store is available
require.Equal(t, "access_type", string(eventManager.Events()[0].Attributes[0].Key))
require.Equal(t, "read", string(eventManager.Events()[0].Attributes[0].Value))
require.Equal(t, "store_key", string(eventManager.Events()[0].Attributes[1].Key))
require.Equal(t, "CacheKvTest", string(eventManager.Events()[0].Attributes[1].Value))
}
7 changes: 6 additions & 1 deletion store/cachekv/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ func (store *Store) GetEvents() []abci.Event {

// Implements Store
func (store *Store) ResetEvents() {
store.mtx.Lock()
defer store.mtx.Unlock()
store.eventManager = sdktypes.NewEventManager()
}

Expand All @@ -75,6 +77,7 @@ func (store *Store) getFromCache(key []byte) []byte {
// Get implements types.KVStore.
func (store *Store) Get(key []byte) (value []byte) {
types.AssertValidKey(key)
store.eventManager.EmitResourceAccessReadEvent("get", store.storeKey, key, value)
return store.getFromCache(key)
}

Expand All @@ -83,11 +86,13 @@ func (store *Store) Set(key []byte, value []byte) {
types.AssertValidKey(key)
types.AssertValidValue(value)
store.setCacheValue(key, value, false, true)
store.eventManager.EmitResourceAccessWriteEvent("set", store.storeKey, key, value)
}

// Has implements types.KVStore.
func (store *Store) Has(key []byte) bool {
value := store.Get(key)
store.eventManager.EmitResourceAccessReadEvent("has", store.storeKey, key, value)
return value != nil
}

Expand Down Expand Up @@ -189,7 +194,7 @@ func (store *Store) iterator(start, end []byte, ascending bool) types.Iterator {
}()
store.dirtyItems(start, end)
cache = newMemIterator(start, end, store.sortedCache, store.deleted, ascending, store.eventManager, store.storeKey)
return NewCacheMergeIterator(parent, cache, ascending, store.storeKey)
return NewCacheMergeIterator(parent, cache, ascending, store.storeKey, store.eventManager)
}

func (store *Store) VersionExists(version int64) bool {
Expand Down

0 comments on commit 3bfa2ca

Please sign in to comment.