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


##########
runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/windmill/client/getdata/ThrottlingGetDataMetricTracker.java:
##########
@@ -0,0 +1,149 @@
+/*
+ * 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);
+    // Active heartbeats should never drop below 0.
+    return () ->
+        getDataMetrics
+            .activeHeartbeats()
+            .getAndUpdate(existing -> Math.max(existing - numHeartbeats, 0));

Review Comment:
   done



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