Search in sources :

Example 1 with AddPartitionsToTxnRequest

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;
        }
    };
}
Also used : AddPartitionsToTxnRequest(org.apache.kafka.common.requests.AddPartitionsToTxnRequest) AbstractRequest(org.apache.kafka.common.requests.AbstractRequest)

Example 2 with AddPartitionsToTxnRequest

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);
}
Also used : Errors(org.apache.kafka.common.protocol.Errors) HashMap(java.util.HashMap) TopicPartition(org.apache.kafka.common.TopicPartition) AddPartitionsToTxnRequest(org.apache.kafka.common.requests.AddPartitionsToTxnRequest) AddPartitionsToTxnResponse(org.apache.kafka.common.requests.AddPartitionsToTxnResponse) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) Test(org.junit.jupiter.api.Test)

Example 3 with AddPartitionsToTxnRequest

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));
}
Also used : AddPartitionsToTxnRequest(org.apache.kafka.common.requests.AddPartitionsToTxnRequest) AddPartitionsToTxnResponse(org.apache.kafka.common.requests.AddPartitionsToTxnResponse)

Aggregations

AddPartitionsToTxnRequest (org.apache.kafka.common.requests.AddPartitionsToTxnRequest)3 AddPartitionsToTxnResponse (org.apache.kafka.common.requests.AddPartitionsToTxnResponse)2 HashMap (java.util.HashMap)1 TopicPartition (org.apache.kafka.common.TopicPartition)1 TopicAuthorizationException (org.apache.kafka.common.errors.TopicAuthorizationException)1 Errors (org.apache.kafka.common.protocol.Errors)1 AbstractRequest (org.apache.kafka.common.requests.AbstractRequest)1 Test (org.junit.jupiter.api.Test)1