Search in sources :

Example 1 with OffsetCommitRequest

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

the class KafkaConsumerTest method prepareOffsetCommitResponse.

private AtomicBoolean prepareOffsetCommitResponse(MockClient client, Node coordinator, final Map<TopicPartition, Long> partitionOffsets) {
    final AtomicBoolean commitReceived = new AtomicBoolean(true);
    Map<TopicPartition, Errors> response = new HashMap<>();
    for (TopicPartition partition : partitionOffsets.keySet()) response.put(partition, Errors.NONE);
    client.prepareResponseFrom(new MockClient.RequestMatcher() {

        @Override
        public boolean matches(AbstractRequest body) {
            OffsetCommitRequest commitRequest = (OffsetCommitRequest) body;
            for (Map.Entry<TopicPartition, Long> partitionOffset : partitionOffsets.entrySet()) {
                OffsetCommitRequest.PartitionData partitionData = commitRequest.offsetData().get(partitionOffset.getKey());
                // verify that the expected offset has been committed
                if (partitionData.offset != partitionOffset.getValue()) {
                    commitReceived.set(false);
                    return false;
                }
            }
            return true;
        }
    }, offsetCommitResponse(response), coordinator);
    return commitReceived;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Errors(org.apache.kafka.common.protocol.Errors) OffsetCommitRequest(org.apache.kafka.common.requests.OffsetCommitRequest) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) PartitionData(org.apache.kafka.common.requests.FetchResponse.PartitionData) TopicPartition(org.apache.kafka.common.TopicPartition) AbstractRequest(org.apache.kafka.common.requests.AbstractRequest) MockClient(org.apache.kafka.clients.MockClient)

Example 2 with OffsetCommitRequest

use of org.apache.kafka.common.requests.OffsetCommitRequest 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 3 with OffsetCommitRequest

use of org.apache.kafka.common.requests.OffsetCommitRequest 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

AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 MockClient (org.apache.kafka.clients.MockClient)3 AbstractRequest (org.apache.kafka.common.requests.AbstractRequest)3 OffsetCommitRequest (org.apache.kafka.common.requests.OffsetCommitRequest)3 HashMap (java.util.HashMap)2 LeaveGroupResponse (org.apache.kafka.common.requests.LeaveGroupResponse)2 LinkedHashMap (java.util.LinkedHashMap)1 OffsetAndMetadata (org.apache.kafka.clients.consumer.OffsetAndMetadata)1 TopicPartition (org.apache.kafka.common.TopicPartition)1 Errors (org.apache.kafka.common.protocol.Errors)1 PartitionData (org.apache.kafka.common.requests.FetchResponse.PartitionData)1 LeaveGroupRequest (org.apache.kafka.common.requests.LeaveGroupRequest)1 OffsetCommitResponse (org.apache.kafka.common.requests.OffsetCommitResponse)1 Test (org.junit.Test)1