aliehsaeedii commented on code in PR #23738:
URL: https://github.com/apache/kafka/pull/23738#discussion_r4229333067
##########
streams/integration-tests/src/test/java/org/apache/kafka/streams/integration/HeadersStoreUpgradeIntegrationTest.java:
##########
@@ -906,238 +825,151 @@ private void
processWindowedKeyValueWithHeadersAndVerify(final String key,
verifyWindowValue(key, value, timestamp, expectedTimestamp,
expectedHeaders);
}
- /**
- * Processor for plain WindowStore (without timestamps or headers).
- */
- private static class PlainWindowedProcessor implements Processor<String,
String, Void, Void> {
- private WindowStore<String, String> store;
-
- @Override
- public void init(final ProcessorContext<Void, Void> context) {
- store = context.getStateStore(WINDOW_STORE_NAME);
- }
-
- @Override
- public void process(final Record<String, String> record) {
- final long windowStart = record.timestamp() - (record.timestamp()
% WINDOW_SIZE_MS);
- store.put(record.key(), record.value(), windowStart);
- }
- }
-
- /**
- * Processor for TimestampedWindowStore (without headers).
- */
- private static class TimestampedWindowedProcessor implements
Processor<String, String, Void, Void> {
- private TimestampedWindowStore<String, String> store;
-
- @Override
- public void init(final ProcessorContext<Void, Void> context) {
- store = context.getStateStore(WINDOW_STORE_NAME);
- }
-
- @Override
- public void process(final Record<String, String> record) {
- final long windowStart = record.timestamp() - (record.timestamp()
% WINDOW_SIZE_MS);
- store.put(record.key(), ValueAndTimestamp.make(record.value(),
record.timestamp()), windowStart);
- }
- }
-
- /**
- * Processor for TimestampedWindowStoreWithHeaders (with headers).
- */
- private static class TimestampedWindowedWithHeadersProcessor implements
Processor<String, String, Void, Void> {
- private TimestampedWindowStoreWithHeaders<String, String> store;
-
- @Override
- public void init(final ProcessorContext<Void, Void> context) {
- store = context.getStateStore(WINDOW_STORE_NAME);
- }
-
- @Override
- public void process(final Record<String, String> record) {
- final long windowStart = record.timestamp() - (record.timestamp()
% WINDOW_SIZE_MS);
- store.put(record.key(),
- ValueTimestampHeaders.make(record.value(), record.timestamp(),
record.headers()),
- windowStart);
- }
- }
+ // ==================== Downgrade Tests ====================
@Test
public void
shouldFailDowngradeFromTimestampedKeyValueStoreWithHeadersToPlainKeyValueStore()
throws Exception {
- final Properties props = props();
- setupAndPopulateKeyValueStoreWithHeaders(props);
- kafkaStreams = null;
+ setupAndPopulateKeyValueStoreWithHeaders();
assertDowngradeThrowsProcessorStateException(
"to plain key-value store",
Stores.keyValueStoreBuilder(
Stores.persistentKeyValueStore(STORE_NAME),
Serdes.String(),
Serdes.String()),
- KeyValueProcessor::new, STORE_NAME, props);
+ KEY_VALUE_PROCESSOR, STORE_NAME,
+ "headers-aware", "Downgrade");
Review Comment:
Now that each caller passes its own fragments, can you add `"to regular
store"` here and `"to timestamped store"` in the timestamped cases? Right now
the plain and timestamped downgrade tests would pass on each other's error.
##########
streams/integration-tests/src/test/java/org/apache/kafka/streams/integration/HeadersStoreUpgradeIntegrationTest.java:
##########
@@ -128,16 +152,43 @@ private Properties props() {
private void buildAndStart(final StoreBuilder<?> storeBuilder,
final ProcessorSupplier<String, String, Void,
Void> processorSupplier,
- final String storeName,
- final Properties props) throws Exception {
+ final String storeName) throws Exception {
final StreamsBuilder builder = new StreamsBuilder();
builder.addStateStore(storeBuilder)
.stream(inputStream, Consumed.with(Serdes.String(),
Serdes.String()))
.process(processorSupplier, storeName);
- kafkaStreams = new KafkaStreams(builder.build(), props);
+ kafkaStreams = new KafkaStreams(builder.build(), streamsConfig);
IntegrationTestUtils.startApplicationAndWaitUntilRunning(kafkaStreams);
}
+ /**
+ * Stops the running instance (leaving the group) and starts a new one
over the same state
+ * directory with the given store and processor — the upgrade step every
migration/proxy test
+ * performs between writing legacy data and reading it back through the
new store type.
+ */
+ private void restart(final StoreBuilder<?> storeBuilder,
+ final ProcessorSupplier<String, String, Void, Void>
processorSupplier,
+ final String storeName) throws Exception {
+ closeAndLeaveGroupBeforeRestart();
+ kafkaStreams = null;
+ buildAndStart(storeBuilder, processorSupplier, storeName);
+ }
+
+ /**
+ * Deletes the local state of the (already closed) instance, so the next
start has to rebuild
+ * its stores from the changelog — the supported way to downgrade from a
headers-aware store.
+ */
+ private void wipeLocalState() {
+ kafkaStreams.cleanUp();
+ kafkaStreams = null;
+ }
+
+ private void closeAndLeaveGroupBeforeRestart() {
+ // Leave the group so the immediate restart with the same application
id
+ // does not wait for the previous member's session timeout.
+
kafkaStreams.close(CloseOptions.groupMembershipOperation(GroupMembershipOperation.LEAVE_GROUP));
Review Comment:
The session migration test used a 5s timeout here before; now this close can
wait forever. Since `CLOSE_TIMEOUT` exists now, add
`.withTimeout(CLOSE_TIMEOUT)` so every close is bounded.
##########
streams/integration-tests/src/test/java/org/apache/kafka/streams/integration/HeadersStoreUpgradeIntegrationTest.java:
##########
@@ -906,238 +825,151 @@ private void
processWindowedKeyValueWithHeadersAndVerify(final String key,
verifyWindowValue(key, value, timestamp, expectedTimestamp,
expectedHeaders);
}
- /**
- * Processor for plain WindowStore (without timestamps or headers).
- */
- private static class PlainWindowedProcessor implements Processor<String,
String, Void, Void> {
- private WindowStore<String, String> store;
-
- @Override
- public void init(final ProcessorContext<Void, Void> context) {
- store = context.getStateStore(WINDOW_STORE_NAME);
- }
-
- @Override
- public void process(final Record<String, String> record) {
- final long windowStart = record.timestamp() - (record.timestamp()
% WINDOW_SIZE_MS);
- store.put(record.key(), record.value(), windowStart);
- }
- }
-
- /**
- * Processor for TimestampedWindowStore (without headers).
- */
- private static class TimestampedWindowedProcessor implements
Processor<String, String, Void, Void> {
- private TimestampedWindowStore<String, String> store;
-
- @Override
- public void init(final ProcessorContext<Void, Void> context) {
- store = context.getStateStore(WINDOW_STORE_NAME);
- }
-
- @Override
- public void process(final Record<String, String> record) {
- final long windowStart = record.timestamp() - (record.timestamp()
% WINDOW_SIZE_MS);
- store.put(record.key(), ValueAndTimestamp.make(record.value(),
record.timestamp()), windowStart);
- }
- }
-
- /**
- * Processor for TimestampedWindowStoreWithHeaders (with headers).
- */
- private static class TimestampedWindowedWithHeadersProcessor implements
Processor<String, String, Void, Void> {
- private TimestampedWindowStoreWithHeaders<String, String> store;
-
- @Override
- public void init(final ProcessorContext<Void, Void> context) {
- store = context.getStateStore(WINDOW_STORE_NAME);
- }
-
- @Override
- public void process(final Record<String, String> record) {
- final long windowStart = record.timestamp() - (record.timestamp()
% WINDOW_SIZE_MS);
- store.put(record.key(),
- ValueTimestampHeaders.make(record.value(), record.timestamp(),
record.headers()),
- windowStart);
- }
- }
+ // ==================== Downgrade Tests ====================
@Test
public void
shouldFailDowngradeFromTimestampedKeyValueStoreWithHeadersToPlainKeyValueStore()
throws Exception {
- final Properties props = props();
- setupAndPopulateKeyValueStoreWithHeaders(props);
- kafkaStreams = null;
+ setupAndPopulateKeyValueStoreWithHeaders();
assertDowngradeThrowsProcessorStateException(
"to plain key-value store",
Stores.keyValueStoreBuilder(
Stores.persistentKeyValueStore(STORE_NAME),
Serdes.String(),
Serdes.String()),
- KeyValueProcessor::new, STORE_NAME, props);
+ KEY_VALUE_PROCESSOR, STORE_NAME,
+ "headers-aware", "Downgrade");
}
@Test
public void
shouldSuccessfullyDowngradeFromTimestampedKeyValueStoreWithHeadersToPlainKeyValueStoreAfterCleanup()
throws Exception {
- final Properties props = props();
- setupAndPopulateKeyValueStoreWithHeaders(props);
-
- kafkaStreams.cleanUp(); // Delete local state
- kafkaStreams = null;
+ setupAndPopulateKeyValueStoreWithHeaders();
+ wipeLocalState();
buildAndStart(
Stores.keyValueStoreBuilder(
Stores.persistentKeyValueStore(STORE_NAME),
Serdes.String(),
Serdes.String()),
- KeyValueProcessor::new, STORE_NAME, props);
+ KEY_VALUE_PROCESSOR, STORE_NAME);
processKeyValueAndVerifyValue("key3", "value3");
processKeyValueAndVerifyValue("key4", "value4");
-
- kafkaStreams.close();
}
@Test
public void
shouldFailDowngradeFromTimestampedKeyValueStoreWithHeadersToTimestampedKeyValueStore()
throws Exception {
- final Properties props = props();
- setupAndPopulateKeyValueStoreWithHeaders(props);
- kafkaStreams = null;
+ setupAndPopulateKeyValueStoreWithHeaders();
assertDowngradeThrowsProcessorStateException(
"to timestamped key-value store",
Stores.timestampedKeyValueStoreBuilder(
Stores.persistentTimestampedKeyValueStore(STORE_NAME),
Serdes.String(),
Serdes.String()),
- TimestampedKeyValueProcessor::new, STORE_NAME, props);
+ TIMESTAMPED_KEY_VALUE_PROCESSOR, STORE_NAME,
+ "headers-aware", "Downgrade");
}
@Test
public void
shouldSuccessfullyDowngradeFromTimestampedKeyValueStoreWithHeadersToTimestampedKeyValueStoreAfterCleanup()
throws Exception {
- final Properties props = props();
- setupAndPopulateKeyValueStoreWithHeaders(props);
-
- kafkaStreams.cleanUp(); // Delete local state
- kafkaStreams = null;
+ setupAndPopulateKeyValueStoreWithHeaders();
+ wipeLocalState();
buildAndStart(
Stores.timestampedKeyValueStoreBuilder(
Stores.persistentTimestampedKeyValueStore(STORE_NAME),
Serdes.String(),
Serdes.String()),
- TimestampedKeyValueProcessor::new, STORE_NAME, props);
+ TIMESTAMPED_KEY_VALUE_PROCESSOR, STORE_NAME);
// verify legacy key, values
verifyLegacyTimestampedValue("key1", "value1", 11L);
verifyLegacyTimestampedValue("key2", "value2", 22L);
processKeyValueAndVerifyTimestampedValue("key3", "value3", 333L);
processKeyValueAndVerifyTimestampedValue("key4", "value4", 444L);
-
- kafkaStreams.close();
}
-
@Test
public void
shouldFailDowngradeFromTimestampedWindowStoreWithHeadersToPlainWindowStore()
throws Exception {
- final Properties props = props();
- setupAndPopulateWindowStoreWithHeaders(props,
List.of(KeyValue.pair("key1", 100L)));
- kafkaStreams = null;
+ setupAndPopulateWindowStoreWithHeaders(List.of(KeyValue.pair("key1",
100L)));
Review Comment:
nit: this uses `List.of`, the next test uses `singletonList`, and the two
after use `asList`. Using `List.of` everywhere would be consistent and drop the
`asList` import.
##########
streams/integration-tests/src/test/java/org/apache/kafka/streams/integration/HeadersStoreUpgradeIntegrationTest.java:
##########
@@ -64,36 +65,51 @@
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.time.Duration;
+import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Properties;
+import java.util.function.BiConsumer;
import java.util.function.Predicate;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static org.apache.kafka.streams.utils.TestUtils.safeUniqueTestName;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.fail;
@Tag("integration")
public class HeadersStoreUpgradeIntegrationTest {
private static final String STORE_NAME = "store";
private static final String WINDOW_STORE_NAME = "window-store";
private static final String SESSION_STORE_NAME = "session-store";
private static final long WINDOW_SIZE_MS = 1000L;
- private static final long RETENTION_MS = Duration.ofDays(1).toMillis();
+ private static final Duration WINDOW_SIZE =
Duration.ofMillis(WINDOW_SIZE_MS);
+ private static final Duration RETENTION = Duration.ofDays(1);
private static final long DEFAULT_STORE_TIMEOUT_MS = 60_000L;
+ private static final Duration CLOSE_TIMEOUT = Duration.ofSeconds(30L);
+ /**
+ * Timestamp reported through the headers-aware view for records written
to a store that does
+ * not preserve timestamps (a plain key-value or window store, migrated or
proxied).
Review Comment:
This is only true for persistent stores. The in-memory migrations get a real
timestamp (see the `persistentStore ? NO_TIMESTAMP : ...` checks), so maybe say
"a persistent plain key-value or window store".
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]