Search in sources :

Example 11 with AbstractRequest

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

the class KafkaConsumerTest method prepareHeartbeatResponse.

private AtomicBoolean prepareHeartbeatResponse(MockClient client, Node coordinator) {
    final AtomicBoolean heartbeatReceived = new AtomicBoolean(false);
    client.prepareResponseFrom(new MockClient.RequestMatcher() {

        @Override
        public boolean matches(AbstractRequest body) {
            heartbeatReceived.set(true);
            return true;
        }
    }, new HeartbeatResponse(Errors.NONE), coordinator);
    return heartbeatReceived;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HeartbeatResponse(org.apache.kafka.common.requests.HeartbeatResponse) AbstractRequest(org.apache.kafka.common.requests.AbstractRequest) MockClient(org.apache.kafka.clients.MockClient)

Example 12 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) {
    Iterator<FutureResponse> iterator = futureResponses.iterator();
    while (iterator.hasNext()) {
        FutureResponse futureResp = iterator.next();
        if (futureResp.node != null && !request.destination().equals(futureResp.node.idString()))
            continue;
        short usableVersion = nodeApiVersions.usableVersion(request.requestBuilder().apiKey());
        AbstractRequest abstractRequest = request.requestBuilder().build(usableVersion);
        if (!futureResp.requestMatcher.matches(abstractRequest))
            throw new IllegalStateException("Next in line response did not match expected request");
        ClientResponse resp = new ClientResponse(request.makeHeader(usableVersion), request.callback(), request.destination(), request.createdTimeMs(), time.milliseconds(), futureResp.disconnected, null, futureResp.responseBody);
        responses.add(resp);
        iterator.remove();
        return;
    }
    this.requests.add(request);
}
Also used : AbstractRequest(org.apache.kafka.common.requests.AbstractRequest)

Example 13 with AbstractRequest

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

the class WorkerCoordinatorTest method testJoinLeaderCannotAssign.

@Test
public void testJoinLeaderCannotAssign() {
    // If the selected leader can't get up to the maximum offset, it will fail to assign and we should immediately
    // need to retry the join.
    // When the first round fails, we'll take an updated config snapshot
    EasyMock.expect(configStorage.snapshot()).andReturn(configState1);
    EasyMock.expect(configStorage.snapshot()).andReturn(configState2);
    PowerMock.replayAll();
    final String memberId = "member";
    client.prepareResponse(groupCoordinatorResponse(node, Errors.NONE));
    coordinator.ensureCoordinatorReady();
    // config mismatch results in assignment error
    client.prepareResponse(joinGroupFollowerResponse(1, memberId, "leader", Errors.NONE));
    MockClient.RequestMatcher matcher = new MockClient.RequestMatcher() {

        @Override
        public boolean matches(AbstractRequest body) {
            SyncGroupRequest sync = (SyncGroupRequest) body;
            return sync.memberId().equals(memberId) && sync.generationId() == 1 && sync.groupAssignment().isEmpty();
        }
    };
    client.prepareResponse(matcher, syncGroupResponse(ConnectProtocol.Assignment.CONFIG_MISMATCH, "leader", 10L, Collections.<String>emptyList(), Collections.<ConnectorTaskId>emptyList(), Errors.NONE));
    client.prepareResponse(joinGroupFollowerResponse(1, memberId, "leader", Errors.NONE));
    client.prepareResponse(matcher, syncGroupResponse(ConnectProtocol.Assignment.NO_ERROR, "leader", 1L, Collections.<String>emptyList(), Collections.singletonList(taskId1x0), Errors.NONE));
    coordinator.ensureActiveGroup();
    PowerMock.verifyAll();
}
Also used : ConnectorTaskId(org.apache.kafka.connect.util.ConnectorTaskId) SyncGroupRequest(org.apache.kafka.common.requests.SyncGroupRequest) AbstractRequest(org.apache.kafka.common.requests.AbstractRequest) MockClient(org.apache.kafka.clients.MockClient) Test(org.junit.Test)

Example 14 with AbstractRequest

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

the class KafkaConsumerTest method prepareRebalance.

private Node prepareRebalance(MockClient client, Node node, final Set<String> subscribedTopics, PartitionAssignor assignor, List<TopicPartition> partitions, Node coordinator) {
    if (coordinator == null) {
        // lookup coordinator
        client.prepareResponseFrom(new GroupCoordinatorResponse(Errors.NONE, node), node);
        coordinator = new Node(Integer.MAX_VALUE - node.id(), node.host(), node.port());
    }
    // join group
    client.prepareResponseFrom(new MockClient.RequestMatcher() {

        @Override
        public boolean matches(AbstractRequest body) {
            JoinGroupRequest joinGroupRequest = (JoinGroupRequest) body;
            PartitionAssignor.Subscription subscription = ConsumerProtocol.deserializeSubscription(joinGroupRequest.groupProtocols().get(0).metadata());
            return subscribedTopics.equals(new HashSet<>(subscription.topics()));
        }
    }, joinGroupFollowerResponse(assignor, 1, "memberId", "leaderId", Errors.NONE), coordinator);
    // sync group
    client.prepareResponseFrom(syncGroupResponse(partitions, Errors.NONE), coordinator);
    return coordinator;
}
Also used : JoinGroupRequest(org.apache.kafka.common.requests.JoinGroupRequest) Node(org.apache.kafka.common.Node) AbstractRequest(org.apache.kafka.common.requests.AbstractRequest) GroupCoordinatorResponse(org.apache.kafka.common.requests.GroupCoordinatorResponse) MockClient(org.apache.kafka.clients.MockClient) HashSet(java.util.HashSet)

Example 15 with AbstractRequest

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

the class AbstractCoordinatorTest method testWakeupAfterJoinGroupSentExternalCompletion.

@Test
public void testWakeupAfterJoinGroupSentExternalCompletion() throws Exception {
    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 e) {
    }
    assertEquals(1, coordinator.onJoinPrepareInvokes);
    assertEquals(0, coordinator.onJoinCompleteInvokes);
    assertFalse(heartbeatReceived.get());
    // the join group completes in this poll()
    consumerClient.poll(0);
    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.Test)

Aggregations

AbstractRequest (org.apache.kafka.common.requests.AbstractRequest)20 MockClient (org.apache.kafka.clients.MockClient)18 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)15 Test (org.junit.Test)13 WakeupException (org.apache.kafka.common.errors.WakeupException)8 SyncGroupRequest (org.apache.kafka.common.requests.SyncGroupRequest)6 JoinGroupRequest (org.apache.kafka.common.requests.JoinGroupRequest)5 LeaveGroupResponse (org.apache.kafka.common.requests.LeaveGroupResponse)4 HashMap (java.util.HashMap)3 LeaveGroupRequest (org.apache.kafka.common.requests.LeaveGroupRequest)3 OffsetCommitRequest (org.apache.kafka.common.requests.OffsetCommitRequest)3 TopicPartition (org.apache.kafka.common.TopicPartition)2 ByteBuffer (java.nio.ByteBuffer)1 Collections.singletonList (java.util.Collections.singletonList)1 Collections.singletonMap (java.util.Collections.singletonMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 OffsetAndMetadata (org.apache.kafka.clients.consumer.OffsetAndMetadata)1