[
https://issues.apache.org/jira/browse/BEAM-10212?focusedWorklogId=625269&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-625269
]
ASF GitHub Bot logged work on BEAM-10212:
-----------------------------------------
Author: ASF GitHub Bot
Created on: 20/Jul/21 11:47
Start Date: 20/Jul/21 11:47
Worklog Time Spent: 10m
Work Description: amaliujia commented on a change in pull request #15170:
URL: https://github.com/apache/beam/pull/15170#discussion_r672677070
##########
File path:
sdks/java/harness/src/main/java/org/apache/beam/fn/harness/state/CachingBeamFnStateClient.java
##########
@@ -0,0 +1,174 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.beam.fn.harness.state;
+
+import com.google.auto.value.AutoValue;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import org.apache.beam.model.fnexecution.v1.BeamFnApi;
+import
org.apache.beam.model.fnexecution.v1.BeamFnApi.ProcessBundleRequest.CacheToken;
+import org.apache.beam.model.fnexecution.v1.BeamFnApi.StateGetResponse;
+import org.apache.beam.model.fnexecution.v1.BeamFnApi.StateKey;
+import
org.apache.beam.model.fnexecution.v1.BeamFnApi.StateKey.IterableSideInput;
+import
org.apache.beam.model.fnexecution.v1.BeamFnApi.StateKey.MultimapKeysSideInput;
+import
org.apache.beam.model.fnexecution.v1.BeamFnApi.StateKey.MultimapSideInput;
+import org.apache.beam.model.fnexecution.v1.BeamFnApi.StateRequest;
+import org.apache.beam.model.fnexecution.v1.BeamFnApi.StateResponse;
+import org.apache.beam.vendor.grpc.v1p36p0.com.google.protobuf.ByteString;
+import
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.cache.LoadingCache;
+
+/**
+ * Wraps a delegate BeamFnStateClient and stores the result of state requests
in cross bundle cache
+ * according to the available cache tokens. If there are no cache tokens for
the state key requested
+ * the request is forwarded to the client and executed normally.
+ */
+public class CachingBeamFnStateClient implements BeamFnStateClient {
+
+ private final BeamFnStateClient beamFnStateClient;
+ private final LoadingCache<StateKey, Map<StateCacheKey, StateGetResponse>>
stateCache;
+ private final Map<CacheToken.SideInput, ByteString> sideInputCacheTokens;
+ private final ByteString userStateToken;
+
+ /**
+ * Creates a CachingBeamFnStateClient that wraps a BeamFnStateClient with a
LoadingCache. Cache
+ * tokens are sent by the runner to indicate which state is able to be
cached.
+ */
+ public CachingBeamFnStateClient(
+ BeamFnStateClient beamFnStateClient,
+ LoadingCache<StateKey, Map<StateCacheKey, StateGetResponse>> stateCache,
+ List<CacheToken> cacheTokenList) {
+ this.beamFnStateClient = beamFnStateClient;
+ this.stateCache = stateCache;
+ this.sideInputCacheTokens = new HashMap<>();
+
+ // Set up cache tokens.
+ ByteString tempUserStateToken = ByteString.EMPTY;
+ for (BeamFnApi.ProcessBundleRequest.CacheToken token : cacheTokenList) {
+ if (token.hasUserState()) {
+ tempUserStateToken = token.getToken();
+ } else if (token.hasSideInput()) {
+ sideInputCacheTokens.put(token.getSideInput(), token.getToken());
+ }
+ }
+ this.userStateToken = tempUserStateToken;
+ }
+
+ /**
+ * Completes the response with a cached value if possible, if not forwards
the response to the
+ * BeamFnStateClient and tries caching the result. All Append and Clear
requests are forwarded.
+ */
+ @Override
+ public void handle(
+ StateRequest.Builder requestBuilder, CompletableFuture<StateResponse>
response) {
+
+ StateKey stateKey = requestBuilder.getStateKey();
+ ByteString cacheToken = getCacheToken(stateKey);
+
+ // If state is not cacheable proceed as normal.
+ if (ByteString.EMPTY.equals(cacheToken)) {
+ beamFnStateClient.handle(requestBuilder, response);
+ return;
+ }
+
+ switch (requestBuilder.getRequestCase()) {
+ case GET:
+ // Check if data is in the cache.
+ StateCacheKey cacheKey =
+ StateCacheKey.create(cacheToken,
requestBuilder.getGet().getContinuationToken());
+ Map<StateCacheKey, StateGetResponse> stateKeyMap =
stateCache.getUnchecked(stateKey);
+ StateGetResponse cachedPage = stateKeyMap.get(cacheKey);
+
+ // 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);
Review comment:
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?
##########
File path:
sdks/java/harness/src/main/java/org/apache/beam/fn/harness/state/CachingBeamFnStateClient.java
##########
@@ -0,0 +1,174 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.beam.fn.harness.state;
+
+import com.google.auto.value.AutoValue;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import org.apache.beam.model.fnexecution.v1.BeamFnApi;
+import
org.apache.beam.model.fnexecution.v1.BeamFnApi.ProcessBundleRequest.CacheToken;
+import org.apache.beam.model.fnexecution.v1.BeamFnApi.StateGetResponse;
+import org.apache.beam.model.fnexecution.v1.BeamFnApi.StateKey;
+import
org.apache.beam.model.fnexecution.v1.BeamFnApi.StateKey.IterableSideInput;
+import
org.apache.beam.model.fnexecution.v1.BeamFnApi.StateKey.MultimapKeysSideInput;
+import
org.apache.beam.model.fnexecution.v1.BeamFnApi.StateKey.MultimapSideInput;
+import org.apache.beam.model.fnexecution.v1.BeamFnApi.StateRequest;
+import org.apache.beam.model.fnexecution.v1.BeamFnApi.StateResponse;
+import org.apache.beam.vendor.grpc.v1p36p0.com.google.protobuf.ByteString;
+import
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.cache.LoadingCache;
+
+/**
+ * Wraps a delegate BeamFnStateClient and stores the result of state requests
in cross bundle cache
+ * according to the available cache tokens. If there are no cache tokens for
the state key requested
+ * the request is forwarded to the client and executed normally.
+ */
+public class CachingBeamFnStateClient implements BeamFnStateClient {
+
+ private final BeamFnStateClient beamFnStateClient;
+ private final LoadingCache<StateKey, Map<StateCacheKey, StateGetResponse>>
stateCache;
+ private final Map<CacheToken.SideInput, ByteString> sideInputCacheTokens;
+ private final ByteString userStateToken;
+
+ /**
+ * Creates a CachingBeamFnStateClient that wraps a BeamFnStateClient with a
LoadingCache. Cache
+ * tokens are sent by the runner to indicate which state is able to be
cached.
+ */
+ public CachingBeamFnStateClient(
+ BeamFnStateClient beamFnStateClient,
+ LoadingCache<StateKey, Map<StateCacheKey, StateGetResponse>> stateCache,
+ List<CacheToken> cacheTokenList) {
+ this.beamFnStateClient = beamFnStateClient;
+ this.stateCache = stateCache;
+ this.sideInputCacheTokens = new HashMap<>();
+
+ // Set up cache tokens.
+ ByteString tempUserStateToken = ByteString.EMPTY;
+ for (BeamFnApi.ProcessBundleRequest.CacheToken token : cacheTokenList) {
+ if (token.hasUserState()) {
+ tempUserStateToken = token.getToken();
+ } else if (token.hasSideInput()) {
+ sideInputCacheTokens.put(token.getSideInput(), token.getToken());
+ }
+ }
+ this.userStateToken = tempUserStateToken;
+ }
+
+ /**
+ * Completes the response with a cached value if possible, if not forwards
the response to the
+ * BeamFnStateClient and tries caching the result. All Append and Clear
requests are forwarded.
+ */
+ @Override
+ public void handle(
+ StateRequest.Builder requestBuilder, CompletableFuture<StateResponse>
response) {
+
+ StateKey stateKey = requestBuilder.getStateKey();
+ ByteString cacheToken = getCacheToken(stateKey);
+
+ // If state is not cacheable proceed as normal.
+ if (ByteString.EMPTY.equals(cacheToken)) {
+ beamFnStateClient.handle(requestBuilder, response);
+ return;
+ }
+
+ switch (requestBuilder.getRequestCase()) {
+ case GET:
+ // Check if data is in the cache.
+ StateCacheKey cacheKey =
+ StateCacheKey.create(cacheToken,
requestBuilder.getGet().getContinuationToken());
+ Map<StateCacheKey, StateGetResponse> stateKeyMap =
stateCache.getUnchecked(stateKey);
+ StateGetResponse cachedPage = stateKeyMap.get(cacheKey);
+
+ // 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);
Review comment:
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).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
Issue Time Tracking
-------------------
Worklog Id: (was: 625269)
Time Spent: 5h 20m (was: 5h 10m)
> Add support for Java SDK harness state caching
> ----------------------------------------------
>
> Key: BEAM-10212
> URL: https://issues.apache.org/jira/browse/BEAM-10212
> Project: Beam
> Issue Type: Sub-task
> Components: sdk-java-harness
> Reporter: Luke Cwik
> Assignee: Anthony Zhu
> Priority: P3
> Labels: Clarified, portability
> Time Spent: 5h 20m
> Remaining Estimate: 0h
>
> Tech spec:
> [https://docs.google.com/document/d/1BOozW0bzBuz4oHJEuZNDOHdzaV5Y56ix58Ozrqm2jFg/edit#heading=h.7ghoih5aig5m]
> Relevant document:
> [https://docs.google.com/document/d/1ltVqIW0XxUXI6grp17TgeyIybk3-nDF8a0-Nqw-s9mY/edit#|https://docs.google.com/document/d/1ltVqIW0XxUXI6grp17TgeyIybk3-nDF8a0-Nqw-s9mY/edit]
> Mailing list link:
> [https://lists.apache.org/thread.html/caa8d9bc6ca871d13de2c5e6ba07fdc76f85d26497d95d90893aa1f6@%3Cdev.beam.apache.org%3E]
>
> See https://issues.apache.org/jira/browse/BEAM-5428 and
> https://issues.apache.org/jira/browse/BEAM-8298 for Python implementation
> details.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)