Java Code Examples for org.apache.flink.streaming.connectors.kafka.internal.FlinkKafkaInternalProducer#commitTransaction()
The following examples show how to use
org.apache.flink.streaming.connectors.kafka.internal.FlinkKafkaInternalProducer#commitTransaction() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FlinkKafkaProducer.java From flink with Apache License 2.0 | 6 votes |
@Override protected void recoverAndCommit(FlinkKafkaProducer.KafkaTransactionState transaction) { if (transaction.isTransactional()) { FlinkKafkaInternalProducer<byte[], byte[]> producer = null; try { producer = initTransactionalProducer(transaction.transactionalId, false); producer.resumeTransaction(transaction.producerId, transaction.epoch); producer.commitTransaction(); } catch (InvalidTxnStateException | ProducerFencedException ex) { // That means we have committed this transaction before. LOG.warn("Encountered error {} while recovering transaction {}. " + "Presumably this transaction has been already committed before", ex, transaction); } finally { if (producer != null) { producer.close(0, TimeUnit.SECONDS); } } } }
Example 2
Source File: FlinkKafkaInternalProducerITCase.java From flink with Apache License 2.0 | 5 votes |
@Test(timeout = 30000L) public void testResumeTransaction() throws IOException { String topicName = "flink-kafka-producer-resume-transaction"; FlinkKafkaInternalProducer<String, String> kafkaProducer = new FlinkKafkaInternalProducer<>(extraProperties); try { kafkaProducer.initTransactions(); kafkaProducer.beginTransaction(); kafkaProducer.send(new ProducerRecord<>(topicName, "42", "42")); kafkaProducer.flush(); long producerId = kafkaProducer.getProducerId(); short epoch = kafkaProducer.getEpoch(); FlinkKafkaInternalProducer<String, String> resumeProducer = new FlinkKafkaInternalProducer<>(extraProperties); try { resumeProducer.resumeTransaction(producerId, epoch); resumeProducer.commitTransaction(); } finally { resumeProducer.close(Duration.ofSeconds(5)); } assertRecord(topicName, "42", "42"); // this shouldn't throw - in case of network split, old producer might attempt to commit it's transaction kafkaProducer.commitTransaction(); // this shouldn't fail also, for same reason as above resumeProducer = new FlinkKafkaInternalProducer<>(extraProperties); try { resumeProducer.resumeTransaction(producerId, epoch); resumeProducer.commitTransaction(); } finally { resumeProducer.close(Duration.ofSeconds(5)); } } finally { kafkaProducer.close(Duration.ofSeconds(5)); } deleteTestTopic(topicName); }
Example 3
Source File: FlinkKafkaInternalProducerITCase.java From flink with Apache License 2.0 | 4 votes |
@Test(timeout = 30000L, expected = IllegalStateException.class) public void testCommitTransactionAfterClosed() { String topicName = "testCommitTransactionAfterClosed"; FlinkKafkaInternalProducer<String, String> kafkaProducer = getClosedProducer(topicName); kafkaProducer.commitTransaction(); }
Example 4
Source File: FlinkKafkaInternalProducerITCase.java From flink with Apache License 2.0 | 4 votes |
@Test(timeout = 30000L, expected = IllegalStateException.class) public void testCommitTransactionAfterClosed() { String topicName = "testCommitTransactionAfterClosed"; FlinkKafkaInternalProducer<String, String> kafkaProducer = getClosedProducer(topicName); kafkaProducer.commitTransaction(); }