...
Discussion thread: here
JIRA:
Jira | ||||||
---|---|---|---|---|---|---|
|
Please keep the discussion on the mailing list rather than commenting on the wiki (wiki discussions get unwieldy fast).
...
In the current IQv2 code, there are noticeable differences when interfacing with plain- kv-store
and ts-kv-store
. Notably, the return type V
acts as a simple value for plain- kv-store
but evolves into ValueAndTimestamp<V>
for ts-kv-store
, which presents type safety issues in the API.
Code Block | ||||
---|---|---|---|---|
| ||||
public <V> void shouldHandleKeyQuery( final Integer key, final Function<V, Integer> valueExtactor, final Integer expectedValue) { ... final KeyQuery<Integer, V> query = KeyQuery.withKey(key); ... final StateQueryRequest<V> request = inStore(STORE_NAME) .withQuery(query) .withPartitions(mkSet(0, 1)) .withPositionBound(PositionBound.at(INPUT_POSITION)); ... final StateQueryResult<V> result = IntegrationTestUtils.iqv2WaitForResult(kafkaStreams, request); ... final QueryResult<V> queryResult = result.getOnlyPartitionResult(); ... final V result1 = queryResult.getResult(); final Integer integer = valueExtactor.apply(result1); assertThat(integer, is(expectedValue)); ... } |
Before the introduction of TimestampedKeyQuery
, when using KeyQuery
, we obtained the result using the following code:
Code Block |
---|
final V result1 = queryResult.getResult(); |
This meant that the returned result could be of two potential data types: plain V
or ValueAndTimestamp<V>
. This was a source of inconsistency. For instance, querying a kv-store
with KeyQuery
would return a V
type, but querying a ts-kv-store
would yield a ValueAndTimestamp<V>
. This behavior is unintuitive and potentially confusing for developers.
To address this inconsistency, we propose that KeyQuery
should be restricted to querying kv-stores
only, ensuring that it always returns a plain V
type, making the behavior of the aforementioned code more predictable. Similarly, RangeQuery
should be dedicated to querying kv-stores
, consistently returning only the plain V
.
For those requiring timestamped values from a ts-kv-store
, we recommend introducing a new query type: TimestampedKeyQuery
. This new query will specifically target ts-kv-stores
and will return ValueAndTimestamp<V>
. Furthermore, to complement this, TimestampedRangeQuery
should be introduced to query ranges in ts-kv-stores
, ensuring that the returned value always includes timestamps.
Code Block | ||||
---|---|---|---|---|
| ||||
public final class TimestampKeyQuery<KTimestampedKeyQuery<K, V> implements Query<ValueAndTimestamp<V>> |
...
Code Block | ||||
---|---|---|---|---|
| ||||
public final class TimestampRangeQuery<KTimestampedRangeQuery<K, V> implements Query<KeyValueIterator<K, ValueAndTimestamp<V>>> |
Why introduce TimestampedKeyQuery
and TimestampedRangeQuery
? The primary motivation behind this is to ensure type safety and foster a clear distinction in our API. They bridge the difference between simple key-value stores and those integrated with timestamps, offering a more robust and intuitive querying mechanism.
...
To address these challenges and streamline the querying experience, we have decided to refine our approach and introduce two specialized query types: TimestampKeyQuery TimestampedKeyQuery
and TimestampRangeQuery TimestampedRangeQuery
.
TimestampKeyQueryTimestampedKeyQuery
: This query type will consistently return ValueAndTimestamp<V>
, ensuring that there's a clear and predictable return type associated with timestamped key-value stores.
...
Code Block | ||||
---|---|---|---|---|
| ||||
@Evolving public final class TimestampedKeyQuery<K, V> implements Query<ValueAndTimestamp<V>> { ... /** * Creates a query that will retrieve the record identified by {@code key} if it exists * (or {@code null} otherwise). * @param key The key to retrieve * @param <K> The type of the key * @param <V> The type of the value that will be retrieved */ public static <K, V> TimestampedKeyQuery<K, V> withKey(final K key) /** * Specifies that the cache should be skipped during query evaluation. This means, that the query will always * get forwarded to the underlying store. */ public TimestampedKeyQuery<K, V> skipCache() /** * The key that was specified for this query. */ public K getKey() /** * The flag whether to skip the cache or not during query evaluation. */ public boolean isSkipCache() } |
TimestampRangeQueryTimestampedRangeQuery
: Tailored for ranges with timestamps, this query will return a KeyValueIterator<K, ValueAndTimestamp<V>>
Code Block | ||||
---|---|---|---|---|
| ||||
@Evolving public final class TimestampedRangeQuery<K, V> implements Query<KeyValueIterator<K, ValueAndTimestamp<V>>> { /** * Interactive range query using a lower and upper bound to filter the keys returned. * @param lower The key that specifies the lower bound of the range * @param upper The key that specifies the upper bound of the range * @param <K> The key type * @param <V> The value type */ public static <K, V> TimestampedRangeQuery<K, V> withRange(final K lower, final K upper) /** * Interactive range query using an upper bound to filter the keys returned. * If both <K,V> are null, RangQuery returns a full range scan. * @param upper The key that specifies the upper bound of the range * @param <K> The key type * @param <V> The value type */ public static <K, V> TimestampedRangeQuery<K, V> withUpperBound(final K upper) /** * Interactive range query using a lower bound to filter the keys returned. * @param lower The key that specifies the lower bound of the range * @param <K> The key type * @param <V> The value type */ public static <K, V> TimestampedRangeQuery<K, V> withLowerBound(final K lower) /** * Interactive scan query that returns all records in the store. * @param <K> The key type * @param <V> The value type */ public static <K, V> TimestampedRangeQuery<K, V> withNoBounds() /** * The lower bound of the query, if specified. */ public Optional<K> getLowerBound() /** * The upper bound of the query, if specified */ public Optional<K> getUpperBound() } |
Previously, MeteredKeyValueStore was equipped to handle both plain V queries and ValueAndTimestamp<V> queries. With this update, all KeyQuery instances will only return the plain V, eliminating the previously supported ValueAndTimestamp<V>. On the other hand, all TimestampKeyQuery instances are now designed to strictly return ValueAndTimestamp<V>.
This restructuring ensures a more intuitive, type-safe, and consistent querying mechanism for users across different types of key-value stores in the IQv2.
Compatibility, Deprecation, and Migration Plan
- Changing the semantics of existing `KeyQuery`
KeyQuery
and `RangeQuery`RangeQuery
is a breaking change. However, both classes are marked as `@Evolving` and@Evolving`
and thus a breaking change in a minor release is allowed without a deprecation period. Given that IQv2 is not yet widely adopted, we believe it’s cleaner to make this breaking change right away. - Adding new query types does not imply any compatibility concerns.
...
To ensure the robustness and accuracy of our new query types, TimestampedKeyQuery
and TimestampedRangeQuery
, it's essential to have thorough test coverage. With that in mind, we propose the creation of two specific test methods:
shouldHandleTimestampedKeyQuery
: This test method will validate the functionality of TimestampedKeyQuery
, ensuring it consistently returns ValueAndTimestamp<V>
as expected.
shouldHandleTimestampedRangeQuery
: This method is tailored to verify the TimestampedRangeQuery
, ensuring that it correctly returns a KeyValueIterator<K, ValueAndTimestamp<V>>
.
We will focus on conducting a detailed test for shouldHandleTimestampedRangeQuery
.
Rejected Alternatives
The alternative would be to deprecate the existing `KeyQuery` KeyQuery
and `RangeQuery` RangeQuery
and add new query types that always return plain value. However, it seems to introduce unnecessary “deprecation noise”, and it would be hard to find good names for these newly added query types. Making a semantics change on the existing queries allows us to keep the existing names.