Search in sources :

Example 11 with KafkaException

use of org.apache.kafka.common.KafkaException in project kafka by apache.

the class ScramFormatter method generateCredential.

public ScramCredential generateCredential(String password, int iterations) {
    try {
        byte[] salt = secureRandomBytes();
        byte[] saltedPassword = saltedPassword(password, salt, iterations);
        byte[] clientKey = clientKey(saltedPassword);
        byte[] storedKey = storedKey(clientKey);
        byte[] serverKey = serverKey(saltedPassword);
        return new ScramCredential(salt, storedKey, serverKey, iterations);
    } catch (InvalidKeyException e) {
        throw new KafkaException("Could not create credential", e);
    }
}
Also used : KafkaException(org.apache.kafka.common.KafkaException) InvalidKeyException(java.security.InvalidKeyException)

Example 12 with KafkaException

use of org.apache.kafka.common.KafkaException in project nifi by apache.

the class ConsumerPoolTest method validatePoolConsumerFails.

@Test
public void validatePoolConsumerFails() throws Exception {
    when(consumer.poll(anyLong())).thenThrow(new KafkaException("oops"));
    try (final ConsumerLease lease = testPool.obtainConsumer(mockSession, mockContext)) {
        try {
            lease.poll();
            fail();
        } catch (final KafkaException ke) {
        }
    }
    testPool.close();
    verify(mockSession, times(0)).create();
    verify(mockSession, times(0)).commit();
    final PoolStats stats = testPool.getPoolStats();
    assertEquals(1, stats.consumerCreatedCount);
    assertEquals(1, stats.consumerClosedCount);
    assertEquals(1, stats.leasesObtainedCount);
}
Also used : KafkaException(org.apache.kafka.common.KafkaException) PoolStats(org.apache.nifi.processors.kafka.pubsub.ConsumerPool.PoolStats) Test(org.junit.Test)

Example 13 with KafkaException

use of org.apache.kafka.common.KafkaException in project apache-kafka-on-k8s by banzaicloud.

the class ProducerBatchTest method testBatchAbort.

@Test
public void testBatchAbort() throws Exception {
    ProducerBatch batch = new ProducerBatch(new TopicPartition("topic", 1), memoryRecordsBuilder, now);
    MockCallback callback = new MockCallback();
    FutureRecordMetadata future = batch.tryAppend(now, null, new byte[10], Record.EMPTY_HEADERS, callback, now);
    KafkaException exception = new KafkaException();
    batch.abort(exception);
    assertTrue(future.isDone());
    assertEquals(1, callback.invocations);
    assertEquals(exception, callback.exception);
    assertNull(callback.metadata);
    // subsequent completion should be ignored
    assertFalse(batch.done(500L, 2342342341L, null));
    assertFalse(batch.done(-1, -1, new KafkaException()));
    assertEquals(1, callback.invocations);
    assertTrue(future.isDone());
    try {
        future.get();
        fail("Future should have thrown");
    } catch (ExecutionException e) {
        assertEquals(exception, e.getCause());
    }
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) KafkaException(org.apache.kafka.common.KafkaException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 14 with KafkaException

use of org.apache.kafka.common.KafkaException in project apache-kafka-on-k8s by banzaicloud.

the class ProducerBatchTest method testBatchCannotAbortTwice.

@Test
public void testBatchCannotAbortTwice() throws Exception {
    ProducerBatch batch = new ProducerBatch(new TopicPartition("topic", 1), memoryRecordsBuilder, now);
    MockCallback callback = new MockCallback();
    FutureRecordMetadata future = batch.tryAppend(now, null, new byte[10], Record.EMPTY_HEADERS, callback, now);
    KafkaException exception = new KafkaException();
    batch.abort(exception);
    assertEquals(1, callback.invocations);
    assertEquals(exception, callback.exception);
    assertNull(callback.metadata);
    try {
        batch.abort(new KafkaException());
        fail("Expected exception from abort");
    } catch (IllegalStateException e) {
    // expected
    }
    assertEquals(1, callback.invocations);
    assertTrue(future.isDone());
    try {
        future.get();
        fail("Future should have thrown");
    } catch (ExecutionException e) {
        assertEquals(exception, e.getCause());
    }
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) KafkaException(org.apache.kafka.common.KafkaException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 15 with KafkaException

use of org.apache.kafka.common.KafkaException in project apache-kafka-on-k8s by banzaicloud.

the class ProducerInterceptorsTest method testOnAcknowledgementWithErrorChain.

@Test
public void testOnAcknowledgementWithErrorChain() {
    List<ProducerInterceptor<Integer, String>> interceptorList = new ArrayList<>();
    AppendProducerInterceptor interceptor1 = new AppendProducerInterceptor("One");
    interceptorList.add(interceptor1);
    ProducerInterceptors<Integer, String> interceptors = new ProducerInterceptors<>(interceptorList);
    // verify that metadata contains both topic and partition
    interceptors.onSendError(producerRecord, new TopicPartition(producerRecord.topic(), producerRecord.partition()), new KafkaException("Test"));
    assertEquals(1, onErrorAckCount);
    assertEquals(1, onErrorAckWithTopicPartitionSetCount);
    // verify that metadata contains both topic and partition (because record already contains partition)
    interceptors.onSendError(producerRecord, null, new KafkaException("Test"));
    assertEquals(2, onErrorAckCount);
    assertEquals(2, onErrorAckWithTopicPartitionSetCount);
    // if producer record does not contain partition, interceptor should get partition == -1
    ProducerRecord<Integer, String> record2 = new ProducerRecord<>("test2", null, 1, "value");
    interceptors.onSendError(record2, null, new KafkaException("Test"));
    assertEquals(3, onErrorAckCount);
    assertEquals(3, onErrorAckWithTopicSetCount);
    assertEquals(2, onErrorAckWithTopicPartitionSetCount);
    // if producer record does not contain partition, but topic/partition is passed to
    // onSendError, then interceptor should get valid partition
    int reassignedPartition = producerRecord.partition() + 1;
    interceptors.onSendError(record2, new TopicPartition(record2.topic(), reassignedPartition), new KafkaException("Test"));
    assertEquals(4, onErrorAckCount);
    assertEquals(4, onErrorAckWithTopicSetCount);
    assertEquals(3, onErrorAckWithTopicPartitionSetCount);
    // if both record and topic/partition are null, interceptor should not receive metadata
    interceptors.onSendError(null, null, new KafkaException("Test"));
    assertEquals(5, onErrorAckCount);
    assertEquals(4, onErrorAckWithTopicSetCount);
    assertEquals(3, onErrorAckWithTopicPartitionSetCount);
    interceptors.close();
}
Also used : ArrayList(java.util.ArrayList) TopicPartition(org.apache.kafka.common.TopicPartition) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) ProducerInterceptor(org.apache.kafka.clients.producer.ProducerInterceptor) KafkaException(org.apache.kafka.common.KafkaException) Test(org.junit.Test)

Aggregations

KafkaException (org.apache.kafka.common.KafkaException)262 Test (org.junit.Test)69 TopicPartition (org.apache.kafka.common.TopicPartition)56 Test (org.junit.jupiter.api.Test)47 HashMap (java.util.HashMap)40 IOException (java.io.IOException)39 StreamsException (org.apache.kafka.streams.errors.StreamsException)34 Map (java.util.Map)32 TimeoutException (org.apache.kafka.common.errors.TimeoutException)28 ArrayList (java.util.ArrayList)27 List (java.util.List)21 ByteBuffer (java.nio.ByteBuffer)19 ExecutionException (java.util.concurrent.ExecutionException)19 ConfigException (org.apache.kafka.common.config.ConfigException)16 TopicAuthorizationException (org.apache.kafka.common.errors.TopicAuthorizationException)14 HashSet (java.util.HashSet)13 Properties (java.util.Properties)13 Set (java.util.Set)11 Collectors (java.util.stream.Collectors)11 RecordMetadata (org.apache.kafka.clients.producer.RecordMetadata)11