m-trieu commented on code in PR #31784: URL: https://github.com/apache/beam/pull/31784#discussion_r1691674692
########## runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/windmill/client/getdata/FanOutWorkRefreshClient.java: ########## @@ -0,0 +1,92 @@ +/* + * 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.getdata; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import org.apache.beam.runners.dataflow.worker.windmill.work.refresh.HeartbeatSender; +import org.apache.beam.runners.dataflow.worker.windmill.work.refresh.Heartbeats; +import org.apache.beam.sdk.annotations.Internal; +import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.util.concurrent.ThreadFactoryBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * {@link WorkRefreshClient} that fans out heartbeats to all {@link HeartbeatSender}(s) in parallel + * passed into {@link #refreshActiveWork(Map)} + */ +@Internal +public final class FanOutWorkRefreshClient implements WorkRefreshClient { + private static final Logger LOG = LoggerFactory.getLogger(FanOutWorkRefreshClient.class); + private static final String FAN_OUT_REFRESH_WORK_EXECUTOR_NAME = + "FanOutActiveWorkRefreshExecutor"; Review Comment: done ########## runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/windmill/client/getdata/ThrottlingGetDataMetricTracker.java: ########## @@ -0,0 +1,146 @@ +/* + * 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.getdata; + +import com.google.auto.value.AutoValue; +import java.io.PrintWriter; +import java.util.concurrent.atomic.AtomicInteger; +import javax.annotation.concurrent.ThreadSafe; +import org.apache.beam.runners.dataflow.worker.util.MemoryMonitor; +import org.apache.beam.sdk.annotations.Internal; +import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.annotations.VisibleForTesting; + +/** + * Wraps GetData calls that tracks metrics for the number of in-flight requests and throttles + * requests when memory pressure is high. + */ +@Internal +@ThreadSafe +public final class ThrottlingGetDataMetricTracker { + private final MemoryMonitor gcThrashingMonitor; + private final GetDataMetrics getDataMetrics; + + public ThrottlingGetDataMetricTracker(MemoryMonitor gcThrashingMonitor) { + this.gcThrashingMonitor = gcThrashingMonitor; + this.getDataMetrics = GetDataMetrics.create(); + } + + /** + * Tracks a GetData call. If there is memory pressure, may throttle requests. Returns an {@link + * AutoCloseable} that will decrement the metric after the call is finished. + */ + public AutoCloseable trackSingleCallWithThrottling(Type callType) { + gcThrashingMonitor.waitForResources(callType.debugName); + AtomicInteger getDataMetricTracker = getDataMetrics.getMetricFor(callType); + getDataMetricTracker.getAndIncrement(); + return getDataMetricTracker::getAndDecrement; + } + + /** + * Tracks heartbeat request metrics. Returns an {@link AutoCloseable} that will decrement the + * metric after the call is finished. + */ + public AutoCloseable trackHeartbeats(int numHeartbeats) { + getDataMetrics + .activeHeartbeats() + .getAndUpdate(currentActiveHeartbeats -> currentActiveHeartbeats + numHeartbeats); + return () -> + getDataMetrics.activeHeartbeats().getAndUpdate(existing -> existing - numHeartbeats); + } + + public void printHtml(PrintWriter writer) { + writer.println("Active Fetches:"); + getDataMetrics.printMetrics(writer); + } + + @VisibleForTesting + GetDataMetrics.ReadOnlySnapshot getMetricsSnapshot() { + return getDataMetrics.snapshot(); + } + + public enum Type { + STATE("GetStateData"), + SIDE_INPUT("GetSideInputData"), + HEARTBEAT("RefreshActiveWork"); + private final String debugName; + + Type(String debugName) { + this.debugName = debugName; + } + + public final String debugName() { + return debugName; + } + } + + @AutoValue + abstract static class GetDataMetrics { Review Comment: removed -- 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