Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Off-Chain, Scalability] Query client caching & history #985

Closed
wants to merge 16 commits into from

Conversation

bryanchriswhite
Copy link
Contributor

@bryanchriswhite bryanchriswhite commented Dec 5, 2024

Summary

Adds caching to the query clients.

---
title: Client caching options
---

classDiagram-v2

class ParamsQuerier__P__sdk_Msg {
    <<interface>>
    GetParams(ctx context.Context) (params P, err error)
    GetParamsAtHeight(ctx context.Context, height int64) (params P, err error)
}

class paramsQuerier__P__sdk_Msg {
    <<interface>>
    GetParams(ctx context.Context) (P, error)
}

class cachedParamsQuerier__P__sdk_Msg__Q__paramsQuerier:::cacheImpl {
    queryClient Q__paramsQuerier[P]
    paramsCache HistoricalQueryCache[P]
    ...
}
ParamsQuerier__P__sdk_Msg --|> cachedParamsQuerier__P__sdk_Msg__Q__paramsQuerier
cachedParamsQuerier__P__sdk_Msg__Q__paramsQuerier --* InMemoryCache: EITHER
cachedParamsQuerier__P__sdk_Msg__Q__paramsQuerier ..* BadgerCache: OR
cachedParamsQuerier__P__sdk_Msg__Q__paramsQuerier --> paramsQuerier__P__sdk_Msg


class QueryCache__T__any {
    <<interface>>
    Get(key string) (value T, err error)
    Set(key string, value T) (err error)
    Delete(key string)
    Clear()
}

class HistoricalQueryCache__T__any {
    <<interface>>
    QueryCache__T
    GetAtHeight(key string, heigt int64) (value T, err error)
    SetAtHeight(key string, value T, heigt int64) (err error)
}
HistoricalQueryCache__T__any --|> QueryCache__T__any

class InMemoryCache:::cacheImpl
InMemoryCache --|> HistoricalQueryCache__T__any

class BadgerCache:::cacheImpl
BadgerCache --|> HistoricalQueryCache__T__any

%% TODO_IN_THIS_COMMIT: uncomment classDef in docs - github can't parse it.
%%classDef cacheImpl fill:#074,color:#fff;
Loading
---
title: Example client integration (session module)
---

classDiagram-v2

class ParamsQuerier__P__sdk_Msg {
    <<interface>>
    GetParams(ctx context.Context) (params P, err error)
    GetParamsAtHeight(ctx context.Context, height int64) (params P, err error)
}


class cachedParamsQuerier__P__sdk_Msg__Q__paramsQuerier {
    queryClient Q__paramsQuerier[P]
    paramsCache HistoricalQueryCache[P]
    ...
}
ParamsQuerier__P__sdk_Msg --|> cachedParamsQuerier__P__sdk_Msg__Q__paramsQuerier

class QueryCache__T__any {
    <<interface>>
    Get(key string) (value T, err error)
    Set(key string, value T) (err error)
    Delete(key string)
    Clear()
}

class HistoricalQueryCache__T__any {
    <<interface>>
    QueryCache__T__any
    GetAtHeight(key string, heigt int64) (value T, err error)
    SetAtHeight(key string, value T, heigt int64) (err error)
}
HistoricalQueryCache__T__any --|> QueryCache__T__any

cachedParamsQuerier__P__sdk_Msg__Q__paramsQuerier --* HistoricalQueryCache__T__any

class SessionQuerier {
    <<interface>>
    ParamsQuerier__P__sdk_Msg
}
SessionQuerier --|> ParamsQuerier__P__sdk_Msg
SessionQuerier --|> sessionQuerier

class sessionQuerier {
    ParamsQuerier__P__sdk_Msg
    sessionCache QueryCache__T__any
    ...
}
sessionQuerier --* cachedParamsQuerier__P__sdk_Msg__Q__paramsQuerier
sessionQuerier --* QueryCache__T__any: (sessionCache)

class paramsQuerier__P__sdk_Msg {
    <<interface>>
    GetParams(ctx context.Context) (P, error)
}
HistoricalQueryCache__T__any ..> paramsQuerier__P__sdk_Msg
sessionQuerier ..|> paramsQuerier__P__sdk_Msg: (non-caching interface adapter)
Loading

Issue

Type of change

Select one or more from the following:

Testing

  • Documentation: make docusaurus_start; only needed if you make doc changes
  • Unit Tests: make go_develop_and_test
  • LocalNet E2E Tests: make test_e2e
  • DevNet E2E Tests: Add the devnet-test-e2e label to the PR.

Sanity Checklist

  • I have tested my changes using the available tooling
  • I have commented my code
  • I have performed a self-review of my own code; both comments & source code
  • I create and reference any new tickets, if applicable
  • I have left TODOs throughout the codebase, if applicable

@bryanchriswhite bryanchriswhite added off-chain Off-chain business logic scalability p0 Top priority labels Dec 5, 2024
@bryanchriswhite bryanchriswhite self-assigned this Dec 5, 2024
); err != nil {
return nil, err
}

querier.sharedQuerier = sharedtypes.NewQueryClient(querier.clientConn)
sq.paramsCache = cache.NewInMemoryCache[*sharedtypes.Params](
// TODO_IN_THIS_COMMIT: extract to constants.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[linter-name (fail-on-found)] reported by reviewdog 🐶
// TODO_IN_THIS_COMMIT: extract to constants.

sq.paramsCache = cache.NewInMemoryCache[*sharedtypes.Params](
// TODO_IN_THIS_COMMIT: extract to constants.
cache.WithHistoricalMode(100),
// TODO_IN_THIS_COMMIT: reconcile the fact that MaxSize doesn't apply to historical mode...
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[linter-name (fail-on-found)] reported by reviewdog 🐶
// TODO_IN_THIS_COMMIT: reconcile the fact that MaxSize doesn't apply to historical mode...

// TODO_IN_THIS_COMMIT: reconcile the fact that MaxSize doesn't apply to historical mode...
//cache.WithMaxSize(1),
cache.WithEvictionPolicy(cache.FirstInFirstOut),
// TODO_IN_THIS_COMMIT: extract to a constant.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[linter-name (fail-on-found)] reported by reviewdog 🐶
// TODO_IN_THIS_COMMIT: extract to a constant.

// EvictionPolicy determines how items are removed when cache is full
type EvictionPolicy string

// TODO_IN_THIS_COMMIT: refactor to an enum.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[linter-name (fail-on-found)] reported by reviewdog 🐶
// TODO_IN_THIS_COMMIT: refactor to an enum.

// historical is whether the cache will cache a single value for each key
// (false) or whether it will cache a history of values for each key (true).
historical bool
// TODO_IN_THIS_COMMIT: godoc...
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[linter-name (fail-on-found)] reported by reviewdog 🐶
// TODO_IN_THIS_COMMIT: godoc...


// Initialize params cache with minimal configuration since we only need latest
sq.paramsCache = cache.NewInMemoryCache[*sessiontypes.Params](
// TODO_IN_THIS_COMMIT: extract to a constant.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[linter-name (fail-on-found)] reported by reviewdog 🐶
// TODO_IN_THIS_COMMIT: extract to a constant.

sq.paramsCache = cache.NewInMemoryCache[*sessiontypes.Params](
// TODO_IN_THIS_COMMIT: extract to a constant.
cache.WithHistoricalMode(100),
// TODO_IN_THIS_COMMIT: reconcile the fact that MaxSize doesn't apply to historical mode...
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[linter-name (fail-on-found)] reported by reviewdog 🐶
// TODO_IN_THIS_COMMIT: reconcile the fact that MaxSize doesn't apply to historical mode...

// TODO_IN_THIS_COMMIT: reconcile the fact that MaxSize doesn't apply to historical mode...
//cache.WithMaxSize(1),
cache.WithEvictionPolicy(cache.FirstInFirstOut),
// TODO_IN_THIS_COMMIT: extract to a constant.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[linter-name (fail-on-found)] reported by reviewdog 🐶
// TODO_IN_THIS_COMMIT: extract to a constant.

); err != nil {
return nil, err
}

sessq.sessionQuerier = sessiontypes.NewQueryClient(sessq.clientConn)
// TODO_IN_THIS_COMMIT: kick off a goroutine that subscribes to params updates and populates the cache.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[linter-name (fail-on-found)] reported by reviewdog 🐶
// TODO_IN_THIS_COMMIT: kick off a goroutine that subscribes to params updates and populates the cache.

@@ -187,6 +187,12 @@ func (b *blockReplayClient) queryLatestBlock(
errCh := make(chan error)

go func() {
// TODO_IN_THIS_COMMIT: extract labels (and values?) to constants.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[linter-name (fail-on-found)] reported by reviewdog 🐶
// TODO_IN_THIS_COMMIT: extract labels (and values?) to constants.

@bryanchriswhite bryanchriswhite added the relayminer Changes related to the Relayminer label Dec 5, 2024
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remaining comments which cannot be posted as a review comment to avoid GitHub Rate Limit

linter-name (fail-on-found)

x/shared/types/query_client.go|22 col 4| // TODO_IN_THIS_COMMIT: godoc...
x/application/types/query_client.go|9 col 4| // TODO_IN_THIS_COMMIT: godoc...
x/application/types/query_client.go|16 col 4| // TODO_IN_THIS_COMMIT: godoc...
x/application/types/query_client.go|21 col 4| // TODO_IN_THIS_COMMIT: investigate generalization...
x/application/types/query_client.go|22 col 4| // TODO_IN_THIS_COMMIT: godoc...
x/service/types/query_client.go|9 col 4| // TODO_IN_THIS_COMMIT: godoc...
x/service/types/query_client.go|16 col 4| // TODO_IN_THIS_COMMIT: godoc...
x/service/types/query_client.go|21 col 4| // TODO_IN_THIS_COMMIT: investigate generalization...
x/service/types/query_client.go|22 col 4| // TODO_IN_THIS_COMMIT: godoc...
x/session/types/query_client.go|9 col 4| // TODO_IN_THIS_COMMIT: godoc...
x/session/types/query_client.go|16 col 4| // TODO_IN_THIS_COMMIT: godoc...
x/session/types/query_client.go|21 col 4| // TODO_IN_THIS_COMMIT: investigate generalization...
x/session/types/query_client.go|22 col 4| // TODO_IN_THIS_COMMIT: godoc...

prooftypes "github.com/pokt-network/poktroll/x/proof/types"
)

// TODO_IN_THIS_COMMIT: comment explaining why we can't use client.ProofQueryClient;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[linter-name (fail-on-found)] reported by reviewdog 🐶
// TODO_IN_THIS_COMMIT: comment explaining why we can't use client.ProofQueryClient;

func NewProofQuerier(
deps depinject.Config,
paramsQuerierOpts ...ParamsQuerierOptionFn,
// TODO_IN_THIS_COMMIT: comment explaining why we can't use client.ProofQueryClient;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[linter-name (fail-on-found)] reported by reviewdog 🐶
// TODO_IN_THIS_COMMIT: comment explaining why we can't use client.ProofQueryClient;

}

querier := &proofQuerier{
// TODO_IN_THIS_COMMIT: extract this to an option.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[linter-name (fail-on-found)] reported by reviewdog 🐶
// TODO_IN_THIS_COMMIT: extract this to an option.

}

sq := &sharedQuerier{
// TODO_IN_THIS_COMMIT: extract this to an option.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[linter-name (fail-on-found)] reported by reviewdog 🐶
// TODO_IN_THIS_COMMIT: extract this to an option.

"github.com/pokt-network/poktroll/pkg/polylog"
)

// TODO_IN_THIS_COMMIT: godoc...
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[linter-name (fail-on-found)] reported by reviewdog 🐶
// TODO_IN_THIS_COMMIT: godoc...

return NewQueryClient(conn).(ProofQueryClient)
}

// TODO_IN_THIS_COMMIT: investigate generalization...
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[linter-name (fail-on-found)] reported by reviewdog 🐶
// TODO_IN_THIS_COMMIT: investigate generalization...

}

// TODO_IN_THIS_COMMIT: investigate generalization...
// TODO_IN_THIS_COMMIT: godoc...
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[linter-name (fail-on-found)] reported by reviewdog 🐶
// TODO_IN_THIS_COMMIT: godoc...

gogogrpc "github.com/cosmos/gogoproto/grpc"
)

// TODO_IN_THIS_COMMIT: godoc...
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[linter-name (fail-on-found)] reported by reviewdog 🐶
// TODO_IN_THIS_COMMIT: godoc...

GetParams(context.Context) (*Params, error)
}

// TODO_IN_THIS_COMMIT: godoc...
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[linter-name (fail-on-found)] reported by reviewdog 🐶
// TODO_IN_THIS_COMMIT: godoc...

return NewQueryClient(conn).(SharedQueryClient)
}

// TODO_IN_THIS_COMMIT: investigate generalization...
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[linter-name (fail-on-found)] reported by reviewdog 🐶
// TODO_IN_THIS_COMMIT: investigate generalization...

gogogrpc "github.com/cosmos/gogoproto/grpc"
)

// TODO_IN_THIS_COMMIT: godoc...
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[linter-name (fail-on-found)] reported by reviewdog 🐶
// TODO_IN_THIS_COMMIT: godoc...

GetParams(context.Context) (*Params, error)
}

// TODO_IN_THIS_COMMIT: godoc...
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[linter-name (fail-on-found)] reported by reviewdog 🐶
// TODO_IN_THIS_COMMIT: godoc...

return NewQueryClient(conn).(SupplierQueryClient)
}

// TODO_IN_THIS_COMMIT: investigate generalization...
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[linter-name (fail-on-found)] reported by reviewdog 🐶
// TODO_IN_THIS_COMMIT: investigate generalization...

}

// TODO_IN_THIS_COMMIT: investigate generalization...
// TODO_IN_THIS_COMMIT: godoc...
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[linter-name (fail-on-found)] reported by reviewdog 🐶
// TODO_IN_THIS_COMMIT: godoc...

}

// TODO_IN_THIS_COMMIT: investigate generalization...
// TODO_IN_THIS_COMMIT: godoc...
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[linter-name (fail-on-found)] reported by reviewdog 🐶
// TODO_IN_THIS_COMMIT: godoc...

}

// TODO_IN_THIS_COMMIT: investigate generalization...
// TODO_IN_THIS_COMMIT: godoc...
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[linter-name (fail-on-found)] reported by reviewdog 🐶
// TODO_IN_THIS_COMMIT: godoc...

gogogrpc "github.com/cosmos/gogoproto/grpc"
)

// TODO_IN_THIS_COMMIT: godoc...
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[linter-name (fail-on-found)] reported by reviewdog 🐶
// TODO_IN_THIS_COMMIT: godoc...

GetParams(context.Context) (*Params, error)
}

// TODO_IN_THIS_COMMIT: godoc...
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[linter-name (fail-on-found)] reported by reviewdog 🐶
// TODO_IN_THIS_COMMIT: godoc...

return NewQueryClient(conn).(SessionQueryClient)
}

// TODO_IN_THIS_COMMIT: investigate generalization...
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[linter-name (fail-on-found)] reported by reviewdog 🐶
// TODO_IN_THIS_COMMIT: investigate generalization...

}

// TODO_IN_THIS_COMMIT: investigate generalization...
// TODO_IN_THIS_COMMIT: godoc...
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[linter-name (fail-on-found)] reported by reviewdog 🐶
// TODO_IN_THIS_COMMIT: godoc...

@@ -12,6 +12,8 @@ go 1.23.0
// github.com/pokt-network/smt/kvstore/pebble => ../smt/kvstore/pebble
// )

replace nhooyr.io/websocket => github.com/coder/websocket v1.8.6
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert

go.sum Outdated
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert

CacheOpts: []cache.CacheOption{
// TODO_IN_THIS_COMMIT: extract to constants.
cache.WithHistoricalMode(100),
// TODO_IN_THIS_COMMIT: reconcile the fact that MaxItems doesn't apply to historical mode...

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[linter-name (fail-on-found)] reported by reviewdog 🐶
// TODO_IN_THIS_COMMIT: reconcile the fact that MaxItems doesn't apply to historical mode...

@@ -51,6 +72,8 @@ func (servq *serviceQuerier) GetService(
Id: serviceId,
}

// TODO_IN_THIS_COMMIT: historically cache services...

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[linter-name (fail-on-found)] reported by reviewdog 🐶
// TODO_IN_THIS_COMMIT: historically cache services...

@@ -71,6 +94,8 @@ func (servq *serviceQuerier) GetServiceRelayDifficulty(
ServiceId: serviceId,
}

// TODO_IN_THIS_COMMIT: historically cache relay mining difficulties...

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[linter-name (fail-on-found)] reported by reviewdog 🐶
// TODO_IN_THIS_COMMIT: historically cache relay mining difficulties...

CacheOpts: []cache.CacheOption{
// TODO_IN_THIS_COMMIT: extract to constants.
cache.WithHistoricalMode(100),
// TODO_IN_THIS_COMMIT: reconcile the fact that MaxKeys doesn't apply to historical mode...

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[linter-name (fail-on-found)] reported by reviewdog 🐶
// TODO_IN_THIS_COMMIT: reconcile the fact that MaxKeys doesn't apply to historical mode...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
off-chain Off-chain business logic p0 Top priority relayminer Changes related to the Relayminer scalability
Projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

1 participant