use of org.apache.kafka.streams.integration.utils.IntegrationTestUtils.TrackingStateRestoreListener in project kafka by apache.
the class RestoreIntegrationTest method shouldRecycleStateFromStandbyTaskPromotedToActiveTaskAndNotRestore.
@Test
public void shouldRecycleStateFromStandbyTaskPromotedToActiveTaskAndNotRestore() throws Exception {
final StreamsBuilder builder = new StreamsBuilder();
builder.table(inputStream, Consumed.with(Serdes.Integer(), Serdes.Integer()), Materialized.as(getCloseCountingStore("store")));
createStateForRestoration(inputStream, 0);
final Properties props1 = props();
props1.put(StreamsConfig.NUM_STANDBY_REPLICAS_CONFIG, 1);
props1.put(StreamsConfig.STATE_DIR_CONFIG, TestUtils.tempDirectory(appId + "-1").getPath());
purgeLocalStreamsState(props1);
final KafkaStreams client1 = new KafkaStreams(builder.build(), props1);
final Properties props2 = props();
props2.put(StreamsConfig.NUM_STANDBY_REPLICAS_CONFIG, 1);
props2.put(StreamsConfig.STATE_DIR_CONFIG, TestUtils.tempDirectory(appId + "-2").getPath());
purgeLocalStreamsState(props2);
final KafkaStreams client2 = new KafkaStreams(builder.build(), props2);
final TrackingStateRestoreListener restoreListener = new TrackingStateRestoreListener();
client1.setGlobalStateRestoreListener(restoreListener);
startApplicationAndWaitUntilRunning(asList(client1, client2), Duration.ofSeconds(60));
waitForCompletion(client1, 1, 30 * 1000L);
waitForCompletion(client2, 1, 30 * 1000L);
waitForStandbyCompletion(client1, 1, 30 * 1000L);
waitForStandbyCompletion(client2, 1, 30 * 1000L);
// Sometimes the store happens to have already been closed sometime during startup, so just keep track
// of where it started and make sure it doesn't happen more times from there
final int initialStoreCloseCount = CloseCountingInMemoryStore.numStoresClosed();
final long initialNunRestoredCount = restoreListener.totalNumRestored();
client2.close();
waitForApplicationState(singletonList(client2), State.NOT_RUNNING, Duration.ofSeconds(60));
waitForApplicationState(singletonList(client1), State.REBALANCING, Duration.ofSeconds(60));
waitForApplicationState(singletonList(client1), State.RUNNING, Duration.ofSeconds(60));
waitForCompletion(client1, 1, 30 * 1000L);
waitForStandbyCompletion(client1, 1, 30 * 1000L);
assertThat(restoreListener.totalNumRestored(), CoreMatchers.equalTo(initialNunRestoredCount));
// After stopping instance 2 and letting instance 1 take over its tasks, we should have closed just two stores
// total: the active and standby tasks on instance 2
assertThat(CloseCountingInMemoryStore.numStoresClosed(), equalTo(initialStoreCloseCount + 2));
client1.close();
waitForApplicationState(singletonList(client2), State.NOT_RUNNING, Duration.ofSeconds(60));
assertThat(CloseCountingInMemoryStore.numStoresClosed(), CoreMatchers.equalTo(initialStoreCloseCount + 4));
}
Aggregations