Search in sources :

Example 1 with OffsetCommitRequestData

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

the class ConsumerCoordinator method sendOffsetCommitRequest.

/**
 * Commit offsets for the specified list of topics and partitions. This is a non-blocking call
 * which returns a request future that can be polled in the case of a synchronous commit or ignored in the
 * asynchronous case.
 *
 * NOTE: This is visible only for testing
 *
 * @param offsets The list of offsets per partition that should be committed.
 * @return A request future whose value indicates whether the commit was successful or not
 */
RequestFuture<Void> sendOffsetCommitRequest(final Map<TopicPartition, OffsetAndMetadata> offsets) {
    if (offsets.isEmpty())
        return RequestFuture.voidSuccess();
    Node coordinator = checkAndGetCoordinator();
    if (coordinator == null)
        return RequestFuture.coordinatorNotAvailable();
    // create the offset commit request
    Map<String, OffsetCommitRequestData.OffsetCommitRequestTopic> requestTopicDataMap = new HashMap<>();
    for (Map.Entry<TopicPartition, OffsetAndMetadata> entry : offsets.entrySet()) {
        TopicPartition topicPartition = entry.getKey();
        OffsetAndMetadata offsetAndMetadata = entry.getValue();
        if (offsetAndMetadata.offset() < 0) {
            return RequestFuture.failure(new IllegalArgumentException("Invalid offset: " + offsetAndMetadata.offset()));
        }
        OffsetCommitRequestData.OffsetCommitRequestTopic topic = requestTopicDataMap.getOrDefault(topicPartition.topic(), new OffsetCommitRequestData.OffsetCommitRequestTopic().setName(topicPartition.topic()));
        topic.partitions().add(new OffsetCommitRequestData.OffsetCommitRequestPartition().setPartitionIndex(topicPartition.partition()).setCommittedOffset(offsetAndMetadata.offset()).setCommittedLeaderEpoch(offsetAndMetadata.leaderEpoch().orElse(RecordBatch.NO_PARTITION_LEADER_EPOCH)).setCommittedMetadata(offsetAndMetadata.metadata()));
        requestTopicDataMap.put(topicPartition.topic(), topic);
    }
    final Generation generation;
    if (subscriptions.hasAutoAssignedPartitions()) {
        generation = generationIfStable();
        // the only thing we can do is fail the commit and let the user rejoin the group in poll().
        if (generation == null) {
            log.info("Failing OffsetCommit request since the consumer is not part of an active group");
            if (rebalanceInProgress()) {
                // CommitFailedException to indicate this is not a fatal error
                return RequestFuture.failure(new RebalanceInProgressException("Offset commit cannot be completed since the " + "consumer is undergoing a rebalance for auto partition assignment. You can try completing the rebalance " + "by calling poll() and then retry the operation."));
            } else {
                return RequestFuture.failure(new CommitFailedException("Offset commit cannot be completed since the " + "consumer is not part of an active group for auto partition assignment; it is likely that the consumer " + "was kicked out of the group."));
            }
        }
    } else {
        generation = Generation.NO_GENERATION;
    }
    OffsetCommitRequest.Builder builder = new OffsetCommitRequest.Builder(new OffsetCommitRequestData().setGroupId(this.rebalanceConfig.groupId).setGenerationId(generation.generationId).setMemberId(generation.memberId).setGroupInstanceId(rebalanceConfig.groupInstanceId.orElse(null)).setTopics(new ArrayList<>(requestTopicDataMap.values())));
    log.trace("Sending OffsetCommit request with {} to coordinator {}", offsets, coordinator);
    return client.send(coordinator, builder).compose(new OffsetCommitResponseHandler(offsets, generation));
}
Also used : HashMap(java.util.HashMap) OffsetCommitRequestData(org.apache.kafka.common.message.OffsetCommitRequestData) RebalanceInProgressException(org.apache.kafka.common.errors.RebalanceInProgressException) Node(org.apache.kafka.common.Node) ArrayList(java.util.ArrayList) OffsetCommitRequest(org.apache.kafka.common.requests.OffsetCommitRequest) TopicPartition(org.apache.kafka.common.TopicPartition) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) Map(java.util.Map) HashMap(java.util.HashMap) RetriableCommitFailedException(org.apache.kafka.clients.consumer.RetriableCommitFailedException) CommitFailedException(org.apache.kafka.clients.consumer.CommitFailedException)

Example 2 with OffsetCommitRequestData

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

the class ConsumerCoordinatorTest method testCoordinatorUnknownInUnsentCallbacksAfterCoordinatorDead.

@Test
public void testCoordinatorUnknownInUnsentCallbacksAfterCoordinatorDead() {
    // When the coordinator is marked dead, all unsent or in-flight requests are cancelled
    // with a disconnect error. This test case ensures that the corresponding callbacks see
    // the coordinator as unknown which prevents additional retries to the same coordinator.
    client.prepareResponse(groupCoordinatorResponse(node, Errors.NONE));
    coordinator.ensureCoordinatorReady(time.timer(Long.MAX_VALUE));
    final AtomicBoolean asyncCallbackInvoked = new AtomicBoolean(false);
    OffsetCommitRequestData offsetCommitRequestData = new OffsetCommitRequestData().setGroupId(groupId).setTopics(Collections.singletonList(new OffsetCommitRequestData.OffsetCommitRequestTopic().setName("foo").setPartitions(Collections.singletonList(new OffsetCommitRequestData.OffsetCommitRequestPartition().setPartitionIndex(0).setCommittedLeaderEpoch(RecordBatch.NO_PARTITION_LEADER_EPOCH).setCommittedMetadata("").setCommittedOffset(13L).setCommitTimestamp(0)))));
    consumerClient.send(coordinator.checkAndGetCoordinator(), new OffsetCommitRequest.Builder(offsetCommitRequestData)).compose(new RequestFutureAdapter<ClientResponse, Object>() {

        @Override
        public void onSuccess(ClientResponse value, RequestFuture<Object> future) {
        }

        @Override
        public void onFailure(RuntimeException e, RequestFuture<Object> future) {
            assertTrue(e instanceof DisconnectException, "Unexpected exception type: " + e.getClass());
            assertTrue(coordinator.coordinatorUnknown());
            asyncCallbackInvoked.set(true);
        }
    });
    coordinator.markCoordinatorUnknown("test cause");
    consumerClient.pollNoWakeup();
    assertTrue(asyncCallbackInvoked.get());
}
Also used : ClientResponse(org.apache.kafka.clients.ClientResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) OffsetCommitRequestData(org.apache.kafka.common.message.OffsetCommitRequestData) DisconnectException(org.apache.kafka.common.errors.DisconnectException) Test(org.junit.jupiter.api.Test)

Example 3 with OffsetCommitRequestData

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

the class OffsetCommitRequestTest method setUp.

@BeforeEach
public void setUp() {
    topics = Arrays.asList(new OffsetCommitRequestTopic().setName(topicOne).setPartitions(Collections.singletonList(new OffsetCommitRequestPartition().setPartitionIndex(partitionOne).setCommittedOffset(offset).setCommittedLeaderEpoch(leaderEpoch).setCommittedMetadata(metadata))), new OffsetCommitRequestTopic().setName(topicTwo).setPartitions(Collections.singletonList(new OffsetCommitRequestPartition().setPartitionIndex(partitionTwo).setCommittedOffset(offset).setCommittedLeaderEpoch(leaderEpoch).setCommittedMetadata(metadata))));
    data = new OffsetCommitRequestData().setGroupId(groupId).setTopics(topics);
}
Also used : OffsetCommitRequestTopic(org.apache.kafka.common.message.OffsetCommitRequestData.OffsetCommitRequestTopic) OffsetCommitRequestData(org.apache.kafka.common.message.OffsetCommitRequestData) OffsetCommitRequestPartition(org.apache.kafka.common.message.OffsetCommitRequestData.OffsetCommitRequestPartition) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with OffsetCommitRequestData

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

the class AlterConsumerGroupOffsetsHandler method buildRequest.

@Override
public OffsetCommitRequest.Builder buildRequest(int coordinatorId, Set<CoordinatorKey> groupIds) {
    validateKeys(groupIds);
    Map<String, OffsetCommitRequestTopic> offsetData = new HashMap<>();
    offsets.forEach((topicPartition, offsetAndMetadata) -> {
        OffsetCommitRequestTopic topic = offsetData.computeIfAbsent(topicPartition.topic(), key -> new OffsetCommitRequestTopic().setName(topicPartition.topic()));
        topic.partitions().add(new OffsetCommitRequestPartition().setCommittedOffset(offsetAndMetadata.offset()).setCommittedLeaderEpoch(offsetAndMetadata.leaderEpoch().orElse(-1)).setCommittedMetadata(offsetAndMetadata.metadata()).setPartitionIndex(topicPartition.partition()));
    });
    OffsetCommitRequestData data = new OffsetCommitRequestData().setGroupId(groupId.idValue).setTopics(new ArrayList<>(offsetData.values()));
    return new OffsetCommitRequest.Builder(data);
}
Also used : OffsetCommitRequestTopic(org.apache.kafka.common.message.OffsetCommitRequestData.OffsetCommitRequestTopic) HashMap(java.util.HashMap) OffsetCommitRequestData(org.apache.kafka.common.message.OffsetCommitRequestData) OffsetCommitRequestPartition(org.apache.kafka.common.message.OffsetCommitRequestData.OffsetCommitRequestPartition)

Aggregations

OffsetCommitRequestData (org.apache.kafka.common.message.OffsetCommitRequestData)4 HashMap (java.util.HashMap)2 OffsetCommitRequestPartition (org.apache.kafka.common.message.OffsetCommitRequestData.OffsetCommitRequestPartition)2 OffsetCommitRequestTopic (org.apache.kafka.common.message.OffsetCommitRequestData.OffsetCommitRequestTopic)2 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 ClientResponse (org.apache.kafka.clients.ClientResponse)1 CommitFailedException (org.apache.kafka.clients.consumer.CommitFailedException)1 OffsetAndMetadata (org.apache.kafka.clients.consumer.OffsetAndMetadata)1 RetriableCommitFailedException (org.apache.kafka.clients.consumer.RetriableCommitFailedException)1 Node (org.apache.kafka.common.Node)1 TopicPartition (org.apache.kafka.common.TopicPartition)1 DisconnectException (org.apache.kafka.common.errors.DisconnectException)1 RebalanceInProgressException (org.apache.kafka.common.errors.RebalanceInProgressException)1 OffsetCommitRequest (org.apache.kafka.common.requests.OffsetCommitRequest)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 Test (org.junit.jupiter.api.Test)1