-
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,23 +17,30 @@ | |
| package org.apache.kafka.streams.kstream.internals; | ||
|
|
||
| import org.apache.kafka.common.utils.Bytes; | ||
| import org.apache.kafka.common.utils.Time; | ||
| import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier; | ||
| import org.apache.kafka.streams.state.KeyValueStore; | ||
| import org.apache.kafka.streams.state.StoreBuilder; | ||
| import org.apache.kafka.streams.state.Stores; | ||
| import org.apache.kafka.streams.state.TimestampedKeyValueStore; | ||
| import org.apache.kafka.streams.state.VersionedBytesStoreSupplier; | ||
| import org.apache.kafka.streams.state.internals.TimestampedKeyValueStoreBuilder; | ||
| import org.apache.kafka.streams.state.internals.VersionedKeyValueStoreBuilder; | ||
|
|
||
| public class TimestampedKeyValueStoreMaterializer<K, V> { | ||
| /** | ||
| * Materializes a key-value store as either a {@link TimestampedKeyValueStoreBuilder} or a | ||
| * {@link VersionedKeyValueStoreBuilder} depending on whether the store is versioned or not. | ||
| */ | ||
| public class KeyValueStoreMaterializer<K, V> { | ||
| private final MaterializedInternal<K, V, KeyValueStore<Bytes, byte[]>> materialized; | ||
|
|
||
| public TimestampedKeyValueStoreMaterializer(final MaterializedInternal<K, V, KeyValueStore<Bytes, byte[]>> materialized) { | ||
| public KeyValueStoreMaterializer(final MaterializedInternal<K, V, KeyValueStore<Bytes, byte[]>> materialized) { | ||
| this.materialized = materialized; | ||
| } | ||
|
|
||
| /** | ||
| * @return StoreBuilder | ||
| */ | ||
| public StoreBuilder<TimestampedKeyValueStore<K, V>> materialize() { | ||
| public StoreBuilder<?> materialize() { | ||
| KeyValueBytesStoreSupplier supplier = (KeyValueBytesStoreSupplier) materialized.storeSupplier(); | ||
| if (supplier == null) { | ||
| switch (materialized.storeType()) { | ||
|
|
@@ -48,20 +55,30 @@ public StoreBuilder<TimestampedKeyValueStore<K, V>> materialize() { | |
| } | ||
| } | ||
|
|
||
| final StoreBuilder<TimestampedKeyValueStore<K, V>> builder = Stores.timestampedKeyValueStoreBuilder( | ||
| supplier, | ||
| materialized.keySerde(), | ||
| materialized.valueSerde()); | ||
| final StoreBuilder<?> builder; | ||
| if (supplier instanceof VersionedBytesStoreSupplier) { | ||
| builder = new VersionedKeyValueStoreBuilder<>( | ||
| (VersionedBytesStoreSupplier) supplier, | ||
| materialized.keySerde(), | ||
| materialized.valueSerde(), | ||
| Time.SYSTEM); | ||
| } else { | ||
| builder = Stores.timestampedKeyValueStoreBuilder( | ||
| supplier, | ||
| materialized.keySerde(), | ||
| materialized.valueSerde()); | ||
| } | ||
|
|
||
| if (materialized.loggingEnabled()) { | ||
| builder.withLoggingEnabled(materialized.logConfig()); | ||
| } else { | ||
| builder.withLoggingDisabled(); | ||
| } | ||
|
|
||
| if (materialized.cachingEnabled()) { | ||
| // versioned stores do not support caching | ||
|
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. Regardless of whether the I think the most we should do is log a warning. Curious to hear other opinions.
Member
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. Don't have a strong opinion. Pinging @guozhangwang and @vvcephei for input.
Member
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. I guess it can't hurt to log at INFO/WARN level about it?
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. OK, I've added an INFO log statement for now, while we wait for additional opinions.
Contributor
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. +1 on an INFO log (or no log at all). Caching is non-functional behavior, so it makes sense for a store that doesn't support it (yet) just to treat it as a request and ignore it. |
||
| if (materialized.cachingEnabled() && !(builder instanceof VersionedKeyValueStoreBuilder)) { | ||
| builder.withCachingEnabled(); | ||
| } | ||
| return builder; | ||
| } | ||
| } | ||
| } | ||
| 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> { | ||
|
|
||
| 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); | ||
| } | ||
| } |
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.