Search in sources :

Example 6 with MetadataRequest

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()));
    }
}
Also used : Node(org.apache.kafka.common.Node) MetadataRequest(org.apache.kafka.common.requests.MetadataRequest) ListTransactionsRequest(org.apache.kafka.common.requests.ListTransactionsRequest) ListTransactionsResponse(org.apache.kafka.common.requests.ListTransactionsResponse) MetadataResponseData(org.apache.kafka.common.message.MetadataResponseData) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) ListTransactionsResponseData(org.apache.kafka.common.message.ListTransactionsResponseData) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 7 with MetadataRequest

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());
}
Also used : Arrays(java.util.Arrays) HashMap(java.util.HashMap) UnknownServerException(org.apache.kafka.common.errors.UnknownServerException) HashSet(java.util.HashSet) MetadataResponseTopic(org.apache.kafka.common.message.MetadataResponseData.MetadataResponseTopic) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) MetadataRequest(org.apache.kafka.common.requests.MetadataRequest) LogContext(org.apache.kafka.common.utils.LogContext) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) TopicPartition(org.apache.kafka.common.TopicPartition) Collections.emptyMap(java.util.Collections.emptyMap) LookupResult(org.apache.kafka.clients.admin.internals.AdminApiLookupStrategy.LookupResult) Utils.mkSet(org.apache.kafka.common.utils.Utils.mkSet) InvalidTopicException(org.apache.kafka.common.errors.InvalidTopicException) Set(java.util.Set) ApiKeys(org.apache.kafka.common.protocol.ApiKeys) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) List(java.util.List) MetadataResponsePartition(org.apache.kafka.common.message.MetadataResponseData.MetadataResponsePartition) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) Errors(org.apache.kafka.common.protocol.Errors) MetadataResponseData(org.apache.kafka.common.message.MetadataResponseData) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) MetadataRequest(org.apache.kafka.common.requests.MetadataRequest) TopicPartition(org.apache.kafka.common.TopicPartition) Test(org.junit.jupiter.api.Test)

Example 8 with MetadataRequest

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");
}
Also used : MetadataRequest(org.apache.kafka.common.requests.MetadataRequest) SecurityProtocol(org.apache.kafka.common.security.auth.SecurityProtocol) RequestHeader(org.apache.kafka.common.requests.RequestHeader) NetworkSend(org.apache.kafka.common.network.NetworkSend) Test(org.junit.jupiter.api.Test)

Aggregations

MetadataRequest (org.apache.kafka.common.requests.MetadataRequest)8 Test (org.junit.jupiter.api.Test)5 MetadataResponseData (org.apache.kafka.common.message.MetadataResponseData)3 MetadataResponse (org.apache.kafka.common.requests.MetadataResponse)3 RequestHeader (org.apache.kafka.common.requests.RequestHeader)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 Cluster (org.apache.kafka.common.Cluster)2 Node (org.apache.kafka.common.Node)2 MetadataResponsePartition (org.apache.kafka.common.message.MetadataResponseData.MetadataResponsePartition)2 MetadataResponseTopic (org.apache.kafka.common.message.MetadataResponseData.MetadataResponseTopic)2 CreateTopicsRequest (org.apache.kafka.common.requests.CreateTopicsRequest)2 SecurityProtocol (org.apache.kafka.common.security.auth.SecurityProtocol)2 InetSocketAddress (java.net.InetSocketAddress)1 ByteBuffer (java.nio.ByteBuffer)1 Arrays (java.util.Arrays)1 Collections.emptyMap (java.util.Collections.emptyMap)1 Collections.singletonMap (java.util.Collections.singletonMap)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1