[FLINK-40403][runtime] Pre-compile subtask-index regexes in MetricStore - #28986
Open
NeQuissimus wants to merge 1 commit into
Open
[FLINK-40403][runtime] Pre-compile subtask-index regexes in MetricStore#28986NeQuissimus wants to merge 1 commit into
NeQuissimus wants to merge 1 commit into
Conversation
MetricStore.TaskMetricStore#retainSubtasks and #isTransientMetric used String#matches(String), which recompiles the regex on every call. Both run once per metric key on every MetricFetcher refresh while the global MetricStore monitor is held (updateCurrentExecutionAttempts is synchronized), so on jobs with a large number of subtasks the per-key Pattern compilation dominates the lock hold time and starves the shared REST handler thread pool (GET /jobs/<id>, /jobs/overview, checkpoints). Hoist the two patterns to static final Pattern fields. Behaviour is unchanged: String#matches delegates to Pattern.compile(regex).matcher(s).matches(), and the regex strings are identical. Generated-by: Pi (Anthropic claude-opus-4-8) Assisted-By: devx/fb6090d2-f9b9-4a68-a423-dc3c9a177b0c
Collaborator
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What is the purpose of the change
MetricStore.TaskMetricStore#retainSubtasksand#isTransientMetricidentify theper-subtask key prefix (e.g.
"0.numRecordsIn") withString#matches(String), whichrecompiles the regex
Patternon every call. Both run once per metric key, andretainSubtasksis invoked once per vertex on everyMetricFetcherrefresh fromMetricStore#updateCurrentExecutionAttempts, which issynchronized— so theO(#metric-keys) work, including a fresh
Pattern.compileper key, executes while thesingle global
MetricStoremonitor is held.On jobs with a large number of subtasks this per-refresh compilation dominates the time
the monitor is held. Because the JobManager REST handler thread pool
(
rest.server.numThreads) is shared between theMetricFetcherand the REST endpoints,the whole pool stalls on that monitor:
GET /jobs/<jid>(which aggregates per-subtask IOmetrics via
MutableIOMetrics#addIOMetrics, taking the same monitor once per subtask)becomes very slow, and endpoints that never touch
MetricStore(/jobs/overview,/jobs/<jid>/checkpoints) queue behind the saturated pool. Observed on a ~2,233-subtaskjob (main chain at parallelism 900): a thread dump taken while
GET /jobs/<jid>hungshowed the entire REST pool contending on the
MetricStoremonitor, one thread RUNNABLEinside
java.util.regex.Pattern/String#matcheswhile holding it. CPU was otherwisenear-idle — this is lock-hold time, not compute.
Brief change log
static final Patternfields toMetricStore.TaskMetricStoreand match viamatcher(...).matches()inretainSubtasksandisTransientMetric, instead ofString#matches(String)which recompiles the pattern on every call. The regex stringsare unchanged.
Verifying this change
This change is a behaviour-preserving refactor already covered by existing tests:
MetricStoreTest#testTaskMetricStoreCleanup(drivesretainSubtasksviaupdateCurrentExecutionAttempts),#testSubtaskMetricStoreCleanup, and#testMalformedNameHandling(empty/nullmetric names).String#matches(regex)isspecified as
Pattern.compile(regex).matcher(s).matches(), so pre-compiling the identicalpatterns does not change matching behaviour.
Does this pull request potentially affect one of the following parts:
@Public(Evolving): noDocumentation
Was generative AI tooling used to co-author this PR?
Generated-by: Pi (Anthropic claude-opus-4-8)