scwhittle commented on code in PR #30255:
URL: https://github.com/apache/beam/pull/30255#discussion_r1487972883


##########
runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/windmill/client/grpc/GrpcDirectCommitWorkStream.java:
##########
@@ -0,0 +1,243 @@
+/*
+ * 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.runners.dataflow.worker.windmill.client.grpc;
+
+import java.util.Set;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicLong;
+import java.util.function.Consumer;
+import java.util.function.Function;
+import javax.annotation.Nullable;
+import org.apache.beam.runners.dataflow.worker.streaming.Commit;
+import org.apache.beam.runners.dataflow.worker.streaming.ComputationState;
+import org.apache.beam.runners.dataflow.worker.streaming.ShardedKey;
+import org.apache.beam.runners.dataflow.worker.streaming.WeightedBoundedQueue;
+import org.apache.beam.runners.dataflow.worker.streaming.Work;
+import org.apache.beam.runners.dataflow.worker.windmill.Windmill;
+import org.apache.beam.runners.dataflow.worker.windmill.Windmill.JobHeader;
+import 
org.apache.beam.runners.dataflow.worker.windmill.Windmill.StreamingCommitResponse;
+import 
org.apache.beam.runners.dataflow.worker.windmill.Windmill.StreamingCommitWorkRequest;
+import 
org.apache.beam.runners.dataflow.worker.windmill.Windmill.WorkItemCommitRequest;
+import 
org.apache.beam.runners.dataflow.worker.windmill.client.AbstractWindmillStream;
+import 
org.apache.beam.runners.dataflow.worker.windmill.client.WindmillStream.AsyncCommitWorkStream;
+import 
org.apache.beam.runners.dataflow.worker.windmill.client.grpc.observers.StreamObserverFactory;
+import 
org.apache.beam.runners.dataflow.worker.windmill.client.throttling.ThrottleTimer;
+import org.apache.beam.sdk.util.BackOff;
+import org.apache.beam.vendor.grpc.v1p60p1.io.grpc.stub.StreamObserver;
+import 
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.annotations.VisibleForTesting;
+import 
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions;
+import 
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.util.concurrent.ThreadFactoryBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Implementation of {@link
+ * 
org.apache.beam.runners.dataflow.worker.windmill.client.WindmillStream.CommitWorkStream}
 that
+ * manages its own commit queue, and asynchronously CommitWork RPCs to 
Streaming Engine as callers
+ * queue commits on the internal queue.
+ *
+ * <p>Callers should call {@link #queueCommit(WorkItemCommitRequest, 
ComputationState, Work)} when
+ * work is ready to be committed.
+ */
+public class GrpcDirectCommitWorkStream extends GrpcCommitWorkStream
+    implements AsyncCommitWorkStream {
+  @VisibleForTesting static final int COMMIT_BATCH_SIZE = 5;
+  private static final Logger LOG = 
LoggerFactory.getLogger(GrpcDirectCommitWorkStream.class);
+  private static final int MAX_COMMIT_QUEUE_BYTES = 500 << 20; // 500MB
+  private final WeightedBoundedQueue<Commit> commitQueue;
+  private final ExecutorService commitSender;
+  private final Consumer<Commit> onFailedCommits;
+  private final AtomicLong activeCommitBytes;
+
+  private GrpcDirectCommitWorkStream(
+      Function<StreamObserver<StreamingCommitResponse>, 
StreamObserver<StreamingCommitWorkRequest>>
+          startCommitWorkRpcFn,
+      BackOff backoff,
+      StreamObserverFactory streamObserverFactory,
+      Set<AbstractWindmillStream<?, ?>> streamRegistry,
+      int logEveryNStreamFailures,
+      ThrottleTimer commitWorkThrottleTimer,
+      JobHeader jobHeader,
+      AtomicLong idGenerator,
+      int streamingRpcBatchLimit,
+      Consumer<Commit> onFailedCommits) {
+    super(
+        startCommitWorkRpcFn,
+        backoff,
+        streamObserverFactory,
+        streamRegistry,
+        logEveryNStreamFailures,
+        commitWorkThrottleTimer,
+        jobHeader,
+        idGenerator,
+        streamingRpcBatchLimit);
+    this.commitQueue =
+        WeightedBoundedQueue.create(
+            MAX_COMMIT_QUEUE_BYTES, commit -> Math.min(MAX_COMMIT_QUEUE_BYTES, 
commit.getSize()));
+    this.commitSender =
+        Executors.newSingleThreadScheduledExecutor(
+            new ThreadFactoryBuilder()
+                .setNameFormat("DirectCommitWorkSenderThread")
+                .setUncaughtExceptionHandler(
+                    (t, e) ->
+                        LOG.error(
+                            "{} failed due to uncaught exception during 
execution. ",
+                            t.getName(),
+                            e))
+                .build());
+    this.onFailedCommits = onFailedCommits;
+    this.activeCommitBytes = new AtomicLong();
+  }
+
+  public static GrpcDirectCommitWorkStream create(
+      Function<StreamObserver<StreamingCommitResponse>, 
StreamObserver<StreamingCommitWorkRequest>>
+          startCommitWorkRpcFn,
+      BackOff backoff,
+      StreamObserverFactory streamObserverFactory,
+      Set<AbstractWindmillStream<?, ?>> streamRegistry,
+      int logEveryNStreamFailures,
+      ThrottleTimer commitWorkThrottleTimer,
+      JobHeader jobHeader,
+      AtomicLong idGenerator,
+      int streamingRpcBatchLimit,
+      Consumer<Commit> onFailedCommits) {
+    GrpcDirectCommitWorkStream commitWorkStream =
+        new GrpcDirectCommitWorkStream(
+            startCommitWorkRpcFn,
+            backoff,
+            streamObserverFactory,
+            streamRegistry,
+            logEveryNStreamFailures,
+            commitWorkThrottleTimer,
+            jobHeader,
+            idGenerator,
+            streamingRpcBatchLimit,
+            onFailedCommits);
+    commitWorkStream.startStream();
+    commitWorkStream.startCommitSender();
+    return commitWorkStream;
+  }
+
+  @Override
+  public void queueCommit(
+      WorkItemCommitRequest workItemCommitRequest, ComputationState 
computationState, Work work) {

Review Comment:
   I meant that this class could avoid having to know about ComputationState, 
that the 
   `state.completeWorkAndScheduleNextWorkForKey(`
   calls could live within the failedConsumer or commitCompleteConsumer.
   
   ie something like this:
   ```
   Consumer<CompletedCommit> consumer = (CompletedCommit c) -> {  
state.completeWorkAndScheduleNextWorkForKey(....); }
   ```
   
   Looks like you might have to pass in the computationId with the commit.



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

Reply via email to