Search in sources :

Example 1 with Timer

use of org.apache.kafka.common.utils.Timer in project kafka by apache.

the class AbstractCoordinatorTest method testTimeoutAndRetryJoinGroupIfNeeded.

@Test
public void testTimeoutAndRetryJoinGroupIfNeeded() throws Exception {
    setupCoordinator();
    mockClient.prepareResponse(groupCoordinatorResponse(node, Errors.NONE));
    coordinator.ensureCoordinatorReady(mockTime.timer(0));
    ExecutorService executor = Executors.newFixedThreadPool(1);
    try {
        Timer firstAttemptTimer = mockTime.timer(REQUEST_TIMEOUT_MS);
        Future<Boolean> firstAttempt = executor.submit(() -> coordinator.joinGroupIfNeeded(firstAttemptTimer));
        mockTime.sleep(REQUEST_TIMEOUT_MS);
        assertFalse(firstAttempt.get());
        assertTrue(consumerClient.hasPendingRequests(coordinatorNode));
        mockClient.respond(joinGroupFollowerResponse(1, memberId, leaderId, Errors.NONE));
        mockClient.prepareResponse(syncGroupResponse(Errors.NONE));
        Timer secondAttemptTimer = mockTime.timer(REQUEST_TIMEOUT_MS);
        Future<Boolean> secondAttempt = executor.submit(() -> coordinator.joinGroupIfNeeded(secondAttemptTimer));
        assertTrue(secondAttempt.get());
    } finally {
        executor.shutdownNow();
        executor.awaitTermination(1000, TimeUnit.MILLISECONDS);
    }
}
Also used : Timer(org.apache.kafka.common.utils.Timer) ExecutorService(java.util.concurrent.ExecutorService) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.jupiter.api.Test)

Example 2 with Timer

use of org.apache.kafka.common.utils.Timer in project kafka by apache.

the class KafkaConsumer method pollForFetches.

/**
 * @throws KafkaException if the rebalance callback throws exception
 */
private Fetch<K, V> pollForFetches(Timer timer) {
    long pollTimeout = coordinator == null ? timer.remainingMs() : Math.min(coordinator.timeToNextPoll(timer.currentTimeMs()), timer.remainingMs());
    // if data is available already, return it immediately
    final Fetch<K, V> fetch = fetcher.collectFetch();
    if (!fetch.isEmpty()) {
        return fetch;
    }
    // send any new fetches (won't resend pending fetches)
    fetcher.sendFetches();
    // updateAssignmentMetadataIfNeeded before this method.
    if (!cachedSubscriptionHasAllFetchPositions && pollTimeout > retryBackoffMs) {
        pollTimeout = retryBackoffMs;
    }
    log.trace("Polling for fetches with timeout {}", pollTimeout);
    Timer pollTimer = time.timer(pollTimeout);
    client.poll(pollTimer, () -> {
        // to ensure that we do not block unnecessarily in poll()
        return !fetcher.hasAvailableFetches();
    });
    timer.update(pollTimer.currentTimeMs());
    return fetcher.collectFetch();
}
Also used : Timer(org.apache.kafka.common.utils.Timer)

Example 3 with Timer

use of org.apache.kafka.common.utils.Timer in project kafka by apache.

the class KafkaConsumer method position.

/**
 * Get the offset of the <i>next record</i> that will be fetched (if a record with that offset exists).
 * This method may issue a remote call to the server if there is no current position
 * for the given partition.
 * <p>
 * This call will block until the position can be determined, an unrecoverable error is
 * encountered (in which case it is thrown to the caller), or the timeout expires.
 *
 * @param partition The partition to get the position for
 * @param timeout The maximum amount of time to await determination of the current position
 * @return The current position of the consumer (that is, the offset of the next record to be fetched)
 * @throws IllegalStateException if the provided TopicPartition is not assigned to this consumer
 * @throws org.apache.kafka.clients.consumer.InvalidOffsetException if no offset is currently defined for
 *             the partition
 * @throws org.apache.kafka.common.errors.WakeupException if {@link #wakeup()} is called before or while this
 *             function is called
 * @throws org.apache.kafka.common.errors.InterruptException if the calling thread is interrupted before or while
 *             this function is called
 * @throws org.apache.kafka.common.errors.TimeoutException if the position cannot be determined before the
 *             passed timeout expires
 * @throws org.apache.kafka.common.errors.AuthenticationException if authentication fails. See the exception for more details
 * @throws org.apache.kafka.common.errors.AuthorizationException if not authorized to the topic or to the
 *             configured groupId. See the exception for more details
 * @throws org.apache.kafka.common.KafkaException for any other unrecoverable errors
 */
@Override
public long position(TopicPartition partition, final Duration timeout) {
    acquireAndEnsureOpen();
    try {
        if (!this.subscriptions.isAssigned(partition))
            throw new IllegalStateException("You can only check the position for partitions assigned to this consumer.");
        Timer timer = time.timer(timeout);
        do {
            SubscriptionState.FetchPosition position = this.subscriptions.validPosition(partition);
            if (position != null)
                return position.offset;
            updateFetchPositions(timer);
            client.poll(timer);
        } while (timer.notExpired());
        throw new TimeoutException("Timeout of " + timeout.toMillis() + "ms expired before the position " + "for partition " + partition + " could be determined");
    } finally {
        release();
    }
}
Also used : Timer(org.apache.kafka.common.utils.Timer) SubscriptionState(org.apache.kafka.clients.consumer.internals.SubscriptionState) TimeoutException(org.apache.kafka.common.errors.TimeoutException)

Example 4 with Timer

use of org.apache.kafka.common.utils.Timer in project kafka by apache.

the class ConsumerNetworkClient method transmitSends.

/**
 * Poll for network IO in best-effort only trying to transmit the ready-to-send request
 * Do not check any pending requests or metadata errors so that no exception should ever
 * be thrown, also no wakeups be triggered and no interrupted exception either.
 */
public void transmitSends() {
    Timer timer = time.timer(0);
    // do not try to handle any disconnects, prev request failures, metadata exception etc;
    // just try once and return immediately
    lock.lock();
    try {
        // send all the requests we can send now
        trySend(timer.currentTimeMs());
        client.poll(0, timer.currentTimeMs());
    } finally {
        lock.unlock();
    }
}
Also used : Timer(org.apache.kafka.common.utils.Timer)

Example 5 with Timer

use of org.apache.kafka.common.utils.Timer in project kafka by apache.

the class KafkaConsumer method partitionsFor.

/**
 * Get metadata about the partitions for a given topic. This method will issue a remote call to the server if it
 * does not already have any metadata about the given topic.
 *
 * @param topic The topic to get partition metadata for
 * @param timeout The maximum of time to await topic metadata
 *
 * @return The list of partitions, which will be empty when the given topic is not found
 * @throws org.apache.kafka.common.errors.WakeupException if {@link #wakeup()} is called before or while this
 *             function is called
 * @throws org.apache.kafka.common.errors.InterruptException if the calling thread is interrupted before or while
 *             this function is called
 * @throws org.apache.kafka.common.errors.AuthenticationException if authentication fails. See the exception for more details
 * @throws org.apache.kafka.common.errors.AuthorizationException if not authorized to the specified topic. See
 *             the exception for more details
 * @throws org.apache.kafka.common.errors.TimeoutException if topic metadata cannot be fetched before expiration
 *             of the passed timeout
 * @throws org.apache.kafka.common.KafkaException for any other unrecoverable errors
 */
@Override
public List<PartitionInfo> partitionsFor(String topic, Duration timeout) {
    acquireAndEnsureOpen();
    try {
        Cluster cluster = this.metadata.fetch();
        List<PartitionInfo> parts = cluster.partitionsForTopic(topic);
        if (!parts.isEmpty())
            return parts;
        Timer timer = time.timer(timeout);
        Map<String, List<PartitionInfo>> topicMetadata = fetcher.getTopicMetadata(new MetadataRequest.Builder(Collections.singletonList(topic), metadata.allowAutoTopicCreation()), timer);
        return topicMetadata.getOrDefault(topic, Collections.emptyList());
    } finally {
        release();
    }
}
Also used : MetadataRequest(org.apache.kafka.common.requests.MetadataRequest) Timer(org.apache.kafka.common.utils.Timer) Cluster(org.apache.kafka.common.Cluster) List(java.util.List) PartitionInfo(org.apache.kafka.common.PartitionInfo)

Aggregations

Timer (org.apache.kafka.common.utils.Timer)5 List (java.util.List)1 ExecutorService (java.util.concurrent.ExecutorService)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 SubscriptionState (org.apache.kafka.clients.consumer.internals.SubscriptionState)1 Cluster (org.apache.kafka.common.Cluster)1 PartitionInfo (org.apache.kafka.common.PartitionInfo)1 TimeoutException (org.apache.kafka.common.errors.TimeoutException)1 MetadataRequest (org.apache.kafka.common.requests.MetadataRequest)1 Test (org.junit.jupiter.api.Test)1