Skip to content

Commit

Permalink
fix: Cache never hits params
Browse files Browse the repository at this point in the history
  • Loading branch information
red-0ne committed Jan 9, 2025
1 parent efb53ff commit f566510
Show file tree
Hide file tree
Showing 40 changed files with 248 additions and 151 deletions.
2 changes: 1 addition & 1 deletion load-testing/tests/relays_stress.feature
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Feature: Loading gateway server with relays
And more actors are staked as follows:
| actor | actor inc amount | blocks per inc | max actors |
| application | 4 | 10 | 12 |
| gateway | 1 | 10 | 1 |
| gateway | 1 | 10 | 3 |
| supplier | 1 | 10 | 3 |
When a load of concurrent relay requests are sent from the applications
Then the number of failed relay requests is "0"
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ type AccountQueryClient interface {

// GetPubKeyFromAddress returns the public key of the given address.
GetPubKeyFromAddress(ctx context.Context, address string) (cryptotypes.PubKey, error)
ResetCache()
ClearCache()
}

// ApplicationQueryClient defines an interface that enables the querying of the
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/query/accquerier.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,5 @@ func (aq *accQuerier) GetPubKeyFromAddress(ctx context.Context, address string)
return pubKey, nil
}

func (aq *accQuerier) ResetCache() {
func (aq *accQuerier) ClearCache() {
}
18 changes: 7 additions & 11 deletions x/application/keeper/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (

// SetApplication set a specific application in the store from its index
func (k Keeper) SetApplication(ctx context.Context, application types.Application) {
if k.cachedApps[application.Address] != nil {
k.cachedApps[application.Address] = &application
if k.cache.Applications[application.Address] != nil {
k.cache.Applications[application.Address] = &application
}

storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
Expand All @@ -27,7 +27,8 @@ func (k Keeper) GetApplication(
ctx context.Context,
appAddr string,
) (app types.Application, found bool) {
if app, found := k.cachedApps[appAddr]; found {
if app, found := k.cache.Applications[appAddr]; found {
k.logger.Info("-----Application cache hit-----")
return *app, true
}

Expand All @@ -53,14 +54,14 @@ func (k Keeper) GetApplication(
app.DelegateeGatewayAddresses = make([]string, 0)
}

k.cachedApps[appAddr] = &app
k.cache.Applications[appAddr] = &app

return app, true
}

// RemoveApplication removes a application from the store
func (k Keeper) RemoveApplication(ctx context.Context, appAddr string) {
delete(k.cachedApps, appAddr)
delete(k.cache.Applications, appAddr)

storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(storeAdapter, types.KeyPrefix(types.ApplicationKeyPrefix))
Expand All @@ -85,15 +86,10 @@ func (k Keeper) GetAllApplications(ctx context.Context) (apps []types.Applicatio
app.PendingUndelegations = make(map[uint64]types.UndelegatingGatewayList)
}

k.cachedApps[app.Address] = &app
k.cache.Applications[app.Address] = &app

apps = append(apps, app)
}

return
}

func (k Keeper) ResetCache() {
k.cachedParams = nil
clear(k.cachedApps)
}
11 changes: 8 additions & 3 deletions x/application/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ type (
gatewayKeeper types.GatewayKeeper
sharedKeeper types.SharedKeeper

cachedParams *types.Params
cachedApps map[string]*types.Application
cache *types.Cache
}
)

Expand Down Expand Up @@ -57,10 +56,16 @@ func NewKeeper(
gatewayKeeper: gatewayKeeper,
sharedKeeper: sharedKeeper,

cachedApps: make(map[string]*types.Application),
cache: &types.Cache{
Applications: make(map[string]*types.Application),
},
}
}

func (k Keeper) ClearCache() {
k.cache.Clear()
}

// GetAuthority returns the module's authority.
func (k Keeper) GetAuthority() string {
return k.authority
Expand Down
9 changes: 5 additions & 4 deletions x/application/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (

// GetParams get all parameters as types.Params
func (k Keeper) GetParams(ctx context.Context) (params types.Params) {
if k.cachedParams != nil {
return *k.cachedParams
if k.cache.Params != nil {
k.logger.Info("-----Application params cache hit-----")
return *k.cache.Params
}

store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
Expand All @@ -22,13 +23,13 @@ func (k Keeper) GetParams(ctx context.Context) (params types.Params) {

k.cdc.MustUnmarshal(paramsBz, &params)

k.cachedParams = &params
k.cache.Params = &params
return params
}

// SetParams set the params
func (k Keeper) SetParams(ctx context.Context, params types.Params) error {
k.cachedParams = &params
k.cache.Params = &params

store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
paramsBz, err := k.cdc.Marshal(&params)
Expand Down
11 changes: 11 additions & 0 deletions x/application/types/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package types

type Cache struct {
Params *Params
Applications map[string]*Application
}

func (c *Cache) Clear() {
c.Params = nil
clear(c.Applications)
}
8 changes: 0 additions & 8 deletions x/proof/keeper/cache.go

This file was deleted.

11 changes: 6 additions & 5 deletions x/proof/keeper/claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (k Keeper) UpsertClaim(ctx context.Context, claim types.Claim) {
primaryKey := types.ClaimPrimaryKey(sessionId, claim.SupplierOperatorAddress)
primaryStore.Set(primaryKey, claimBz)

k.cachedClaims[sessionId] = &claim
k.cache.Claims[sessionId] = &claim

logger.Info(fmt.Sprintf("upserted claim for supplier %s with primaryKey %s", claim.SupplierOperatorAddress, primaryKey))

Expand All @@ -43,13 +43,14 @@ func (k Keeper) UpsertClaim(ctx context.Context, claim types.Claim) {

// GetClaim returns a claim from its index
func (k Keeper) GetClaim(ctx context.Context, sessionId, supplierOperatorAddr string) (_ types.Claim, isClaimFound bool) {
if claim, found := k.cachedClaims[sessionId]; found {
if claim, found := k.cache.Claims[sessionId]; found {
k.logger.Info("-----Supplier cache hit-----")
return *claim, true
}

claim, found := k.getClaimByPrimaryKey(ctx, types.ClaimPrimaryKey(sessionId, supplierOperatorAddr))
if found {
k.cachedClaims[sessionId] = &claim
k.cache.Claims[sessionId] = &claim
}

return claim, found
Expand All @@ -64,7 +65,7 @@ func (k Keeper) RemoveClaim(ctx context.Context, sessionId, supplierOperatorAddr

// Check if the claim exists
primaryKey := types.ClaimPrimaryKey(sessionId, supplierOperatorAddr)
delete(k.cachedClaims, sessionId)
delete(k.cache.Claims, sessionId)
foundClaim, isClaimFound := k.getClaimByPrimaryKey(ctx, primaryKey)
if !isClaimFound {
logger.Error(fmt.Sprintf("trying to delete non-existent claim with primary key %s for supplier %s and session %s", primaryKey, supplierOperatorAddr, sessionId))
Expand Down Expand Up @@ -98,7 +99,7 @@ func (k Keeper) GetAllClaims(ctx context.Context) (claims []types.Claim) {
for ; iterator.Valid(); iterator.Next() {
var claim types.Claim
k.cdc.MustUnmarshal(iterator.Value(), &claim)
k.cachedClaims[claim.GetSessionHeader().GetSessionId()] = &claim
k.cache.Claims[claim.GetSessionHeader().GetSessionId()] = &claim
claims = append(claims, claim)
}

Expand Down
15 changes: 10 additions & 5 deletions x/proof/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ type (
accountQuerier client.AccountQueryClient
sharedQuerier client.SharedQueryClient

cachedParams *types.Params
cachedProofs map[string]*types.Proof
cachedClaims map[string]*types.Claim
cache *types.Cache
}
)

Expand Down Expand Up @@ -106,11 +104,18 @@ func NewKeeper(
accountQuerier: accountQuerier,
sharedQuerier: sharedQuerier,

cachedProofs: make(map[string]*types.Proof),
cachedClaims: make(map[string]*types.Claim),
cache: &types.Cache{
Proofs: make(map[string]*types.Proof),
Claims: make(map[string]*types.Claim),
},
}
}

func (k Keeper) ClearCache() {
k.cache.Clear()
k.accountQuerier.ClearCache()
}

// GetAuthority returns the module's authority.
func (k Keeper) GetAuthority() string {
return k.authority
Expand Down
9 changes: 5 additions & 4 deletions x/proof/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (

// GetParams get all parameters as types.Params
func (k Keeper) GetParams(ctx context.Context) (params types.Params) {
if k.cachedParams != nil {
return *k.cachedParams
if k.cache.Params != nil {
k.logger.Info("-----Proof params cache hit-----")
return *k.cache.Params
}

store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
Expand All @@ -21,13 +22,13 @@ func (k Keeper) GetParams(ctx context.Context) (params types.Params) {
}

k.cdc.MustUnmarshal(paramsBz, &params)
k.cachedParams = &params
k.cache.Params = &params
return params
}

// SetParams set the params
func (k Keeper) SetParams(ctx context.Context, params types.Params) error {
k.cachedParams = &params
k.cache.Params = &params

store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
paramsBz, err := k.cdc.Marshal(&params)
Expand Down
11 changes: 6 additions & 5 deletions x/proof/keeper/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (k Keeper) UpsertProof(ctx context.Context, proof types.Proof) {
primaryKey := types.ProofPrimaryKey(sessionId, proof.GetSupplierOperatorAddress())
primaryStore.Set(primaryKey, proofBz)

k.cachedProofs[sessionId] = &proof
k.cache.Proofs[sessionId] = &proof

logger.Info(
fmt.Sprintf("upserted proof for supplier %s with primaryKey %s", proof.GetSupplierOperatorAddress(), primaryKey),
Expand All @@ -45,13 +45,14 @@ func (k Keeper) UpsertProof(ctx context.Context, proof types.Proof) {

// GetProof returns a proof from its index
func (k Keeper) GetProof(ctx context.Context, sessionId, supplierOperatorAddr string) (_ types.Proof, isProofFound bool) {
if proof, found := k.cachedProofs[sessionId]; found {
if proof, found := k.cache.Proofs[sessionId]; found {
k.logger.Info("-----Proof cache hit-----")
return *proof, true
}

proof, found := k.getProofByPrimaryKey(ctx, types.ProofPrimaryKey(sessionId, supplierOperatorAddr))
if found {
k.cachedProofs[sessionId] = &proof
k.cache.Proofs[sessionId] = &proof
}

return proof, found
Expand All @@ -66,7 +67,7 @@ func (k Keeper) RemoveProof(ctx context.Context, sessionId, supplierOperatorAddr

// Check if the proof exists
primaryKey := types.ProofPrimaryKey(sessionId, supplierOperatorAddr)
delete(k.cachedProofs, sessionId)
delete(k.cache.Proofs, sessionId)
foundProof, isProofFound := k.getProofByPrimaryKey(ctx, primaryKey)
if !isProofFound {
logger.Error(
Expand Down Expand Up @@ -114,7 +115,7 @@ func (k Keeper) GetAllProofs(ctx context.Context) (proofs []types.Proof) {
for ; iterator.Valid(); iterator.Next() {
var proof types.Proof
k.cdc.MustUnmarshal(iterator.Value(), &proof)
k.cachedProofs[proof.GetSessionHeader().GetSessionId()] = &proof
k.cache.Proofs[proof.GetSessionHeader().GetSessionId()] = &proof
proofs = append(proofs, proof)
}

Expand Down
2 changes: 1 addition & 1 deletion x/proof/keeper/query_claim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func TestClaimQuerySingle(t *testing.T) {
nullify.Fill(response),
)
}
keeper.ResetCache()
keeper.ClearCache()
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion x/proof/keeper/query_proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func TestProofQuerySingle(t *testing.T) {
nullify.Fill(response),
)
}
keeper.ResetCache()
keeper.ClearCache()
})
}
}
Expand Down
4 changes: 3 additions & 1 deletion x/proof/types/account_query_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"context"
fmt "fmt"

cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -63,6 +64,7 @@ func (accountQueryClient *AccountKeeperQueryClient) GetPubKeyFromAddress(
address string,
) (cryptotypes.PubKey, error) {
if acc, found := accountQueryClient.accountPubKeyCache[address]; found {
fmt.Println("-----PubKey cache hit-----")
return acc, nil
}

Expand All @@ -85,6 +87,6 @@ func (accountQueryClient *AccountKeeperQueryClient) GetPubKeyFromAddress(
return pubKey, nil
}

func (accountQueryClient *AccountKeeperQueryClient) ResetCache() {
func (accountQueryClient *AccountKeeperQueryClient) ClearCache() {
clear(accountQueryClient.accountPubKeyCache)
}
13 changes: 13 additions & 0 deletions x/proof/types/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package types

type Cache struct {
Params *Params
Claims map[string]*Claim
Proofs map[string]*Proof
}

func (c *Cache) Clear() {
c.Params = nil
clear(c.Claims)
clear(c.Proofs)
}
7 changes: 0 additions & 7 deletions x/service/keeper/cache.go

This file was deleted.

14 changes: 9 additions & 5 deletions x/service/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ type (

bankKeeper types.BankKeeper

cachedParams *types.Params
cachedServices map[string]*sharedtypes.Service
cachedRelayMiningDifficulty map[string]*types.RelayMiningDifficulty
cache *types.Cache
}
)

Expand All @@ -50,11 +48,17 @@ func NewKeeper(

bankKeeper: bankKeeper,

cachedServices: make(map[string]*sharedtypes.Service),
cachedRelayMiningDifficulty: make(map[string]*types.RelayMiningDifficulty),
cache: &types.Cache{
Services: make(map[string]*sharedtypes.Service),
RelayMiningDifficulty: make(map[string]*types.RelayMiningDifficulty),
},
}
}

func (k Keeper) ClearCache() {
k.cache.Clear()
}

// GetAuthority returns the module's authority.
func (k Keeper) GetAuthority() string {
return k.authority
Expand Down
Loading

0 comments on commit f566510

Please sign in to comment.