Search in sources :

Example 21 with SyncGroupRequest

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

the class AbstractCoordinatorTest method testWakeupAfterSyncGroupSentExternalCompletion.

@Test
public void testWakeupAfterSyncGroupSentExternalCompletion() throws Exception {
    setupCoordinator();
    mockClient.prepareResponse(groupCoordinatorResponse(node, Errors.NONE));
    mockClient.prepareResponse(joinGroupFollowerResponse(1, memberId, leaderId, Errors.NONE));
    mockClient.prepareResponse(new MockClient.RequestMatcher() {

        private int invocations = 0;

        @Override
        public boolean matches(AbstractRequest body) {
            invocations++;
            boolean isSyncGroupRequest = body instanceof SyncGroupRequest;
            if (isSyncGroupRequest && invocations == 1)
                // wakeup after the request returns
                consumerClient.wakeup();
            return isSyncGroupRequest;
        }
    }, 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(mockTime.timer(0));
    coordinator.ensureActiveGroup();
    assertEquals(1, coordinator.onJoinPrepareInvokes);
    assertEquals(1, coordinator.onJoinCompleteInvokes);
    awaitFirstHeartbeat(heartbeatReceived);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SyncGroupRequest(org.apache.kafka.common.requests.SyncGroupRequest) 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)

Example 22 with SyncGroupRequest

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

the class AbstractCoordinatorTest method joinGroupWithProtocolTypeAndName.

private boolean joinGroupWithProtocolTypeAndName(String joinGroupResponseProtocolType, String syncGroupResponseProtocolType, String syncGroupResponseProtocolName) {
    setupCoordinator();
    mockClient.reset();
    mockClient.prepareResponse(groupCoordinatorResponse(node, Errors.NONE));
    coordinator.ensureCoordinatorReady(mockTime.timer(0));
    mockClient.prepareResponse(body -> {
        if (!(body instanceof JoinGroupRequest)) {
            return false;
        }
        JoinGroupRequest joinGroupRequest = (JoinGroupRequest) body;
        return joinGroupRequest.data().protocolType().equals(PROTOCOL_TYPE);
    }, joinGroupFollowerResponse(defaultGeneration, memberId, "memberid", Errors.NONE, joinGroupResponseProtocolType));
    mockClient.prepareResponse(body -> {
        if (!(body instanceof SyncGroupRequest)) {
            return false;
        }
        SyncGroupRequest syncGroupRequest = (SyncGroupRequest) body;
        return syncGroupRequest.data().protocolType().equals(PROTOCOL_TYPE) && syncGroupRequest.data().protocolName().equals(PROTOCOL_NAME);
    }, syncGroupResponse(Errors.NONE, syncGroupResponseProtocolType, syncGroupResponseProtocolName));
    return coordinator.joinGroupIfNeeded(mockTime.timer(5000L));
}
Also used : JoinGroupRequest(org.apache.kafka.common.requests.JoinGroupRequest) SyncGroupRequest(org.apache.kafka.common.requests.SyncGroupRequest)

Example 23 with SyncGroupRequest

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

the class AbstractCoordinatorTest method testWakeupAfterSyncGroupReceived.

@Test
public void testWakeupAfterSyncGroupReceived() throws Exception {
    setupCoordinator();
    mockClient.prepareResponse(groupCoordinatorResponse(node, Errors.NONE));
    mockClient.prepareResponse(joinGroupFollowerResponse(1, memberId, leaderId, Errors.NONE));
    mockClient.prepareResponse(body -> {
        boolean isSyncGroupRequest = body instanceof SyncGroupRequest;
        if (isSyncGroupRequest)
            // wakeup after the request returns
            consumerClient.wakeup();
        return isSyncGroupRequest;
    }, 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) SyncGroupRequest(org.apache.kafka.common.requests.SyncGroupRequest) WakeupException(org.apache.kafka.common.errors.WakeupException) Test(org.junit.jupiter.api.Test)

Example 24 with SyncGroupRequest

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

the class WorkerCoordinatorTest method testNormalJoinGroupLeader.

@Test
public void testNormalJoinGroupLeader() {
    EasyMock.expect(configStorage.snapshot()).andReturn(configState1);
    PowerMock.replayAll();
    final String consumerId = "leader";
    client.prepareResponse(FindCoordinatorResponse.prepareResponse(Errors.NONE, groupId, node));
    coordinator.ensureCoordinatorReady(time.timer(Long.MAX_VALUE));
    // normal join group
    Map<String, Long> memberConfigOffsets = new HashMap<>();
    memberConfigOffsets.put("leader", 1L);
    memberConfigOffsets.put("member", 1L);
    client.prepareResponse(joinGroupLeaderResponse(1, consumerId, memberConfigOffsets, Errors.NONE));
    client.prepareResponse(body -> {
        SyncGroupRequest sync = (SyncGroupRequest) body;
        return sync.data().memberId().equals(consumerId) && sync.data().generationId() == 1 && sync.groupAssignments().containsKey(consumerId);
    }, syncGroupResponse(ConnectProtocol.Assignment.NO_ERROR, "leader", 1L, Collections.singletonList(connectorId1), Collections.emptyList(), Errors.NONE));
    coordinator.ensureActiveGroup();
    assertFalse(coordinator.rejoinNeededOrPending());
    assertEquals(0, rebalanceListener.revokedCount);
    assertEquals(1, rebalanceListener.assignedCount);
    assertFalse(rebalanceListener.assignment.failed());
    assertEquals(1L, rebalanceListener.assignment.offset());
    assertEquals("leader", rebalanceListener.assignment.leader());
    assertEquals(Collections.singletonList(connectorId1), rebalanceListener.assignment.connectors());
    assertEquals(Collections.emptyList(), rebalanceListener.assignment.tasks());
    PowerMock.verifyAll();
}
Also used : HashMap(java.util.HashMap) SyncGroupRequest(org.apache.kafka.common.requests.SyncGroupRequest) Test(org.junit.Test)

Example 25 with SyncGroupRequest

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

the class WorkerCoordinatorTest method testNormalJoinGroupFollower.

@Test
public void testNormalJoinGroupFollower() {
    EasyMock.expect(configStorage.snapshot()).andReturn(configState1);
    PowerMock.replayAll();
    final String memberId = "member";
    client.prepareResponse(FindCoordinatorResponse.prepareResponse(Errors.NONE, groupId, node));
    coordinator.ensureCoordinatorReady(time.timer(Long.MAX_VALUE));
    // normal join group
    client.prepareResponse(joinGroupFollowerResponse(1, memberId, "leader", Errors.NONE));
    client.prepareResponse(body -> {
        SyncGroupRequest sync = (SyncGroupRequest) body;
        return sync.data().memberId().equals(memberId) && sync.data().generationId() == 1 && sync.data().assignments().isEmpty();
    }, syncGroupResponse(ConnectProtocol.Assignment.NO_ERROR, "leader", 1L, Collections.emptyList(), Collections.singletonList(taskId1x0), Errors.NONE));
    coordinator.ensureActiveGroup();
    assertFalse(coordinator.rejoinNeededOrPending());
    assertEquals(0, rebalanceListener.revokedCount);
    assertEquals(1, rebalanceListener.assignedCount);
    assertFalse(rebalanceListener.assignment.failed());
    assertEquals(1L, rebalanceListener.assignment.offset());
    assertEquals(Collections.emptyList(), rebalanceListener.assignment.connectors());
    assertEquals(Collections.singletonList(taskId1x0), rebalanceListener.assignment.tasks());
    PowerMock.verifyAll();
}
Also used : SyncGroupRequest(org.apache.kafka.common.requests.SyncGroupRequest) Test(org.junit.Test)

Aggregations

SyncGroupRequest (org.apache.kafka.common.requests.SyncGroupRequest)25 Test (org.junit.jupiter.api.Test)13 Test (org.junit.Test)10 MockClient (org.apache.kafka.clients.MockClient)9 AbstractRequest (org.apache.kafka.common.requests.AbstractRequest)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8 WakeupException (org.apache.kafka.common.errors.WakeupException)8 TopicPartition (org.apache.kafka.common.TopicPartition)7 ArrayList (java.util.ArrayList)5 Collections.singletonList (java.util.Collections.singletonList)5 List (java.util.List)5 Collections.emptyList (java.util.Collections.emptyList)4 HashMap (java.util.HashMap)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 JoinGroupRequest (org.apache.kafka.common.requests.JoinGroupRequest)2 ConnectorTaskId (org.apache.kafka.connect.util.ConnectorTaskId)2 Collections.singletonMap (java.util.Collections.singletonMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 OffsetAndMetadata (org.apache.kafka.clients.consumer.OffsetAndMetadata)1