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);
}
}
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);
}
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());
}
}
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());
}
}
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();
}
Aggregations