You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

This page is meant as a template for writing a KIP. To create a KIP choose Tools->Copy on this page and modify with your content and replace the heading with the next KIP number and a description of your issue. Replace anything in italics with your own description.

Status

Current state: Under Discussion

Discussion thread: here 

JIRA: here 

Please keep the discussion on the mailing list rather than commenting on the wiki (wiki discussions get unwieldy fast).

Motivation


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.

Even if IQv2 hasn't gained widespread adoption, an immediate fix might bring compatibility concerns.

This brings us to the essence of our proposal: the introduction of distinct query types. One that returns a plain value, another for values accompanied by timestamps.

Although it's viable to issue a "plain value" query against a ts-kv-store and then unpack the return value, doing the reverse — launching a ValueAndTimestamp<V> query against a plain-kv-store — seems illogical.

Our vision is that KeyValue should always return a plain V. However, for ts-stores, there's a pronounced need for specialized query types. Ultimately, the user should only be required to define V.

To address these concerns, we propose:


public final class KeyQuery<K, V> implements Query<V>
public final class TimestampKeyQuery<K, V> implements Query<ValueAndTimestamp<V>>


Why introduce TimestampKeyQuery and TimestampRangeQuery? 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.

Proposed Changes

Within the current IQv2 codebase, there have been distinct interactions between plain-kv-store and ts-kv-store. These differences, especially in return types, have raised concerns over type safety within the API.

To address these challenges and streamline the querying experience, we have decided to refine our approach and introduce two specialized query types: TimestampKeyQuery and TimestampRangeQuery.

TimestampKeyQuery: This query type will consistently return ValueAndTimestamp<V>, ensuring that there's a clear and predictable return type associated with timestamped key-value stores.


TimestampKeyQuery
@Evolving
public final class TimestampKeyQuery<K, V> implements Query<ValueAndTimestamp<V>> {}


TimestampRangeQuery: Tailored for ranges with timestamps, this query will return a KeyValueIterator<K, ValueAndTimestamp<V>>

TimestampRangeQuery
@Evolving
public final class TimestampRangeQuery<K, V> implements Query<KeyValueIterator<K, ValueAndTimestamp<V>>> {}

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

  • Utilizing the existing RangeQuery and KeyQuery class, we can make some modifications to realize the concepts of TimestampKeyQuery  and TimestampRangeQuery. 
  • Since nothing is deprecated in this KIP, users have no need to migrate unless they want to.

Test Plan

To ensure the robustness and accuracy of our new query types, TimestampKeyQuery and TimestampRangeQuery, it's essential to have thorough test coverage. With that in mind, we propose the creation of two specific test methods:

shouldHandleTimestampKeyQuery: This test method will validate the functionality of TimestampKeyQuery, ensuring it consistently returns ValueAndTimestamp<V> as expected.

shouldHandleTimestampKeyQuery
public <V> void shouldHandleTimestampKeyQuery(
            final Integer key,
            final Function<ValueAndTimestamp<V>, Integer> valueExtactor,
            final Integer expectedValue) {

        final TimestampKeyQuery<Integer, V> query = TimestampKeyQuery.withKey(key);
        final StateQueryRequest<ValueAndTimestamp<V>> request =
                inStore(STORE_NAME)
                        .withQuery(query)
                        .withPartitions(mkSet(0, 1))
                        .withPositionBound(PositionBound.at(INPUT_POSITION));

        final StateQueryResult<ValueAndTimestamp<V>> result =
                IntegrationTestUtils.iqv2WaitForResult(kafkaStreams, request);

        final QueryResult<ValueAndTimestamp<V>> queryResult = result.getOnlyPartitionResult();
     ...

        final ValueAndTimestamp<V> result1 = queryResult.getResult();
        final Integer integer = valueExtactor.apply(result1);
     ...
    }

shouldHandleTimestampRangeQuery: This method is tailored to verify the TimestampRangeQuery, ensuring that it correctly returns a KeyValueIterator<K, ValueAndTimestamp<V>>.

shouldHandleTimestampRangeQueries
private <T> void shouldHandleTimestampRangeQueries(final Function<ValueAndTimestamp<T>, Integer> extractor) {
        shouldHandleTimestampRangeQuery(
            Optional.of(0),
            Optional.of(4),
            extractor,
            mkSet(1, 3, 5, 7, 9)
        );

        ...
    }

We will focus on conducting a detailed test for shouldHandleTimestampRangeQuery.

shouldHandleTimestampRangeQuery
public <V> void shouldHandleTimestampRangeQuery(
            final Optional<Integer> lower,
            final Optional<Integer> upper,
            final Function<ValueAndTimestamp<V>, Integer> valueExtactor,
            final Set<Integer> expectedValue) {

        final TimestampRangeQuery<Integer, V> query;

        query = TimestampRangeQuery.withRange(lower.orElse(null), upper.orElse(null));

        final StateQueryRequest<KeyValueIterator<Integer, ValueAndTimestamp<V>>> request =
                inStore(STORE_NAME)
                        .withQuery(query)
                        .withPartitions(mkSet(0, 1))
                        .withPositionBound(PositionBound.at(INPUT_POSITION));
        final StateQueryResult<KeyValueIterator<Integer, ValueAndTimestamp<V>>> result =
                IntegrationTestUtils.iqv2WaitForResult(kafkaStreams, request);
            ...
            final Map<Integer, QueryResult<KeyValueIterator<Integer, ValueAndTimestamp<V>>>> queryResult = result.getPartitionResults();
       
             ...

                try (final KeyValueIterator<Integer, ValueAndTimestamp<V>> iterator = queryResult.get(partition).getResult()) {
                    while (iterator.hasNext()) {
                        actualValue.add(valueExtactor.apply(iterator.next().value));
                    }
                }
               ...
        }
    }


Rejected Alternatives

Initially, our approach was to directly use TimestampKeyQuery and TimestampRangeQuery within each store. This implied that every store would return a ValueAndTimestamp<byte[]>. However, this method introduced complexities due to type transformations.

Given that data within stores under the metered store is typically formatted as <Byte, byte[]>, we would need to wrap the byte[] into ValueAndTimestamp<byte[]> to produce the desired output.

To streamline this, we opted to leverage existing methods to fetch the byte[] directly, which already encapsulates both value and timestamp. We then perform the deserialization at the meteredTimestampKeyValueStore level, the outermost layer, ensuring that the final output is ValueAndTimestamp<V>.

  • No labels