-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-14491: [13/N] Add versioned store builder and materializer #13274
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,8 @@ | |
| import org.apache.kafka.streams.state.TimestampedKeyValueStore; | ||
| import org.apache.kafka.streams.state.TimestampedWindowStore; | ||
| import org.apache.kafka.streams.state.ValueAndTimestamp; | ||
| import org.apache.kafka.streams.state.VersionedKeyValueStore; | ||
| import org.apache.kafka.streams.state.VersionedRecord; | ||
| import org.apache.kafka.streams.state.WindowStore; | ||
| import org.apache.kafka.streams.state.WindowStoreIterator; | ||
| import org.apache.kafka.streams.state.internals.WrappedStateStore; | ||
|
|
@@ -68,6 +70,8 @@ public void close() { | |
| static StateStore getReadOnlyStore(final StateStore global) { | ||
| if (global instanceof TimestampedKeyValueStore) { | ||
| return new TimestampedKeyValueStoreReadOnlyDecorator<>((TimestampedKeyValueStore<?, ?>) global); | ||
| } else if (global instanceof VersionedKeyValueStore) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This addition, and the analogous one in |
||
| return new VersionedKeyValueStoreReadOnlyDecorator<>((VersionedKeyValueStore<?, ?>) global); | ||
| } else if (global instanceof KeyValueStore) { | ||
| return new KeyValueStoreReadOnlyDecorator<>((KeyValueStore<?, ?>) global); | ||
| } else if (global instanceof TimestampedWindowStore) { | ||
|
|
@@ -159,6 +163,35 @@ private TimestampedKeyValueStoreReadOnlyDecorator(final TimestampedKeyValueStore | |
| } | ||
| } | ||
|
|
||
| static class VersionedKeyValueStoreReadOnlyDecorator<K, V> | ||
| extends AbstractReadOnlyDecorator<VersionedKeyValueStore<K, V>, K, V> | ||
| implements VersionedKeyValueStore<K, V> { | ||
|
|
||
| private VersionedKeyValueStoreReadOnlyDecorator(final VersionedKeyValueStore<K, V> inner) { | ||
| super(inner); | ||
| } | ||
|
|
||
| @Override | ||
| public void put(final K key, final V value, final long timestamp) { | ||
| throw new UnsupportedOperationException(ERROR_MESSAGE); | ||
| } | ||
|
|
||
| @Override | ||
| public VersionedRecord<V> delete(final K key, final long timestamp) { | ||
| throw new UnsupportedOperationException(ERROR_MESSAGE); | ||
| } | ||
|
|
||
| @Override | ||
| public VersionedRecord<V> get(final K key) { | ||
| return wrapped().get(key); | ||
| } | ||
|
|
||
| @Override | ||
| public VersionedRecord<V> get(final K key, final long asOfTimestamp) { | ||
| return wrapped().get(key, asOfTimestamp); | ||
| } | ||
| } | ||
|
|
||
| static class WindowStoreReadOnlyDecorator<K, V> | ||
| extends AbstractReadOnlyDecorator<WindowStore<K, V>, K, V> | ||
| implements WindowStore<K, V> { | ||
|
|
||
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
80 changes: 80 additions & 0 deletions
80
...src/main/java/org/apache/kafka/streams/state/internals/VersionedKeyValueStoreBuilder.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,80 @@ | ||
| /* | ||
| * 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.serialization.Serde; | ||
| import org.apache.kafka.common.utils.Bytes; | ||
| import org.apache.kafka.common.utils.Time; | ||
| import org.apache.kafka.streams.state.KeyValueStore; | ||
|
|
||
| import java.util.Objects; | ||
| import org.apache.kafka.streams.state.StoreBuilder; | ||
| import org.apache.kafka.streams.state.ValueAndTimestamp; | ||
| import org.apache.kafka.streams.state.VersionedBytesStore; | ||
| import org.apache.kafka.streams.state.VersionedBytesStoreSupplier; | ||
| import org.apache.kafka.streams.state.VersionedKeyValueStore; | ||
|
|
||
| public class VersionedKeyValueStoreBuilder<K, V> | ||
| extends AbstractStoreBuilder<K, V, VersionedKeyValueStore<K, V>> { | ||
|
|
||
| private final VersionedBytesStoreSupplier storeSupplier; | ||
|
|
||
| public VersionedKeyValueStoreBuilder(final VersionedBytesStoreSupplier storeSupplier, | ||
| final Serde<K> keySerde, | ||
| final Serde<V> valueSerde, | ||
| final Time time) { | ||
| super( | ||
| storeSupplier.name(), | ||
| keySerde, | ||
| valueSerde, | ||
| time); | ||
| Objects.requireNonNull(storeSupplier, "storeSupplier can't be null"); | ||
| Objects.requireNonNull(storeSupplier.metricsScope(), "storeSupplier's metricsScope can't be null"); | ||
| this.storeSupplier = storeSupplier; | ||
| } | ||
|
|
||
| @Override | ||
| public VersionedKeyValueStore<K, V> build() { | ||
| final KeyValueStore<Bytes, byte[]> store = storeSupplier.get(); | ||
| if (!(store instanceof VersionedBytesStore)) { | ||
| throw new IllegalStateException("VersionedBytesStoreSupplier.get() must return an instance of VersionedBytesStore"); | ||
| } | ||
|
|
||
| final Serde<ValueAndTimestamp<V>> valueAndTimestampSerde = valueSerde == null | ||
| ? null | ||
| : new NullableValueAndTimestampSerde<>(valueSerde); | ||
|
|
||
| return new MeteredVersionedKeyValueStore<>( | ||
| maybeWrapLogging((VersionedBytesStore) store), // no caching layer for versioned stores | ||
| storeSupplier.metricsScope(), | ||
| time, | ||
| keySerde, | ||
| valueAndTimestampSerde); | ||
| } | ||
|
|
||
| @Override | ||
| public StoreBuilder<VersionedKeyValueStore<K, V>> withCachingEnabled() { | ||
| throw new IllegalStateException("Versioned stores do not support caching"); | ||
| } | ||
|
|
||
| private VersionedBytesStore maybeWrapLogging(final VersionedBytesStore inner) { | ||
| if (!enableLogging) { | ||
| return inner; | ||
| } | ||
| return new ChangeLoggingVersionedKeyValueBytesStore(inner); | ||
| } | ||
| } |
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.
While renaming this class in IntelliJ, I got a warning that a class with this name existed in a previous version of Kafka Streams. I think this is fine since it's an internal class, but wanted to call it out in case there are compatibility concerns I'm not aware of.