Search in sources :

Example 11 with SyncGroupRequest

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

the class ConsumerCoordinatorTest method testForceMetadataDeleteForPatternSubscriptionDuringRebalance.

@Test
public void testForceMetadataDeleteForPatternSubscriptionDuringRebalance() {
    try (ConsumerCoordinator coordinator = buildCoordinator(rebalanceConfig, new Metrics(), assignors, true, subscriptions)) {
        subscriptions.subscribe(Pattern.compile("test.*"), rebalanceListener);
        client.updateMetadata(RequestTestUtils.metadataUpdateWith(1, new HashMap<String, Integer>() {

            {
                put(topic1, 1);
                put(topic2, 1);
            }
        }));
        coordinator.maybeUpdateSubscriptionMetadata();
        assertEquals(new HashSet<>(Arrays.asList(topic1, topic2)), subscriptions.subscription());
        client.prepareResponse(groupCoordinatorResponse(node, Errors.NONE));
        coordinator.ensureCoordinatorReady(time.timer(Long.MAX_VALUE));
        MetadataResponse deletedMetadataResponse = RequestTestUtils.metadataUpdateWith(1, new HashMap<String, Integer>() {

            {
                put(topic1, 1);
            }
        });
        // Instrument the test so that metadata will contain only one topic after next refresh.
        client.prepareMetadataUpdate(deletedMetadataResponse);
        client.prepareResponse(joinGroupFollowerResponse(1, consumerId, "leader", Errors.NONE));
        client.prepareResponse(body -> {
            SyncGroupRequest sync = (SyncGroupRequest) body;
            return sync.data().memberId().equals(consumerId) && sync.data().generationId() == 1 && sync.groupAssignments().isEmpty();
        }, syncGroupResponse(singletonList(t1p), Errors.NONE));
        partitionAssignor.prepare(singletonMap(consumerId, singletonList(t1p)));
        // This will trigger rebalance.
        coordinator.poll(time.timer(Long.MAX_VALUE));
        // Make sure that the metadata was refreshed during the rebalance and thus subscriptions now contain only one topic.
        assertEquals(singleton(topic1), subscriptions.subscription());
        // Refresh the metadata again. Since there have been no changes since the last refresh, it won't trigger
        // rebalance again.
        metadata.requestUpdate();
        consumerClient.poll(time.timer(Long.MAX_VALUE));
        assertFalse(coordinator.rejoinNeededOrPending());
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Metrics(org.apache.kafka.common.metrics.Metrics) HashMap(java.util.HashMap) SyncGroupRequest(org.apache.kafka.common.requests.SyncGroupRequest) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) Test(org.junit.jupiter.api.Test)

Example 12 with SyncGroupRequest

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

the class AbstractCoordinatorTest method testWakeupAfterSyncGroupReceivedExternalCompletion.

@Test
public void testWakeupAfterSyncGroupReceivedExternalCompletion() 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 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) WakeupException(org.apache.kafka.common.errors.WakeupException) Test(org.junit.jupiter.api.Test)

Example 13 with SyncGroupRequest

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

the class AbstractCoordinatorTest method testNoGenerationWillNotTriggerProtocolNameCheck.

@Test
public void testNoGenerationWillNotTriggerProtocolNameCheck() {
    final String wrongProtocolName = "wrong-name";
    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, PROTOCOL_TYPE));
    mockClient.prepareResponse(body -> {
        if (!(body instanceof SyncGroupRequest)) {
            return false;
        }
        coordinator.resetGenerationOnLeaveGroup();
        SyncGroupRequest syncGroupRequest = (SyncGroupRequest) body;
        return syncGroupRequest.data().protocolType().equals(PROTOCOL_TYPE) && syncGroupRequest.data().protocolName().equals(PROTOCOL_NAME);
    }, syncGroupResponse(Errors.NONE, PROTOCOL_TYPE, wrongProtocolName));
    // let the retry to complete successfully to break out of the while loop
    mockClient.prepareResponse(body -> {
        if (!(body instanceof JoinGroupRequest)) {
            return false;
        }
        JoinGroupRequest joinGroupRequest = (JoinGroupRequest) body;
        return joinGroupRequest.data().protocolType().equals(PROTOCOL_TYPE);
    }, joinGroupFollowerResponse(1, memberId, "memberid", Errors.NONE, PROTOCOL_TYPE));
    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, PROTOCOL_TYPE, PROTOCOL_NAME));
    // No exception shall be thrown as the generation is reset.
    coordinator.joinGroupIfNeeded(mockTime.timer(100L));
}
Also used : JoinGroupRequest(org.apache.kafka.common.requests.JoinGroupRequest) SyncGroupRequest(org.apache.kafka.common.requests.SyncGroupRequest) Test(org.junit.jupiter.api.Test)

Example 14 with SyncGroupRequest

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

the class AbstractCoordinatorTest method testWakeupAfterSyncGroupSent.

@Test
public void testWakeupAfterSyncGroupSent() throws Exception {
    setupCoordinator(RETRY_BACKOFF_MS);
    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());
    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 15 with SyncGroupRequest

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

the class AbstractCoordinatorTest method testWakeupAfterSyncGroupReceivedExternalCompletion.

@Test
public void testWakeupAfterSyncGroupReceivedExternalCompletion() throws Exception {
    setupCoordinator(RETRY_BACKOFF_MS);
    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)

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