use of org.apache.kafka.common.requests.MetadataRequest in project kafka by apache.
the class KafkaAdminClientTest method testListTransactions.
@Test
public void testListTransactions() throws Exception {
try (AdminClientUnitTestEnv env = mockClientEnv()) {
MetadataResponseData.MetadataResponseBrokerCollection brokers = new MetadataResponseData.MetadataResponseBrokerCollection();
env.cluster().nodes().forEach(node -> {
brokers.add(new MetadataResponseData.MetadataResponseBroker().setHost(node.host()).setNodeId(node.id()).setPort(node.port()).setRack(node.rack()));
});
env.kafkaClient().prepareResponse(request -> request instanceof MetadataRequest, new MetadataResponse(new MetadataResponseData().setBrokers(brokers), MetadataResponseData.HIGHEST_SUPPORTED_VERSION));
List<TransactionListing> expected = Arrays.asList(new TransactionListing("foo", 12345L, TransactionState.ONGOING), new TransactionListing("bar", 98765L, TransactionState.PREPARE_ABORT), new TransactionListing("baz", 13579L, TransactionState.COMPLETE_COMMIT));
assertEquals(Utils.mkSet(0, 1, 2), env.cluster().nodes().stream().map(Node::id).collect(Collectors.toSet()));
env.cluster().nodes().forEach(node -> {
ListTransactionsResponseData response = new ListTransactionsResponseData().setErrorCode(Errors.NONE.code());
TransactionListing listing = expected.get(node.id());
response.transactionStates().add(new ListTransactionsResponseData.TransactionState().setTransactionalId(listing.transactionalId()).setProducerId(listing.producerId()).setTransactionState(listing.state().toString()));
env.kafkaClient().prepareResponseFrom(request -> request instanceof ListTransactionsRequest, new ListTransactionsResponse(response), node);
});
ListTransactionsResult result = env.adminClient().listTransactions();
assertEquals(new HashSet<>(expected), new HashSet<>(result.all().get()));
}
}
use of org.apache.kafka.common.requests.MetadataRequest in project kafka by apache.
the class PartitionLeaderStrategyTest method testBuildLookupRequest.
@Test
public void testBuildLookupRequest() {
Set<TopicPartition> topicPartitions = mkSet(new TopicPartition("foo", 0), new TopicPartition("bar", 0), new TopicPartition("foo", 1), new TopicPartition("baz", 0));
PartitionLeaderStrategy strategy = newStrategy();
MetadataRequest allRequest = strategy.buildRequest(topicPartitions).build();
assertEquals(mkSet("foo", "bar", "baz"), new HashSet<>(allRequest.topics()));
assertFalse(allRequest.allowAutoTopicCreation());
MetadataRequest partialRequest = strategy.buildRequest(topicPartitions.stream().filter(tp -> tp.topic().equals("foo")).collect(Collectors.toSet())).build();
assertEquals(mkSet("foo"), new HashSet<>(partialRequest.topics()));
assertFalse(partialRequest.allowAutoTopicCreation());
}
use of org.apache.kafka.common.requests.MetadataRequest in project kafka by apache.
the class SaslAuthenticatorTest method testDisallowedKafkaRequestsBeforeAuthentication.
/**
* Tests that Kafka requests that are forbidden until successful authentication result
* in authentication failure and do not cause any failures in the server.
*/
@Test
public void testDisallowedKafkaRequestsBeforeAuthentication() throws Exception {
SecurityProtocol securityProtocol = SecurityProtocol.SASL_PLAINTEXT;
configureMechanisms("PLAIN", Arrays.asList("PLAIN"));
server = createEchoServer(securityProtocol);
// Send metadata request before Kafka SASL handshake request
String node1 = "invalid1";
createClientConnection(SecurityProtocol.PLAINTEXT, node1);
MetadataRequest metadataRequest1 = new MetadataRequest.Builder(Collections.singletonList("sometopic"), true).build();
RequestHeader metadataRequestHeader1 = new RequestHeader(ApiKeys.METADATA, metadataRequest1.version(), "someclient", 1);
selector.send(new NetworkSend(node1, metadataRequest1.toSend(metadataRequestHeader1)));
NetworkTestUtils.waitForChannelClose(selector, node1, ChannelState.READY.state());
selector.close();
// Test good connection still works
createAndCheckClientConnection(securityProtocol, "good1");
// Send metadata request after Kafka SASL handshake request
String node2 = "invalid2";
createClientConnection(SecurityProtocol.PLAINTEXT, node2);
sendHandshakeRequestReceiveResponse(node2, (short) 1);
MetadataRequest metadataRequest2 = new MetadataRequest.Builder(Collections.singletonList("sometopic"), true).build();
RequestHeader metadataRequestHeader2 = new RequestHeader(ApiKeys.METADATA, metadataRequest2.version(), "someclient", 2);
selector.send(new NetworkSend(node2, metadataRequest2.toSend(metadataRequestHeader2)));
NetworkTestUtils.waitForChannelClose(selector, node2, ChannelState.READY.state());
selector.close();
// Test good connection still works
createAndCheckClientConnection(securityProtocol, "good2");
}
Aggregations