Search in sources :

Example 16 with RetriableException

use of org.apache.kafka.common.errors.RetriableException in project kafka by apache.

the class Fetcher method validateOffsetsAsync.

/**
 * For each partition which needs validation, make an asynchronous request to get the end-offsets for the partition
 * with the epoch less than or equal to the epoch the partition last saw.
 *
 * Requests are grouped by Node for efficiency.
 */
private void validateOffsetsAsync(Map<TopicPartition, FetchPosition> partitionsToValidate) {
    final Map<Node, Map<TopicPartition, FetchPosition>> regrouped = regroupFetchPositionsByLeader(partitionsToValidate);
    long nextResetTimeMs = time.milliseconds() + requestTimeoutMs;
    regrouped.forEach((node, fetchPositions) -> {
        if (node.isEmpty()) {
            metadata.requestUpdate();
            return;
        }
        NodeApiVersions nodeApiVersions = apiVersions.get(node.idString());
        if (nodeApiVersions == null) {
            client.tryConnect(node);
            return;
        }
        if (!hasUsableOffsetForLeaderEpochVersion(nodeApiVersions)) {
            log.debug("Skipping validation of fetch offsets for partitions {} since the broker does not " + "support the required protocol version (introduced in Kafka 2.3)", fetchPositions.keySet());
            for (TopicPartition partition : fetchPositions.keySet()) {
                subscriptions.completeValidation(partition);
            }
            return;
        }
        subscriptions.setNextAllowedRetry(fetchPositions.keySet(), nextResetTimeMs);
        RequestFuture<OffsetForEpochResult> future = offsetsForLeaderEpochClient.sendAsyncRequest(node, fetchPositions);
        future.addListener(new RequestFutureListener<OffsetForEpochResult>() {

            @Override
            public void onSuccess(OffsetForEpochResult offsetsResult) {
                List<SubscriptionState.LogTruncation> truncations = new ArrayList<>();
                if (!offsetsResult.partitionsToRetry().isEmpty()) {
                    subscriptions.setNextAllowedRetry(offsetsResult.partitionsToRetry(), time.milliseconds() + retryBackoffMs);
                    metadata.requestUpdate();
                }
                // For each OffsetsForLeader response, check if the end-offset is lower than our current offset
                // for the partition. If so, it means we have experienced log truncation and need to reposition
                // that partition's offset.
                // 
                // In addition, check whether the returned offset and epoch are valid. If not, then we should reset
                // its offset if reset policy is configured, or throw out of range exception.
                offsetsResult.endOffsets().forEach((topicPartition, respEndOffset) -> {
                    FetchPosition requestPosition = fetchPositions.get(topicPartition);
                    Optional<SubscriptionState.LogTruncation> truncationOpt = subscriptions.maybeCompleteValidation(topicPartition, requestPosition, respEndOffset);
                    truncationOpt.ifPresent(truncations::add);
                });
                if (!truncations.isEmpty()) {
                    maybeSetOffsetForLeaderException(buildLogTruncationException(truncations));
                }
            }

            @Override
            public void onFailure(RuntimeException e) {
                subscriptions.requestFailed(fetchPositions.keySet(), time.milliseconds() + retryBackoffMs);
                metadata.requestUpdate();
                if (!(e instanceof RetriableException)) {
                    maybeSetOffsetForLeaderException(e);
                }
            }
        });
    });
}
Also used : ListOffsetsRequest(org.apache.kafka.common.requests.ListOffsetsRequest) SerializationException(org.apache.kafka.common.errors.SerializationException) PriorityQueue(java.util.PriorityQueue) LogTruncationException(org.apache.kafka.clients.consumer.LogTruncationException) KafkaException(org.apache.kafka.common.KafkaException) OffsetResetStrategy(org.apache.kafka.clients.consumer.OffsetResetStrategy) ControlRecordType(org.apache.kafka.common.record.ControlRecordType) ByteBuffer(java.nio.ByteBuffer) FetchResponseData(org.apache.kafka.common.message.FetchResponseData) Record(org.apache.kafka.common.record.Record) Cluster(org.apache.kafka.common.Cluster) BufferSupplier(org.apache.kafka.common.utils.BufferSupplier) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RecordBatch(org.apache.kafka.common.record.RecordBatch) ListOffsetsResponse(org.apache.kafka.common.requests.ListOffsetsResponse) LogContext(org.apache.kafka.common.utils.LogContext) Map(java.util.Map) MetricName(org.apache.kafka.common.MetricName) FetchResponse(org.apache.kafka.common.requests.FetchResponse) TimestampType(org.apache.kafka.common.record.TimestampType) Value(org.apache.kafka.common.metrics.stats.Value) MessageFormatter(org.slf4j.helpers.MessageFormatter) TopicPartition(org.apache.kafka.common.TopicPartition) Sensor(org.apache.kafka.common.metrics.Sensor) Time(org.apache.kafka.common.utils.Time) Collection(java.util.Collection) RecordDeserializationException(org.apache.kafka.common.errors.RecordDeserializationException) InvalidTopicException(org.apache.kafka.common.errors.InvalidTopicException) Set(java.util.Set) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) PartitionInfo(org.apache.kafka.common.PartitionInfo) OffsetAndTimestamp(org.apache.kafka.clients.consumer.OffsetAndTimestamp) Collectors(java.util.stream.Collectors) OffsetForEpochResult(org.apache.kafka.clients.consumer.internals.OffsetsForLeaderEpochClient.OffsetForEpochResult) CloseableIterator(org.apache.kafka.common.utils.CloseableIterator) List(java.util.List) Metrics(org.apache.kafka.common.metrics.Metrics) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) ListOffsetsTopicResponse(org.apache.kafka.common.message.ListOffsetsResponseData.ListOffsetsTopicResponse) RecordTooLargeException(org.apache.kafka.common.errors.RecordTooLargeException) Errors(org.apache.kafka.common.protocol.Errors) Optional(java.util.Optional) Node(org.apache.kafka.common.Node) FetchRequest(org.apache.kafka.common.requests.FetchRequest) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) Queue(java.util.Queue) FetchPosition(org.apache.kafka.clients.consumer.internals.SubscriptionState.FetchPosition) ClientResponse(org.apache.kafka.clients.ClientResponse) NodeApiVersions(org.apache.kafka.clients.NodeApiVersions) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Uuid(org.apache.kafka.common.Uuid) Max(org.apache.kafka.common.metrics.stats.Max) Headers(org.apache.kafka.common.header.Headers) Metadata(org.apache.kafka.clients.Metadata) StaleMetadataException(org.apache.kafka.clients.StaleMetadataException) FetchSessionHandler(org.apache.kafka.clients.FetchSessionHandler) HashMap(java.util.HashMap) RetriableException(org.apache.kafka.common.errors.RetriableException) ListOffsetsPartitionResponse(org.apache.kafka.common.message.ListOffsetsResponseData.ListOffsetsPartitionResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) MetadataRequest(org.apache.kafka.common.requests.MetadataRequest) ApiVersion(org.apache.kafka.common.message.ApiVersionsResponseData.ApiVersion) Deserializer(org.apache.kafka.common.serialization.Deserializer) OffsetOutOfRangeException(org.apache.kafka.clients.consumer.OffsetOutOfRangeException) Utils(org.apache.kafka.common.utils.Utils) TimeoutException(org.apache.kafka.common.errors.TimeoutException) Logger(org.slf4j.Logger) ListOffsetsPartition(org.apache.kafka.common.message.ListOffsetsRequestData.ListOffsetsPartition) Iterator(java.util.Iterator) ApiKeys(org.apache.kafka.common.protocol.ApiKeys) ApiVersions(org.apache.kafka.clients.ApiVersions) IsolationLevel(org.apache.kafka.common.IsolationLevel) CorruptRecordException(org.apache.kafka.common.errors.CorruptRecordException) WindowedCount(org.apache.kafka.common.metrics.stats.WindowedCount) Avg(org.apache.kafka.common.metrics.stats.Avg) Min(org.apache.kafka.common.metrics.stats.Min) Closeable(java.io.Closeable) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) Meter(org.apache.kafka.common.metrics.stats.Meter) Gauge(org.apache.kafka.common.metrics.Gauge) ArrayDeque(java.util.ArrayDeque) Comparator(java.util.Comparator) OffsetsForLeaderEpochRequest(org.apache.kafka.common.requests.OffsetsForLeaderEpochRequest) Collections(java.util.Collections) Timer(org.apache.kafka.common.utils.Timer) Optional(java.util.Optional) NodeApiVersions(org.apache.kafka.clients.NodeApiVersions) Node(org.apache.kafka.common.Node) FetchPosition(org.apache.kafka.clients.consumer.internals.SubscriptionState.FetchPosition) TopicPartition(org.apache.kafka.common.TopicPartition) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) OffsetForEpochResult(org.apache.kafka.clients.consumer.internals.OffsetsForLeaderEpochClient.OffsetForEpochResult) RetriableException(org.apache.kafka.common.errors.RetriableException)

Example 17 with RetriableException

use of org.apache.kafka.common.errors.RetriableException in project kafka by apache.

the class ConsumerCoordinator method doCommitOffsetsAsync.

private RequestFuture<Void> doCommitOffsetsAsync(final Map<TopicPartition, OffsetAndMetadata> offsets, final OffsetCommitCallback callback) {
    RequestFuture<Void> future = sendOffsetCommitRequest(offsets);
    final OffsetCommitCallback cb = callback == null ? defaultOffsetCommitCallback : callback;
    future.addListener(new RequestFutureListener<Void>() {

        @Override
        public void onSuccess(Void value) {
            if (interceptors != null)
                interceptors.onCommit(offsets);
            completedOffsetCommits.add(new OffsetCommitCompletion(cb, offsets, null));
        }

        @Override
        public void onFailure(RuntimeException e) {
            Exception commitException = e;
            if (e instanceof RetriableException) {
                commitException = new RetriableCommitFailedException(e);
            }
            completedOffsetCommits.add(new OffsetCommitCompletion(cb, offsets, commitException));
            if (commitException instanceof FencedInstanceIdException) {
                asyncCommitFenced.set(true);
            }
        }
    });
    return future;
}
Also used : RetriableCommitFailedException(org.apache.kafka.clients.consumer.RetriableCommitFailedException) OffsetCommitCallback(org.apache.kafka.clients.consumer.OffsetCommitCallback) FencedInstanceIdException(org.apache.kafka.common.errors.FencedInstanceIdException) GroupAuthorizationException(org.apache.kafka.common.errors.GroupAuthorizationException) UnstableOffsetCommitException(org.apache.kafka.common.errors.UnstableOffsetCommitException) KafkaException(org.apache.kafka.common.KafkaException) WakeupException(org.apache.kafka.common.errors.WakeupException) RebalanceInProgressException(org.apache.kafka.common.errors.RebalanceInProgressException) RetriableCommitFailedException(org.apache.kafka.clients.consumer.RetriableCommitFailedException) RetriableException(org.apache.kafka.common.errors.RetriableException) InterruptException(org.apache.kafka.common.errors.InterruptException) TimeoutException(org.apache.kafka.common.errors.TimeoutException) FencedInstanceIdException(org.apache.kafka.common.errors.FencedInstanceIdException) TopicAuthorizationException(org.apache.kafka.common.errors.TopicAuthorizationException) CommitFailedException(org.apache.kafka.clients.consumer.CommitFailedException) RetriableException(org.apache.kafka.common.errors.RetriableException)

Example 18 with RetriableException

use of org.apache.kafka.common.errors.RetriableException in project streamsx.kafka by IBMStreams.

the class AbstractKafkaConsumerClient method runPollLoop.

/**
 * Runs the loop polling for Kafka messages until an event is received in the event queue.
 * @param pollTimeout the timeout in milliseconds used to wait for new Kafka messages if there are less than the maximum batch size.
 * @param throttleSleepMillis the time in milliseconds the polling thread sleeps after each poll.
 *
 * @throws InterruptedException
 */
protected void runPollLoop(long pollTimeout, long throttleSleepMillis) throws InterruptedException {
    if (throttleSleepMillis > 0l) {
        logger.log(DEBUG_LEVEL, MsgFormatter.format("Initiating throttled polling (sleep time = {0} ms); maxPollRecords = {1}", throttleSleepMillis, getMaxPollRecords()));
    } else {
        logger.log(DEBUG_LEVEL, MsgFormatter.format("Initiating polling; maxPollRecords = {0}", getMaxPollRecords()));
    }
    synchronized (drainBuffer) {
        if (!drainBuffer.isEmpty()) {
            final int bufSz = drainBuffer.size();
            final int capacity = messageQueue.remainingCapacity();
            // restore records that have been put aside to the drain buffer
            if (capacity < bufSz) {
                String msg = MsgFormatter.format("drain buffer size {0} > capacity of message queue {1}", bufSz, capacity);
                logger.error("runPollLoop() - " + msg);
                // must restart operator.
                throw new RuntimeException(msg);
            }
            messageQueue.addAll(drainBuffer);
            final int qSize = messageQueue.size();
            drainBuffer.clear();
            logger.log(DEBUG_LEVEL, MsgFormatter.format("runPollLoop(): {0,number,#} consumer records added from drain buffer to the message queue. Message queue size is {1,number,#} now.", bufSz, qSize));
        }
    }
    // continue polling for messages until a new event
    // arrives in the event queue
    fetchPaused = consumer.paused().size() > 0;
    logger.log(DEBUG_LEVEL, "previously paused partitions: " + consumer.paused());
    while (eventQueue.isEmpty()) {
        boolean doPoll = true;
        // can wait for 100 ms; throws InterruptedException:
        try {
            checkSpaceInMessageQueueAndPauseFetching(false);
        } catch (IllegalStateException e) {
            logger.warn("runPollLoop(): " + e.getLocalizedMessage());
            // no space, could not pause - do not call poll
            doPoll = false;
        }
        if (doPoll) {
            try {
                final long now = System.currentTimeMillis();
                final long timeBetweenPolls = now - lastPollTimestamp;
                if (lastPollTimestamp > 0) {
                    // this is not the first 'poll'
                    if (timeBetweenPolls >= maxPollIntervalMs) {
                        logger.warn(// $NON-NLS-1$
                        "Kafka client did'nt poll often enaugh for messages. " + "Maximum time between two polls is currently " + // $NON-NLS-1$
                        maxPollIntervalMs + // $NON-NLS-1$
                        " milliseconds. Consider to set consumer property '" + ConsumerConfig.MAX_POLL_INTERVAL_MS_CONFIG + "' to a value higher than " + // $NON-NLS-1$
                        timeBetweenPolls);
                    }
                }
                lastPollTimestamp = System.currentTimeMillis();
                EnqueResult r = pollAndEnqueue(pollTimeout, throttleSleepMillis > 0l);
                final int nMessages = r.getNumRecords();
                if (nMessages > 0) {
                    pollExcFilter.reset();
                }
                final long nQueuedBytes = r.getSumTotalSize();
                final Level l = Level.DEBUG;
                // final Level l = DEBUG_LEVEL;
                if (logger.isEnabledFor(l) && nMessages > 0) {
                    logger.log(l, MsgFormatter.format("{0,number,#} records with total {1,number,#}/{2,number,#}/{3,number,#} bytes (key/value/sum) fetched and enqueued", nMessages, r.getSumKeySize(), r.getSumValueSize(), nQueuedBytes));
                }
                tryAdjustMinFreeMemory(nQueuedBytes, nMessages);
                nPendingMessages.setValue(messageQueue.size());
                if (throttleSleepMillis > 0l) {
                    synchronized (throttledPollWaitMonitor) {
                        throttledPollWaitMonitor.wait(throttleSleepMillis);
                    }
                }
            } catch (RetriableException e) {
                logger.warn("Retriable exception (ignored, may succeed if retried): " + e, e);
                logger.info("Going to sleep for 100 ms before next poll ...");
                Thread.sleep(100l);
            } catch (SerializationException e) {
                // https://issues.apache.org/jira/browse/KAFKA-4740)
                throw e;
            } catch (Exception e) {
                if (pollExcFilter.filter(e)) {
                    logger.warn(e);
                } else {
                    logger.error(e);
                    throw new KafkaOperatorRuntimeException("Consecutive number of exceptions too high.", e);
                }
            }
        }
    }
    // $NON-NLS-1$
    logger.debug("Stop polling. Message in event queue: " + eventQueue.peek().getEventType());
}
Also used : KafkaOperatorRuntimeException(com.ibm.streamsx.kafka.KafkaOperatorRuntimeException) SerializationException(org.apache.kafka.common.errors.SerializationException) Level(org.apache.log4j.Level) KafkaOperatorRuntimeException(com.ibm.streamsx.kafka.KafkaOperatorRuntimeException) Checkpoint(com.ibm.streams.operator.state.Checkpoint) SerializationException(org.apache.kafka.common.errors.SerializationException) ConsumerCommitFailedException(com.ibm.streamsx.kafka.ConsumerCommitFailedException) KafkaClientInitializationException(com.ibm.streamsx.kafka.KafkaClientInitializationException) KafkaOperatorRuntimeException(com.ibm.streamsx.kafka.KafkaOperatorRuntimeException) KafkaMetricException(com.ibm.streamsx.kafka.KafkaMetricException) RetriableException(org.apache.kafka.common.errors.RetriableException) UnknownTopicException(com.ibm.streamsx.kafka.UnknownTopicException) KafkaConfigurationException(com.ibm.streamsx.kafka.KafkaConfigurationException) CommitFailedException(org.apache.kafka.clients.consumer.CommitFailedException) RetriableException(org.apache.kafka.common.errors.RetriableException)

Aggregations

RetriableException (org.apache.kafka.common.errors.RetriableException)18 KafkaException (org.apache.kafka.common.KafkaException)9 TopicAuthorizationException (org.apache.kafka.common.errors.TopicAuthorizationException)8 HashMap (java.util.HashMap)7 Map (java.util.Map)7 LinkedHashMap (java.util.LinkedHashMap)6 TopicPartition (org.apache.kafka.common.TopicPartition)6 ArrayList (java.util.ArrayList)5 ClientResponse (org.apache.kafka.clients.ClientResponse)5 CommitFailedException (org.apache.kafka.clients.consumer.CommitFailedException)5 TimeoutException (org.apache.kafka.common.errors.TimeoutException)5 Errors (org.apache.kafka.common.protocol.Errors)5 List (java.util.List)4 OffsetCommitCallback (org.apache.kafka.clients.consumer.OffsetCommitCallback)4 RetriableCommitFailedException (org.apache.kafka.clients.consumer.RetriableCommitFailedException)4 Cluster (org.apache.kafka.common.Cluster)4 Node (org.apache.kafka.common.Node)4 GroupAuthorizationException (org.apache.kafka.common.errors.GroupAuthorizationException)4 InterruptException (org.apache.kafka.common.errors.InterruptException)4 InvalidTopicException (org.apache.kafka.common.errors.InvalidTopicException)4