Skip to content

Conversation

@aaron-steinfeld
Copy link
Contributor

@aaron-steinfeld aaron-steinfeld commented May 25, 2021

Description

In many places across many repos, we try to solve the problem of caching remote call results in different ways - usually by using the call arguments as a cache key, or sometimes the tenant id and the call arguments. This is meant to provide a reusable way of doing so, that leaves the meaningful parts of the request context as an implementation detail. The idea is that, as more data is added to our request context (for example, user identifiers), that will flow into cache keys without requiring widespread implementation changes.

Testing

Added UTs

Checklist:

  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • Any dependent changes have been merged and published in downstream modules

@aaron-steinfeld
Copy link
Contributor Author

whoops - meant to draft!

@github-actions

This comment has been minimized.

@codecov
Copy link

codecov bot commented May 25, 2021

Codecov Report

Merging #16 (a1f5502) into main (614ba57) will increase coverage by 4.15%.
The diff coverage is 77.41%.

Impacted file tree graph

@@             Coverage Diff              @@
##               main      #16      +/-   ##
============================================
+ Coverage     67.33%   71.49%   +4.15%     
- Complexity       64       78      +14     
============================================
  Files            15       16       +1     
  Lines           199      228      +29     
  Branches          9       12       +3     
============================================
+ Hits            134      163      +29     
+ Misses           57       55       -2     
- Partials          8       10       +2     
Flag Coverage Δ
unit 71.49% <77.41%> (+4.15%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...ertrace/core/grpcutils/context/RequestContext.java 59.25% <0.00%> (+7.25%) ⬆️
...e/core/grpcutils/context/DefaultContextualKey.java 80.00% <80.00%> (ø)
...ore/grpcutils/context/RequestContextConstants.java 85.71% <100.00%> (+85.71%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 614ba57...a1f5502. Read the comment docs.

@aaron-steinfeld aaron-steinfeld marked this pull request as ready for review May 28, 2021 13:36
@github-actions

This comment has been minimized.

public static final Set<String> HEADER_PREFIXES_TO_BE_PROPAGATED =
Set.of(TENANT_ID_HEADER_KEY, "X-B3-", "grpc-trace-bin",
"traceparent", "tracestate", AUTHORIZATION_HEADER);
static final Set<String> CACHE_MEANINGFUL_HEADERS =
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I know there's some debate whether authorization is meaningful. I'll remove it if needed to unblock this since we can revisit this later in a common way, but I maintain it's better to have the default implementation err on the side of being always correct at the cost of potential duplication.

@aaron-steinfeld
Copy link
Contributor Author

@tim-mwangi @skjindal93 @avinashkolluru - can I please get a review on this? If people would prefer me remove the auth header to defer that debate that's fine, but I'd like to use this elsewhere to prevent making the ~20th implementation of this pattern :)

private final T data;
private final Map<String, String> meaningfulContextHeaders;

DefaultContextualKey(RequestContext context, T data) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we always have to pass in some data to make the request context as a cache key? Can we also have a constructor, which just takes in the RequestContext as an argument?

Copy link
Contributor

Choose a reason for hiding this comment

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

I am a bit confused why do we need data (which I believe is an input to the GRPC function calls) as a parameter for the cache key

Basically, why should the contextual key change on a different data?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do we always have to pass in some data to make the request context as a cache key? Can we also have a constructor, which just takes in the RequestContext as an argument?

We don't always need to use data, there's a no arg version (this is always built off the context) https://github.com/hypertrace/java-grpc-utils/pull/16/files#diff-ef190554e9c2549e8421789de3b00e6c5fae5294934d883251699dff75676817R87 )

I'll create flavors of the function execution methods that don't provide data, too.

I am a bit confused why do we need data (which I believe is an input to the GRPC function calls) as a parameter for the cache key

Basically, why should the contextual key change on a different data?

So I attempt to convey the distinction by calling a Contextual key rather than a Context key - that is, it's a key that accounts for the context rather than a key for the context. Since the former is a superset of the latter, it felt more flexible to me as it can both handle cases where we solely want a per-context key, but also cases where we, for example, want to cache something like an entity by id.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated the first part.

@github-actions

This comment has been minimized.

@aaron-steinfeld aaron-steinfeld merged commit eb6b0d1 into main Jun 3, 2021
@aaron-steinfeld aaron-steinfeld deleted the reusable-cache-key branch June 3, 2021 15:55
@github-actions
Copy link

github-actions bot commented Jun 3, 2021

Unit Test Results

11 files  +1  11 suites  +1   11s ⏱️ ±0s
49 tests +3  49 ✔️ +3  0 💤 ±0  0 ❌ ±0 

Results for commit eb6b0d1. ± Comparison against base commit 614ba57.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants