Search in sources :

Example 1 with ReassignmentInProgressException

use of org.apache.kafka.common.errors.ReassignmentInProgressException in project cruise-control by linkedin.

the class KafkaCruiseControlUtils method maybeIncreasePartitionCount.

/**
 * Increase the partition count of the given existing topic to the desired partition count (if needed).
 *
 * @param adminClient The adminClient to send describeTopics and createPartitions requests.
 * @param topicToAddPartitions Existing topic to add more partitions if needed -- cannot be {@code null}.
 * @return {@link CompletionType#COMPLETED} if the request is completed successfully, {@link CompletionType#COMPLETED_WITH_ERROR} if there
 * are any exceptions, and {@link CompletionType#NO_ACTION} if the partition count is already greater than or equal to the requested count.
 */
public static CompletionType maybeIncreasePartitionCount(AdminClient adminClient, NewTopic topicToAddPartitions) {
    String topicName = topicToAddPartitions.name();
    // Retrieve partition count of topic to check if it needs a partition count update.
    TopicDescription topicDescription;
    try {
        topicDescription = adminClient.describeTopics(Collections.singletonList(topicName)).values().get(topicName).get(CLIENT_REQUEST_TIMEOUT_MS, TimeUnit.MILLISECONDS);
    } catch (InterruptedException | ExecutionException | TimeoutException e) {
        LOG.warn("Partition count increase check for topic {} failed due to failure to describe cluster.", topicName, e);
        return CompletionType.COMPLETED_WITH_ERROR;
    }
    // Update partition count of topic if needed.
    if (topicDescription.partitions().size() < topicToAddPartitions.numPartitions()) {
        CreatePartitionsResult createPartitionsResult = adminClient.createPartitions(Collections.singletonMap(topicName, NewPartitions.increaseTo(topicToAddPartitions.numPartitions())));
        try {
            createPartitionsResult.values().get(topicName).get(CLIENT_REQUEST_TIMEOUT_MS, TimeUnit.MILLISECONDS);
        } catch (InterruptedException | ExecutionException | TimeoutException e) {
            LOG.warn("Partition count increase to {} for topic {} failed{}.", topicToAddPartitions.numPartitions(), topicName, (e.getCause() instanceof ReassignmentInProgressException) ? " due to ongoing reassignment" : "", e);
            return CompletionType.COMPLETED_WITH_ERROR;
        }
    } else {
        return CompletionType.NO_ACTION;
    }
    return CompletionType.COMPLETED;
}
Also used : ReassignmentInProgressException(org.apache.kafka.common.errors.ReassignmentInProgressException) CreatePartitionsResult(org.apache.kafka.clients.admin.CreatePartitionsResult) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 CreatePartitionsResult (org.apache.kafka.clients.admin.CreatePartitionsResult)1 TopicDescription (org.apache.kafka.clients.admin.TopicDescription)1 ReassignmentInProgressException (org.apache.kafka.common.errors.ReassignmentInProgressException)1