org.apache.flink.streaming.connectors.kafka.internal.FlinkKafkaProducer Java Examples
The following examples show how to use
org.apache.flink.streaming.connectors.kafka.internal.FlinkKafkaProducer.
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: FlinkKafkaProducer011.java From Flink-CEPplus with Apache License 2.0 | 6 votes |
@Override protected void recoverAndCommit(KafkaTransactionState transaction) { if (transaction.isTransactional()) { try ( FlinkKafkaProducer<byte[], byte[]> 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); } } }
Example #2
Source File: FlinkKafkaProducer011.java From Flink-CEPplus with Apache License 2.0 | 6 votes |
@Override protected KafkaTransactionState beginTransaction() throws FlinkKafka011Exception { switch (semantic) { case EXACTLY_ONCE: FlinkKafkaProducer<byte[], byte[]> producer = createTransactionalProducer(); producer.beginTransaction(); return new KafkaTransactionState(producer.getTransactionalId(), producer); case AT_LEAST_ONCE: case NONE: // Do not create new producer on each beginTransaction() if it is not necessary final KafkaTransactionState currentTransaction = currentTransaction(); if (currentTransaction != null && currentTransaction.producer != null) { return new KafkaTransactionState(currentTransaction.producer); } return new KafkaTransactionState(initNonTransactionalProducer(true)); default: throw new UnsupportedOperationException("Not implemented semantic"); } }
Example #3
Source File: FlinkKafkaProducer011.java From flink with Apache License 2.0 | 6 votes |
private void abortTransactions(Set<String> transactionalIds) { transactionalIds.parallelStream().forEach(transactionalId -> { // don't mess with the original configuration or any other properties of the // original object // -> create an internal kafka producer on our own and do not rely on // initTransactionalProducer(). final Properties myConfig = new Properties(); myConfig.putAll(producerConfig); initTransactionalProducerConfig(myConfig, transactionalId); try (FlinkKafkaProducer<byte[], byte[]> kafkaProducer = new FlinkKafkaProducer<>(myConfig)) { // it suffices to call initTransactions - this will abort any lingering transactions kafkaProducer.initTransactions(); } }); }
Example #4
Source File: FlinkKafkaProducer011.java From flink with Apache License 2.0 | 6 votes |
private void abortTransactions(Set<String> transactionalIds) { transactionalIds.parallelStream().forEach(transactionalId -> { // don't mess with the original configuration or any other properties of the // original object // -> create an internal kafka producer on our own and do not rely on // initTransactionalProducer(). final Properties myConfig = new Properties(); myConfig.putAll(producerConfig); initTransactionalProducerConfig(myConfig, transactionalId); try (FlinkKafkaProducer<byte[], byte[]> kafkaProducer = new FlinkKafkaProducer<>(myConfig)) { // it suffices to call initTransactions - this will abort any lingering transactions kafkaProducer.initTransactions(); } }); }
Example #5
Source File: FlinkKafkaProducer011.java From flink with Apache License 2.0 | 6 votes |
@Override protected KafkaTransactionState beginTransaction() throws FlinkKafka011Exception { switch (semantic) { case EXACTLY_ONCE: FlinkKafkaProducer<byte[], byte[]> producer = createTransactionalProducer(); producer.beginTransaction(); return new KafkaTransactionState(producer.getTransactionalId(), producer); case AT_LEAST_ONCE: case NONE: // Do not create new producer on each beginTransaction() if it is not necessary final KafkaTransactionState currentTransaction = currentTransaction(); if (currentTransaction != null && currentTransaction.producer != null) { return new KafkaTransactionState(currentTransaction.producer); } return new KafkaTransactionState(initNonTransactionalProducer(true)); default: throw new UnsupportedOperationException("Not implemented semantic"); } }
Example #6
Source File: FlinkKafkaProducer011.java From flink with Apache License 2.0 | 6 votes |
@Override protected void recoverAndCommit(KafkaTransactionState transaction) { if (transaction.isTransactional()) { try ( FlinkKafkaProducer<byte[], byte[]> 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); } } }
Example #7
Source File: FlinkKafkaProducer011.java From flink with Apache License 2.0 | 6 votes |
@Override protected KafkaTransactionState beginTransaction() throws FlinkKafka011Exception { switch (semantic) { case EXACTLY_ONCE: FlinkKafkaProducer<byte[], byte[]> producer = createTransactionalProducer(); producer.beginTransaction(); return new KafkaTransactionState(producer.getTransactionalId(), producer); case AT_LEAST_ONCE: case NONE: // Do not create new producer on each beginTransaction() if it is not necessary final KafkaTransactionState currentTransaction = currentTransaction(); if (currentTransaction != null && currentTransaction.producer != null) { return new KafkaTransactionState(currentTransaction.producer); } return new KafkaTransactionState(initNonTransactionalProducer(true)); default: throw new UnsupportedOperationException("Not implemented semantic"); } }
Example #8
Source File: FlinkKafkaProducer011.java From flink with Apache License 2.0 | 6 votes |
@Override protected void recoverAndCommit(KafkaTransactionState transaction) { if (transaction.isTransactional()) { try ( FlinkKafkaProducer<byte[], byte[]> 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); } } }
Example #9
Source File: FlinkKafkaProducer011.java From flink with Apache License 2.0 | 5 votes |
KafkaTransactionState( @Nullable String transactionalId, long producerId, short epoch, FlinkKafkaProducer<byte[], byte[]> producer) { this.transactionalId = transactionalId; this.producerId = producerId; this.epoch = epoch; this.producer = producer; }
Example #10
Source File: FlinkKafkaProducerITCase.java From flink with Apache License 2.0 | 5 votes |
private FlinkKafkaProducer<String, String> getClosedProducer(String topicName) { FlinkKafkaProducer<String, String> kafkaProducer = new FlinkKafkaProducer<>(extraProperties); kafkaProducer.initTransactions(); kafkaProducer.beginTransaction(); kafkaProducer.send(new ProducerRecord<>(topicName, "42", "42")); kafkaProducer.close(); return kafkaProducer; }
Example #11
Source File: FlinkKafkaProducerITCase.java From flink with Apache License 2.0 | 5 votes |
@Test(timeout = 30000L) public void testHappyPath() throws IOException { String topicName = "flink-kafka-producer-happy-path"; try (Producer<String, String> kafkaProducer = new FlinkKafkaProducer<>(extraProperties)) { kafkaProducer.initTransactions(); kafkaProducer.beginTransaction(); kafkaProducer.send(new ProducerRecord<>(topicName, "42", "42")); kafkaProducer.commitTransaction(); } assertRecord(topicName, "42", "42"); deleteTestTopic(topicName); }
Example #12
Source File: FlinkKafkaProducerITCase.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"; try (FlinkKafkaProducer<String, String> kafkaProducer = new FlinkKafkaProducer<>(extraProperties)) { kafkaProducer.initTransactions(); kafkaProducer.beginTransaction(); kafkaProducer.send(new ProducerRecord<>(topicName, "42", "42")); kafkaProducer.flush(); long producerId = kafkaProducer.getProducerId(); short epoch = kafkaProducer.getEpoch(); try (FlinkKafkaProducer<String, String> resumeProducer = new FlinkKafkaProducer<>(extraProperties)) { resumeProducer.resumeTransaction(producerId, epoch); resumeProducer.commitTransaction(); } 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 try (FlinkKafkaProducer<String, String> resumeProducer = new FlinkKafkaProducer<>(extraProperties)) { resumeProducer.resumeTransaction(producerId, epoch); resumeProducer.commitTransaction(); } } deleteTestTopic(topicName); }
Example #13
Source File: FlinkKafkaProducerITCase.java From flink with Apache License 2.0 | 5 votes |
@Test(timeout = 30000L, expected = IllegalStateException.class) public void testBeginTransactionAfterClosed() { FlinkKafkaProducer<String, String> kafkaProducer = new FlinkKafkaProducer<>(extraProperties); kafkaProducer.initTransactions(); kafkaProducer.close(); kafkaProducer.beginTransaction(); }
Example #14
Source File: FlinkKafkaProducerITCase.java From flink with Apache License 2.0 | 5 votes |
@Test(timeout = 30000L, expected = IllegalStateException.class) public void testAbortTransactionAfterClosed() { String topicName = "testAbortTransactionAfterClosed"; FlinkKafkaProducer<String, String> kafkaProducer = getClosedProducer(topicName); kafkaProducer.abortTransaction(); kafkaProducer.resumeTransaction(0L, (short) 1); }
Example #15
Source File: FlinkKafkaProducerITCase.java From flink with Apache License 2.0 | 5 votes |
private FlinkKafkaProducer<String, String> getClosedProducer(String topicName) { FlinkKafkaProducer<String, String> kafkaProducer = new FlinkKafkaProducer<>(extraProperties); kafkaProducer.initTransactions(); kafkaProducer.beginTransaction(); kafkaProducer.send(new ProducerRecord<>(topicName, "42", "42")); kafkaProducer.close(); return kafkaProducer; }
Example #16
Source File: FlinkKafkaProducer011.java From flink with Apache License 2.0 | 5 votes |
@Override protected void recoverAndAbort(KafkaTransactionState transaction) { if (transaction.isTransactional()) { try ( FlinkKafkaProducer<byte[], byte[]> producer = initTransactionalProducer(transaction.transactionalId, false)) { producer.initTransactions(); } } }
Example #17
Source File: FlinkKafkaProducer011.java From flink with Apache License 2.0 | 5 votes |
/** * For each checkpoint we create new {@link FlinkKafkaProducer} so that new transactions will not clash * with transactions created during previous checkpoints ({@code producer.initTransactions()} assures that we * obtain new producerId and epoch counters). */ private FlinkKafkaProducer<byte[], byte[]> createTransactionalProducer() throws FlinkKafka011Exception { String transactionalId = availableTransactionalIds.poll(); if (transactionalId == null) { throw new FlinkKafka011Exception( FlinkKafka011ErrorCode.PRODUCERS_POOL_EMPTY, "Too many ongoing snapshots. Increase kafka producers pool size or decrease number of concurrent checkpoints."); } FlinkKafkaProducer<byte[], byte[]> producer = initTransactionalProducer(transactionalId, true); producer.initTransactions(); return producer; }
Example #18
Source File: FlinkKafkaProducer011.java From flink with Apache License 2.0 | 5 votes |
@VisibleForTesting @Internal public KafkaTransactionState( @Nullable String transactionalId, long producerId, short epoch, FlinkKafkaProducer<byte[], byte[]> producer) { this.transactionalId = transactionalId; this.producerId = producerId; this.epoch = epoch; this.producer = producer; }
Example #19
Source File: FlinkKafkaProducerITCase.java From flink with Apache License 2.0 | 5 votes |
@Test(timeout = 30000L) public void testHappyPath() throws IOException { String topicName = "flink-kafka-producer-happy-path"; try (Producer<String, String> kafkaProducer = new FlinkKafkaProducer<>(extraProperties)) { kafkaProducer.initTransactions(); kafkaProducer.beginTransaction(); kafkaProducer.send(new ProducerRecord<>(topicName, "42", "42")); kafkaProducer.commitTransaction(); } assertRecord(topicName, "42", "42"); deleteTestTopic(topicName); }
Example #20
Source File: FlinkKafkaProducerITCase.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"; try (FlinkKafkaProducer<String, String> kafkaProducer = new FlinkKafkaProducer<>(extraProperties)) { kafkaProducer.initTransactions(); kafkaProducer.beginTransaction(); kafkaProducer.send(new ProducerRecord<>(topicName, "42", "42")); kafkaProducer.flush(); long producerId = kafkaProducer.getProducerId(); short epoch = kafkaProducer.getEpoch(); try (FlinkKafkaProducer<String, String> resumeProducer = new FlinkKafkaProducer<>(extraProperties)) { resumeProducer.resumeTransaction(producerId, epoch); resumeProducer.commitTransaction(); } 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 try (FlinkKafkaProducer<String, String> resumeProducer = new FlinkKafkaProducer<>(extraProperties)) { resumeProducer.resumeTransaction(producerId, epoch); resumeProducer.commitTransaction(); } } deleteTestTopic(topicName); }
Example #21
Source File: FlinkKafkaProducerITCase.java From flink with Apache License 2.0 | 5 votes |
@Test(timeout = 30000L, expected = IllegalStateException.class) public void testBeginTransactionAfterClosed() { FlinkKafkaProducer<String, String> kafkaProducer = new FlinkKafkaProducer<>(extraProperties); kafkaProducer.initTransactions(); kafkaProducer.close(); kafkaProducer.beginTransaction(); }
Example #22
Source File: FlinkKafkaProducerITCase.java From flink with Apache License 2.0 | 5 votes |
@Test(timeout = 30000L, expected = IllegalStateException.class) public void testAbortTransactionAfterClosed() { String topicName = "testAbortTransactionAfterClosed"; FlinkKafkaProducer<String, String> kafkaProducer = getClosedProducer(topicName); kafkaProducer.abortTransaction(); kafkaProducer.resumeTransaction(0L, (short) 1); }
Example #23
Source File: FlinkKafkaProducerITCase.java From flink with Apache License 2.0 | 5 votes |
@Test(timeout = 30000L) public void testProducerWhenCommitEmptyPartitionsToOutdatedTxnCoordinator() throws Exception { String topic = "flink-kafka-producer-txn-coordinator-changed"; createTestTopic(topic, 1, 2); try (Producer<String, String> kafkaProducer = new FlinkKafkaProducer<>(extraProperties)) { kafkaProducer.initTransactions(); kafkaProducer.beginTransaction(); restartBroker(kafkaServer.getLeaderToShutDown("__transaction_state")); kafkaProducer.flush(); kafkaProducer.commitTransaction(); } deleteTestTopic(topic); }
Example #24
Source File: FlinkKafkaProducer011.java From Flink-CEPplus with Apache License 2.0 | 5 votes |
@Override protected void recoverAndAbort(KafkaTransactionState transaction) { if (transaction.isTransactional()) { try ( FlinkKafkaProducer<byte[], byte[]> producer = initTransactionalProducer(transaction.transactionalId, false)) { producer.initTransactions(); } } }
Example #25
Source File: FlinkKafkaProducer011.java From Flink-CEPplus with Apache License 2.0 | 5 votes |
KafkaTransactionState( @Nullable String transactionalId, long producerId, short epoch, FlinkKafkaProducer<byte[], byte[]> producer) { this.transactionalId = transactionalId; this.producerId = producerId; this.epoch = epoch; this.producer = producer; }
Example #26
Source File: FlinkKafkaProducerITCase.java From Flink-CEPplus with Apache License 2.0 | 5 votes |
@Test(timeout = 30000L) public void testHappyPath() throws IOException { String topicName = "flink-kafka-producer-happy-path"; try (Producer<String, String> kafkaProducer = new FlinkKafkaProducer<>(extraProperties)) { kafkaProducer.initTransactions(); kafkaProducer.beginTransaction(); kafkaProducer.send(new ProducerRecord<>(topicName, "42", "42")); kafkaProducer.commitTransaction(); } assertRecord(topicName, "42", "42"); deleteTestTopic(topicName); }
Example #27
Source File: FlinkKafkaProducerITCase.java From Flink-CEPplus with Apache License 2.0 | 5 votes |
@Test(timeout = 30000L) public void testResumeTransaction() throws IOException { String topicName = "flink-kafka-producer-resume-transaction"; try (FlinkKafkaProducer<String, String> kafkaProducer = new FlinkKafkaProducer<>(extraProperties)) { kafkaProducer.initTransactions(); kafkaProducer.beginTransaction(); kafkaProducer.send(new ProducerRecord<>(topicName, "42", "42")); kafkaProducer.flush(); long producerId = kafkaProducer.getProducerId(); short epoch = kafkaProducer.getEpoch(); try (FlinkKafkaProducer<String, String> resumeProducer = new FlinkKafkaProducer<>(extraProperties)) { resumeProducer.resumeTransaction(producerId, epoch); resumeProducer.commitTransaction(); } 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 try (FlinkKafkaProducer<String, String> resumeProducer = new FlinkKafkaProducer<>(extraProperties)) { resumeProducer.resumeTransaction(producerId, epoch); resumeProducer.commitTransaction(); } } deleteTestTopic(topicName); }
Example #28
Source File: FlinkKafkaProducer011.java From flink with Apache License 2.0 | 5 votes |
@Override protected void recoverAndAbort(KafkaTransactionState transaction) { if (transaction.isTransactional()) { try ( FlinkKafkaProducer<byte[], byte[]> producer = initTransactionalProducer(transaction.transactionalId, false)) { producer.initTransactions(); } } }
Example #29
Source File: FlinkKafkaProducer011.java From flink with Apache License 2.0 | 5 votes |
/** * For each checkpoint we create new {@link FlinkKafkaProducer} so that new transactions will not clash * with transactions created during previous checkpoints ({@code producer.initTransactions()} assures that we * obtain new producerId and epoch counters). */ private FlinkKafkaProducer<byte[], byte[]> createTransactionalProducer() throws FlinkKafka011Exception { String transactionalId = availableTransactionalIds.poll(); if (transactionalId == null) { throw new FlinkKafka011Exception( FlinkKafka011ErrorCode.PRODUCERS_POOL_EMPTY, "Too many ongoing snapshots. Increase kafka producers pool size or decrease number of concurrent checkpoints."); } FlinkKafkaProducer<byte[], byte[]> producer = initTransactionalProducer(transactionalId, true); producer.initTransactions(); return producer; }
Example #30
Source File: FlinkKafkaProducer011.java From Flink-CEPplus with Apache License 2.0 | 5 votes |
/** * For each checkpoint we create new {@link FlinkKafkaProducer} so that new transactions will not clash * with transactions created during previous checkpoints ({@code producer.initTransactions()} assures that we * obtain new producerId and epoch counters). */ private FlinkKafkaProducer<byte[], byte[]> createTransactionalProducer() throws FlinkKafka011Exception { String transactionalId = availableTransactionalIds.poll(); if (transactionalId == null) { throw new FlinkKafka011Exception( FlinkKafka011ErrorCode.PRODUCERS_POOL_EMPTY, "Too many ongoing snapshots. Increase kafka producers pool size or decrease number of concurrent checkpoints."); } FlinkKafkaProducer<byte[], byte[]> producer = initTransactionalProducer(transactionalId, true); producer.initTransactions(); return producer; }