Search in sources :

Example 1 with TrackingStateRestoreListener

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));
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) KafkaStreams(org.apache.kafka.streams.KafkaStreams) Properties(java.util.Properties) TrackingStateRestoreListener(org.apache.kafka.streams.integration.utils.IntegrationTestUtils.TrackingStateRestoreListener) OffsetCheckpoint(org.apache.kafka.streams.state.internals.OffsetCheckpoint) IntegrationTest(org.apache.kafka.test.IntegrationTest) Test(org.junit.Test)

Aggregations

Properties (java.util.Properties)1 KafkaStreams (org.apache.kafka.streams.KafkaStreams)1 StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)1 TrackingStateRestoreListener (org.apache.kafka.streams.integration.utils.IntegrationTestUtils.TrackingStateRestoreListener)1 OffsetCheckpoint (org.apache.kafka.streams.state.internals.OffsetCheckpoint)1 IntegrationTest (org.apache.kafka.test.IntegrationTest)1 Test (org.junit.Test)1