Search in sources :

Example 16 with LeaderNotAvailableException

use of org.apache.kafka.common.errors.LeaderNotAvailableException in project kafka by apache.

the class KafkaBasedLogTest method testProducerError.

@Test
public void testProducerError() throws Exception {
    expectStart();
    TestFuture<RecordMetadata> tp0Future = new TestFuture<>();
    ProducerRecord<String, String> tp0Record = new ProducerRecord<>(TOPIC, TP0_KEY, TP0_VALUE);
    Capture<org.apache.kafka.clients.producer.Callback> callback0 = EasyMock.newCapture();
    EasyMock.expect(producer.send(EasyMock.eq(tp0Record), EasyMock.capture(callback0))).andReturn(tp0Future);
    expectStop();
    PowerMock.replayAll();
    Map<TopicPartition, Long> endOffsets = new HashMap<>();
    endOffsets.put(TP0, 0L);
    endOffsets.put(TP1, 0L);
    consumer.updateEndOffsets(endOffsets);
    store.start();
    assertEquals(CONSUMER_ASSIGNMENT, consumer.assignment());
    assertEquals(0L, consumer.position(TP0));
    assertEquals(0L, consumer.position(TP1));
    final AtomicReference<Throwable> setException = new AtomicReference<>();
    store.send(TP0_KEY, TP0_VALUE, (metadata, exception) -> {
        // Should only be invoked once
        assertNull(setException.get());
        setException.set(exception);
    });
    KafkaException exc = new LeaderNotAvailableException("Error");
    tp0Future.resolve(exc);
    callback0.getValue().onCompletion(null, exc);
    assertNotNull(setException.get());
    store.stop();
    assertFalse(Whitebox.<Thread>getInternalState(store, "thread").isAlive());
    assertTrue(consumer.closed());
    PowerMock.verifyAll();
}
Also used : HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) LeaderNotAvailableException(org.apache.kafka.common.errors.LeaderNotAvailableException) RecordMetadata(org.apache.kafka.clients.producer.RecordMetadata) TopicPartition(org.apache.kafka.common.TopicPartition) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) KafkaException(org.apache.kafka.common.KafkaException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 17 with LeaderNotAvailableException

use of org.apache.kafka.common.errors.LeaderNotAvailableException in project kafka by apache.

the class KafkaBasedLogTest method testReadEndOffsetsUsingAdminThatFailsWithRetriable.

@Test
public void testReadEndOffsetsUsingAdminThatFailsWithRetriable() throws Exception {
    // Create a log that uses the admin supplier
    setupWithAdmin();
    expectProducerAndConsumerCreate();
    Set<TopicPartition> tps = new HashSet<>(Arrays.asList(TP0, TP1));
    Map<TopicPartition, Long> endOffsets = new HashMap<>();
    endOffsets.put(TP0, 0L);
    endOffsets.put(TP1, 0L);
    // Getting end offsets upon startup should work fine
    admin.endOffsets(EasyMock.eq(tps));
    PowerMock.expectLastCall().andReturn(endOffsets).times(1);
    // Getting end offsets using the admin client should fail with leader not available
    admin.endOffsets(EasyMock.eq(tps));
    PowerMock.expectLastCall().andThrow(new LeaderNotAvailableException("retry"));
    PowerMock.replayAll();
    store.start();
    assertThrows(LeaderNotAvailableException.class, () -> store.readEndOffsets(tps));
}
Also used : HashMap(java.util.HashMap) TopicPartition(org.apache.kafka.common.TopicPartition) LeaderNotAvailableException(org.apache.kafka.common.errors.LeaderNotAvailableException) HashSet(java.util.HashSet) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

LeaderNotAvailableException (org.apache.kafka.common.errors.LeaderNotAvailableException)17 HashMap (java.util.HashMap)10 UnknownTopicOrPartitionException (org.apache.kafka.common.errors.UnknownTopicOrPartitionException)10 Test (org.junit.Test)10 TopicDescription (org.apache.kafka.clients.admin.TopicDescription)8 KafkaFuture (org.apache.kafka.common.KafkaFuture)8 TimeoutException (org.apache.kafka.common.errors.TimeoutException)8 AdminClient (org.apache.kafka.clients.admin.AdminClient)6 MockAdminClient (org.apache.kafka.clients.admin.MockAdminClient)6 TopicPartition (org.apache.kafka.common.TopicPartition)6 KafkaFutureImpl (org.apache.kafka.common.internals.KafkaFutureImpl)6 StreamsConfig (org.apache.kafka.streams.StreamsConfig)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 NewTopic (org.apache.kafka.clients.admin.NewTopic)5 Node (org.apache.kafka.common.Node)5 Collection (java.util.Collection)4 Collections (java.util.Collections)4 Optional (java.util.Optional)4 Set (java.util.Set)4