KAFKA-14491: [12/N] Relax requirement that KTable stores must be TimestampedKVStores#13264
Conversation
| // next try versioned store | ||
| versionedStore = context.getStateStore(storeName); | ||
| } catch (final ClassCastException e) { | ||
| throw new InvalidStateStoreException("KTable source state store must implement either TimestampedKeyValueStore or VersionedKeyValueStore."); |
There was a problem hiding this comment.
Should we include the type of the store we found in the error message?
| throw new IllegalStateException("KeyValueStoreWrapper must be initialized with either timestamped or versioned store"); | ||
| } | ||
|
|
||
| public StateStore getStore() { |
There was a problem hiding this comment.
Do we really need this method? It seem whoever uses KeyValueStoreWrapper can keep a handle of the store themselves?
If we think it's convenient to keep this helper, should we add a Store store member variable, and implement this one a simple return store? (Similar for name(), init() etc below?)
If we don't get the correct stores passed into the constructor we fail anyway, and it seem a lot of unncessary boiler plate code in those "non-functional helpers"
There was a problem hiding this comment.
I prefer to keep this method because otherwise either (1) both the caller and KeyValueStoreWrapper would have to call context.getStateStore(storeName) when the caller needs the state store in addition to the wrapper, which feels redundant, or (2) if we avoid the duplication by not having the wrapper call context.getStateStore(storeName) then the caller would always have to call context.getStateStore(storeName) even when it doesn't need the state store itself (only the wrapper), which feels clunky.
If we think it's convenient to keep this helper, should we add a Store store member variable, and implement this one a simple return store?
We can do that. Inside KeyValueStoreWrapper we still have to track whether the store is a timestamped store or a versioned store, in order to use the appropriate interfaces in put() and get(). I could introduce a private enum for this purpose; feels a little overkill but I can't think of anything better. I'll go ahead and make the change.
There was a problem hiding this comment.
My proposal was actually to have three member, the previously existing KeyValueStore keyValueStore, VersionedStore versionStore and a new Store store. So we just simplify the code for the case when we don't care about the store type, but when we care we check if keyValueStore != null or versionStore != null and do the right thing.
Sorry for not explaining it good enough -- using an enum works, too, I guess, but also tend to agree it a little overkill. But you made the change already... So maybe also not worth to revert it again to sue KeyValueStore keyValueStore and VersionedStore versionStore as you did originally.
The only question (by might be pre-mature optimization): we know need to cast on very single get/put call using the enum. Sounds like more overhead that using KeyValueStore keyValueStore/VersionedStore versionStoreand check fornull` as originally?
There was a problem hiding this comment.
Yeah, your point about casting overhead is valid. I had hoped it'd be fairly minimal, but that's moot now since I went ahead and updated the PR to use your proposal (three store pointers) in response to your comment about test failures (see below).
| } | ||
|
|
||
| @Test | ||
| public void shouldGetTimestampedStore() { |
There was a problem hiding this comment.
If we add the Store store member, it seem we can cut 50% of the tests below?
There was a problem hiding this comment.
I added the Store store member which indeed simplified the code in KeyValueStoreWrapper, but have chosen to keep these tests here for coverage.
| * @return StoreBuilder | ||
| */ | ||
| public StoreBuilder<TimestampedKeyValueStore<K, V>> materialize() { | ||
| public StoreBuilder<?> materialize() { |
There was a problem hiding this comment.
Why can't we keep the generic typer here? I understand that we need to make it <?> in KTableImpl (and the "graph node" code below) but not sure why we need <?> here, too?
There was a problem hiding this comment.
I we need it later, it's fine to do it right away. Maybe a little cleaner from a "commit history" POV do only to it later.
| * @return StoreBuilder | ||
| */ | ||
| public StoreBuilder<TimestampedKeyValueStore<K, V>> materialize() { | ||
| public StoreBuilder<?> materialize() { |
| throw new IllegalStateException("KeyValueStoreWrapper must be initialized with either timestamped or versioned store"); | ||
| } | ||
|
|
||
| public StateStore getStore() { |
There was a problem hiding this comment.
I prefer to keep this method because otherwise either (1) both the caller and KeyValueStoreWrapper would have to call context.getStateStore(storeName) when the caller needs the state store in addition to the wrapper, which feels redundant, or (2) if we avoid the duplication by not having the wrapper call context.getStateStore(storeName) then the caller would always have to call context.getStateStore(storeName) even when it doesn't need the state store itself (only the wrapper), which feels clunky.
If we think it's convenient to keep this helper, should we add a Store store member variable, and implement this one a simple return store?
We can do that. Inside KeyValueStoreWrapper we still have to track whether the store is a timestamped store or a versioned store, in order to use the appropriate interfaces in put() and get(). I could introduce a private enum for this purpose; feels a little overkill but I can't think of anything better. I'll go ahead and make the change.
| } | ||
|
|
||
| @Test | ||
| public void shouldGetTimestampedStore() { |
There was a problem hiding this comment.
I added the Store store member which indeed simplified the code in KeyValueStoreWrapper, but have chosen to keep these tests here for coverage.
| public KeyValueStoreWrapper(final ProcessorContext<?, ?> context, final String storeName) { | ||
| store = context.getStateStore(storeName); | ||
|
|
||
| if (store instanceof TimestampedKeyValueStore) { |
There was a problem hiding this comment.
It's interesting that these checks don't verify the generic types (i.e., K and V) but IIUC the code in ProcessorContextImpl#getStateStore() for casting state stores to the requested type does not check generic types either, so I don't believe there's a visible change in behavior here.
There was a problem hiding this comment.
I think this is fine. -- Even if we have the cast as in the original code, I don't think it would be safer. The compile check cannot verify the generic types anyway, because getStateStore does not contain any information about it -- and at runtime, the generic types are gone and the actually "cast" if necessary from Object to K (or V) would happen somewhere else in the code.
|
There is test failures. Could it be related to this PR? If not related to this PR, we should check JIRA if there is already a flaky test ticket, and if not create one. |
Good catch. These failures only appeared when I switched from the original try-catch approach for casting stores to either TimestampedKeyValueStore or VersionedKeyValueStore, to fetching the store as a generic StateStore and then performing |
As part of introducing versioned key-value stores in KIP-889, we want to lift the existing DSL restriction that KTable stores are always TimestampedKeyValueStores to allow for KTable stores which are VersionedKeyValueStores instead. This PR lifts the restriction by replacing raw usages of TimestampedKeyValueStore with a new KeyValueStoreWrapper which supports either TimestampedKeyValueStore or VersionedKeyValueStore. (Until versioned stores are actually exposed to users in a follow-up PR, all KTables stores will continue to be TimestampedKeyValueStores.)
The loss of type-safety from
StoreBuilder<TimestampedKeyValueStore<K, VR>>toStoreBuilder<?>is unfortunate but necessary in order to allow for top-level VersionedKeyValueStores, so that users may access versioned stores for use intransform()operations and interactive queries.Committer Checklist (excluded from commit message)