Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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:   [One of "Under Discussion", "Accepted", "Rejected"]

Discussion thread: here [Change the link from the KIP proposal email archive to your own email thread] 

JIRA: here [Change the link from KAFKA-1 to your own ticket] 

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

Motivation

Describe the problems you are trying to solve.

Public Interfaces

Briefly list any new interfaces that will be introduced as part of this proposal or any existing interfaces that will be removed or changed. The purpose of this section is to concisely call out the public contract that will come along with this feature.

A public interface is any change to the following:

  • Binary log format

  • The network protocol and api behavior

  • Any class in the public packages under clientsConfiguration, especially client configuration

    • org/apache/kafka/common/serialization

    • org/apache/kafka/common

    • org/apache/kafka/common/errors

    • org/apache/kafka/clients/producer

    • org/apache/kafka/clients/consumer (eventually, once stable)

  • Monitoring

  • Command line tools and arguments

  • Anything else that will likely break existing users in some way when they upgrade

Proposed Changes


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:


Code Block
languagejava
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.


Code Block
languagejava
titleTimestampKeyQuery
@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>>

Code Block
languagejava
titleTimestampRangeQuery
@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 IQv2Describe the new thing you want to do in appropriate detail. This may be fairly extensive and have large subsections of its own. Or it may be a few sentences. Use judgement based on the scope of the change.

Compatibility, Deprecation, and Migration Plan

  • What impact (if any) will there be on existing users?
  • If we are changing behavior how will we phase out the older behavior?
  • If we need special migration tools, describe them here.
  • When will we remove the existing behavior?

Test Plan

Describe in few sentences how the KIP will be tested. We are mostly interested in system tests (since unit-tests are specific to implementation details). How will we know that the implementation works as expected? How will we know nothing broke?

Rejected Alternatives

  • 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.

Code Block
languagejava
titleshouldHandleTimestampKeyQuery
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>>.

Code Block
languagejava
titleshouldHandleTimestampRangeQueries
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.

Code Block
languagejava
titleshouldHandleTimestampRangeQuery
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>If there are alternative ways of accomplishing the same thing, what were they? The purpose of this section is to motivate why the design is the way it is and not some other way.