Repository navigation
feat(flink): report read lag and pending work from the Source V2 split enumerator #20735
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
Open
skywalker0618
wants to merge
7
commits into
apache:master
from
skywalker0618:shihuanl/flink-source-v2-read-lag-metrics
+1,495
−25
Open
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
08f8800
feat(flink): report read lag and pending work from the Source V2 spli…
skywalker0618 29e584d
fix(flink): address review comments on the Source V2 read lag metrics
skywalker0618 9bd2d73
fix(flink): refresh undiscovered commits async and group shared range…
skywalker0618 46d234d
Merge remote-tracking branch 'upstream/master' into shihuanl/flink-so…
skywalker0618 2e37915
fix(flink): address follow-up review comments on Source V2 read lag m…
skywalker0618 23df152
refactor(flink): focus source lag metrics on read lag and pending splits
danny0405 2835f20
refactor(flink): extract split read lag lookups from discovery
danny0405 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
242 changes: 242 additions & 0 deletions
242
...atasource/hudi-flink/src/main/java/org/apache/hudi/metrics/FlinkStreamReadLagMetrics.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,242 @@ | ||
| /* | ||
| * 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.hudi.metrics; | ||
|
|
||
| import org.apache.hudi.common.util.VisibleForTesting; | ||
| import org.apache.hudi.source.split.HoodieSourceSplitLagInfo; | ||
|
|
||
| import org.apache.flink.metrics.MetricGroup; | ||
|
|
||
| import javax.annotation.Nullable; | ||
|
|
||
| import java.time.Clock; | ||
| import java.time.Duration; | ||
| import java.time.Instant; | ||
| import java.util.Collection; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.IdentityHashMap; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.TreeMap; | ||
|
|
||
| /** | ||
| * Metrics of how far the streaming read lags behind the commits of the table, tracked by the split | ||
| * enumerator. | ||
| * | ||
| * <p>A commit is pending until every split that reads it is finished: either it is not discovered yet, | ||
| * or some of its splits are queued or being read. The lag is computed when the gauge is read, so it keeps growing while the readers stall. The methods are | ||
| * called from the coordinator thread and the gauges are read by the metric reporter thread. | ||
| * | ||
| * <p>Splits that the readers restore from their own state are not known to the enumerator, so they are | ||
| * not counted until they are added back. | ||
| */ | ||
| public class FlinkStreamReadLagMetrics extends HoodieFlinkMetrics { | ||
|
|
||
| private final Clock clock; | ||
|
|
||
| /** | ||
| * Lag info of the discovered splits that are not finished yet, keyed by split key. | ||
| */ | ||
| private final Map<String, HoodieSourceSplitLagInfo> pendingSplits = new HashMap<>(); | ||
|
|
||
| /** | ||
| * Number of pending splits that read each commit, keyed by instant time. | ||
| */ | ||
| private final Map<String, Integer> inFlightCommits = new HashMap<>(); | ||
|
|
||
| /** | ||
| * Number of pending splits by the completion time of the oldest commit they read. | ||
| */ | ||
| private final TreeMap<Instant, Integer> oldestCompletionTimes = new TreeMap<>(); | ||
|
|
||
| private int undiscoveredCommits; | ||
|
|
||
| /** | ||
| * Completion time of the oldest undiscovered commit, null if there is none. | ||
| */ | ||
| @Nullable | ||
| private Instant oldestUndiscoveredCompletionTime; | ||
|
|
||
| private long pendingRecords; | ||
|
|
||
| private long pendingBytes; | ||
|
|
||
| public FlinkStreamReadLagMetrics(MetricGroup metricGroup, String tableName) { | ||
| this(metricGroup, tableName, Clock.systemUTC()); | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| FlinkStreamReadLagMetrics(MetricGroup metricGroup, String tableName, Clock clock) { | ||
| super(metricGroup.addGroup("table", tableName)); | ||
| this.clock = clock; | ||
| } | ||
|
|
||
| @Override | ||
| public void registerMetrics() { | ||
| metricGroup.gauge("readLagSeconds", this::getReadLagSeconds); | ||
| metricGroup.gauge("pendingCommits", this::getPendingCommits); | ||
| metricGroup.gauge("undiscoveredCommits", this::getUndiscoveredCommits); | ||
| metricGroup.gauge("inFlightCommits", this::getInFlightCommits); | ||
| metricGroup.gauge("pendingSplits", this::getPendingSplits); | ||
| metricGroup.gauge("pendingRecords", this::getPendingRecords); | ||
| metricGroup.gauge("pendingBytes", this::getPendingBytes); | ||
| } | ||
|
|
||
| /** | ||
| * Starts tracking the splits that were discovered, restored or added back, keyed by split key. | ||
| * Splits that are already tracked are ignored. | ||
| * | ||
| * @return the keys of the splits that were newly added | ||
| */ | ||
| public synchronized Set<String> addSplits(Map<String, HoodieSourceSplitLagInfo> lagInfos) { | ||
| Set<String> addedKeys = new HashSet<>(); | ||
| Map<Collection<String>, Integer> commitRangeCounts = new IdentityHashMap<>(); | ||
| Map<Instant, Integer> completionTimeCounts = new HashMap<>(); | ||
| lagInfos.forEach((splitKey, lagInfo) -> { | ||
| if (pendingSplits.putIfAbsent(splitKey, lagInfo) == null) { | ||
| addedKeys.add(splitKey); | ||
| commitRangeCounts.merge(lagInfo.getReadCommits(), 1, Integer::sum); | ||
| if (lagInfo.getOldestCommitCompletionTime() != null) { | ||
| completionTimeCounts.merge(lagInfo.getOldestCommitCompletionTime(), 1, Integer::sum); | ||
| } | ||
| pendingRecords += lagInfo.getEstimatedRecords(); | ||
| pendingBytes += lagInfo.getEstimatedBytes(); | ||
| } | ||
| }); | ||
| addCounts(commitRangeCounts, completionTimeCounts); | ||
| return addedKeys; | ||
| } | ||
|
|
||
| /** | ||
| * Replaces the lag info of the splits that are still tracked, keyed by split key. | ||
| */ | ||
| public synchronized void updateSplits(Map<String, HoodieSourceSplitLagInfo> lagInfos) { | ||
| Map<Collection<String>, Integer> removedCommitRanges = new IdentityHashMap<>(); | ||
| Map<Instant, Integer> removedCompletionTimes = new HashMap<>(); | ||
| Map<Collection<String>, Integer> addedCommitRanges = new IdentityHashMap<>(); | ||
| Map<Instant, Integer> addedCompletionTimes = new HashMap<>(); | ||
| lagInfos.forEach((splitKey, lagInfo) -> { | ||
| HoodieSourceSplitLagInfo previous = pendingSplits.get(splitKey); | ||
| if (previous != null) { | ||
| removedCommitRanges.merge(previous.getReadCommits(), 1, Integer::sum); | ||
| if (previous.getOldestCommitCompletionTime() != null) { | ||
| removedCompletionTimes.merge(previous.getOldestCommitCompletionTime(), 1, Integer::sum); | ||
| } | ||
| pendingRecords -= previous.getEstimatedRecords(); | ||
| pendingBytes -= previous.getEstimatedBytes(); | ||
|
|
||
| pendingSplits.put(splitKey, lagInfo); | ||
| addedCommitRanges.merge(lagInfo.getReadCommits(), 1, Integer::sum); | ||
| if (lagInfo.getOldestCommitCompletionTime() != null) { | ||
| addedCompletionTimes.merge(lagInfo.getOldestCommitCompletionTime(), 1, Integer::sum); | ||
| } | ||
| pendingRecords += lagInfo.getEstimatedRecords(); | ||
| pendingBytes += lagInfo.getEstimatedBytes(); | ||
| } | ||
| }); | ||
| removeCounts(removedCommitRanges, removedCompletionTimes); | ||
| addCounts(addedCommitRanges, addedCompletionTimes); | ||
| } | ||
|
|
||
| /** | ||
| * Stops tracking the splits whose records were all emitted. Splits that are not tracked are ignored. | ||
| */ | ||
| public synchronized void finishSplits(Collection<String> splitKeys) { | ||
| Map<Collection<String>, Integer> removedCommitRanges = new IdentityHashMap<>(); | ||
| Map<Instant, Integer> removedCompletionTimes = new HashMap<>(); | ||
| for (String splitKey : splitKeys) { | ||
| HoodieSourceSplitLagInfo lagInfo = pendingSplits.remove(splitKey); | ||
| if (lagInfo != null) { | ||
| removedCommitRanges.merge(lagInfo.getReadCommits(), 1, Integer::sum); | ||
| if (lagInfo.getOldestCommitCompletionTime() != null) { | ||
| removedCompletionTimes.merge(lagInfo.getOldestCommitCompletionTime(), 1, Integer::sum); | ||
| } | ||
| pendingRecords -= lagInfo.getEstimatedRecords(); | ||
| pendingBytes -= lagInfo.getEstimatedBytes(); | ||
| } | ||
| } | ||
| removeCounts(removedCommitRanges, removedCompletionTimes); | ||
| } | ||
|
|
||
| /** | ||
| * Sets the number of completed commits that are not discovered yet and the completion time of the | ||
| * oldest one. | ||
| */ | ||
| public synchronized void setUndiscoveredCommits(int count, @Nullable Instant oldestCompletionTime) { | ||
| this.undiscoveredCommits = count; | ||
| this.oldestUndiscoveredCompletionTime = count > 0 ? oldestCompletionTime : null; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the seconds since the oldest pending commit completed, 0 if no commit is pending. | ||
| */ | ||
| private synchronized long getReadLagSeconds() { | ||
| Instant oldest = oldestUndiscoveredCompletionTime; | ||
| if (!oldestCompletionTimes.isEmpty() && (oldest == null || oldestCompletionTimes.firstKey().isBefore(oldest))) { | ||
| oldest = oldestCompletionTimes.firstKey(); | ||
| } | ||
| if (oldest == null) { | ||
| return 0L; | ||
| } | ||
| return Duration.between(oldest, clock.instant()).getSeconds(); | ||
| } | ||
|
|
||
| private synchronized long getPendingCommits() { | ||
| return (long) undiscoveredCommits + inFlightCommits.size(); | ||
| } | ||
|
|
||
| private synchronized long getUndiscoveredCommits() { | ||
| return undiscoveredCommits; | ||
| } | ||
|
|
||
| private synchronized long getInFlightCommits() { | ||
| return inFlightCommits.size(); | ||
| } | ||
|
|
||
| private synchronized long getPendingSplits() { | ||
| return pendingSplits.size(); | ||
| } | ||
|
|
||
| private synchronized long getPendingRecords() { | ||
| return pendingRecords; | ||
| } | ||
|
|
||
| private synchronized long getPendingBytes() { | ||
| return pendingBytes; | ||
| } | ||
|
|
||
| private void addCounts(Map<Collection<String>, Integer> commitRangeCounts, Map<Instant, Integer> completionTimeCounts) { | ||
| commitRangeCounts.forEach((commits, count) -> | ||
| commits.forEach(commit -> inFlightCommits.merge(commit, count, Integer::sum))); | ||
| completionTimeCounts.forEach((completionTime, count) -> | ||
| oldestCompletionTimes.merge(completionTime, count, Integer::sum)); | ||
| } | ||
|
|
||
| private void removeCounts(Map<Collection<String>, Integer> commitRangeCounts, Map<Instant, Integer> completionTimeCounts) { | ||
| commitRangeCounts.forEach((commits, count) -> | ||
| commits.forEach(commit -> decrement(inFlightCommits, commit, count))); | ||
| completionTimeCounts.forEach((completionTime, count) -> | ||
| decrement(oldestCompletionTimes, completionTime, count)); | ||
| } | ||
|
|
||
| private static <K> void decrement(Map<K, Integer> counts, K key, int delta) { | ||
| counts.computeIfPresent(key, (k, count) -> count > delta ? count - delta : null); | ||
| } | ||
| } | ||
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
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.
Small performance point:
addedKeyscollects every new split on the coordinator thread, but onlyonSplitsAddedBackconsumes the returned keys. Ordinary discovery and checkpoint restore discard the set. Could we collect those keys only for the add-back path? That saves a temporary hash entry per split in the largest batches, before they are published to the assigner.