Search in sources :

Example 11 with LeaveGroupResponseData

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

the class LeaveGroupResponseTest method testParse.

@Test
public void testParse() {
    Map<Errors, Integer> expectedErrorCounts = Collections.singletonMap(Errors.NOT_COORDINATOR, 1);
    LeaveGroupResponseData data = new LeaveGroupResponseData().setErrorCode(Errors.NOT_COORDINATOR.code()).setThrottleTimeMs(throttleTimeMs);
    for (short version : ApiKeys.LEAVE_GROUP.allVersions()) {
        ByteBuffer buffer = MessageUtil.toByteBuffer(data, version);
        LeaveGroupResponse leaveGroupResponse = LeaveGroupResponse.parse(buffer, version);
        assertEquals(expectedErrorCounts, leaveGroupResponse.errorCounts());
        if (version >= 1) {
            assertEquals(throttleTimeMs, leaveGroupResponse.throttleTimeMs());
        } else {
            assertEquals(DEFAULT_THROTTLE_TIME, leaveGroupResponse.throttleTimeMs());
        }
        assertEquals(Errors.NOT_COORDINATOR, leaveGroupResponse.error());
    }
}
Also used : Errors(org.apache.kafka.common.protocol.Errors) LeaveGroupResponseData(org.apache.kafka.common.message.LeaveGroupResponseData) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 12 with LeaveGroupResponseData

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

the class LeaveGroupRequestTest method testSingleLeaveConstructor.

@Test
public void testSingleLeaveConstructor() {
    final LeaveGroupRequestData expectedData = new LeaveGroupRequestData().setGroupId(groupId).setMemberId(memberIdOne);
    List<MemberIdentity> singleMember = Collections.singletonList(new MemberIdentity().setMemberId(memberIdOne));
    builder = new LeaveGroupRequest.Builder(groupId, singleMember);
    for (short version = 0; version <= 2; version++) {
        LeaveGroupRequest request = builder.build(version);
        assertEquals(expectedData, request.data());
        assertEquals(singleMember, request.members());
        int expectedThrottleTime = version >= 1 ? throttleTimeMs : AbstractResponse.DEFAULT_THROTTLE_TIME;
        LeaveGroupResponse expectedResponse = new LeaveGroupResponse(new LeaveGroupResponseData().setErrorCode(Errors.NOT_CONTROLLER.code()).setThrottleTimeMs(expectedThrottleTime));
        assertEquals(expectedResponse, request.getErrorResponse(throttleTimeMs, Errors.NOT_CONTROLLER.exception()));
    }
}
Also used : LeaveGroupRequestData(org.apache.kafka.common.message.LeaveGroupRequestData) MemberIdentity(org.apache.kafka.common.message.LeaveGroupRequestData.MemberIdentity) LeaveGroupResponseData(org.apache.kafka.common.message.LeaveGroupResponseData) Test(org.junit.jupiter.api.Test)

Example 13 with LeaveGroupResponseData

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

the class ConsumerCoordinatorTest method testCommitAfterLeaveGroup.

@Test
public void testCommitAfterLeaveGroup() {
    // enable auto-assignment
    subscriptions.subscribe(singleton(topic1), rebalanceListener);
    joinAsFollowerAndReceiveAssignment(coordinator, singletonList(t1p));
    // now switch to manual assignment
    client.prepareResponse(new LeaveGroupResponse(new LeaveGroupResponseData().setErrorCode(Errors.NONE.code())));
    subscriptions.unsubscribe();
    coordinator.maybeLeaveGroup("test commit after leave");
    subscriptions.assignFromUser(singleton(t1p));
    // the client should not reuse generation/memberId from auto-subscribed generation
    client.prepareResponse(body -> {
        OffsetCommitRequest commitRequest = (OffsetCommitRequest) body;
        return commitRequest.data().memberId().equals(OffsetCommitRequest.DEFAULT_MEMBER_ID) && commitRequest.data().generationId() == OffsetCommitRequest.DEFAULT_GENERATION_ID;
    }, offsetCommitResponse(singletonMap(t1p, Errors.NONE)));
    AtomicBoolean success = new AtomicBoolean(false);
    coordinator.commitOffsetsAsync(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) LeaveGroupResponseData(org.apache.kafka.common.message.LeaveGroupResponseData) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) Test(org.junit.jupiter.api.Test)

Example 14 with LeaveGroupResponseData

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

the class ConsumerCoordinatorTest method gracefulCloseTest.

private void gracefulCloseTest(ConsumerCoordinator coordinator, boolean shouldLeaveGroup) {
    final AtomicBoolean commitRequested = new AtomicBoolean();
    final AtomicBoolean leaveGroupRequested = new AtomicBoolean();
    client.prepareResponse(body -> {
        commitRequested.set(true);
        OffsetCommitRequest commitRequest = (OffsetCommitRequest) body;
        return commitRequest.data().groupId().equals(groupId);
    }, new OffsetCommitResponse(new OffsetCommitResponseData()));
    if (shouldLeaveGroup)
        client.prepareResponse(body -> {
            leaveGroupRequested.set(true);
            LeaveGroupRequest leaveRequest = (LeaveGroupRequest) body;
            return leaveRequest.data().groupId().equals(groupId);
        }, new LeaveGroupResponse(new LeaveGroupResponseData().setErrorCode(Errors.NONE.code())));
    client.prepareResponse(body -> {
        commitRequested.set(true);
        OffsetCommitRequest commitRequest = (OffsetCommitRequest) body;
        return commitRequest.data().groupId().equals(groupId);
    }, new OffsetCommitResponse(new OffsetCommitResponseData()));
    coordinator.close();
    assertTrue(commitRequested.get(), "Commit not requested");
    assertEquals(shouldLeaveGroup, leaveGroupRequested.get(), "leaveGroupRequested should be " + shouldLeaveGroup);
    if (shouldLeaveGroup) {
        assertEquals(1, rebalanceListener.revokedCount);
        assertEquals(singleton(t1p), rebalanceListener.revoked);
    }
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) MockTime(org.apache.kafka.common.utils.MockTime) Arrays(java.util.Arrays) ConsumerGroupMetadata(org.apache.kafka.clients.consumer.ConsumerGroupMetadata) LeaveGroupRequest(org.apache.kafka.common.requests.LeaveGroupRequest) KafkaException(org.apache.kafka.common.KafkaException) OffsetResetStrategy(org.apache.kafka.clients.consumer.OffsetResetStrategy) HeartbeatResponse(org.apache.kafka.common.requests.HeartbeatResponse) Collections.singletonList(java.util.Collections.singletonList) Future(java.util.concurrent.Future) Utils.mkMap(org.apache.kafka.common.utils.Utils.mkMap) SyncGroupRequest(org.apache.kafka.common.requests.SyncGroupRequest) RecordBatch(org.apache.kafka.common.record.RecordBatch) LogContext(org.apache.kafka.common.utils.LogContext) Map(java.util.Map) OffsetCommitCallback(org.apache.kafka.clients.consumer.OffsetCommitCallback) TestUtils(org.apache.kafka.test.TestUtils) Utils.mkSet(org.apache.kafka.common.utils.Utils.mkSet) Set(java.util.Set) Executors(java.util.concurrent.Executors) Metrics(org.apache.kafka.common.metrics.Metrics) Utils.mkEntry(org.apache.kafka.common.utils.Utils.mkEntry) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Errors(org.apache.kafka.common.protocol.Errors) Node(org.apache.kafka.common.Node) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) NodeApiVersions(org.apache.kafka.clients.NodeApiVersions) AuthenticationException(org.apache.kafka.common.errors.AuthenticationException) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.fail(org.junit.jupiter.api.Assertions.fail) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) RetriableCommitFailedException(org.apache.kafka.clients.consumer.RetriableCommitFailedException) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) RequestTestUtils(org.apache.kafka.common.requests.RequestTestUtils) ArrayList(java.util.ArrayList) Type(org.apache.kafka.common.protocol.types.Type) OffsetFetchResponse(org.apache.kafka.common.requests.OffsetFetchResponse) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Topic(org.apache.kafka.common.internals.Topic) OffsetCommitResponse(org.apache.kafka.common.requests.OffsetCommitResponse) SyncGroupResponseData(org.apache.kafka.common.message.SyncGroupResponseData) SystemTime(org.apache.kafka.common.utils.SystemTime) Schema(org.apache.kafka.common.protocol.types.Schema) FencedInstanceIdException(org.apache.kafka.common.errors.FencedInstanceIdException) Struct(org.apache.kafka.common.protocol.types.Struct) TestUtils.toSet(org.apache.kafka.test.TestUtils.toSet) OffsetCommitResponseData(org.apache.kafka.common.message.OffsetCommitResponseData) AfterEach(org.junit.jupiter.api.AfterEach) COOPERATIVE_STICKY_ASSIGNOR_NAME(org.apache.kafka.clients.consumer.CooperativeStickyAssignor.COOPERATIVE_STICKY_ASSIGNOR_NAME) GroupRebalanceConfig(org.apache.kafka.clients.GroupRebalanceConfig) CommitFailedException(org.apache.kafka.clients.consumer.CommitFailedException) UnsupportedVersionException(org.apache.kafka.common.errors.UnsupportedVersionException) FindCoordinatorResponse(org.apache.kafka.common.requests.FindCoordinatorResponse) JoinGroupRequest(org.apache.kafka.common.requests.JoinGroupRequest) GroupAuthorizationException(org.apache.kafka.common.errors.GroupAuthorizationException) TimeoutException(java.util.concurrent.TimeoutException) MemberIdentity(org.apache.kafka.common.message.LeaveGroupRequestData.MemberIdentity) ByteBuffer(java.nio.ByteBuffer) Collections.singleton(java.util.Collections.singleton) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Metric(org.apache.kafka.common.Metric) MetricName(org.apache.kafka.common.MetricName) OffsetCommitRequestData(org.apache.kafka.common.message.OffsetCommitRequestData) EAGER(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.RebalanceProtocol.EAGER) TopicPartition(org.apache.kafka.common.TopicPartition) HeartbeatResponseData(org.apache.kafka.common.message.HeartbeatResponseData) WakeupException(org.apache.kafka.common.errors.WakeupException) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) ClusterResourceListeners(org.apache.kafka.common.internals.ClusterResourceListeners) RebalanceInProgressException(org.apache.kafka.common.errors.RebalanceInProgressException) Test(org.junit.jupiter.api.Test) List(java.util.List) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) Optional(java.util.Optional) KafkaMetric(org.apache.kafka.common.metrics.KafkaMetric) Field(org.apache.kafka.common.protocol.types.Field) Pattern(java.util.regex.Pattern) JoinGroupResponse(org.apache.kafka.common.requests.JoinGroupResponse) ClientResponse(org.apache.kafka.clients.ClientResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) LeaveGroupResponse(org.apache.kafka.common.requests.LeaveGroupResponse) HashSet(java.util.HashSet) OffsetMetadataTooLarge(org.apache.kafka.common.errors.OffsetMetadataTooLarge) ArgumentCaptor(org.mockito.ArgumentCaptor) SyncGroupResponse(org.apache.kafka.common.requests.SyncGroupResponse) Collections.singletonMap(java.util.Collections.singletonMap) COOPERATIVE(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor.RebalanceProtocol.COOPERATIVE) ExecutorService(java.util.concurrent.ExecutorService) ConsumerPartitionAssignor(org.apache.kafka.clients.consumer.ConsumerPartitionAssignor) Utils(org.apache.kafka.common.utils.Utils) OffsetCommitRequest(org.apache.kafka.common.requests.OffsetCommitRequest) MockClient(org.apache.kafka.clients.MockClient) Iterator(java.util.Iterator) Collections.emptySet(java.util.Collections.emptySet) LeaveGroupResponseData(org.apache.kafka.common.message.LeaveGroupResponseData) ApiKeys(org.apache.kafka.common.protocol.ApiKeys) JoinGroupResponseData(org.apache.kafka.common.message.JoinGroupResponseData) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) DisconnectException(org.apache.kafka.common.errors.DisconnectException) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) JoinGroupRequestData(org.apache.kafka.common.message.JoinGroupRequestData) Collections(java.util.Collections) ApiException(org.apache.kafka.common.errors.ApiException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LeaveGroupResponse(org.apache.kafka.common.requests.LeaveGroupResponse) OffsetCommitRequest(org.apache.kafka.common.requests.OffsetCommitRequest) LeaveGroupResponseData(org.apache.kafka.common.message.LeaveGroupResponseData) OffsetCommitResponse(org.apache.kafka.common.requests.OffsetCommitResponse) LeaveGroupRequest(org.apache.kafka.common.requests.LeaveGroupRequest) OffsetCommitResponseData(org.apache.kafka.common.message.OffsetCommitResponseData)

Example 15 with LeaveGroupResponseData

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

the class KafkaConsumerTest method testGracefulClose.

@Test
public void testGracefulClose() throws Exception {
    Map<TopicPartition, Errors> response = new HashMap<>();
    response.put(tp0, Errors.NONE);
    OffsetCommitResponse commitResponse = offsetCommitResponse(response);
    LeaveGroupResponse leaveGroupResponse = new LeaveGroupResponse(new LeaveGroupResponseData().setErrorCode(Errors.NONE.code()));
    consumerCloseTest(5000, Arrays.asList(commitResponse, leaveGroupResponse), 0, false);
}
Also used : Errors(org.apache.kafka.common.protocol.Errors) LeaveGroupResponse(org.apache.kafka.common.requests.LeaveGroupResponse) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) LeaveGroupResponseData(org.apache.kafka.common.message.LeaveGroupResponseData) TopicPartition(org.apache.kafka.common.TopicPartition) OffsetCommitResponse(org.apache.kafka.common.requests.OffsetCommitResponse) 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