Search in sources :

Example 21 with CheckpointV1

use of org.apache.samza.checkpoint.CheckpointV1 in project samza by apache.

the class TestKafkaCheckpointManager method testConsumerStopsAfterInitialRead.

@Test
public void testConsumerStopsAfterInitialRead() throws Exception {
    setupSystemFactory(config());
    CheckpointV1 checkpointV1 = buildCheckpointV1(INPUT_SSP0, "0");
    setupConsumer(ImmutableList.of(newCheckpointV1Envelope(TASK0, checkpointV1, "0")));
    KafkaCheckpointManager kafkaCheckpointManager = buildKafkaCheckpointManager(true, config());
    kafkaCheckpointManager.register(TASK0);
    assertEquals(checkpointV1, kafkaCheckpointManager.readLastCheckpoint(TASK0));
    // 1 call to get actual checkpoints, 1 call for empty poll to signal done reading
    verify(this.systemConsumer, times(2)).poll(ImmutableSet.of(CHECKPOINT_SSP), SystemConsumer.BLOCK_ON_OUTSTANDING_MESSAGES);
    verify(this.systemConsumer).stop();
    // reading checkpoint again should not read more messages from the consumer
    assertEquals(checkpointV1, kafkaCheckpointManager.readLastCheckpoint(TASK0));
    verifyNoMoreInteractions(this.systemConsumer);
}
Also used : CheckpointV1(org.apache.samza.checkpoint.CheckpointV1) Test(org.junit.Test)

Example 22 with CheckpointV1

use of org.apache.samza.checkpoint.CheckpointV1 in project samza by apache.

the class TestKafkaCheckpointManager method testWriteCheckpointShouldRecreateSystemProducerOnFailure.

@Test
public void testWriteCheckpointShouldRecreateSystemProducerOnFailure() {
    setupSystemFactory(config());
    SystemProducer secondKafkaProducer = mock(SystemProducer.class);
    // override default mock behavior to return a second producer on the second call to create a producer
    when(this.systemFactory.getProducer(CHECKPOINT_SYSTEM, config(), this.metricsRegistry, KafkaCheckpointManager.class.getSimpleName())).thenReturn(this.systemProducer, secondKafkaProducer);
    // first producer throws an exception on flush
    doThrow(new RuntimeException("flush failed")).when(this.systemProducer).flush(TASK0.getTaskName());
    KafkaCheckpointManager kafkaCheckpointManager = buildKafkaCheckpointManager(true, config());
    kafkaCheckpointManager.register(TASK0);
    CheckpointV1 checkpointV1 = buildCheckpointV1(INPUT_SSP0, "0");
    kafkaCheckpointManager.writeCheckpoint(TASK0, checkpointV1);
    // first producer should be stopped
    verify(this.systemProducer).stop();
    // register and start the second producer
    verify(secondKafkaProducer).register(TASK0.getTaskName());
    verify(secondKafkaProducer).start();
    // check that the second producer was given the message to send out
    ArgumentCaptor<OutgoingMessageEnvelope> outgoingMessageEnvelopeArgumentCaptor = ArgumentCaptor.forClass(OutgoingMessageEnvelope.class);
    verify(secondKafkaProducer).send(eq(TASK0.getTaskName()), outgoingMessageEnvelopeArgumentCaptor.capture());
    assertEquals(CHECKPOINT_SSP, outgoingMessageEnvelopeArgumentCaptor.getValue().getSystemStream());
    assertEquals(new KafkaCheckpointLogKey(KafkaCheckpointLogKey.CHECKPOINT_V1_KEY_TYPE, TASK0, GROUPER_FACTORY_CLASS), KAFKA_CHECKPOINT_LOG_KEY_SERDE.fromBytes((byte[]) outgoingMessageEnvelopeArgumentCaptor.getValue().getKey()));
    assertEquals(checkpointV1, CHECKPOINT_V1_SERDE.fromBytes((byte[]) outgoingMessageEnvelopeArgumentCaptor.getValue().getMessage()));
    verify(secondKafkaProducer).flush(TASK0.getTaskName());
}
Also used : SystemProducer(org.apache.samza.system.SystemProducer) CheckpointV1(org.apache.samza.checkpoint.CheckpointV1) OutgoingMessageEnvelope(org.apache.samza.system.OutgoingMessageEnvelope) Test(org.junit.Test)

Aggregations

CheckpointV1 (org.apache.samza.checkpoint.CheckpointV1)22 HashMap (java.util.HashMap)14 SystemStreamPartition (org.apache.samza.system.SystemStreamPartition)13 Test (org.junit.Test)13 Checkpoint (org.apache.samza.checkpoint.Checkpoint)12 TaskName (org.apache.samza.container.TaskName)12 Partition (org.apache.samza.Partition)11 Map (java.util.Map)10 ImmutableMap (com.google.common.collect.ImmutableMap)9 SamzaException (org.apache.samza.SamzaException)8 SystemStream (org.apache.samza.system.SystemStream)8 KafkaChangelogSSPOffset (org.apache.samza.checkpoint.kafka.KafkaChangelogSSPOffset)7 File (java.io.File)6 CheckpointId (org.apache.samza.checkpoint.CheckpointId)6 CheckpointManager (org.apache.samza.checkpoint.CheckpointManager)6 CheckpointV2 (org.apache.samza.checkpoint.CheckpointV2)6 MapConfig (org.apache.samza.config.MapConfig)6 Collections (java.util.Collections)5 CompletableFuture (java.util.concurrent.CompletableFuture)5 TaskInstanceMetrics (org.apache.samza.container.TaskInstanceMetrics)5