Search in sources :

Example 21 with AbstractRequest

use of org.apache.kafka.common.requests.AbstractRequest in project apache-kafka-on-k8s by banzaicloud.

the class KafkaConsumerTest method prepareOffsetCommitResponse.

private AtomicBoolean prepareOffsetCommitResponse(MockClient client, Node coordinator, final Map<TopicPartition, Long> partitionOffsets) {
    final AtomicBoolean commitReceived = new AtomicBoolean(true);
    Map<TopicPartition, Errors> response = new HashMap<>();
    for (TopicPartition partition : partitionOffsets.keySet()) response.put(partition, Errors.NONE);
    client.prepareResponseFrom(new MockClient.RequestMatcher() {

        @Override
        public boolean matches(AbstractRequest body) {
            OffsetCommitRequest commitRequest = (OffsetCommitRequest) body;
            for (Map.Entry<TopicPartition, Long> partitionOffset : partitionOffsets.entrySet()) {
                OffsetCommitRequest.PartitionData partitionData = commitRequest.offsetData().get(partitionOffset.getKey());
                // verify that the expected offset has been committed
                if (partitionData.offset != partitionOffset.getValue()) {
                    commitReceived.set(false);
                    return false;
                }
            }
            return true;
        }
    }, offsetCommitResponse(response), coordinator);
    return commitReceived;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Errors(org.apache.kafka.common.protocol.Errors) OffsetCommitRequest(org.apache.kafka.common.requests.OffsetCommitRequest) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) PartitionData(org.apache.kafka.common.requests.FetchResponse.PartitionData) TopicPartition(org.apache.kafka.common.TopicPartition) AbstractRequest(org.apache.kafka.common.requests.AbstractRequest) MockClient(org.apache.kafka.clients.MockClient)

Example 22 with AbstractRequest

use of org.apache.kafka.common.requests.AbstractRequest in project apache-kafka-on-k8s by banzaicloud.

the class TransactionManagerTest method produceRequestMatcher.

private MockClient.RequestMatcher produceRequestMatcher(final long pid, final short epoch) {
    return new MockClient.RequestMatcher() {

        @Override
        public boolean matches(AbstractRequest body) {
            ProduceRequest produceRequest = (ProduceRequest) body;
            MemoryRecords records = produceRequest.partitionRecordsOrFail().get(tp0);
            assertNotNull(records);
            Iterator<MutableRecordBatch> batchIterator = records.batches().iterator();
            assertTrue(batchIterator.hasNext());
            MutableRecordBatch batch = batchIterator.next();
            assertFalse(batchIterator.hasNext());
            assertTrue(batch.isTransactional());
            assertEquals(pid, batch.producerId());
            assertEquals(epoch, batch.producerEpoch());
            assertEquals(transactionalId, produceRequest.transactionalId());
            return true;
        }
    };
}
Also used : MutableRecordBatch(org.apache.kafka.common.record.MutableRecordBatch) ProduceRequest(org.apache.kafka.common.requests.ProduceRequest) AbstractRequest(org.apache.kafka.common.requests.AbstractRequest) MemoryRecords(org.apache.kafka.common.record.MemoryRecords)

Example 23 with AbstractRequest

use of org.apache.kafka.common.requests.AbstractRequest 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 24 with AbstractRequest

use of org.apache.kafka.common.requests.AbstractRequest in project kafka by apache.

the class MockClient method send.

@Override
public void send(ClientRequest request, long now) {
    if (!connectionState(request.destination()).isReady(now))
        throw new IllegalStateException("Cannot send " + request + " since the destination is not ready");
    // Check if the request is directed to a node with a pending authentication error.
    for (Iterator<Map.Entry<Node, Long>> authErrorIter = pendingAuthenticationErrors.entrySet().iterator(); authErrorIter.hasNext(); ) {
        Map.Entry<Node, Long> entry = authErrorIter.next();
        Node node = entry.getKey();
        long backoffMs = entry.getValue();
        if (node.idString().equals(request.destination())) {
            authErrorIter.remove();
            // Set up a disconnected ClientResponse and create an authentication error
            // for the affected node.
            authenticationFailed(node, backoffMs);
            AbstractRequest.Builder<?> builder = request.requestBuilder();
            short version = nodeApiVersions.latestUsableVersion(request.apiKey(), builder.oldestAllowedVersion(), builder.latestAllowedVersion());
            ClientResponse resp = new ClientResponse(request.makeHeader(version), request.callback(), request.destination(), request.createdTimeMs(), time.milliseconds(), true, null, new AuthenticationException("Authentication failed"), null);
            responses.add(resp);
            return;
        }
    }
    Iterator<FutureResponse> iterator = futureResponses.iterator();
    while (iterator.hasNext()) {
        FutureResponse futureResp = iterator.next();
        if (futureResp.node != null && !request.destination().equals(futureResp.node.idString()))
            continue;
        AbstractRequest.Builder<?> builder = request.requestBuilder();
        try {
            short version = nodeApiVersions.latestUsableVersion(request.apiKey(), builder.oldestAllowedVersion(), builder.latestAllowedVersion());
            UnsupportedVersionException unsupportedVersionException = null;
            if (futureResp.isUnsupportedRequest) {
                unsupportedVersionException = new UnsupportedVersionException("Api " + request.apiKey() + " with version " + version);
            } else {
                AbstractRequest abstractRequest = request.requestBuilder().build(version);
                if (!futureResp.requestMatcher.matches(abstractRequest))
                    throw new IllegalStateException("Request matcher did not match next-in-line request " + abstractRequest + " with prepared response " + futureResp.responseBody);
            }
            ClientResponse resp = new ClientResponse(request.makeHeader(version), request.callback(), request.destination(), request.createdTimeMs(), time.milliseconds(), futureResp.disconnected, unsupportedVersionException, null, futureResp.responseBody);
            responses.add(resp);
        } catch (UnsupportedVersionException unsupportedVersionException) {
            ClientResponse resp = new ClientResponse(request.makeHeader(builder.latestAllowedVersion()), request.callback(), request.destination(), request.createdTimeMs(), time.milliseconds(), false, unsupportedVersionException, null, null);
            responses.add(resp);
        }
        iterator.remove();
        return;
    }
    this.requests.add(request);
}
Also used : AuthenticationException(org.apache.kafka.common.errors.AuthenticationException) Node(org.apache.kafka.common.Node) AbstractRequest(org.apache.kafka.common.requests.AbstractRequest) HashMap(java.util.HashMap) Map(java.util.Map) UnsupportedVersionException(org.apache.kafka.common.errors.UnsupportedVersionException)

Example 25 with AbstractRequest

use of org.apache.kafka.common.requests.AbstractRequest in project kafka by apache.

the class AbstractCoordinatorTest method testWakeupAfterJoinGroupSent.

@Test
public void testWakeupAfterJoinGroupSent() throws Exception {
    setupCoordinator();
    mockClient.prepareResponse(groupCoordinatorResponse(node, Errors.NONE));
    mockClient.prepareResponse(new MockClient.RequestMatcher() {

        private int invocations = 0;

        @Override
        public boolean matches(AbstractRequest body) {
            invocations++;
            boolean isJoinGroupRequest = body instanceof JoinGroupRequest;
            if (isJoinGroupRequest && invocations == 1)
                // simulate wakeup before the request returns
                throw new WakeupException();
            return isJoinGroupRequest;
        }
    }, joinGroupFollowerResponse(1, memberId, leaderId, Errors.NONE));
    mockClient.prepareResponse(syncGroupResponse(Errors.NONE));
    AtomicBoolean heartbeatReceived = prepareFirstHeartbeat();
    try {
        coordinator.ensureActiveGroup();
        fail("Should have woken up from ensureActiveGroup()");
    } catch (WakeupException ignored) {
    }
    assertEquals(1, coordinator.onJoinPrepareInvokes);
    assertEquals(0, coordinator.onJoinCompleteInvokes);
    assertFalse(heartbeatReceived.get());
    coordinator.ensureActiveGroup();
    assertEquals(1, coordinator.onJoinPrepareInvokes);
    assertEquals(1, coordinator.onJoinCompleteInvokes);
    awaitFirstHeartbeat(heartbeatReceived);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) JoinGroupRequest(org.apache.kafka.common.requests.JoinGroupRequest) AbstractRequest(org.apache.kafka.common.requests.AbstractRequest) WakeupException(org.apache.kafka.common.errors.WakeupException) MockClient(org.apache.kafka.clients.MockClient) Test(org.junit.jupiter.api.Test)

Aggregations

AbstractRequest (org.apache.kafka.common.requests.AbstractRequest)48 MockClient (org.apache.kafka.clients.MockClient)38 Test (org.junit.Test)27 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)20 WakeupException (org.apache.kafka.common.errors.WakeupException)12 Node (org.apache.kafka.common.Node)11 ProduceRequest (org.apache.kafka.common.requests.ProduceRequest)9 SyncGroupRequest (org.apache.kafka.common.requests.SyncGroupRequest)9 JoinGroupRequest (org.apache.kafka.common.requests.JoinGroupRequest)8 HashMap (java.util.HashMap)7 RecordMetadata (org.apache.kafka.clients.producer.RecordMetadata)7 TopicPartition (org.apache.kafka.common.TopicPartition)6 Map (java.util.Map)5 MemoryRecords (org.apache.kafka.common.record.MemoryRecords)5 LeaveGroupResponse (org.apache.kafka.common.requests.LeaveGroupResponse)4 Collections.singletonMap (java.util.Collections.singletonMap)3 LinkedHashMap (java.util.LinkedHashMap)3 Metrics (org.apache.kafka.common.metrics.Metrics)3 MutableRecordBatch (org.apache.kafka.common.record.MutableRecordBatch)3 FetchRequest (org.apache.kafka.common.requests.FetchRequest)3