|
28 | 28 | import java.util.concurrent.TimeoutException; |
29 | 29 | import org.apache.kafka.clients.ClientUtils; |
30 | 30 | import org.apache.kafka.clients.CommonClientConfigs; |
| 31 | +import org.apache.kafka.clients.admin.Admin; |
31 | 32 | import org.apache.kafka.clients.admin.AdminClient; |
32 | 33 | import org.apache.kafka.clients.admin.AlterConfigOp; |
33 | 34 | import org.apache.kafka.clients.admin.AlterConfigsResult; |
@@ -375,7 +376,6 @@ protected void maybeIncreaseTopicPartitionCount() { |
375 | 376 | String cruiseControlMetricsTopic = _metricsTopic.name(); |
376 | 377 |
|
377 | 378 | try { |
378 | | - // For compatibility with Kafka 4.0 and beyond we must use new API methods. |
379 | 379 | TopicDescription topicDescription = getTopicDescription(_adminClient, cruiseControlMetricsTopic); |
380 | 380 |
|
381 | 381 | if (topicDescription.partitions().size() < _metricsTopic.numPartitions()) { |
@@ -514,68 +514,22 @@ private void setIfAbsent(Properties props, String key, String value) { |
514 | 514 | } |
515 | 515 |
|
516 | 516 | /** |
517 | | - * Attempts to retrieve the method for mapping topic names to futures from the {@link org.apache.kafka.clients.admin.DescribeTopicsResult} class. |
518 | | - * This method first tries to get the {@code topicNameValues()} method, which is available in Kafka 3.1.0 and later. |
519 | | - * If the method is not found, it falls back to trying to retrieve the {@code values()} method, which is available in Kafka 3.9.0 and earlier. |
520 | | - * |
521 | | - * If neither of these methods is found, a {@link RuntimeException} is thrown. |
522 | | - * |
523 | | - * <p>This method is useful for ensuring compatibility with both older and newer versions of Kafka clients.</p> |
524 | | - * |
525 | | - * @return the {@link Method} object representing the {@code topicNameValues()} or {@code values()} method. |
526 | | - * @throws RuntimeException if neither the {@code values()} nor {@code topicNameValues()} methods are found. |
527 | | - */ |
528 | | - /* test */ static Method topicNameValuesMethod() { |
529 | | - // |
530 | | - Method topicDescriptionMethod = null; |
531 | | - try { |
532 | | - // First we try to get the topicNameValues() method |
533 | | - topicDescriptionMethod = DescribeTopicsResult.class.getMethod("topicNameValues"); |
534 | | - } catch (NoSuchMethodException exception) { |
535 | | - LOG.info("Failed to get method topicNameValues() from DescribeTopicsResult class since we are probably on kafka 3.0.0 or older: ", exception); |
536 | | - } |
537 | | - |
538 | | - if (topicDescriptionMethod == null) { |
539 | | - try { |
540 | | - // Second we try to get the values() method |
541 | | - topicDescriptionMethod = DescribeTopicsResult.class.getMethod("values"); |
542 | | - } catch (NoSuchMethodException exception) { |
543 | | - LOG.info("Failed to get method values() from DescribeTopicsResult class: ", exception); |
544 | | - } |
545 | | - } |
546 | | - |
547 | | - if (topicDescriptionMethod != null) { |
548 | | - return topicDescriptionMethod; |
549 | | - } else { |
550 | | - throw new RuntimeException("Unable to find both values() and topicNameValues() method in the DescribeTopicsResult class "); |
551 | | - } |
552 | | - } |
553 | | - |
554 | | - /** |
555 | | - * Retrieves the {@link TopicDescription} for the specified Kafka topic, handling compatibility |
556 | | - * with Kafka versions 4.0 and above. This method uses reflection to invoke the appropriate method |
557 | | - * for retrieving topic description information, depending on the Kafka version. |
| 517 | + * Retrieves the {@link TopicDescription} for the specified Kafka topic. |
558 | 518 | * |
559 | 519 | * @param adminClient The Kafka {@link AdminClient} used to interact with the Kafka cluster. |
560 | 520 | * @param ccMetricsTopic The name of the Kafka topic for which the description is to be retrieved. |
561 | 521 | * |
562 | 522 | * @return The {@link TopicDescription} for the specified Kafka topic. |
563 | 523 | * |
564 | | - * @throws KafkaTopicDescriptionException If an error occurs while retrieving the topic description, |
565 | | - * or if the topic name retrieval method cannot be found or invoked properly. This includes |
566 | | - * exceptions related to reflection (e.g., {@link NoSuchMethodException}), invocation issues, |
567 | | - * execution exceptions, timeouts, and interruptions. |
| 524 | + * @throws KafkaTopicDescriptionException If an error occurs while retrieving the topic description. |
568 | 525 | */ |
569 | | - /* test */ static TopicDescription getTopicDescription(AdminClient adminClient, String ccMetricsTopic) throws KafkaTopicDescriptionException { |
| 526 | + /* test */ static TopicDescription getTopicDescription(Admin adminClient, String ccMetricsTopic) throws KafkaTopicDescriptionException { |
570 | 527 | try { |
571 | | - // For compatibility with Kafka 4.0 and beyond we must use new API methods. |
572 | | - Method topicDescriptionMethod = topicNameValuesMethod(); |
573 | | - DescribeTopicsResult describeTopicsResult = adminClient.describeTopics(Collections.singletonList(ccMetricsTopic)); |
574 | | - Map<String, KafkaFuture<TopicDescription>> topicDescriptionMap = (Map<String, KafkaFuture<TopicDescription>>) topicDescriptionMethod |
575 | | - .invoke(describeTopicsResult); |
576 | | - return topicDescriptionMap.get(ccMetricsTopic).get(CLIENT_REQUEST_TIMEOUT_MS, TimeUnit.MILLISECONDS); |
577 | | - } catch (InvocationTargetException | IllegalAccessException | ExecutionException | InterruptedException | TimeoutException e) { |
578 | | - throw new KafkaTopicDescriptionException(String.format("Unable to retrieve config of Cruise Cruise Control metrics topic {}.", |
| 528 | + DescribeTopicsResult describeTopicsResult = adminClient.describeTopics(Collections.singletonList(ccMetricsTopic)); |
| 529 | + Map<String, KafkaFuture<TopicDescription>> topicDescriptionMap = describeTopicsResult.topicNameValues(); |
| 530 | + return topicDescriptionMap.get(ccMetricsTopic).get(CLIENT_REQUEST_TIMEOUT_MS, TimeUnit.MILLISECONDS); |
| 531 | + } catch (ExecutionException | InterruptedException | TimeoutException e) { |
| 532 | + throw new KafkaTopicDescriptionException(String.format("Unable to retrieve config of Cruise Cruise Control metrics topic %s.", |
579 | 533 | ccMetricsTopic), e); |
580 | 534 | } |
581 | 535 | } |
|
0 commit comments