m-trieu commented on code in PR #31317:
URL: https://github.com/apache/beam/pull/31317#discussion_r1614297131


##########
runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/windmill/work/processing/StreamingWorkScheduler.java:
##########
@@ -0,0 +1,437 @@
+/*
+ * 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.work.processing;
+
+import com.google.api.services.dataflow.model.MapTask;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Optional;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Function;
+import java.util.function.Supplier;
+import javax.annotation.concurrent.ThreadSafe;
+import org.apache.beam.runners.dataflow.options.DataflowWorkerHarnessOptions;
+import org.apache.beam.runners.dataflow.worker.DataflowExecutionStateSampler;
+import org.apache.beam.runners.dataflow.worker.DataflowMapTaskExecutor;
+import org.apache.beam.runners.dataflow.worker.DataflowMapTaskExecutorFactory;
+import org.apache.beam.runners.dataflow.worker.DataflowWorkExecutor;
+import org.apache.beam.runners.dataflow.worker.HotKeyLogger;
+import org.apache.beam.runners.dataflow.worker.ReaderCache;
+import org.apache.beam.runners.dataflow.worker.WorkItemCancelledException;
+import 
org.apache.beam.runners.dataflow.worker.logging.DataflowWorkerLoggingMDC;
+import org.apache.beam.runners.dataflow.worker.streaming.ComputationState;
+import org.apache.beam.runners.dataflow.worker.streaming.ExecutionState;
+import 
org.apache.beam.runners.dataflow.worker.streaming.KeyCommitTooLargeException;
+import org.apache.beam.runners.dataflow.worker.streaming.StageInfo;
+import org.apache.beam.runners.dataflow.worker.streaming.Work;
+import 
org.apache.beam.runners.dataflow.worker.streaming.harness.StreamingCounters;
+import 
org.apache.beam.runners.dataflow.worker.streaming.sideinput.SideInputStateFetcher;
+import org.apache.beam.runners.dataflow.worker.util.BoundedQueueExecutor;
+import 
org.apache.beam.runners.dataflow.worker.util.common.worker.ElementCounter;
+import 
org.apache.beam.runners.dataflow.worker.util.common.worker.OutputObjectAndByteCounter;
+import org.apache.beam.runners.dataflow.worker.windmill.Windmill;
+import org.apache.beam.runners.dataflow.worker.windmill.client.commits.Commit;
+import 
org.apache.beam.runners.dataflow.worker.windmill.state.WindmillStateCache;
+import 
org.apache.beam.runners.dataflow.worker.windmill.state.WindmillStateReader;
+import 
org.apache.beam.runners.dataflow.worker.windmill.work.processing.failures.FailureTracker;
+import 
org.apache.beam.runners.dataflow.worker.windmill.work.processing.failures.WorkFailureProcessor;
+import org.apache.beam.sdk.annotations.Internal;
+import org.apache.beam.sdk.coders.Coder;
+import org.apache.beam.sdk.fn.IdGenerator;
+import org.apache.beam.vendor.grpc.v1p60p1.com.google.protobuf.ByteString;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.joda.time.Duration;
+import org.joda.time.Instant;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Schedules execution of user code to process a {@link
+ * org.apache.beam.runners.dataflow.worker.windmill.Windmill.WorkItem} then 
commits the work item
+ * back to streaming execution backend.
+ */
+@Internal
+@ThreadSafe
+public final class StreamingWorkScheduler {
+  private static final Logger LOG = 
LoggerFactory.getLogger(StreamingWorkScheduler.class);
+
+  private final DataflowWorkerHarnessOptions options;
+  private final Supplier<Instant> clock;
+  private final ExecutionStateFactory executionStateFactory;
+  private final SideInputStateFetcher sideInputStateFetcher;
+  private final FailureTracker failureTracker;
+  private final WorkFailureProcessor workFailureProcessor;
+  private final StreamingCommitFinalizer commitFinalizer;
+  private final StreamingCounters streamingCounters;
+  private final HotKeyLogger hotKeyLogger;
+  private final ConcurrentMap<String, StageInfo> stageInfoMap;
+  private final DataflowExecutionStateSampler sampler;
+  private final AtomicInteger maxWorkItemCommitBytes;
+
+  public StreamingWorkScheduler(
+      DataflowWorkerHarnessOptions options,
+      Supplier<Instant> clock,
+      ExecutionStateFactory executionStateFactory,
+      SideInputStateFetcher sideInputStateFetcher,
+      FailureTracker failureTracker,
+      WorkFailureProcessor workFailureProcessor,
+      StreamingCommitFinalizer commitFinalizer,
+      StreamingCounters streamingCounters,
+      HotKeyLogger hotKeyLogger,
+      ConcurrentMap<String, StageInfo> stageInfoMap,
+      DataflowExecutionStateSampler sampler,
+      AtomicInteger maxWorkItemCommitBytes) {
+    this.options = options;
+    this.clock = clock;
+    this.executionStateFactory = executionStateFactory;
+    this.sideInputStateFetcher = sideInputStateFetcher;
+    this.failureTracker = failureTracker;
+    this.workFailureProcessor = workFailureProcessor;
+    this.commitFinalizer = commitFinalizer;
+    this.streamingCounters = streamingCounters;
+    this.hotKeyLogger = hotKeyLogger;
+    this.stageInfoMap = stageInfoMap;
+    this.sampler = sampler;
+    this.maxWorkItemCommitBytes = maxWorkItemCommitBytes;
+  }
+
+  public static StreamingWorkScheduler create(
+      DataflowWorkerHarnessOptions options,
+      Supplier<Instant> clock,
+      ReaderCache readerCache,
+      DataflowMapTaskExecutorFactory mapTaskExecutorFactory,
+      BoundedQueueExecutor workExecutor,
+      Function<String, WindmillStateCache.ForComputation> stateCacheFactory,
+      Function<Windmill.GlobalDataRequest, Windmill.GlobalData> 
fetchGlobalDataFn,
+      FailureTracker failureTracker,
+      WorkFailureProcessor workFailureProcessor,
+      StreamingCounters streamingCounters,
+      HotKeyLogger hotKeyLogger,
+      DataflowExecutionStateSampler sampler,
+      AtomicInteger maxWorkItemCommitBytes,
+      IdGenerator idGenerator,
+      ConcurrentMap<String, StageInfo> stageInfoMap) {
+    ExecutionStateFactory executionStateFactory =
+        new ExecutionStateFactory(
+            options,
+            mapTaskExecutorFactory,
+            readerCache,
+            stateCacheFactory,
+            sampler,
+            streamingCounters.pendingDeltaCounters(),
+            idGenerator);
+
+    return new StreamingWorkScheduler(
+        options,
+        clock,
+        executionStateFactory,
+        new SideInputStateFetcher(fetchGlobalDataFn, options),
+        failureTracker,
+        workFailureProcessor,
+        StreamingCommitFinalizer.create(workExecutor),
+        streamingCounters,
+        hotKeyLogger,
+        stageInfoMap,
+        sampler,
+        maxWorkItemCommitBytes);
+  }
+
+  private static long computeShuffleBytesRead(Windmill.WorkItem workItem) {
+    return workItem.getMessageBundlesList().stream()
+        .flatMap(bundle -> bundle.getMessagesList().stream())
+        .map(Windmill.Message::getSerializedSize)
+        .map(size -> (long) size)
+        .reduce(0L, Long::sum);
+  }
+
+  private static Windmill.WorkItemCommitRequest.Builder 
initializeOutputBuilder(
+      ByteString key, Windmill.WorkItem workItem) {
+    return Windmill.WorkItemCommitRequest.newBuilder()
+        .setKey(key)
+        .setShardingKey(workItem.getShardingKey())
+        .setWorkToken(workItem.getWorkToken())
+        .setCacheToken(workItem.getCacheToken());
+  }
+
+  private static Windmill.WorkItemCommitRequest buildWorkItemTruncationRequest(
+      ByteString key, Windmill.WorkItem workItem, int estimatedCommitSize) {
+    Windmill.WorkItemCommitRequest.Builder outputBuilder = 
initializeOutputBuilder(key, workItem);
+    outputBuilder.setExceedsMaxWorkItemCommitBytes(true);
+    outputBuilder.setEstimatedWorkItemCommitBytes(estimatedCommitSize);
+    return outputBuilder.build();
+  }
+
+  /** Sets the stage name and workId of the Thread executing the {@link Work} 
for logging. */
+  private static void setUpWorkLoggingContext(String workLatencyTrackingId, 
String computationId) {
+    DataflowWorkerLoggingMDC.setWorkId(workLatencyTrackingId);
+    DataflowWorkerLoggingMDC.setStageName(computationId);
+  }
+
+  private static String getShuffleTaskStepName(MapTask mapTask) {
+    // The MapTask instruction is ordered by dependencies, such that the first 
element is
+    // always going to be the shuffle task.
+    return mapTask.getInstructions().get(0).getName();
+  }
+
+  private static long computeSourceBytesProcessed(
+      DataflowWorkExecutor workExecutor, String sourceBytesCounterName) {
+    HashMap<String, ElementCounter> counters =
+        ((DataflowMapTaskExecutor) workExecutor)
+            .getReadOperation()
+            .receivers[0]
+            .getOutputCounters();
+
+    return Optional.ofNullable(counters.get(sourceBytesCounterName))
+        .map(counter -> ((OutputObjectAndByteCounter) 
counter).getByteCount().getAndReset())
+        .orElse(0L);
+  }
+
+  /** Resets logging context of the Thread executing the {@link Work} for 
logging. */
+  private void resetWorkLoggingContext(String workLatencyTrackingId) {
+    sampler.resetForWorkId(workLatencyTrackingId);
+    DataflowWorkerLoggingMDC.setWorkId(null);
+    DataflowWorkerLoggingMDC.setStageName(null);
+  }
+
+  /**
+   * Schedule work for execution. Work may be executed immediately, or queued 
and executed in the
+   * future. Only one work may be "active" (currently executing) per key at a 
time.
+   */
+  public void scheduleWork(
+      ComputationState computationState,
+      Windmill.WorkItem workItem,
+      Work.Watermarks watermarks,
+      Work.ProcessingContext.WithProcessWorkFn processingContext,
+      Collection<Windmill.LatencyAttribution> getWorkStreamLatencies) {
+    Work scheduledWork =
+        Work.create(
+            workItem,
+            watermarks,
+            processingContext.setProcessWorkFnAndBuild(work -> 
execute(computationState, work)),

Review Comment:
   because of the dependency graph made some modifications
   
   but was able to remove this lazy binding and seperate Work from 
Consumer<Work>, combining them in ExecutableWork.java



-- 
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-unsubscr...@beam.apache.org

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

Reply via email to