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);
}
}
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();
}
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();
}
}
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();
}
}
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();
}
}
Aggregations