You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2021/07/14 21:30:36 UTC

[GitHub] [beam] lukecwik commented on a change in pull request #15170: [BEAM-10212] Add caching state client wrapper

lukecwik commented on a change in pull request #15170:
URL: https://github.com/apache/beam/pull/15170#discussion_r669936810



##########
File path: sdks/java/harness/src/main/java/org/apache/beam/fn/harness/state/CachingBeamFnStateClient.java
##########
@@ -0,0 +1,179 @@
+/*
+ * 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 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.StateGetRequest;
+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.repackaged.core.org.apache.commons.lang3.tuple.ImmutablePair;
+import org.apache.beam.repackaged.core.org.apache.commons.lang3.tuple.Pair;
+import org.apache.beam.vendor.grpc.v1p36p0.com.google.protobuf.ByteString;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.cache.Cache;
+
+public class CachingBeamFnStateClient implements BeamFnStateClient {
+
+  private final BeamFnStateClient beamFnStateClient;
+  private final Cache<Pair<StateKey, ByteString>, StateGetResponse> stateCache;
+  private Map<Pair<String, String>, ByteString> sideInputCacheTokens;

Review comment:
       I would suggest using an `@AutoValue` around a simple struct like class instead of a `Pair` for readability and code comprehension if it becomes necessary.

##########
File path: sdks/java/harness/src/main/java/org/apache/beam/fn/harness/state/CachingBeamFnStateClient.java
##########
@@ -0,0 +1,179 @@
+/*
+ * 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 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.StateGetRequest;
+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.repackaged.core.org.apache.commons.lang3.tuple.ImmutablePair;
+import org.apache.beam.repackaged.core.org.apache.commons.lang3.tuple.Pair;
+import org.apache.beam.vendor.grpc.v1p36p0.com.google.protobuf.ByteString;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.cache.Cache;
+
+public class CachingBeamFnStateClient implements BeamFnStateClient {
+
+  private final BeamFnStateClient beamFnStateClient;
+  private final Cache<Pair<StateKey, ByteString>, StateGetResponse> stateCache;
+  private Map<Pair<String, String>, ByteString> sideInputCacheTokens;
+  private ByteString userStateToken;
+
+  public CachingBeamFnStateClient(
+      BeamFnStateClient beamFnStateClient,
+      Cache<Pair<StateKey, ByteString>, StateGetResponse> stateCache,
+      List<CacheToken> cacheTokenList) {
+    this.beamFnStateClient = beamFnStateClient;
+    this.stateCache = stateCache;
+    this.sideInputCacheTokens = new HashMap<>();
+    this.userStateToken = null;
+
+    // Set up cache tokens
+    for (BeamFnApi.ProcessBundleRequest.CacheToken token : cacheTokenList) {
+      if (token.hasUserState()) {
+        this.userStateToken = token.getToken();

Review comment:
       You typically have to use a local variable first to "find" the value and then assign it to the final member variable to get around it being "final" in a for loop.

##########
File path: sdks/java/harness/src/main/java/org/apache/beam/fn/harness/state/CachingBeamFnStateClient.java
##########
@@ -0,0 +1,179 @@
+/*
+ * 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 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.StateGetRequest;
+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.repackaged.core.org.apache.commons.lang3.tuple.ImmutablePair;
+import org.apache.beam.repackaged.core.org.apache.commons.lang3.tuple.Pair;
+import org.apache.beam.vendor.grpc.v1p36p0.com.google.protobuf.ByteString;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.cache.Cache;
+
+public class CachingBeamFnStateClient implements BeamFnStateClient {
+
+  private final BeamFnStateClient beamFnStateClient;
+  private final Cache<Pair<StateKey, ByteString>, StateGetResponse> stateCache;
+  private Map<Pair<String, String>, ByteString> sideInputCacheTokens;
+  private ByteString userStateToken;
+
+  public CachingBeamFnStateClient(
+      BeamFnStateClient beamFnStateClient,
+      Cache<Pair<StateKey, ByteString>, StateGetResponse> stateCache,
+      List<CacheToken> cacheTokenList) {
+    this.beamFnStateClient = beamFnStateClient;
+    this.stateCache = stateCache;
+    this.sideInputCacheTokens = new HashMap<>();
+    this.userStateToken = null;
+
+    // Set up cache tokens
+    for (BeamFnApi.ProcessBundleRequest.CacheToken token : cacheTokenList) {
+      if (token.hasUserState()) {
+        this.userStateToken = token.getToken();
+      } else if (token.hasSideInput()) {
+        Pair<String, String> sideInput =
+            new ImmutablePair<>(
+                token.getSideInput().getTransformId(), token.getSideInput().getSideInputId());
+        sideInputCacheTokens.put(sideInput, token.getToken());
+      }
+    }
+  }
+
+  @Override
+  public void handle(
+      BeamFnApi.StateRequest.Builder requestBuilder,
+      CompletableFuture<BeamFnApi.StateResponse> response) {
+
+    StateRequest request = requestBuilder.build();
+    StateKey stateKey = request.getStateKey();
+    ByteString cacheToken = getCacheToken(stateKey);
+    StateResponse.Builder responseBuilder = StateResponse.newBuilder();
+
+    // If not cacheable proceed as normal
+    if (cacheToken == null) {
+      beamFnStateClient.handle(requestBuilder, response);
+      return;
+    }
+
+    // Check if data is in the cache already
+    StateGetResponse cachedFirstPage = null;
+    Pair<StateKey, ByteString> cacheKey = new ImmutablePair<>(stateKey, cacheToken);
+    cachedFirstPage = stateCache.getIfPresent(cacheKey);
+
+    switch (request.getRequestCase()) {
+      case GET:
+        if (ByteString.EMPTY.equals(request.getGet().getContinuationToken())) {
+          if (cachedFirstPage == null) {
+            beamFnStateClient.handle(requestBuilder, response);
+            try {
+              stateCache.put(cacheKey, response.get().getGet());
+            } catch (Exception e) {
+              // response should already be completed exceptionally
+              return;
+            }
+          } else {
+            response.complete(
+                responseBuilder.setId(requestBuilder.getId()).setGet(cachedFirstPage).build());
+          }
+        } else {
+          beamFnStateClient.handle(requestBuilder, response);
+        }
+
+        return;
+
+      case APPEND:
+        if (cachedFirstPage == null) {
+          CompletableFuture<StateResponse> responseFuture = new CompletableFuture<>();
+          beamFnStateClient.handle(
+              StateRequest.newBuilder()

Review comment:
       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, ...).

##########
File path: sdks/java/harness/src/main/java/org/apache/beam/fn/harness/state/CachingBeamFnStateClient.java
##########
@@ -0,0 +1,179 @@
+/*
+ * 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 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.StateGetRequest;
+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.repackaged.core.org.apache.commons.lang3.tuple.ImmutablePair;
+import org.apache.beam.repackaged.core.org.apache.commons.lang3.tuple.Pair;
+import org.apache.beam.vendor.grpc.v1p36p0.com.google.protobuf.ByteString;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.cache.Cache;
+
+public class CachingBeamFnStateClient implements BeamFnStateClient {
+
+  private final BeamFnStateClient beamFnStateClient;
+  private final Cache<Pair<StateKey, ByteString>, StateGetResponse> stateCache;
+  private Map<Pair<String, String>, ByteString> sideInputCacheTokens;
+  private ByteString userStateToken;
+
+  public CachingBeamFnStateClient(
+      BeamFnStateClient beamFnStateClient,
+      Cache<Pair<StateKey, ByteString>, StateGetResponse> stateCache,
+      List<CacheToken> cacheTokenList) {
+    this.beamFnStateClient = beamFnStateClient;
+    this.stateCache = stateCache;
+    this.sideInputCacheTokens = new HashMap<>();
+    this.userStateToken = null;
+
+    // Set up cache tokens
+    for (BeamFnApi.ProcessBundleRequest.CacheToken token : cacheTokenList) {
+      if (token.hasUserState()) {
+        this.userStateToken = token.getToken();
+      } else if (token.hasSideInput()) {
+        Pair<String, String> sideInput =
+            new ImmutablePair<>(
+                token.getSideInput().getTransformId(), token.getSideInput().getSideInputId());
+        sideInputCacheTokens.put(sideInput, token.getToken());
+      }
+    }
+  }
+
+  @Override
+  public void handle(
+      BeamFnApi.StateRequest.Builder requestBuilder,
+      CompletableFuture<BeamFnApi.StateResponse> response) {
+
+    StateRequest request = requestBuilder.build();
+    StateKey stateKey = request.getStateKey();
+    ByteString cacheToken = getCacheToken(stateKey);
+    StateResponse.Builder responseBuilder = StateResponse.newBuilder();
+
+    // If not cacheable proceed as normal
+    if (cacheToken == null) {
+      beamFnStateClient.handle(requestBuilder, response);
+      return;
+    }
+
+    // Check if data is in the cache already
+    StateGetResponse cachedFirstPage = null;
+    Pair<StateKey, ByteString> cacheKey = new ImmutablePair<>(stateKey, cacheToken);
+    cachedFirstPage = stateCache.getIfPresent(cacheKey);
+
+    switch (request.getRequestCase()) {
+      case GET:
+        if (ByteString.EMPTY.equals(request.getGet().getContinuationToken())) {
+          if (cachedFirstPage == null) {
+            beamFnStateClient.handle(requestBuilder, response);
+            try {
+              stateCache.put(cacheKey, response.get().getGet());

Review comment:
       You don't want to resolve the future here as this will make all calls block. You want to add a completion stage to the future so when it is done it will populate the cache.
   
   This will also remove the try/catch block since you can make the completion stage only execute on success.

##########
File path: sdks/java/harness/src/main/java/org/apache/beam/fn/harness/state/CachingBeamFnStateClient.java
##########
@@ -0,0 +1,179 @@
+/*
+ * 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 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.StateGetRequest;
+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.repackaged.core.org.apache.commons.lang3.tuple.ImmutablePair;
+import org.apache.beam.repackaged.core.org.apache.commons.lang3.tuple.Pair;
+import org.apache.beam.vendor.grpc.v1p36p0.com.google.protobuf.ByteString;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.cache.Cache;
+
+public class CachingBeamFnStateClient implements BeamFnStateClient {

Review comment:
       class comment

##########
File path: sdks/java/harness/src/main/java/org/apache/beam/fn/harness/state/CachingBeamFnStateClient.java
##########
@@ -0,0 +1,179 @@
+/*
+ * 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 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.StateGetRequest;
+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.repackaged.core.org.apache.commons.lang3.tuple.ImmutablePair;
+import org.apache.beam.repackaged.core.org.apache.commons.lang3.tuple.Pair;
+import org.apache.beam.vendor.grpc.v1p36p0.com.google.protobuf.ByteString;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.cache.Cache;
+
+public class CachingBeamFnStateClient implements BeamFnStateClient {
+
+  private final BeamFnStateClient beamFnStateClient;
+  private final Cache<Pair<StateKey, ByteString>, StateGetResponse> stateCache;
+  private Map<Pair<String, String>, ByteString> sideInputCacheTokens;
+  private ByteString userStateToken;
+
+  public CachingBeamFnStateClient(
+      BeamFnStateClient beamFnStateClient,
+      Cache<Pair<StateKey, ByteString>, StateGetResponse> stateCache,
+      List<CacheToken> cacheTokenList) {
+    this.beamFnStateClient = beamFnStateClient;
+    this.stateCache = stateCache;
+    this.sideInputCacheTokens = new HashMap<>();
+    this.userStateToken = null;
+
+    // Set up cache tokens
+    for (BeamFnApi.ProcessBundleRequest.CacheToken token : cacheTokenList) {
+      if (token.hasUserState()) {
+        this.userStateToken = token.getToken();
+      } else if (token.hasSideInput()) {
+        Pair<String, String> sideInput =
+            new ImmutablePair<>(
+                token.getSideInput().getTransformId(), token.getSideInput().getSideInputId());
+        sideInputCacheTokens.put(sideInput, token.getToken());
+      }
+    }
+  }
+
+  @Override
+  public void handle(
+      BeamFnApi.StateRequest.Builder requestBuilder,
+      CompletableFuture<BeamFnApi.StateResponse> response) {
+
+    StateRequest request = requestBuilder.build();
+    StateKey stateKey = request.getStateKey();
+    ByteString cacheToken = getCacheToken(stateKey);
+    StateResponse.Builder responseBuilder = StateResponse.newBuilder();
+
+    // If not cacheable proceed as normal
+    if (cacheToken == null) {
+      beamFnStateClient.handle(requestBuilder, response);
+      return;
+    }
+
+    // Check if data is in the cache already
+    StateGetResponse cachedFirstPage = null;

Review comment:
       As a follow-up after this feature is working, consider using a LoadingCache so that multiple requests for the same statekey will all resolve to the same CompletableFuture and/or cache value.
   
   Add a comment to that effect for now.

##########
File path: sdks/java/harness/src/main/java/org/apache/beam/fn/harness/state/CachingBeamFnStateClient.java
##########
@@ -0,0 +1,179 @@
+/*
+ * 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 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.StateGetRequest;
+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.repackaged.core.org.apache.commons.lang3.tuple.ImmutablePair;
+import org.apache.beam.repackaged.core.org.apache.commons.lang3.tuple.Pair;
+import org.apache.beam.vendor.grpc.v1p36p0.com.google.protobuf.ByteString;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.cache.Cache;
+
+public class CachingBeamFnStateClient implements BeamFnStateClient {
+
+  private final BeamFnStateClient beamFnStateClient;
+  private final Cache<Pair<StateKey, ByteString>, StateGetResponse> stateCache;
+  private Map<Pair<String, String>, ByteString> sideInputCacheTokens;
+  private ByteString userStateToken;
+
+  public CachingBeamFnStateClient(
+      BeamFnStateClient beamFnStateClient,
+      Cache<Pair<StateKey, ByteString>, StateGetResponse> stateCache,
+      List<CacheToken> cacheTokenList) {
+    this.beamFnStateClient = beamFnStateClient;
+    this.stateCache = stateCache;
+    this.sideInputCacheTokens = new HashMap<>();
+    this.userStateToken = null;
+
+    // Set up cache tokens
+    for (BeamFnApi.ProcessBundleRequest.CacheToken token : cacheTokenList) {
+      if (token.hasUserState()) {
+        this.userStateToken = token.getToken();
+      } else if (token.hasSideInput()) {
+        Pair<String, String> sideInput =
+            new ImmutablePair<>(
+                token.getSideInput().getTransformId(), token.getSideInput().getSideInputId());
+        sideInputCacheTokens.put(sideInput, token.getToken());
+      }
+    }
+  }
+
+  @Override
+  public void handle(
+      BeamFnApi.StateRequest.Builder requestBuilder,
+      CompletableFuture<BeamFnApi.StateResponse> response) {
+
+    StateRequest request = requestBuilder.build();
+    StateKey stateKey = request.getStateKey();
+    ByteString cacheToken = getCacheToken(stateKey);
+    StateResponse.Builder responseBuilder = StateResponse.newBuilder();
+
+    // If not cacheable proceed as normal
+    if (cacheToken == null) {
+      beamFnStateClient.handle(requestBuilder, response);
+      return;
+    }
+
+    // Check if data is in the cache already
+    StateGetResponse cachedFirstPage = null;
+    Pair<StateKey, ByteString> cacheKey = new ImmutablePair<>(stateKey, cacheToken);
+    cachedFirstPage = stateCache.getIfPresent(cacheKey);
+
+    switch (request.getRequestCase()) {
+      case GET:
+        if (ByteString.EMPTY.equals(request.getGet().getContinuationToken())) {
+          if (cachedFirstPage == null) {
+            beamFnStateClient.handle(requestBuilder, response);
+            try {
+              stateCache.put(cacheKey, response.get().getGet());
+            } catch (Exception e) {
+              // response should already be completed exceptionally
+              return;
+            }
+          } else {
+            response.complete(
+                responseBuilder.setId(requestBuilder.getId()).setGet(cachedFirstPage).build());
+          }
+        } else {
+          beamFnStateClient.handle(requestBuilder, response);
+        }
+
+        return;
+
+      case APPEND:
+        if (cachedFirstPage == null) {
+          CompletableFuture<StateResponse> responseFuture = new CompletableFuture<>();
+          beamFnStateClient.handle(
+              StateRequest.newBuilder()
+                  .setStateKey(stateKey)
+                  .setGet(StateGetRequest.getDefaultInstance()),
+              responseFuture);
+          try {
+            cachedFirstPage = responseFuture.get().getGet();
+            stateCache.put(cacheKey, cachedFirstPage);
+          } catch (Exception e) {
+            // If we can't cache the value just send append as before
+            beamFnStateClient.handle(requestBuilder, response);
+            return;
+          }
+        }
+
+        if (ByteString.EMPTY.equals(cachedFirstPage.getContinuationToken())) {
+          cachedFirstPage =
+              StateGetResponse.newBuilder()
+                  .setData(cachedFirstPage.getData().concat(requestBuilder.getAppend().getData()))
+                  .setContinuationToken(ByteString.EMPTY)
+                  .build();
+          stateCache.put(cacheKey, cachedFirstPage);
+        }
+
+        beamFnStateClient.handle(requestBuilder, response);
+        return;
+
+      case CLEAR:
+        stateCache.put(cacheKey, StateGetResponse.getDefaultInstance());

Review comment:
       Wouldn't you want to remove all values associated with the `StateKey` and insert the default using the first page which is represented as `""`. You might want to have a two level map `StateKey -> Cache Token (ByteString) -> Data`

##########
File path: sdks/java/harness/src/main/java/org/apache/beam/fn/harness/state/CachingBeamFnStateClient.java
##########
@@ -0,0 +1,179 @@
+/*
+ * 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 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.StateGetRequest;
+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.repackaged.core.org.apache.commons.lang3.tuple.ImmutablePair;
+import org.apache.beam.repackaged.core.org.apache.commons.lang3.tuple.Pair;
+import org.apache.beam.vendor.grpc.v1p36p0.com.google.protobuf.ByteString;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.cache.Cache;
+
+public class CachingBeamFnStateClient implements BeamFnStateClient {
+
+  private final BeamFnStateClient beamFnStateClient;
+  private final Cache<Pair<StateKey, ByteString>, StateGetResponse> stateCache;
+  private Map<Pair<String, String>, ByteString> sideInputCacheTokens;
+  private ByteString userStateToken;
+
+  public CachingBeamFnStateClient(
+      BeamFnStateClient beamFnStateClient,
+      Cache<Pair<StateKey, ByteString>, StateGetResponse> stateCache,
+      List<CacheToken> cacheTokenList) {
+    this.beamFnStateClient = beamFnStateClient;
+    this.stateCache = stateCache;
+    this.sideInputCacheTokens = new HashMap<>();
+    this.userStateToken = null;
+
+    // Set up cache tokens
+    for (BeamFnApi.ProcessBundleRequest.CacheToken token : cacheTokenList) {
+      if (token.hasUserState()) {
+        this.userStateToken = token.getToken();
+      } else if (token.hasSideInput()) {
+        Pair<String, String> sideInput =
+            new ImmutablePair<>(
+                token.getSideInput().getTransformId(), token.getSideInput().getSideInputId());
+        sideInputCacheTokens.put(sideInput, token.getToken());
+      }
+    }
+  }
+
+  @Override
+  public void handle(
+      BeamFnApi.StateRequest.Builder requestBuilder,
+      CompletableFuture<BeamFnApi.StateResponse> response) {
+
+    StateRequest request = requestBuilder.build();
+    StateKey stateKey = request.getStateKey();
+    ByteString cacheToken = getCacheToken(stateKey);
+    StateResponse.Builder responseBuilder = StateResponse.newBuilder();
+
+    // If not cacheable proceed as normal
+    if (cacheToken == null) {
+      beamFnStateClient.handle(requestBuilder, response);
+      return;
+    }
+
+    // Check if data is in the cache already
+    StateGetResponse cachedFirstPage = null;
+    Pair<StateKey, ByteString> cacheKey = new ImmutablePair<>(stateKey, cacheToken);
+    cachedFirstPage = stateCache.getIfPresent(cacheKey);

Review comment:
       `cachedFirstPage` here is misnomer, it is just `cachedPage`

##########
File path: sdks/java/harness/src/main/java/org/apache/beam/fn/harness/state/CachingBeamFnStateClient.java
##########
@@ -0,0 +1,179 @@
+/*
+ * 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 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.StateGetRequest;
+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.repackaged.core.org.apache.commons.lang3.tuple.ImmutablePair;
+import org.apache.beam.repackaged.core.org.apache.commons.lang3.tuple.Pair;
+import org.apache.beam.vendor.grpc.v1p36p0.com.google.protobuf.ByteString;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.cache.Cache;
+
+public class CachingBeamFnStateClient implements BeamFnStateClient {
+
+  private final BeamFnStateClient beamFnStateClient;
+  private final Cache<Pair<StateKey, ByteString>, StateGetResponse> stateCache;
+  private Map<Pair<String, String>, ByteString> sideInputCacheTokens;
+  private ByteString userStateToken;

Review comment:
       ```suggestion
     private final Map<Pair<String, String>, ByteString> sideInputCacheTokens;
     private final ByteString userStateToken;
   ```

##########
File path: sdks/java/harness/src/main/java/org/apache/beam/fn/harness/state/CachingBeamFnStateClient.java
##########
@@ -0,0 +1,179 @@
+/*
+ * 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 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.StateGetRequest;
+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.repackaged.core.org.apache.commons.lang3.tuple.ImmutablePair;
+import org.apache.beam.repackaged.core.org.apache.commons.lang3.tuple.Pair;
+import org.apache.beam.vendor.grpc.v1p36p0.com.google.protobuf.ByteString;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.cache.Cache;
+
+public class CachingBeamFnStateClient implements BeamFnStateClient {
+
+  private final BeamFnStateClient beamFnStateClient;
+  private final Cache<Pair<StateKey, ByteString>, StateGetResponse> stateCache;
+  private Map<Pair<String, String>, ByteString> sideInputCacheTokens;
+  private ByteString userStateToken;
+
+  public CachingBeamFnStateClient(
+      BeamFnStateClient beamFnStateClient,
+      Cache<Pair<StateKey, ByteString>, StateGetResponse> stateCache,
+      List<CacheToken> cacheTokenList) {
+    this.beamFnStateClient = beamFnStateClient;
+    this.stateCache = stateCache;
+    this.sideInputCacheTokens = new HashMap<>();
+    this.userStateToken = null;
+
+    // Set up cache tokens
+    for (BeamFnApi.ProcessBundleRequest.CacheToken token : cacheTokenList) {
+      if (token.hasUserState()) {
+        this.userStateToken = token.getToken();
+      } else if (token.hasSideInput()) {
+        Pair<String, String> sideInput =
+            new ImmutablePair<>(
+                token.getSideInput().getTransformId(), token.getSideInput().getSideInputId());
+        sideInputCacheTokens.put(sideInput, token.getToken());
+      }
+    }
+  }
+
+  @Override
+  public void handle(
+      BeamFnApi.StateRequest.Builder requestBuilder,
+      CompletableFuture<BeamFnApi.StateResponse> response) {
+
+    StateRequest request = requestBuilder.build();
+    StateKey stateKey = request.getStateKey();
+    ByteString cacheToken = getCacheToken(stateKey);
+    StateResponse.Builder responseBuilder = StateResponse.newBuilder();
+
+    // If not cacheable proceed as normal
+    if (cacheToken == null) {
+      beamFnStateClient.handle(requestBuilder, response);
+      return;
+    }
+
+    // Check if data is in the cache already
+    StateGetResponse cachedFirstPage = null;
+    Pair<StateKey, ByteString> cacheKey = new ImmutablePair<>(stateKey, cacheToken);
+    cachedFirstPage = stateCache.getIfPresent(cacheKey);
+
+    switch (request.getRequestCase()) {
+      case GET:
+        if (ByteString.EMPTY.equals(request.getGet().getContinuationToken())) {

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




-- 
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: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org