org.apache.kafka.common.errors.ProducerFencedException Java Examples

The following examples show how to use org.apache.kafka.common.errors.ProducerFencedException. 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: TestPublisherLease.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testPoisonOnExceptionCreatingTransaction() {
    final PoisonCountingLease lease = new PoisonCountingLease();

    final FlowFile flowFile = Mockito.spy(new MockFlowFile(1L));
    // Need a size grater than zero to make the lease reads the InputStream.
    Mockito.when(flowFile.getSize()).thenReturn(1L);
    doAnswer(new Answer<Object>() {
        @Override
        public Object answer(final InvocationOnMock invocationOnMock) {
            throw new ProducerFencedException("Intenitional exception thrown from unit test");
        }
    }).when(producer).beginTransaction();

    try {
        lease.beginTransaction();
        Assert.fail("Expected ProducerFencedException");
    } catch (final ProducerFencedException pfe) {
        // expected
    }

    assertEquals(1, lease.getPoisonCount());
}
 
Example #2
Source File: TestPublishKafka_2_0.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testPublisherPoisonedIfFencedDuringTransactionCreation() {
    runner.enqueue("hello world");
    runner.enqueue("Hello World");

    doAnswer(new Answer<Object>() {
        @Override
        public Object answer(final InvocationOnMock invocationOnMock) {
            throw new ProducerFencedException("Intentional ProducedFencedException for unit test");
        }
    }).when(mockLease).beginTransaction();

    runner.run();
    runner.assertAllFlowFilesTransferred(PublishKafka_2_0.REL_FAILURE, 2);

    verify(mockLease, times(1)).poison();
    verify(mockLease, times(1)).close();
}
 
Example #3
Source File: FlinkKafkaProducer.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
protected void recoverAndCommit(FlinkKafkaProducer.KafkaTransactionState transaction) {
	if (transaction.isTransactional()) {
		try (
			FlinkKafkaInternalProducer<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 #4
Source File: TestPublishKafkaRecord_2_0.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testFailureWhenCreationgTransaction() {
    runner.enqueue("John Doe, 48");

    doAnswer(new Answer<Object>() {
        @Override
        public Object answer(final InvocationOnMock invocationOnMock) {
            throw new ProducerFencedException("Intentional ProducedFencedException for unit test");
        }
    }).when(mockLease).beginTransaction();

    runner.run();
    runner.assertAllFlowFilesTransferred(PublishKafkaRecord_2_0.REL_FAILURE, 1);

    verify(mockLease, times(1)).poison();
    verify(mockLease, times(1)).close();
}
 
Example #5
Source File: FlinkKafkaProducer.java    From flink with Apache License 2.0 6 votes vote down vote up
@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 #6
Source File: TestPublisherLease.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testPoisonOnExceptionCreatingTransaction() {
    final PoisonCountingLease lease = new PoisonCountingLease();

    final FlowFile flowFile = Mockito.spy(new MockFlowFile(1L));
    // Need a size grater than zero to make the lease reads the InputStream.
    Mockito.when(flowFile.getSize()).thenReturn(1L);
    doAnswer(new Answer<Object>() {
        @Override
        public Object answer(final InvocationOnMock invocationOnMock) {
            throw new ProducerFencedException("Intenitional exception thrown from unit test");
        }
    }).when(producer).beginTransaction();

    try {
        lease.beginTransaction();
        Assert.fail("Expected ProducerFencedException");
    } catch (final ProducerFencedException pfe) {
        // expected
    }

    assertEquals(1, lease.getPoisonCount());
}
 
Example #7
Source File: FlinkKafkaProducer011.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@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 #8
Source File: FlinkKafkaProducer011.java    From flink with Apache License 2.0 6 votes vote down vote up
@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 6 votes vote down vote up
@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 #10
Source File: FlinkKafkaInternalProducer.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void beginTransaction() throws ProducerFencedException {
	synchronized (producerClosingLock) {
		ensureNotClosed();
		kafkaProducer.beginTransaction();
	}
}
 
Example #11
Source File: TransactionOnlySend.java    From BigData-In-Practice with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    Properties properties = new Properties();
    properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,                StringSerializer.class.getName());
    properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,                StringSerializer.class.getName());
    properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, brokerList);
    properties.put(ProducerConfig.TRANSACTIONAL_ID_CONFIG, transactionId);

    KafkaProducer<String, String> producer = new KafkaProducer<>(properties);

    producer.initTransactions();
    producer.beginTransaction();

    try {
        //处理业务逻辑并创建ProducerRecord
        ProducerRecord<String, String> record1 = new ProducerRecord<>(topic, "msg1");
        producer.send(record1);
        ProducerRecord<String, String> record2 = new ProducerRecord<>(topic, "msg2");
        producer.send(record2);
        ProducerRecord<String, String> record3 = new ProducerRecord<>(topic, "msg3");
        producer.send(record3);
        //处理一些其它逻辑
        producer.commitTransaction();
    } catch (ProducerFencedException e) {
        producer.abortTransaction();
    }
    producer.close();
}
 
Example #12
Source File: LiKafkaInstrumentedProducerImpl.java    From li-apache-kafka-clients with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void beginTransaction() throws ProducerFencedException {
  verifyOpen();

  delegateLock.readLock().lock();
  try {
    delegate.beginTransaction();
  } finally {
    delegateLock.readLock().unlock();
  }
}
 
Example #13
Source File: LiKafkaInstrumentedProducerImpl.java    From li-apache-kafka-clients with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void commitTransaction() throws ProducerFencedException {
  verifyOpen();

  delegateLock.readLock().lock();
  try {
    delegate.commitTransaction();
  } finally {
    delegateLock.readLock().unlock();
  }
}
 
Example #14
Source File: LiKafkaInstrumentedProducerImpl.java    From li-apache-kafka-clients with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void sendOffsetsToTransaction(Map<TopicPartition, OffsetAndMetadata> offsets, String consumerGroupId) throws ProducerFencedException {
  verifyOpen();

  delegateLock.readLock().lock();
  try {
    delegate.sendOffsetsToTransaction(offsets, consumerGroupId);
  } finally {
    delegateLock.readLock().unlock();
  }
}
 
Example #15
Source File: LiKafkaInstrumentedProducerImpl.java    From li-apache-kafka-clients with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void abortTransaction() throws ProducerFencedException {
  verifyOpen();

  delegateLock.readLock().lock();
  try {
    delegate.abortTransaction();
  } finally {
    delegateLock.readLock().unlock();
  }
}
 
Example #16
Source File: FlinkKafkaInternalProducer.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void sendOffsetsToTransaction(Map<TopicPartition, OffsetAndMetadata> offsets, String consumerGroupId) throws ProducerFencedException {
	synchronized (producerClosingLock) {
		ensureNotClosed();
		kafkaProducer.sendOffsetsToTransaction(offsets, consumerGroupId);
	}
}
 
Example #17
Source File: FlinkKafkaProducerITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
private void closeIgnoringProducerFenced(AutoCloseable autoCloseable) throws Exception {
	try {
		autoCloseable.close();
	}
	catch (Exception ex) {
		if (!(ex.getCause() instanceof ProducerFencedException)) {
			throw ex;
		}
	}
}
 
Example #18
Source File: FlinkKafkaProducer.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void beginTransaction() throws ProducerFencedException {
	synchronized (producerClosingLock) {
		ensureNotClosed();
		kafkaProducer.beginTransaction();
	}
}
 
Example #19
Source File: FlinkKafkaProducer.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void commitTransaction() throws ProducerFencedException {
	synchronized (producerClosingLock) {
		ensureNotClosed();
		kafkaProducer.commitTransaction();
	}
}
 
Example #20
Source File: FlinkKafkaProducer.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void abortTransaction() throws ProducerFencedException {
	synchronized (producerClosingLock) {
		ensureNotClosed();
		kafkaProducer.abortTransaction();
	}
}
 
Example #21
Source File: FlinkKafkaProducer.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void sendOffsetsToTransaction(Map<TopicPartition, OffsetAndMetadata> offsets, String consumerGroupId) throws ProducerFencedException {
	synchronized (producerClosingLock) {
		ensureNotClosed();
		kafkaProducer.sendOffsetsToTransaction(offsets, consumerGroupId);
	}
}
 
Example #22
Source File: FlinkKafkaInternalProducer.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void abortTransaction() throws ProducerFencedException {
	synchronized (producerClosingLock) {
		ensureNotClosed();
		kafkaProducer.abortTransaction();
	}
}
 
Example #23
Source File: FlinkKafkaProducer.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void sendOffsetsToTransaction(Map<TopicPartition, OffsetAndMetadata> offsets, String consumerGroupId) throws ProducerFencedException {
	synchronized (producerClosingLock) {
		ensureNotClosed();
		kafkaProducer.sendOffsetsToTransaction(offsets, consumerGroupId);
	}
}
 
Example #24
Source File: KafkaPublisherTest.java    From extension-kafka with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static DefaultProducerFactory<String, byte[]> producerFactoryWithFencedExceptionOnCommit() {
    DefaultProducerFactory<String, byte[]> producerFactory = mock(DefaultProducerFactory.class);
    Producer<String, byte[]> producer = mock(Producer.class, "ExceptionOnCommitTxMock");
    when(producerFactory.confirmationMode()).thenReturn(ConfirmationMode.TRANSACTIONAL);
    when(producerFactory.createProducer()).thenReturn(producer);
    doThrow(ProducerFencedException.class).when(producer).commitTransaction();
    return producerFactory;
}
 
Example #25
Source File: FlinkKafkaProducer011ITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
private void closeIgnoringProducerFenced(AutoCloseable autoCloseable) throws Exception {
	try {
		autoCloseable.close();
	}
	catch (Exception ex) {
		if (!(ex.getCause() instanceof ProducerFencedException)) {
			throw ex;
		}
	}
}
 
Example #26
Source File: FlinkKafkaProducer011ITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
private void closeIgnoringProducerFenced(AutoCloseable autoCloseable) throws Exception {
	try {
		autoCloseable.close();
	}
	catch (Exception ex) {
		if (!(ex.getCause() instanceof ProducerFencedException)) {
			throw ex;
		}
	}
}
 
Example #27
Source File: KafkaPublisherTest.java    From extension-kafka with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static DefaultProducerFactory<String, byte[]> producerFactoryWithFencedExceptionOnBeginTransaction() {
    DefaultProducerFactory<String, byte[]> producerFactory =
            mock(DefaultProducerFactory.class, "FactoryForExceptionOnBeginTx");
    Producer<String, byte[]> producer = mock(Producer.class, "ExceptionOnBeginTxMock");
    when(producerFactory.confirmationMode()).thenReturn(ConfirmationMode.TRANSACTIONAL);
    when(producerFactory.createProducer()).thenReturn(producer);
    doThrow(ProducerFencedException.class).when(producer).beginTransaction();
    return producerFactory;
}
 
Example #28
Source File: TransactionConsumeTransformProduce.java    From BigData-In-Practice with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    //初始化生产者和消费者
    KafkaConsumer<String, String> consumer = new KafkaConsumer<>(getConsumerProperties());
    consumer.subscribe(Collections.singletonList("topic-source"));
    KafkaProducer<String, String> producer = new KafkaProducer<>(getProducerProperties());
    //初始化事务
    producer.initTransactions();
    while (true) {
        ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(1000));
        if (!records.isEmpty()) {
            Map<TopicPartition, OffsetAndMetadata> offsets = new HashMap<>();
            //开启事务
            producer.beginTransaction();
            try {
                for (TopicPartition partition : records.partitions()) {
                    List<ConsumerRecord<String, String>> partitionRecords = records.records(partition);
                    for (ConsumerRecord<String, String> record : partitionRecords) {
                        //do some logical processing.
                        ProducerRecord<String, String> producerRecord =
                                new ProducerRecord<>("topic-sink", record.key(), record.value());
                        //消费-生产模型
                        producer.send(producerRecord);
                    }
                    long lastConsumedOffset = partitionRecords.
                            get(partitionRecords.size() - 1).offset();
                    offsets.put(partition, new OffsetAndMetadata(lastConsumedOffset + 1));
                }
                //提交消费位移
                producer.sendOffsetsToTransaction(offsets, "groupId");
                //提交事务
                producer.commitTransaction();
            } catch (ProducerFencedException e) {
                //log the exception
                //中止事务
                producer.abortTransaction();
            }
        }
    }
}
 
Example #29
Source File: KafkaPublisher.java    From extension-kafka with Apache License 2.0 5 votes vote down vote up
private void tryCommit(Producer<?, ?> producer, MonitorCallback monitorCallback) {
    try {
        producer.commitTransaction();
        monitorCallback.reportSuccess();
    } catch (ProducerFencedException e) {
        logger.warn("Unable to commit transaction", e);
        monitorCallback.reportFailure(e);
        throw new EventPublicationFailedException(
                "Event publication failed, exception occurred while committing Kafka transaction", e
        );
    }
}
 
Example #30
Source File: KafkaPublisher.java    From extension-kafka with Apache License 2.0 5 votes vote down vote up
private void tryBeginTxn(Producer<?, ?> producer) {
    try {
        producer.beginTransaction();
    } catch (ProducerFencedException e) {
        logger.warn("Unable to begin transaction", e);
        throw new EventPublicationFailedException(
                "Event publication failed, exception occurred while starting Kafka transaction", e
        );
    }
}