Repository navigation
[Flink] Throttle Flink metric updates from source readers #40610
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+157
−4
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
...s/flink/src/test/java/org/apache/beam/runners/flink/metrics/ReaderInvocationUtilTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| /* | ||
| * 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.flink.metrics; | ||
|
|
||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.never; | ||
| import static org.mockito.Mockito.times; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.atomic.AtomicLong; | ||
| import org.apache.beam.runners.core.metrics.MetricsContainerImpl; | ||
| import org.apache.beam.runners.flink.FlinkPipelineOptions; | ||
| import org.apache.beam.sdk.io.Source; | ||
| import org.apache.beam.sdk.options.PipelineOptionsFactory; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
|
|
||
| /** Tests {@link ReaderInvocationUtil}. */ | ||
| public class ReaderInvocationUtilTest { | ||
|
|
||
| private static final String STEP = "step"; | ||
| private static final long INTERVAL_NANOS = | ||
| TimeUnit.MILLISECONDS.toNanos(ReaderInvocationUtil.FLINK_METRICS_UPDATE_INTERVAL_MILLIS); | ||
|
|
||
| private final AtomicLong clock = new AtomicLong(); | ||
| private FlinkMetricContainerBase container; | ||
| private Source.Reader<String> reader; | ||
|
|
||
| @Before | ||
| @SuppressWarnings("unchecked") | ||
| public void setUp() { | ||
| container = mock(FlinkMetricContainerBase.class); | ||
| when(container.getMetricsContainer(STEP)).thenReturn(new MetricsContainerImpl(STEP)); | ||
| reader = mock(Source.Reader.class); | ||
| } | ||
|
|
||
| private ReaderInvocationUtil<String, Source.Reader<String>> invoker(boolean disableMetrics) { | ||
| FlinkPipelineOptions options = PipelineOptionsFactory.as(FlinkPipelineOptions.class); | ||
| options.setDisableMetrics(disableMetrics); | ||
| return new ReaderInvocationUtil<>(STEP, options, container, clock::get); | ||
| } | ||
|
|
||
| @Test | ||
| public void updatesFlinkMetricsAtMostOncePerInterval() throws IOException { | ||
| when(reader.advance()).thenReturn(true); | ||
| ReaderInvocationUtil<String, Source.Reader<String>> invoker = invoker(false); | ||
|
|
||
| // The first call registers the metrics in Flink. | ||
| invoker.invokeAdvance(reader); | ||
| verify(container, times(1)).updateMetrics(STEP); | ||
|
|
||
| for (int i = 0; i < 1000; i++) { | ||
| invoker.invokeAdvance(reader); | ||
| } | ||
| verify(container, times(1)).updateMetrics(STEP); | ||
|
|
||
| clock.addAndGet(INTERVAL_NANOS); | ||
| invoker.invokeAdvance(reader); | ||
| invoker.invokeAdvance(reader); | ||
| verify(container, times(2)).updateMetrics(STEP); | ||
|
|
||
| clock.addAndGet(INTERVAL_NANOS); | ||
| invoker.invokeAdvance(reader); | ||
| verify(container, times(3)).updateMetrics(STEP); | ||
| } | ||
|
|
||
| @Test | ||
| public void updatesFlinkMetricsWhenTheReaderHasNoData() throws IOException { | ||
| when(reader.start()).thenReturn(true); | ||
| when(reader.advance()).thenReturn(true, true, false); | ||
| ReaderInvocationUtil<String, Source.Reader<String>> invoker = invoker(false); | ||
|
|
||
| invoker.invokeStart(reader); | ||
| verify(container, times(1)).updateMetrics(STEP); | ||
|
|
||
| invoker.invokeAdvance(reader); | ||
| invoker.invokeAdvance(reader); | ||
| verify(container, times(1)).updateMetrics(STEP); | ||
|
|
||
| invoker.invokeAdvance(reader); | ||
| verify(container, times(2)).updateMetrics(STEP); | ||
| } | ||
|
|
||
| @Test | ||
| public void doesNotUpdateFlinkMetricsWhenDisabled() throws IOException { | ||
| when(reader.start()).thenReturn(false); | ||
| when(reader.advance()).thenReturn(false); | ||
| ReaderInvocationUtil<String, Source.Reader<String>> invoker = invoker(true); | ||
|
|
||
| invoker.invokeStart(reader); | ||
| clock.addAndGet(INTERVAL_NANOS); | ||
| invoker.invokeAdvance(reader); | ||
|
|
||
| verify(container, never()).updateMetrics(STEP); | ||
| verify(container, never()).getMetricsContainer(STEP); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟠 High: For
UnboundedSource.UnboundedReader(used inFlinkUnboundedSourceReaderandUnboundedSourceWrapper),start()/advance()returningfalsemeans no record is currently available rather than EOF.Because both
FlinkUnboundedSourceReader#nextReaderWithDataandUnboundedSourceWrapper#runshare a singleReaderInvocationUtilinstance across all splits assigned to the subtask and poll them round-robin, if even one split is idle (or is an already-exhaustedBoundedToUnboundedSourceAdaptersplit inFlinkUnboundedSourceReader), polling that split returnsfalsebefore every record read from an active split. With|| !dataAvailable, this bypasses the 1-second throttle and callscontainer.updateMetrics(stepName)on every single record (and every 50ms per split when completely idle).Consider either restricting the
!dataAvailableflush toBoundedSource.BoundedReader(e.g., passingreader instanceof BoundedSource.BoundedReaderor checking it inmaybeUpdateMetrics), or flushing final metrics explicitly when the source reader/operator closes.