Search in sources :

Example 1 with LeaveGroupResponseData

use of org.apache.kafka.common.message.LeaveGroupResponseData in project kafka by apache.

the class LeaveGroupResponseTest method testEqualityWithSerialization.

@Test
public void testEqualityWithSerialization() {
    LeaveGroupResponseData responseData = new LeaveGroupResponseData().setErrorCode(Errors.NONE.code()).setThrottleTimeMs(throttleTimeMs);
    for (short version : ApiKeys.LEAVE_GROUP.allVersions()) {
        LeaveGroupResponse primaryResponse = LeaveGroupResponse.parse(MessageUtil.toByteBuffer(responseData, version), version);
        LeaveGroupResponse secondaryResponse = LeaveGroupResponse.parse(MessageUtil.toByteBuffer(responseData, version), version);
        assertEquals(primaryResponse, primaryResponse);
        assertEquals(primaryResponse, secondaryResponse);
        assertEquals(primaryResponse.hashCode(), secondaryResponse.hashCode());
    }
}
Also used : LeaveGroupResponseData(org.apache.kafka.common.message.LeaveGroupResponseData) Test(org.junit.jupiter.api.Test)

Example 2 with LeaveGroupResponseData

use of org.apache.kafka.common.message.LeaveGroupResponseData in project kafka by apache.

the class ConsumerCoordinatorTest method testPendingMemberShouldLeaveGroup.

/**
 * This test checks if a consumer that has a valid member ID but an invalid generation
 * ({@link org.apache.kafka.clients.consumer.internals.AbstractCoordinator.Generation#NO_GENERATION})
 * can still execute a leave group request. Such a situation may arise when a consumer has initiated a JoinGroup
 * request without a memberId, but is shutdown or restarted before it has a chance to initiate and complete the
 * second request.
 */
@Test
public void testPendingMemberShouldLeaveGroup() {
    final String consumerId = "consumer-id";
    subscriptions.subscribe(singleton(topic1), rebalanceListener);
    client.prepareResponse(groupCoordinatorResponse(node, Errors.NONE));
    coordinator.ensureCoordinatorReady(time.timer(Long.MAX_VALUE));
    // here we return a DEFAULT_GENERATION_ID, but valid member id and leader id.
    client.prepareResponse(joinGroupFollowerResponse(-1, consumerId, "leader-id", Errors.MEMBER_ID_REQUIRED));
    // execute join group
    coordinator.joinGroupIfNeeded(time.timer(0));
    final AtomicBoolean received = new AtomicBoolean(false);
    client.prepareResponse(body -> {
        received.set(true);
        LeaveGroupRequest leaveRequest = (LeaveGroupRequest) body;
        return validateLeaveGroup(groupId, consumerId, leaveRequest);
    }, new LeaveGroupResponse(new LeaveGroupResponseData().setErrorCode(Errors.NONE.code())));
    coordinator.maybeLeaveGroup("pending member leaves");
    assertTrue(received.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LeaveGroupResponse(org.apache.kafka.common.requests.LeaveGroupResponse) LeaveGroupResponseData(org.apache.kafka.common.message.LeaveGroupResponseData) LeaveGroupRequest(org.apache.kafka.common.requests.LeaveGroupRequest) Test(org.junit.jupiter.api.Test)

Example 3 with LeaveGroupResponseData

use of org.apache.kafka.common.message.LeaveGroupResponseData in project kafka by apache.

the class ConsumerCoordinatorTest method testMaybeLeaveGroup.

@Test
public void testMaybeLeaveGroup() {
    subscriptions.subscribe(singleton(topic1), rebalanceListener);
    joinAsFollowerAndReceiveAssignment(coordinator, singletonList(t1p));
    final AtomicBoolean received = new AtomicBoolean(false);
    client.prepareResponse(body -> {
        received.set(true);
        LeaveGroupRequest leaveRequest = (LeaveGroupRequest) body;
        return validateLeaveGroup(groupId, consumerId, leaveRequest);
    }, new LeaveGroupResponse(new LeaveGroupResponseData().setErrorCode(Errors.NONE.code())));
    coordinator.maybeLeaveGroup("test maybe leave group");
    assertTrue(received.get());
    AbstractCoordinator.Generation generation = coordinator.generationIfStable();
    assertNull(generation);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LeaveGroupResponse(org.apache.kafka.common.requests.LeaveGroupResponse) LeaveGroupResponseData(org.apache.kafka.common.message.LeaveGroupResponseData) LeaveGroupRequest(org.apache.kafka.common.requests.LeaveGroupRequest) Test(org.junit.jupiter.api.Test)

Example 4 with LeaveGroupResponseData

use of org.apache.kafka.common.message.LeaveGroupResponseData in project kafka by apache.

the class ConsumerCoordinatorTest method testLeaveGroupOnClose.

@Test
public void testLeaveGroupOnClose() {
    subscriptions.subscribe(singleton(topic1), rebalanceListener);
    joinAsFollowerAndReceiveAssignment(coordinator, singletonList(t1p));
    final AtomicBoolean received = new AtomicBoolean(false);
    client.prepareResponse(body -> {
        received.set(true);
        LeaveGroupRequest leaveRequest = (LeaveGroupRequest) body;
        return validateLeaveGroup(groupId, consumerId, leaveRequest);
    }, new LeaveGroupResponse(new LeaveGroupResponseData().setErrorCode(Errors.NONE.code())));
    coordinator.close(time.timer(0));
    assertTrue(received.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LeaveGroupResponse(org.apache.kafka.common.requests.LeaveGroupResponse) LeaveGroupResponseData(org.apache.kafka.common.message.LeaveGroupResponseData) LeaveGroupRequest(org.apache.kafka.common.requests.LeaveGroupRequest) Test(org.junit.jupiter.api.Test)

Example 5 with LeaveGroupResponseData

use of org.apache.kafka.common.message.LeaveGroupResponseData in project kafka by apache.

the class KafkaAdminClientTest method testRemoveMembersFromGroupRetryBackoff.

@Test
public void testRemoveMembersFromGroupRetryBackoff() throws Exception {
    MockTime time = new MockTime();
    int retryBackoff = 100;
    try (final AdminClientUnitTestEnv env = new AdminClientUnitTestEnv(time, mockCluster(3, 0), newStrMap(AdminClientConfig.RETRY_BACKOFF_MS_CONFIG, "" + retryBackoff))) {
        MockClient mockClient = env.kafkaClient();
        mockClient.setNodeApiVersions(NodeApiVersions.create());
        AtomicLong firstAttemptTime = new AtomicLong(0);
        AtomicLong secondAttemptTime = new AtomicLong(0);
        mockClient.prepareResponse(prepareFindCoordinatorResponse(Errors.NONE, env.cluster().controller()));
        env.kafkaClient().prepareResponse(body -> {
            firstAttemptTime.set(time.milliseconds());
            return true;
        }, new LeaveGroupResponse(new LeaveGroupResponseData().setErrorCode(Errors.NOT_COORDINATOR.code())));
        mockClient.prepareResponse(prepareFindCoordinatorResponse(Errors.NONE, env.cluster().controller()));
        MemberResponse responseOne = new MemberResponse().setGroupInstanceId("instance-1").setErrorCode(Errors.NONE.code());
        env.kafkaClient().prepareResponse(body -> {
            secondAttemptTime.set(time.milliseconds());
            return true;
        }, new LeaveGroupResponse(new LeaveGroupResponseData().setErrorCode(Errors.NONE.code()).setMembers(Collections.singletonList(responseOne))));
        Collection<MemberToRemove> membersToRemove = singletonList(new MemberToRemove("instance-1"));
        final KafkaFuture<Void> future = env.adminClient().removeMembersFromConsumerGroup(GROUP_ID, new RemoveMembersFromConsumerGroupOptions(membersToRemove)).all();
        TestUtils.waitForCondition(() -> mockClient.numAwaitingResponses() == 1, "Failed awaiting RemoveMembersFromGroup first request failure");
        TestUtils.waitForCondition(() -> ((KafkaAdminClient) env.adminClient()).numPendingCalls() == 1, "Failed to add retry RemoveMembersFromGroup call on first failure");
        time.sleep(retryBackoff);
        future.get();
        long actualRetryBackoff = secondAttemptTime.get() - firstAttemptTime.get();
        assertEquals(retryBackoff, actualRetryBackoff, "RemoveMembersFromGroup retry did not await expected backoff!");
    }
}
Also used : MemberResponse(org.apache.kafka.common.message.LeaveGroupResponseData.MemberResponse) LeaveGroupResponse(org.apache.kafka.common.requests.LeaveGroupResponse) AtomicLong(java.util.concurrent.atomic.AtomicLong) LeaveGroupResponseData(org.apache.kafka.common.message.LeaveGroupResponseData) MockTime(org.apache.kafka.common.utils.MockTime) MockClient(org.apache.kafka.clients.MockClient) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Aggregations

LeaveGroupResponseData (org.apache.kafka.common.message.LeaveGroupResponseData)15 Test (org.junit.jupiter.api.Test)14 LeaveGroupResponse (org.apache.kafka.common.requests.LeaveGroupResponse)12 LeaveGroupRequest (org.apache.kafka.common.requests.LeaveGroupRequest)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 Errors (org.apache.kafka.common.protocol.Errors)5 MockTime (org.apache.kafka.common.utils.MockTime)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5 ByteBuffer (java.nio.ByteBuffer)3 HashMap (java.util.HashMap)3 MockClient (org.apache.kafka.clients.MockClient)3 OffsetAndMetadata (org.apache.kafka.clients.consumer.OffsetAndMetadata)3 Cluster (org.apache.kafka.common.Cluster)3 TopicPartition (org.apache.kafka.common.TopicPartition)3 LeaveGroupRequestData (org.apache.kafka.common.message.LeaveGroupRequestData)3 MemberIdentity (org.apache.kafka.common.message.LeaveGroupRequestData.MemberIdentity)3 OffsetCommitResponse (org.apache.kafka.common.requests.OffsetCommitResponse)3 Time (org.apache.kafka.common.utils.Time)3 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2