use of org.apache.kafka.common.requests.FindCoordinatorRequest in project kafka by apache.
the class KafkaProducerTest method testInitTransactionTimeout.
@Test
public void testInitTransactionTimeout() {
Map<String, Object> configs = new HashMap<>();
configs.put(ProducerConfig.TRANSACTIONAL_ID_CONFIG, "bad-transaction");
configs.put(ProducerConfig.MAX_BLOCK_MS_CONFIG, 500);
configs.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9000");
Time time = new MockTime(1);
MetadataResponse initialUpdateResponse = RequestTestUtils.metadataUpdateWith(1, singletonMap("topic", 1));
ProducerMetadata metadata = newMetadata(0, Long.MAX_VALUE);
metadata.updateWithCurrentRequestVersion(initialUpdateResponse, false, time.milliseconds());
MockClient client = new MockClient(time, metadata);
try (Producer<String, String> producer = kafkaProducer(configs, new StringSerializer(), new StringSerializer(), metadata, client, null, time)) {
client.prepareResponse(request -> request instanceof FindCoordinatorRequest && ((FindCoordinatorRequest) request).data().keyType() == FindCoordinatorRequest.CoordinatorType.TRANSACTION.id(), FindCoordinatorResponse.prepareResponse(Errors.NONE, "bad-transaction", NODE));
assertThrows(TimeoutException.class, producer::initTransactions);
client.prepareResponse(request -> request instanceof FindCoordinatorRequest && ((FindCoordinatorRequest) request).data().keyType() == FindCoordinatorRequest.CoordinatorType.TRANSACTION.id(), FindCoordinatorResponse.prepareResponse(Errors.NONE, "bad-transaction", NODE));
client.prepareResponse(initProducerIdResponse(1L, (short) 5, Errors.NONE));
// retry initialization should work
producer.initTransactions();
}
}
use of org.apache.kafka.common.requests.FindCoordinatorRequest in project kafka by apache.
the class TransactionManagerTest method prepareFindCoordinatorResponse.
private void prepareFindCoordinatorResponse(Errors error, boolean shouldDisconnect, final CoordinatorType coordinatorType, final String coordinatorKey) {
client.prepareResponse(body -> {
FindCoordinatorRequest findCoordinatorRequest = (FindCoordinatorRequest) body;
assertEquals(coordinatorType, CoordinatorType.forId(findCoordinatorRequest.data().keyType()));
String key = findCoordinatorRequest.data().coordinatorKeys().isEmpty() ? findCoordinatorRequest.data().key() : findCoordinatorRequest.data().coordinatorKeys().get(0);
assertEquals(coordinatorKey, key);
return true;
}, FindCoordinatorResponse.prepareResponse(error, coordinatorKey, brokerNode), shouldDisconnect);
}
use of org.apache.kafka.common.requests.FindCoordinatorRequest in project kafka by apache.
the class KafkaAdminClientTest method testRetryDescribeTransactionsAfterNotCoordinatorError.
@Test
public void testRetryDescribeTransactionsAfterNotCoordinatorError() throws Exception {
MockTime time = new MockTime();
int retryBackoffMs = 100;
Cluster cluster = mockCluster(3, 0);
Map<String, Object> configOverride = newStrMap(AdminClientConfig.RETRY_BACKOFF_MS_CONFIG, "" + retryBackoffMs);
try (AdminClientUnitTestEnv env = new AdminClientUnitTestEnv(time, cluster, configOverride)) {
String transactionalId = "foo";
Iterator<Node> nodeIterator = env.cluster().nodes().iterator();
Node coordinator1 = nodeIterator.next();
Node coordinator2 = nodeIterator.next();
env.kafkaClient().prepareResponse(request -> request instanceof FindCoordinatorRequest, new FindCoordinatorResponse(new FindCoordinatorResponseData().setCoordinators(Arrays.asList(new FindCoordinatorResponseData.Coordinator().setKey(transactionalId).setErrorCode(Errors.NONE.code()).setNodeId(coordinator1.id()).setHost(coordinator1.host()).setPort(coordinator1.port())))));
env.kafkaClient().prepareResponseFrom(request -> {
if (!(request instanceof DescribeTransactionsRequest)) {
return false;
} else {
// Backoff needed here for the retry of FindCoordinator
time.sleep(retryBackoffMs);
return true;
}
}, new DescribeTransactionsResponse(new DescribeTransactionsResponseData().setTransactionStates(singletonList(new DescribeTransactionsResponseData.TransactionState().setErrorCode(Errors.NOT_COORDINATOR.code()).setTransactionalId(transactionalId)))), coordinator1);
env.kafkaClient().prepareResponse(request -> request instanceof FindCoordinatorRequest, new FindCoordinatorResponse(new FindCoordinatorResponseData().setCoordinators(Arrays.asList(new FindCoordinatorResponseData.Coordinator().setKey(transactionalId).setErrorCode(Errors.NONE.code()).setNodeId(coordinator2.id()).setHost(coordinator2.host()).setPort(coordinator2.port())))));
TransactionDescription expected = new TransactionDescription(coordinator2.id(), TransactionState.COMPLETE_COMMIT, 12345L, 15, 10000L, OptionalLong.empty(), emptySet());
env.kafkaClient().prepareResponseFrom(request -> request instanceof DescribeTransactionsRequest, new DescribeTransactionsResponse(new DescribeTransactionsResponseData().setTransactionStates(singletonList(new DescribeTransactionsResponseData.TransactionState().setErrorCode(Errors.NONE.code()).setProducerEpoch((short) expected.producerEpoch()).setProducerId(expected.producerId()).setTransactionalId(transactionalId).setTransactionTimeoutMs(10000).setTransactionStartTimeMs(-1).setTransactionState(expected.state().toString())))), coordinator2);
DescribeTransactionsResult result = env.adminClient().describeTransactions(singleton(transactionalId));
KafkaFuture<TransactionDescription> future = result.description(transactionalId);
assertEquals(expected, future.get());
}
}
use of org.apache.kafka.common.requests.FindCoordinatorRequest in project kafka by apache.
the class KafkaAdminClientTest method testDescribeTransactions.
@Test
public void testDescribeTransactions() throws Exception {
try (AdminClientUnitTestEnv env = mockClientEnv()) {
String transactionalId = "foo";
Node coordinator = env.cluster().nodes().iterator().next();
TransactionDescription expected = new TransactionDescription(coordinator.id(), TransactionState.COMPLETE_COMMIT, 12345L, 15, 10000L, OptionalLong.empty(), emptySet());
env.kafkaClient().prepareResponse(request -> request instanceof FindCoordinatorRequest, prepareFindCoordinatorResponse(Errors.NONE, transactionalId, coordinator));
env.kafkaClient().prepareResponseFrom(request -> request instanceof DescribeTransactionsRequest, new DescribeTransactionsResponse(new DescribeTransactionsResponseData().setTransactionStates(singletonList(new DescribeTransactionsResponseData.TransactionState().setErrorCode(Errors.NONE.code()).setProducerEpoch((short) expected.producerEpoch()).setProducerId(expected.producerId()).setTransactionalId(transactionalId).setTransactionTimeoutMs(10000).setTransactionStartTimeMs(-1).setTransactionState(expected.state().toString())))), coordinator);
DescribeTransactionsResult result = env.adminClient().describeTransactions(singleton(transactionalId));
KafkaFuture<TransactionDescription> future = result.description(transactionalId);
assertEquals(expected, future.get());
}
}
use of org.apache.kafka.common.requests.FindCoordinatorRequest in project kafka by apache.
the class KafkaProducerTest method testInitTransactionsResponseAfterTimeout.
@Test
public void testInitTransactionsResponseAfterTimeout() throws Exception {
int maxBlockMs = 500;
Map<String, Object> configs = new HashMap<>();
configs.put(ProducerConfig.TRANSACTIONAL_ID_CONFIG, "bad-transaction");
configs.put(ProducerConfig.MAX_BLOCK_MS_CONFIG, maxBlockMs);
configs.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9000");
Time time = new MockTime();
MetadataResponse initialUpdateResponse = RequestTestUtils.metadataUpdateWith(1, singletonMap("topic", 1));
ProducerMetadata metadata = newMetadata(0, Long.MAX_VALUE);
metadata.updateWithCurrentRequestVersion(initialUpdateResponse, false, time.milliseconds());
MockClient client = new MockClient(time, metadata);
ExecutorService executor = Executors.newFixedThreadPool(1);
Producer<String, String> producer = kafkaProducer(configs, new StringSerializer(), new StringSerializer(), metadata, client, null, time);
try {
client.prepareResponse(request -> request instanceof FindCoordinatorRequest && ((FindCoordinatorRequest) request).data().keyType() == FindCoordinatorRequest.CoordinatorType.TRANSACTION.id(), FindCoordinatorResponse.prepareResponse(Errors.NONE, "bad-transaction", NODE));
Future<?> future = executor.submit(producer::initTransactions);
TestUtils.waitForCondition(client::hasInFlightRequests, "Timed out while waiting for expected `InitProducerId` request to be sent");
time.sleep(maxBlockMs);
TestUtils.assertFutureThrows(future, TimeoutException.class);
client.respond(initProducerIdResponse(1L, (short) 5, Errors.NONE));
Thread.sleep(1000);
producer.initTransactions();
} finally {
producer.close(Duration.ZERO);
}
}
Aggregations