rkhachatryan commented on a change in pull request #14507: URL: https://github.com/apache/flink/pull/14507#discussion_r550185523
########## File path: flink-metrics/flink-metrics-core/src/main/java/org/apache/flink/metrics/EMAGaugeSampler.java ########## @@ -0,0 +1,53 @@ +/* + * 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.flink.metrics; + +import javax.annotation.Nullable; + +/** + * A {@link EMAGaugeSampler} provides an exponential moving average view over a {@link Gauge}. + * + * <p>As other {@link View}s, advantage of this class is that it is updated in regular intervals by + * a background thread. + */ +public class EMAGaugeSampler<T extends Number> implements Gauge<Double>, View { + + /** The underlying sampled metric. */ + private final Gauge<T> source; + /** Alpha parameter of the exponential moving average. */ + private final double alpha; + /** The index in the array for the current time. */ + @Nullable + private double currentAverage; + + public EMAGaugeSampler(Gauge<T> source, double alpha) { + this.source = source; + this.alpha = alpha; + } + + @Override + public void update() { + currentAverage = currentAverage * alpha + source.getValue().doubleValue() * (1.0 - alpha); Review comment: 1. How about using only `source.getValue().doubleValue()` for the first update? Otherwise, the value can be too small in the beginning. 2. Calling only `source.getValue` inside **this** gauge seems error-prone: it assumes `source.update()` is already called. So I'd replace the nested gauge with a function. ########## File path: flink-metrics/flink-metrics-core/src/main/java/org/apache/flink/metrics/EMAGaugeSampler.java ########## @@ -0,0 +1,53 @@ +/* + * 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.flink.metrics; + +import javax.annotation.Nullable; + +/** + * A {@link EMAGaugeSampler} provides an exponential moving average view over a {@link Gauge}. + * + * <p>As other {@link View}s, advantage of this class is that it is updated in regular intervals by + * a background thread. + */ +public class EMAGaugeSampler<T extends Number> implements Gauge<Double>, View { Review comment: nit: `EmaGaugeSampler` (camel case is easier to read) ########## File path: flink-runtime/src/test/java/org/apache/flink/runtime/taskmanager/TaskTest.java ########## @@ -989,6 +990,62 @@ public void testNoBackPressureIfTaskNotStarted() throws Exception { assertFalse(task.isBackPressured()); } + @Test + public void testIsCausingBackPressure() throws Exception { Review comment: nit: `testIsCausingBackPressureMetric` ########## File path: flink-runtime/src/main/java/org/apache/flink/runtime/taskexecutor/TaskExecutor.java ########## @@ -696,7 +697,24 @@ private void stopTaskExecutorServices() throws Exception { partitionStateChecker, getRpcService().getExecutor()); - taskMetricGroup.gauge(MetricNames.IS_BACKPRESSURED, task::isBackPressured); + taskMetricGroup.gauge(MetricNames.IS_BACK_PRESSURED, task::isBackPressured); Review comment: `isBackPressured` is used more often now which might lead to many `volatile` reads (`Future.isDone`). However, these reads can be eliminated by exiting the loop as soon as some future is not done (instead of `!allOf(futures).isDone`). It would also make the code more readable IMO. WDYT? ########## File path: flink-runtime/src/main/java/org/apache/flink/runtime/taskmanager/Task.java ########## @@ -519,6 +519,11 @@ public boolean isBackPressured() { return !CompletableFuture.allOf(outputFutures).isDone(); } + public boolean isCausingBackPressure() { + return !isBackPressured() + && metrics.getIOMetricGroup().getIdleTimeMsPerSecond().getRate() < 1.0; Review comment: Swap `&&` to avoid unnecessary `volatile` reads? ########## File path: flink-metrics/flink-metrics-core/src/main/java/org/apache/flink/metrics/EMAGaugeSampler.java ########## @@ -0,0 +1,53 @@ +/* + * 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.flink.metrics; + +import javax.annotation.Nullable; + +/** + * A {@link EMAGaugeSampler} provides an exponential moving average view over a {@link Gauge}. + * + * <p>As other {@link View}s, advantage of this class is that it is updated in regular intervals by + * a background thread. + */ +public class EMAGaugeSampler<T extends Number> implements Gauge<Double>, View { + + /** The underlying sampled metric. */ + private final Gauge<T> source; + /** Alpha parameter of the exponential moving average. */ + private final double alpha; + /** The index in the array for the current time. */ + @Nullable + private double currentAverage; + + public EMAGaugeSampler(Gauge<T> source, double alpha) { + this.source = source; + this.alpha = alpha; Review comment: I'd check that `alpha` is in `0..1` and (`source` is not null). ########## File path: docs/ops/metrics.md ########## @@ -1245,7 +1245,22 @@ Certain RocksDB native metrics are available but disabled by default, you can fi </tr> <tr> <td>isBackPressured</td> - <td>Whether the task is back-pressured.</td> + <td>Whether the task is currently back-pressured.</td> + <td>Gauge</td> + </tr> + <tr> + <td>isBackPressuredRatio</td> + <td>Ratio how often this task is back-pressured. Can have values between 0.0 and 1.0, where value 1.0 means the task is constantly back-pressured. It is created by sampling isBackPressured and averaging the sampled value using the exponential moving average algorithm.</td> Review comment: How about replacing "Ratio how often" with "The percentage of time" ? I think both aren't very accurate, but 2nd is easier to understand (to me). ########## File path: flink-metrics/flink-metrics-core/src/main/java/org/apache/flink/metrics/EMAGaugeSampler.java ########## @@ -0,0 +1,53 @@ +/* + * 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.flink.metrics; + +import javax.annotation.Nullable; + +/** + * A {@link EMAGaugeSampler} provides an exponential moving average view over a {@link Gauge}. + * + * <p>As other {@link View}s, advantage of this class is that it is updated in regular intervals by + * a background thread. + */ +public class EMAGaugeSampler<T extends Number> implements Gauge<Double>, View { + + /** The underlying sampled metric. */ + private final Gauge<T> source; + /** Alpha parameter of the exponential moving average. */ + private final double alpha; + /** The index in the array for the current time. */ + @Nullable + private double currentAverage; Review comment: This can't be nullable (initialize with negative?). ########## File path: flink-runtime/src/main/java/org/apache/flink/runtime/metrics/MetricNames.java ########## @@ -57,7 +57,11 @@ private MetricNames() {} public static final String MEMORY_COMMITTED = "Committed"; public static final String MEMORY_MAX = "Max"; - public static final String IS_BACKPRESSURED = "isBackPressured"; + public static final String IS_BACK_PRESSURED = "isBackPressured"; + public static final String IS_BACK_PRESSURED_RATIO = "isBackPressuredRatio"; Review comment: I see that `IS_BACK_PRESSURED_RATIO` name is derived from `IS_BACK_PRESSURED` but it still sounds strange to me. Looking at just a single metric is even more confusing. So I'd name it `BACK_PRESSURED_RATIO` (also in docs). ditto: `IS_CAUSING_BACK_PRESSURE_RATIO` ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected]
