use of org.apache.kafka.clients.producer.internals.ProducerMetadata in project kafka by apache.
the class KafkaProducerTest method testMeasureAbortTransactionDuration.
@Test
public void testMeasureAbortTransactionDuration() {
Map<String, Object> configs = new HashMap<>();
configs.put(ProducerConfig.TRANSACTIONAL_ID_CONFIG, "some.id");
configs.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9000");
Time time = new MockTime(1);
MetadataResponse initialUpdateResponse = RequestTestUtils.metadataUpdateWith(1, singletonMap("topic", 1));
ProducerMetadata metadata = newMetadata(0, Long.MAX_VALUE);
MockClient client = new MockClient(time, metadata);
client.updateMetadata(initialUpdateResponse);
client.prepareResponse(FindCoordinatorResponse.prepareResponse(Errors.NONE, "some.id", NODE));
client.prepareResponse(initProducerIdResponse(1L, (short) 5, Errors.NONE));
try (KafkaProducer<String, String> producer = kafkaProducer(configs, new StringSerializer(), new StringSerializer(), metadata, client, null, time)) {
producer.initTransactions();
client.prepareResponse(endTxnResponse(Errors.NONE));
producer.beginTransaction();
producer.abortTransaction();
double first = getMetricValue(producer, "txn-abort-time-ns-total");
assertTrue(first > 0);
client.prepareResponse(endTxnResponse(Errors.NONE));
producer.beginTransaction();
producer.abortTransaction();
assertTrue(getMetricValue(producer, "txn-abort-time-ns-total") > first);
}
}
use of org.apache.kafka.clients.producer.internals.ProducerMetadata in project kafka by apache.
the class KafkaProducerTest method testCommitTransactionWithRecordTooLargeException.
@Test
public void testCommitTransactionWithRecordTooLargeException() throws Exception {
Map<String, Object> configs = new HashMap<>();
configs.put(ProducerConfig.TRANSACTIONAL_ID_CONFIG, "some.id");
configs.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9000");
configs.put(ProducerConfig.MAX_REQUEST_SIZE_CONFIG, 1000);
Time time = new MockTime(1);
MetadataResponse initialUpdateResponse = RequestTestUtils.metadataUpdateWith(1, singletonMap("topic", 1));
ProducerMetadata metadata = mock(ProducerMetadata.class);
MockClient client = new MockClient(time, metadata);
client.updateMetadata(initialUpdateResponse);
client.prepareResponse(FindCoordinatorResponse.prepareResponse(Errors.NONE, "some.id", NODE));
client.prepareResponse(initProducerIdResponse(1L, (short) 5, Errors.NONE));
when(metadata.fetch()).thenReturn(onePartitionCluster);
String largeString = IntStream.range(0, 1000).mapToObj(i -> "*").collect(Collectors.joining());
ProducerRecord<String, String> largeRecord = new ProducerRecord<>(topic, "large string", largeString);
try (KafkaProducer<String, String> producer = kafkaProducer(configs, new StringSerializer(), new StringSerializer(), metadata, client, null, time)) {
producer.initTransactions();
client.prepareResponse(endTxnResponse(Errors.NONE));
producer.beginTransaction();
TestUtils.assertFutureError(producer.send(largeRecord), RecordTooLargeException.class);
assertThrows(KafkaException.class, producer::commitTransaction);
}
}
use of org.apache.kafka.clients.producer.internals.ProducerMetadata in project kafka by apache.
the class KafkaProducerTest method testCloseIsForcedOnPendingInitProducerId.
@Test
public void testCloseIsForcedOnPendingInitProducerId() throws InterruptedException {
Map<String, Object> configs = new HashMap<>();
configs.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9000");
configs.put(ProducerConfig.TRANSACTIONAL_ID_CONFIG, "this-is-a-transactional-id");
Time time = new MockTime();
MetadataResponse initialUpdateResponse = RequestTestUtils.metadataUpdateWith(1, singletonMap("testTopic", 1));
ProducerMetadata metadata = newMetadata(0, Long.MAX_VALUE);
metadata.updateWithCurrentRequestVersion(initialUpdateResponse, false, time.milliseconds());
MockClient client = new MockClient(time, metadata);
Producer<String, String> producer = kafkaProducer(configs, new StringSerializer(), new StringSerializer(), metadata, client, null, time);
ExecutorService executorService = Executors.newSingleThreadExecutor();
CountDownLatch assertionDoneLatch = new CountDownLatch(1);
client.prepareResponse(FindCoordinatorResponse.prepareResponse(Errors.NONE, "this-is-a-transactional-id", NODE));
executorService.submit(() -> {
assertThrows(KafkaException.class, producer::initTransactions);
assertionDoneLatch.countDown();
});
client.waitForRequests(1, 2000);
producer.close(Duration.ofMillis(1000));
assertionDoneLatch.await(5000, TimeUnit.MILLISECONDS);
}
use of org.apache.kafka.clients.producer.internals.ProducerMetadata in project kafka by apache.
the class KafkaProducerTest method testTransactionalMethodThrowsWhenSenderClosed.
@Test
public void testTransactionalMethodThrowsWhenSenderClosed() {
Map<String, Object> configs = new HashMap<>();
configs.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9000");
configs.put(ProducerConfig.TRANSACTIONAL_ID_CONFIG, "this-is-a-transactional-id");
Time time = new MockTime();
MetadataResponse initialUpdateResponse = RequestTestUtils.metadataUpdateWith(1, emptyMap());
ProducerMetadata metadata = newMetadata(0, Long.MAX_VALUE);
metadata.updateWithCurrentRequestVersion(initialUpdateResponse, false, time.milliseconds());
MockClient client = new MockClient(time, metadata);
Producer<String, String> producer = kafkaProducer(configs, new StringSerializer(), new StringSerializer(), metadata, client, null, time);
producer.close();
assertThrows(IllegalStateException.class, producer::initTransactions);
}
use of org.apache.kafka.clients.producer.internals.ProducerMetadata in project kafka by apache.
the class KafkaProducerTest method testCallbackHandlesError.
@Test
public void testCallbackHandlesError() throws Exception {
Map<String, Object> configs = new HashMap<>();
configs.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9000");
configs.put(ProducerConfig.MAX_BLOCK_MS_CONFIG, "1000");
Time time = new MockTime();
ProducerMetadata producerMetadata = newMetadata(0, Long.MAX_VALUE);
MockClient client = new MockClient(time, producerMetadata);
// Invalid topic name due to space
String invalidTopicName = "topic abc";
try (Producer<String, String> producer = kafkaProducer(configs, new StringSerializer(), new StringSerializer(), producerMetadata, client, null, time)) {
ProducerRecord<String, String> record = new ProducerRecord<>(invalidTopicName, "HelloKafka");
// Here's the important piece of the test. Let's make sure that the RecordMetadata we get
// is non-null and adheres to the onCompletion contract.
Callback callBack = (recordMetadata, exception) -> {
assertNotNull(exception);
assertNotNull(recordMetadata);
assertNotNull(recordMetadata.topic(), "Topic name should be valid even on send failure");
assertEquals(invalidTopicName, recordMetadata.topic());
assertNotNull(recordMetadata.partition(), "Partition should be valid even on send failure");
assertFalse(recordMetadata.hasOffset());
assertEquals(ProduceResponse.INVALID_OFFSET, recordMetadata.offset());
assertFalse(recordMetadata.hasTimestamp());
assertEquals(RecordBatch.NO_TIMESTAMP, recordMetadata.timestamp());
assertEquals(-1, recordMetadata.serializedKeySize());
assertEquals(-1, recordMetadata.serializedValueSize());
assertEquals(-1, recordMetadata.partition());
};
producer.send(record, callBack);
}
}
Aggregations