-
Notifications
You must be signed in to change notification settings - Fork 15.4k
MINOR: De-duplicate the metering lifecycle across Metered*WithHeaders iterators #22975
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
bbejeck
merged 7 commits into
apache:trunk
from
Jess668:dedup-metered-withheaders-readonly-iterators
Aug 11, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
585fc82
De-duplicate Metered*WithHeaders read-only-record iterators
Jess668 151d3a7
MINOR: address review — make metering base lifecycle-only, add close-…
Jess668 2edf8f2
MINOR: address review round 2 — final startTimestamp, two-sample dura…
Jess668 228b4dc
MINOR: also two-sample the pre-existing session shouldTimeIteratorDur…
Jess668 b16f780
MINOR: address review round 3 — pin operation-sensor latency, final c…
Jess668 ad694cf
trigger ci
Jess668 22e9220
trigger ci
Jess668 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
103 changes: 103 additions & 0 deletions
103
streams/src/main/java/org/apache/kafka/streams/state/internals/AbstractMeteredIterator.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,103 @@ | ||
| /* | ||
| * 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.kafka.streams.state.internals; | ||
|
|
||
| import org.apache.kafka.common.metrics.Sensor; | ||
| import org.apache.kafka.common.utils.Time; | ||
| import org.apache.kafka.streams.state.KeyValueIterator; | ||
|
|
||
| import java.util.Set; | ||
| import java.util.concurrent.atomic.LongAdder; | ||
|
|
||
| /** | ||
| * Shared metering lifecycle for the metered iterators of the {@code Metered*WithHeaders} stores, | ||
| * whatever result type they yield: the {@code KeyValueIterator}s returned by the store's own range/ | ||
| * fetch/find methods and the {@code ReadOnlyRecordIterator}s that back the headers-aware IQv2 | ||
| * range/window/session query types. | ||
| * | ||
| * <p>Every such iterator opens over a raw {@code KeyValueIterator<RawKey, byte[]>} and needs the | ||
| * same bookkeeping: stamp the open time (for the {@code oldest-iterator-open-since-ms} metric), | ||
| * register in {@code numOpenIterators}/{@code openIterators}, and on {@link #close()} record the | ||
| * operation and iterator-duration sensors and deregister. This base is deliberately result-type | ||
| * agnostic -- it implements only {@link MeteredIterator} and does not bind the yielded key/value | ||
| * types -- so each subclass declares its own result interface (a {@code KeyValueIterator} or a | ||
| * {@code ReadOnlyRecordIterator}) and implements just the parts that genuinely differ: the | ||
| * deserializing {@code next()} (and, for the {@code KeyValueIterator}s, a peeking {@code hasNext()} | ||
| * and {@code peekNextKey()}). | ||
| * | ||
| * @param <RawKey> the raw iterator's key type | ||
| */ | ||
| abstract class AbstractMeteredIterator<RawKey> implements MeteredIterator { | ||
|
Jess668 marked this conversation as resolved.
|
||
|
|
||
| final KeyValueIterator<RawKey, byte[]> iter; | ||
| private final Sensor operationSensor; | ||
| private final Sensor iteratorSensor; | ||
| private final Time time; | ||
| private final LongAdder numOpenIterators; | ||
| private final Set<MeteredIterator> openIterators; | ||
| private final long startNs; | ||
| private final long startTimestampMs; | ||
|
|
||
| AbstractMeteredIterator(final KeyValueIterator<RawKey, byte[]> iter, | ||
| final Sensor operationSensor, | ||
| final Sensor iteratorSensor, | ||
| final Time time, | ||
| final LongAdder numOpenIterators, | ||
| final Set<MeteredIterator> openIterators) { | ||
| this.iter = iter; | ||
| this.operationSensor = operationSensor; | ||
| this.iteratorSensor = iteratorSensor; | ||
| this.time = time; | ||
| this.numOpenIterators = numOpenIterators; | ||
| this.openIterators = openIterators; | ||
| this.startNs = time.nanoseconds(); | ||
| this.startTimestampMs = time.milliseconds(); | ||
| numOpenIterators.increment(); | ||
| openIterators.add(this); | ||
| } | ||
|
|
||
| // Final: the constructor's openIterators.add(this) sorts through this via the set's | ||
| // startTimestamp comparator, i.e. on a not-yet-fully-constructed object. Keeping it final stops a | ||
| // subclass from overriding it with something that reads its own not-yet-assigned state. | ||
| @Override | ||
| public final long startTimestamp() { | ||
| return startTimestampMs; | ||
| } | ||
|
|
||
| /** | ||
| * Delegates to the raw iterator. Subclasses that buffer a peeked element (the | ||
| * {@code KeyValueIterator}s) override this to also account for the buffered element. | ||
| */ | ||
| public boolean hasNext() { | ||
| return iter.hasNext(); | ||
| } | ||
|
|
||
| // Final: this owns the metering lifecycle (sensor recording, numOpenIterators decrement, | ||
| // openIterators deregistration). A subclass that overrode it and forgot super.close() would | ||
| // silently drop the decrement and deregistration. Subclasses vary only in next()/hasNext(). | ||
| public final void close() { | ||
| try { | ||
| iter.close(); | ||
| } finally { | ||
| final long duration = time.nanoseconds() - startNs; | ||
| operationSensor.record(duration); | ||
| iteratorSensor.record(duration); | ||
|
Jess668 marked this conversation as resolved.
|
||
| numOpenIterators.decrement(); | ||
| openIterators.remove(this); | ||
|
Jess668 marked this conversation as resolved.
|
||
| } | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.
MeteredWindowedKeyValueIterator,MeteredWindowStoreIteratorandMeteredKeyValueStoreIteratorstill hand-roll this exact lifecycle, field for field. The first is the base ofMeteredWindowedKeyValueWithHeadersIterator, so oneMetered*WithHeadersiterator is still left out. Should we do a follow-up making them extend this class, after which the javadoc'sMetered*WithHeadersscoping can go.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.
agreed, will do the migration in a follow up to keep this PR scoped to the
*WithHeadersextraction