Search in sources :

Example 16 with AbstractRequest

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

the class AbstractCoordinatorTest method testWakeupAfterSyncGroupReceived.

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

        @Override
        public boolean matches(AbstractRequest 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 e) {
    }
    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) AbstractRequest(org.apache.kafka.common.requests.AbstractRequest) WakeupException(org.apache.kafka.common.errors.WakeupException) MockClient(org.apache.kafka.clients.MockClient) Test(org.junit.Test)

Example 17 with AbstractRequest

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

the class AbstractCoordinatorTest method testWakeupAfterJoinGroupReceived.

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

        @Override
        public boolean matches(AbstractRequest body) {
            boolean isJoinGroupRequest = body instanceof JoinGroupRequest;
            if (isJoinGroupRequest)
                // wakeup after the request returns
                consumerClient.wakeup();
            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());
    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)

Example 18 with AbstractRequest

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

the class AbstractCoordinatorTest method testWakeupAfterSyncGroupSentExternalCompletion.

@Test
public void testWakeupAfterSyncGroupSentExternalCompletion() throws Exception {
    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)
                // simulate wakeup after the request sent
                throw new WakeupException();
            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(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.Test)

Example 19 with AbstractRequest

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

the class ConsumerCoordinatorTest method testCommitAfterLeaveGroup.

@Test
public void testCommitAfterLeaveGroup() {
    // enable auto-assignment
    subscriptions.subscribe(singleton(topic1), rebalanceListener);
    client.prepareResponse(groupCoordinatorResponse(node, Errors.NONE));
    coordinator.ensureCoordinatorReady();
    client.prepareResponse(joinGroupFollowerResponse(1, "consumer", "leader", Errors.NONE));
    client.prepareResponse(syncGroupResponse(singletonList(t1p), Errors.NONE));
    client.prepareMetadataUpdate(cluster, Collections.<String>emptySet());
    coordinator.joinGroupIfNeeded();
    // now switch to manual assignment
    client.prepareResponse(new LeaveGroupResponse(Errors.NONE));
    subscriptions.unsubscribe();
    coordinator.maybeLeaveGroup();
    subscriptions.assignFromUser(singleton(t1p));
    // the client should not reuse generation/memberId from auto-subscribed generation
    client.prepareResponse(new MockClient.RequestMatcher() {

        @Override
        public boolean matches(AbstractRequest body) {
            OffsetCommitRequest commitRequest = (OffsetCommitRequest) body;
            return commitRequest.memberId().equals(OffsetCommitRequest.DEFAULT_MEMBER_ID) && commitRequest.generationId() == OffsetCommitRequest.DEFAULT_GENERATION_ID;
        }
    }, offsetCommitResponse(Collections.singletonMap(t1p, Errors.NONE)));
    AtomicBoolean success = new AtomicBoolean(false);
    coordinator.commitOffsetsAsync(Collections.singletonMap(t1p, new OffsetAndMetadata(100L)), callback(success));
    coordinator.invokeCompletedOffsetCommitCallbacks();
    assertTrue(success.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LeaveGroupResponse(org.apache.kafka.common.requests.LeaveGroupResponse) OffsetCommitRequest(org.apache.kafka.common.requests.OffsetCommitRequest) AbstractRequest(org.apache.kafka.common.requests.AbstractRequest) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) MockClient(org.apache.kafka.clients.MockClient) Test(org.junit.Test)

Example 20 with AbstractRequest

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

the class ConsumerCoordinatorTest method gracefulCloseTest.

private void gracefulCloseTest(ConsumerCoordinator coordinator, boolean dynamicAssignment) throws Exception {
    final AtomicBoolean commitRequested = new AtomicBoolean();
    final AtomicBoolean leaveGroupRequested = new AtomicBoolean();
    client.prepareResponse(new MockClient.RequestMatcher() {

        @Override
        public boolean matches(AbstractRequest body) {
            commitRequested.set(true);
            OffsetCommitRequest commitRequest = (OffsetCommitRequest) body;
            return commitRequest.groupId().equals(groupId);
        }
    }, new OffsetCommitResponse(new HashMap<TopicPartition, Errors>()));
    client.prepareResponse(new MockClient.RequestMatcher() {

        @Override
        public boolean matches(AbstractRequest body) {
            leaveGroupRequested.set(true);
            LeaveGroupRequest leaveRequest = (LeaveGroupRequest) body;
            return leaveRequest.groupId().equals(groupId);
        }
    }, new LeaveGroupResponse(Errors.NONE));
    coordinator.close();
    assertTrue("Commit not requested", commitRequested.get());
    if (dynamicAssignment)
        assertTrue("Leave group not requested", leaveGroupRequested.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LeaveGroupResponse(org.apache.kafka.common.requests.LeaveGroupResponse) OffsetCommitRequest(org.apache.kafka.common.requests.OffsetCommitRequest) HashMap(java.util.HashMap) OffsetCommitResponse(org.apache.kafka.common.requests.OffsetCommitResponse) AbstractRequest(org.apache.kafka.common.requests.AbstractRequest) LeaveGroupRequest(org.apache.kafka.common.requests.LeaveGroupRequest) MockClient(org.apache.kafka.clients.MockClient)

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