use of org.apache.kafka.common.KafkaException in project kafka by apache.
the class KafkaProducerTest method testConstructorFailureCloseResource.
@Test
public void testConstructorFailureCloseResource() {
Properties props = new Properties();
props.setProperty(ProducerConfig.CLIENT_ID_CONFIG, "testConstructorClose");
props.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "some.invalid.hostname.foo.bar.local:9999");
props.setProperty(ProducerConfig.METRIC_REPORTER_CLASSES_CONFIG, MockMetricsReporter.class.getName());
final int oldInitCount = MockMetricsReporter.INIT_COUNT.get();
final int oldCloseCount = MockMetricsReporter.CLOSE_COUNT.get();
try (KafkaProducer<byte[], byte[]> ignored = new KafkaProducer<>(props, new ByteArraySerializer(), new ByteArraySerializer())) {
fail("should have caught an exception and returned");
} catch (KafkaException e) {
assertEquals(oldInitCount + 1, MockMetricsReporter.INIT_COUNT.get());
assertEquals(oldCloseCount + 1, MockMetricsReporter.CLOSE_COUNT.get());
assertEquals("Failed to construct kafka producer", e.getMessage());
}
}
use of org.apache.kafka.common.KafkaException in project kafka by apache.
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 kafka by apache.
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.complete(500L, 2342342341L));
assertFalse(batch.completeExceptionally(new KafkaException(), index -> 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 kafka by apache.
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();
}
use of org.apache.kafka.common.KafkaException in project kafka by apache.
the class ConsumerCoordinator method onLeavePrepare.
@Override
public void onLeavePrepare() {
// Save the current Generation, as the hb thread can change it at any time
final Generation currentGeneration = generation();
log.debug("Executing onLeavePrepare with generation {}", currentGeneration);
// we should reset assignment and trigger the callback before leaving group
SortedSet<TopicPartition> droppedPartitions = new TreeSet<>(COMPARATOR);
droppedPartitions.addAll(subscriptions.assignedPartitions());
if (subscriptions.hasAutoAssignedPartitions() && !droppedPartitions.isEmpty()) {
final Exception e;
if ((currentGeneration.generationId == Generation.NO_GENERATION.generationId || currentGeneration.memberId.equals(Generation.NO_GENERATION.memberId)) || rebalanceInProgress()) {
e = invokePartitionsLost(droppedPartitions);
} else {
e = invokePartitionsRevoked(droppedPartitions);
}
subscriptions.assignFromSubscribed(Collections.emptySet());
if (e != null) {
throw new KafkaException("User rebalance callback throws an error", e);
}
}
}
Aggregations