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

[BEAM-10212] Add caching state client wrapper #15170

Merged
merged 12 commits into from Jul 22, 2021

Conversation

anthonyqzhu
Copy link
Contributor

Introduces a caching state wrapper that stores delegate state client responses in the provided cache.

Unit tests test caching of user and side input state, and test that appends and clears are reflected in the cache.


Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:

  • Choose reviewer(s) and mention them in a comment (R: @username).
  • Format the pull request title like [BEAM-XXX] Fixes bug in ApproximateQuantiles, where you replace BEAM-XXX with the appropriate JIRA issue, if applicable. This will automatically link the pull request to the issue.
  • Update CHANGES.md with noteworthy changes.
  • If this contribution is large, please file an Apache Individual Contributor License Agreement.

See the Contributor Guide for more tips on how to make review process smoother.

ValidatesRunner compliance status (on master branch)

Lang ULR Dataflow Flink Samza Spark Twister2
Go --- Build Status Build Status Build Status Build Status ---
Java Build Status Build Status
Build Status
Build Status
Build Status
Build Status
Build Status
Build Status
Build Status
Build Status
Build Status
Build Status
Build Status
Build Status
Build Status
Build Status
Python --- Build Status
Build Status
Build Status
Build Status
Build Status
--- Build Status ---
XLang Build Status Build Status Build Status --- Build Status ---

Examples testing status on various runners

Lang ULR Dataflow Flink Samza Spark Twister2
Go --- --- --- --- --- --- ---
Java --- Build Status
Build Status
Build Status
--- --- --- --- ---
Python --- --- --- --- --- --- ---
XLang --- --- --- --- --- --- ---

Post-Commit SDK/Transform Integration Tests Status (on master branch)

Go Java Python
Build Status Build Status Build Status
Build Status
Build Status

Pre-Commit Tests Status (on master branch)

--- Java Python Go Website Whitespace Typescript
Non-portable Build Status
Build Status
Build Status
Build Status
Build Status
Build Status Build Status Build Status Build Status
Portable --- Build Status Build Status --- --- ---

See .test-infra/jenkins/README for trigger phrase, status and link of all Jenkins jobs.

GitHub Actions Tests Status (on master branch)

Build python source distribution and wheels
Python tests
Java tests

See CI.md for more information about GitHub Actions CI.

@anthonyqzhu
Copy link
Contributor Author

R: @amaliujia

@anthonyqzhu
Copy link
Contributor Author

R: @lukecwik

@amaliujia
Copy link
Contributor

cc @tysonjh


switch (request.getRequestCase()) {
case GET:
if (ByteString.EMPTY.equals(request.getGet().getContinuationToken())) {
Copy link
Member

Choose a reason for hiding this comment

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

This looks like we are only ever caching the first page and subsequent ones aren't handled.

Copy link
Contributor

Choose a reason for hiding this comment

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

So should we cache all pages or just the first page?

if (cachedFirstPage == null) {
CompletableFuture<StateResponse> responseFuture = new CompletableFuture<>();
beamFnStateClient.handle(
StateRequest.newBuilder()
Copy link
Member

Choose a reason for hiding this comment

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

Until we get the rest working, let us treat APPEND as a cache eviction instead. Handling APPEND is more complicated since we want to merge existing data with the new data without needing to have a "get" call and there are a couple of scenarios (e.g., there is no existing data, the last page is still cached, ...).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

left append as a regular call to the state client for now

@anthonyqzhu
Copy link
Contributor Author

Run Java PreCommit

}

@AutoValue
public abstract static class StateCacheKey {
Copy link
Contributor

Choose a reason for hiding this comment

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

Add comments please. Note: every class, public method, and tricky bits of code should have comments. There are some exceptions of course, like getters/setters or trivial self explanatory methods.

If you're curious about learning more, you can refer to Google's public style guide for some good practices: https://google.github.io/styleguide/javaguide.html

@anthonyqzhu
Copy link
Contributor Author

@lukecwik @amaliujia @tysonjh made some fix-up changes, let me know if you have any other suggestions!

// If data is not cached, add callback to add response to cache on completion.
// Otherwise, complete the response with the cached data.
if (cachedPage == null) {
beamFnStateClient.handle(requestBuilder, response);
Copy link
Contributor

Choose a reason for hiding this comment

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

Question: how is the GET served when cache miss? When cache hits, you have response.complete, do you need the same for cache miss after you put the state into the cache?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The response.complete is executed by the beamFnStateClient.handle call here, but if it is a cache hit we can complete the response immediately so we do not need to forward the request to the delegate state client

Copy link
Contributor

Choose a reason for hiding this comment

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

Then can you confirm that after complete, your thenAccept will still be executed? (I am not a async API expert so I am not sure).

Copy link
Contributor

@amaliujia amaliujia left a comment

Choose a reason for hiding this comment

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

Overall LGTM. Left a question for the async logic when cache misses.

@lukecwik @tysonjh do you have any additional comment before this PR is merged.

@lukecwik
Copy link
Member

Overall LGTM. Left a question for the async logic when cache misses.

@lukecwik @tysonjh do you have any additional comment before this PR is merged.

Yes, I would like to take another look.

public class CachingBeamFnStateClient implements BeamFnStateClient {

private final BeamFnStateClient beamFnStateClient;
private final LoadingCache<StateKey, Map<StateCacheKey, StateGetResponse>> stateCache;
Copy link
Member

Choose a reason for hiding this comment

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

It doesn't seem like we are using the Loading part of LoadingCache, did you mean to have the CachingBeamFnStateClient perform the loading?

This would generally be a good thing since requests that go for the same state key could resolve down to a single request to the runner instead of multiple.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Leaving this for a future PR

@anthonyqzhu
Copy link
Contributor Author

retest this please

@amaliujia amaliujia closed this Jul 21, 2021
@amaliujia amaliujia reopened this Jul 21, 2021
@anthonyqzhu
Copy link
Contributor Author

@lukecwik addressed comments if you want to take another look

@amaliujia
Copy link
Contributor

Run Java PreCommit

1 similar comment
@amaliujia
Copy link
Contributor

Run Java PreCommit

@amaliujia
Copy link
Contributor

As Luke is OOO for weeks and comments have been addressed. I will merge this PR to unblock Anthony. If there are still other issues, we can address those in patches.

Failed Java Tests seems have been fixed. I will give it another try.

@amaliujia amaliujia closed this Jul 22, 2021
@amaliujia amaliujia reopened this Jul 22, 2021
@amaliujia
Copy link
Contributor

Run Java_Examples_Dataflow_Java11 PreCommit

@amaliujia amaliujia merged commit 63257ed into apache:master Jul 22, 2021
ryanthompson591 pushed a commit to ryanthompson591/beam that referenced this pull request Aug 10, 2021
* [BEAM-10212] Add caching state client wrapper.
calvinleungyk pushed a commit to calvinleungyk/beam that referenced this pull request Sep 22, 2021
* [BEAM-10212] Add caching state client wrapper.
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.

None yet

4 participants