use of org.apache.kafka.common.requests.AddPartitionsToTxnRequest in project apache-kafka-on-k8s by banzaicloud.
the class TransactionManagerTest method addPartitionsRequestMatcher.
private MockClient.RequestMatcher addPartitionsRequestMatcher(final TopicPartition topicPartition, final short epoch, final long pid) {
return new MockClient.RequestMatcher() {
@Override
public boolean matches(AbstractRequest body) {
AddPartitionsToTxnRequest addPartitionsToTxnRequest = (AddPartitionsToTxnRequest) body;
assertEquals(pid, addPartitionsToTxnRequest.producerId());
assertEquals(epoch, addPartitionsToTxnRequest.producerEpoch());
assertEquals(singletonList(topicPartition), addPartitionsToTxnRequest.partitions());
assertEquals(transactionalId, addPartitionsToTxnRequest.transactionalId());
return true;
}
};
}
use of org.apache.kafka.common.requests.AddPartitionsToTxnRequest in project kafka by apache.
the class TransactionManagerTest method testCommitWithTopicAuthorizationFailureInAddPartitionsInFlight.
@Test
public void testCommitWithTopicAuthorizationFailureInAddPartitionsInFlight() throws InterruptedException {
final TopicPartition tp0 = new TopicPartition("foo", 0);
final TopicPartition tp1 = new TopicPartition("bar", 0);
doInitTransactions();
// Begin a transaction, send two records, and begin commit
transactionManager.beginTransaction();
transactionManager.maybeAddPartition(tp0);
transactionManager.maybeAddPartition(tp1);
FutureRecordMetadata firstPartitionAppend = appendToAccumulator(tp0);
FutureRecordMetadata secondPartitionAppend = appendToAccumulator(tp1);
TransactionalRequestResult commitResult = transactionManager.beginCommit();
// We send the AddPartitionsToTxn request in the first sender call
sender.runOnce();
assertFalse(transactionManager.hasError());
assertFalse(commitResult.isCompleted());
assertFalse(firstPartitionAppend.isDone());
// The AddPartitionsToTxn response returns in the next call with the error
Map<TopicPartition, Errors> errors = new HashMap<>();
errors.put(tp0, Errors.TOPIC_AUTHORIZATION_FAILED);
errors.put(tp1, Errors.OPERATION_NOT_ATTEMPTED);
client.respond(body -> {
AddPartitionsToTxnRequest request = (AddPartitionsToTxnRequest) body;
assertEquals(new HashSet<>(request.partitions()), new HashSet<>(errors.keySet()));
return true;
}, new AddPartitionsToTxnResponse(0, errors));
sender.runOnce();
assertTrue(transactionManager.hasError());
assertFalse(commitResult.isCompleted());
assertFalse(firstPartitionAppend.isDone());
assertFalse(secondPartitionAppend.isDone());
// The next call aborts the records, which have not yet been sent. It should
// not block because there are no requests pending and we still need to cancel
// the pending transaction commit.
sender.runOnce();
assertTrue(commitResult.isCompleted());
TestUtils.assertFutureThrows(firstPartitionAppend, KafkaException.class);
TestUtils.assertFutureThrows(secondPartitionAppend, KafkaException.class);
assertTrue(commitResult.error() instanceof TopicAuthorizationException);
}
use of org.apache.kafka.common.requests.AddPartitionsToTxnRequest in project kafka by apache.
the class TransactionManagerTest method prepareAddPartitionsToTxn.
private void prepareAddPartitionsToTxn(final Map<TopicPartition, Errors> errors) {
client.prepareResponse(body -> {
AddPartitionsToTxnRequest request = (AddPartitionsToTxnRequest) body;
assertEquals(new HashSet<>(request.partitions()), new HashSet<>(errors.keySet()));
return true;
}, new AddPartitionsToTxnResponse(0, errors));
}
Aggregations