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/16 21:52:31 UTC

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

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



##########
File path: sdks/java/harness/src/main/java/org/apache/beam/fn/harness/state/CachingBeamFnStateClient.java
##########
@@ -0,0 +1,167 @@
+/*
+ * 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;
+
+  public CachingBeamFnStateClient(

Review comment:
       Can you add a comment to this? It should include a 'sentence fragment' (e.g. `Creates a CachingBeamFnStateClient that wraps a BeamFnStateClient with a LoadingCache.`)
   
   You could also describe what the `cacheTokenList` is for since it isn't obvious to me without looking through the code.
     

##########
File path: sdks/java/harness/src/main/java/org/apache/beam/fn/harness/state/CachingBeamFnStateClient.java
##########
@@ -0,0 +1,167 @@
+/*
+ * 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;
+
+  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;
+  }
+
+  @Override
+  public void handle(
+      StateRequest.Builder requestBuilder, CompletableFuture<StateResponse> response) {
+
+    StateRequest request = requestBuilder.build();
+    StateKey stateKey = request.getStateKey();
+    ByteString cacheToken = getCacheToken(stateKey);
+
+    // If not cacheable proceed as normal
+    if (ByteString.EMPTY.equals(cacheToken)) {
+      beamFnStateClient.handle(requestBuilder, response);
+      return;
+    }
+
+    switch (request.getRequestCase()) {
+      case GET:
+        // Check if data is in the cache already
+        StateGetResponse cachedPage;
+        StateCacheKey cacheKey =
+            StateCacheKey.create(cacheToken, request.getGet().getContinuationToken());
+        Map<StateCacheKey, StateGetResponse> stateKeyMap = stateCache.getUnchecked(stateKey);
+        cachedPage = stateKeyMap.get(cacheKey);
+
+        if (cachedPage == null) {
+          beamFnStateClient.handle(requestBuilder, response);
+          CompletableFuture<Void> callback =
+              response.thenAccept(
+                  stateResponse -> {
+                    stateCache.getUnchecked(stateKey).put(cacheKey, stateResponse.getGet());
+                  });
+
+          callback.getNow(null);
+        } else {
+          response.complete(
+              StateResponse.newBuilder().setId(requestBuilder.getId()).setGet(cachedPage).build());
+        }
+
+        return;
+
+      case APPEND:
+        // Treat appends as normal for now and do not cache

Review comment:
       Create a Jira issue and modify the comment to reference the issue, e.g.:
   
   `// TODO(BEAM-XXXX): Support APPEND in CachingBeamFnStateClient.`.

##########
File path: sdks/java/harness/src/main/java/org/apache/beam/fn/harness/state/CachingBeamFnStateClient.java
##########
@@ -0,0 +1,167 @@
+/*
+ * 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;
+
+  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;
+  }
+
+  @Override
+  public void handle(
+      StateRequest.Builder requestBuilder, CompletableFuture<StateResponse> response) {
+
+    StateRequest request = requestBuilder.build();
+    StateKey stateKey = request.getStateKey();
+    ByteString cacheToken = getCacheToken(stateKey);
+
+    // If not cacheable proceed as normal
+    if (ByteString.EMPTY.equals(cacheToken)) {
+      beamFnStateClient.handle(requestBuilder, response);
+      return;
+    }
+
+    switch (request.getRequestCase()) {
+      case GET:
+        // Check if data is in the cache already
+        StateGetResponse cachedPage;
+        StateCacheKey cacheKey =
+            StateCacheKey.create(cacheToken, request.getGet().getContinuationToken());
+        Map<StateCacheKey, StateGetResponse> stateKeyMap = stateCache.getUnchecked(stateKey);
+        cachedPage = stateKeyMap.get(cacheKey);
+
+        if (cachedPage == null) {
+          beamFnStateClient.handle(requestBuilder, response);
+          CompletableFuture<Void> callback =
+              response.thenAccept(
+                  stateResponse -> {
+                    stateCache.getUnchecked(stateKey).put(cacheKey, stateResponse.getGet());
+                  });
+
+          callback.getNow(null);
+        } else {
+          response.complete(
+              StateResponse.newBuilder().setId(requestBuilder.getId()).setGet(cachedPage).build());
+        }
+
+        return;
+
+      case APPEND:
+        // Treat appends as normal for now and do not cache
+        beamFnStateClient.handle(requestBuilder, response);
+        return;
+
+      case CLEAR:
+        Map<StateCacheKey, StateGetResponse> clearedData = new HashMap<>();
+        StateCacheKey newKey = StateCacheKey.create(cacheToken, ByteString.EMPTY);
+        clearedData.put(newKey, StateGetResponse.getDefaultInstance());
+        stateCache.put(stateKey, clearedData);
+        beamFnStateClient.handle(requestBuilder, response);
+        return;
+
+      default:
+        throw new IllegalStateException(
+            String.format("Unknown request type %s", request.getRequestCase()));
+    }
+  }
+
+  private ByteString getCacheToken(BeamFnApi.StateKey stateKey) {
+    // type: (beam_fn_api_pb2.StateKey) -> Optional[bytes]
+    if (stateKey.hasBagUserState()) {
+      return userStateToken;
+    } else if (stateKey.hasRunner()) {
+      // TODO: Support runner state key caching
+      return ByteString.EMPTY;
+    } else {
+      CacheToken.SideInput.Builder sideInputBuilder = CacheToken.SideInput.newBuilder();
+      if (stateKey.hasIterableSideInput()) {
+        IterableSideInput iterableSideInput = stateKey.getIterableSideInput();
+        sideInputBuilder
+            .setTransformId(iterableSideInput.getTransformId())
+            .setSideInputId(iterableSideInput.getSideInputId());
+      } else if (stateKey.hasMultimapSideInput()) {
+        MultimapSideInput multimapSideInput = stateKey.getMultimapSideInput();
+        sideInputBuilder
+            .setTransformId(multimapSideInput.getTransformId())
+            .setSideInputId(multimapSideInput.getSideInputId());
+      } else if (stateKey.hasMultimapKeysSideInput()) {
+        MultimapKeysSideInput multimapKeysSideInput = stateKey.getMultimapKeysSideInput();
+        sideInputBuilder
+            .setTransformId(multimapKeysSideInput.getTransformId())
+            .setSideInputId(multimapKeysSideInput.getSideInputId());
+      }
+      return sideInputCacheTokens.getOrDefault(sideInputBuilder.build(), ByteString.EMPTY);
+    }
+  }
+
+  @AutoValue
+  public abstract static class StateCacheKey {

Review comment:
       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

##########
File path: sdks/java/harness/src/main/java/org/apache/beam/fn/harness/state/CachingBeamFnStateClient.java
##########
@@ -0,0 +1,167 @@
+/*
+ * 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;
+
+  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;
+  }
+
+  @Override
+  public void handle(
+      StateRequest.Builder requestBuilder, CompletableFuture<StateResponse> response) {
+
+    StateRequest request = requestBuilder.build();
+    StateKey stateKey = request.getStateKey();
+    ByteString cacheToken = getCacheToken(stateKey);
+
+    // If not cacheable proceed as normal
+    if (ByteString.EMPTY.equals(cacheToken)) {
+      beamFnStateClient.handle(requestBuilder, response);
+      return;
+    }
+
+    switch (request.getRequestCase()) {
+      case GET:
+        // Check if data is in the cache already
+        StateGetResponse cachedPage;
+        StateCacheKey cacheKey =
+            StateCacheKey.create(cacheToken, request.getGet().getContinuationToken());
+        Map<StateCacheKey, StateGetResponse> stateKeyMap = stateCache.getUnchecked(stateKey);
+        cachedPage = stateKeyMap.get(cacheKey);
+
+        if (cachedPage == null) {
+          beamFnStateClient.handle(requestBuilder, response);
+          CompletableFuture<Void> callback =
+              response.thenAccept(
+                  stateResponse -> {
+                    stateCache.getUnchecked(stateKey).put(cacheKey, stateResponse.getGet());
+                  });
+
+          callback.getNow(null);
+        } else {
+          response.complete(
+              StateResponse.newBuilder().setId(requestBuilder.getId()).setGet(cachedPage).build());
+        }
+
+        return;
+
+      case APPEND:
+        // Treat appends as normal for now and do not cache
+        beamFnStateClient.handle(requestBuilder, response);
+        return;
+
+      case CLEAR:
+        Map<StateCacheKey, StateGetResponse> clearedData = new HashMap<>();
+        StateCacheKey newKey = StateCacheKey.create(cacheToken, ByteString.EMPTY);
+        clearedData.put(newKey, StateGetResponse.getDefaultInstance());
+        stateCache.put(stateKey, clearedData);
+        beamFnStateClient.handle(requestBuilder, response);
+        return;
+
+      default:
+        throw new IllegalStateException(
+            String.format("Unknown request type %s", request.getRequestCase()));
+    }
+  }
+
+  private ByteString getCacheToken(BeamFnApi.StateKey stateKey) {
+    // type: (beam_fn_api_pb2.StateKey) -> Optional[bytes]
+    if (stateKey.hasBagUserState()) {
+      return userStateToken;
+    } else if (stateKey.hasRunner()) {
+      // TODO: Support runner state key caching

Review comment:
       Is there a Jira issue for this you can reference? If not, please make one.




-- 
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