org.apache.kafka.clients.admin.DescribeClusterResult Java Examples

The following examples show how to use org.apache.kafka.clients.admin.DescribeClusterResult. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KafkaOpenMetadataTopicConnector.java    From egeria with Apache License 2.0 6 votes vote down vote up
boolean getRunningBrokers(Properties connectionProperties ) {

            boolean found = false;
            try {
                AdminClient adminClient = KafkaAdminClient.create(connectionProperties);
                DescribeClusterResult describeClusterResult = adminClient.describeCluster();
                Collection<Node> brokers = describeClusterResult.nodes().get();
                if (!brokers.isEmpty()) {
                    found = true;
                }
            } catch (Exception e) {
                //gulp down any exceptions, the waiting method will control any audit logging
                //but keep a copy for reference
                lastException = e;
            }

            return found;
        }
 
Example #2
Source File: KafkaAdminFactoryTest.java    From kafka-webview with MIT License 6 votes vote down vote up
/**
 * Test that KafkaAdminFactory can create a working AdminClient when connecting to a non-ssl cluster.
 */
@Test
public void testCreateNonSslAdminClient() throws ExecutionException, InterruptedException {
    // Create Cluster config
    final ClusterConfig clusterConfig = ClusterConfig.newBuilder()
        .withBrokerHosts(sharedKafkaTestResource.getKafkaConnectString())
        .build();

    final KafkaAdminFactory kafkaAdminFactory = new KafkaAdminFactory(
        new KafkaClientConfigUtil("not/used", "MyPrefix")
    );

    // Create instance
    try (final AdminClient adminClient = kafkaAdminFactory.create(clusterConfig, "MyClientId")) {

        // Call method to validate things work as expected
        final DescribeClusterResult results = adminClient.describeCluster();
        assertNotNull("Should have a non-null result", results);

        // Request future result
        final Collection<Node> nodes = results.nodes().get();
        assertNotNull("Should have non-null node result", nodes);
        assertFalse("Should have non-empty node", nodes.isEmpty());
    }
}
 
Example #3
Source File: KafkaTopicClientImpl.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 5 votes vote down vote up
private static boolean isTopicDeleteEnabled(final AdminClient adminClient) {
  try {
    DescribeClusterResult describeClusterResult = adminClient.describeCluster();
    Collection<Node> nodes = describeClusterResult.nodes().get();
    if (nodes.isEmpty()) {
      log.warn("No available broker found to fetch config info.");
      throw new KsqlException("Could not fetch broker information. KSQL cannot initialize");
    }

    ConfigResource resource = new ConfigResource(
        ConfigResource.Type.BROKER,
        String.valueOf(nodes.iterator().next().id())
    );

    Map<ConfigResource, Config> config = executeWithRetries(
        () -> adminClient.describeConfigs(Collections.singleton(resource)).all());

    return config.get(resource)
        .entries()
        .stream()
        .anyMatch(configEntry -> configEntry.name().equalsIgnoreCase("delete.topic.enable")
                                 && configEntry.value().equalsIgnoreCase("true"));

  } catch (final Exception e) {
    log.error("Failed to initialize TopicClient: {}", e.getMessage());
    throw new KsqlException("Could not fetch broker information. KSQL cannot initialize", e);
  }
}
 
Example #4
Source File: DefaultKafkaClusterProxy.java    From kafka-message-tool with MIT License 5 votes vote down vote up
private void describeCluster() throws InterruptedException, ExecutionException, TimeoutException {
    final DescribeClusterResult describeClusterResult = kafkaClientsAdminClient.describeCluster();
    final KafkaFuture<String> stringKafkaFuture = describeClusterResult.clusterId();
    clusterSummary.setClusterId(stringKafkaFuture.get(ApplicationConstants.FUTURE_GET_TIMEOUT_MS, TimeUnit.MILLISECONDS));
    final int controllerNodeId = getControllerNodeId(describeClusterResult);
    final Collection<Node> nodes = describeClusterResult.nodes().get(ApplicationConstants.FUTURE_GET_TIMEOUT_MS, TimeUnit.MILLISECONDS);

    describeNodes(nodes, controllerNodeId);
}
 
Example #5
Source File: KafkaHealthIndicator.java    From kafka-graphs with Apache License 2.0 5 votes vote down vote up
@Override
public Mono<Health> health() {
    Health.Builder builder = new Health.Builder();
    Properties properties = new Properties();
    properties.put("bootstrap.servers", props.getBootstrapServers());
    try (AdminClient adminClient = AdminClient.create(properties)) {
        DescribeClusterResult result = adminClient.describeCluster(new DescribeClusterOptions().timeoutMs(3000));
        builder.withDetail("clusterId", result.clusterId().get());
        builder.up();
    } catch (Exception e) {
        builder.down();
    }
    return Mono.just(builder.build());
}
 
Example #6
Source File: KafkaTestUtils.java    From kafka-junit with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Describe nodes within Kafka cluster.
 * @return Collection of nodes within the Kafka cluster.
 */
public List<Node> describeClusterNodes() {
    // Create admin client
    try (final AdminClient adminClient = getAdminClient()) {
        final DescribeClusterResult describeClusterResult = adminClient.describeCluster();
        return new ArrayList<>(describeClusterResult.nodes().get());
    } catch (final InterruptedException | ExecutionException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
 
Example #7
Source File: KafkaAdmin.java    From feeyo-redisproxy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * 获取kafka集群配置信息
 */
public Collection<Node> getClusterNodes() {
	try {
		DescribeClusterOptions dco = new DescribeClusterOptions();
		dco.timeoutMs(5 * 1000);
		DescribeClusterResult dcr = adminClient.describeCluster(dco);
		return dcr.nodes().get();
	} catch (Exception e) {
		return null;
	}
}
 
Example #8
Source File: KafkaHealthIndicator.java    From micronaut-kafka with Apache License 2.0 4 votes vote down vote up
@Override
public Flowable<HealthResult> getResult() {
    DescribeClusterResult result = adminClient.describeCluster(
            new DescribeClusterOptions().timeoutMs(
                    (int) defaultConfiguration.getHealthTimeout().toMillis()
            )
    );

    Flowable<String> clusterId = Flowable.fromFuture(result.clusterId());
    Flowable<Collection<Node>> nodes = Flowable.fromFuture(result.nodes());
    Flowable<Node> controller = Flowable.fromFuture(result.controller());

    return controller.switchMap(node -> {
        String brokerId = node.idString();
        ConfigResource configResource = new ConfigResource(ConfigResource.Type.BROKER, brokerId);
        DescribeConfigsResult configResult = adminClient.describeConfigs(Collections.singletonList(configResource));
        Flowable<Map<ConfigResource, Config>> configs = Flowable.fromFuture(configResult.all());
        return configs.switchMap(resources -> {
            Config config = resources.get(configResource);
            ConfigEntry ce = config.get(REPLICATION_PROPERTY);
            int replicationFactor = Integer.parseInt(ce.value());
            return nodes.switchMap(nodeList -> clusterId.map(clusterIdString -> {
                int nodeCount = nodeList.size();
                HealthResult.Builder builder;
                if (nodeCount >= replicationFactor) {
                    builder = HealthResult.builder(ID, HealthStatus.UP);
                } else {
                    builder = HealthResult.builder(ID, HealthStatus.DOWN);
                }
                return builder
                        .details(CollectionUtils.mapOf(
                                "brokerId", brokerId,
                                "clusterId", clusterIdString,
                                "nodes", nodeCount
                        )).build();
            }));
        });
    }).onErrorReturn(throwable ->
            HealthResult.builder(ID, HealthStatus.DOWN)
                    .exception(throwable).build()
    );
}
 
Example #9
Source File: DefaultKafkaClusterProxy.java    From kafka-message-tool with MIT License 4 votes vote down vote up
private int getControllerNodeId(DescribeClusterResult describeClusterResult) throws InterruptedException,
                                                                                    ExecutionException,
                                                                                    TimeoutException {
    final KafkaFuture<Node> controller = describeClusterResult.controller();
    return controller.get(ApplicationConstants.FUTURE_GET_TIMEOUT_MS, TimeUnit.MILLISECONDS).id();
}
 
Example #10
Source File: MultiClusterTopicManagementServiceTest.java    From kafka-monitor with Apache License 2.0 4 votes vote down vote up
@Test
protected void MultiClusterTopicManagementServiceTopicCreationTest() throws Exception {

  Mockito.doCallRealMethod().when(_topicManagementHelper).maybeCreateTopic();

  Mockito.when(_topicManagementHelper._adminClient.describeCluster())
      .thenReturn(Mockito.mock(DescribeClusterResult.class));
  Mockito.when(_topicManagementHelper._adminClient.describeCluster().nodes())
      .thenReturn(Mockito.mock(KafkaFuture.class));
  Mockito.when(_topicManagementHelper._adminClient.describeCluster().nodes().get()).thenReturn(nodeSet);

  Mockito.when(_topicManagementHelper._adminClient.createTopics(Mockito.anyCollection()))
      .thenReturn(_createTopicsResult);
  Mockito.when(_topicManagementHelper._adminClient.createTopics(Mockito.anyCollection()).values())
      .thenReturn(_kafkaFutureMap);
  Mockito.when(
      _topicManagementHelper._adminClient.createTopics(Mockito.anyCollection()).values().get(SERVICE_TEST_TOPIC))
      .thenReturn(_kafkaFuture);

  Answer<Object> createKafkaTopicFutureAnswer = new Answer<Object>() {
    /**
     * @param invocation the invocation on the mocked TopicManagementHelper.
     * @return NULL value.
     * @throws Throwable the throwable to be thrown when Exception occurs.
     */
    @Override
    public Void answer(InvocationOnMock invocation) throws Throwable {

      Mockito.when(_topicManagementHelper._adminClient.describeTopics(Collections.singleton(SERVICE_TEST_TOPIC)))
          .thenReturn(Mockito.mock(DescribeTopicsResult.class));
      Mockito.when(
          _topicManagementHelper._adminClient.describeTopics(Collections.singleton(SERVICE_TEST_TOPIC)).values())
          .thenReturn(Mockito.mock(Map.class));
      Mockito.when(_topicManagementHelper._adminClient.describeTopics(Collections.singleton(SERVICE_TEST_TOPIC))
          .values()
          .get(SERVICE_TEST_TOPIC)).thenReturn(Mockito.mock(KafkaFuture.class));
      Mockito.when(_topicManagementHelper._adminClient.describeTopics(Collections.singleton(SERVICE_TEST_TOPIC))
          .values()
          .get(SERVICE_TEST_TOPIC)
          .get()).thenReturn(Mockito.mock(TopicDescription.class));
      Mockito.when(_topicManagementHelper._adminClient.describeTopics(Collections.singleton(SERVICE_TEST_TOPIC))
          .values()
          .get(SERVICE_TEST_TOPIC)
          .get()
          .name()).thenReturn(SERVICE_TEST_TOPIC);
      return null;
    }
  };

  Mockito.when(_topicManagementHelper._topicFactory.createTopicIfNotExist(Mockito.anyString(), Mockito.anyShort(),
      Mockito.anyDouble(), Mockito.any(), Mockito.any())).thenAnswer(createKafkaTopicFutureAnswer);

  _topicManagementHelper.maybeCreateTopic();

  Assert.assertNotNull(_topicManagementHelper._adminClient.describeTopics(Collections.singleton(SERVICE_TEST_TOPIC))
      .values()
      .get(SERVICE_TEST_TOPIC)
      .get());
  Assert.assertEquals(_topicManagementHelper._adminClient.describeTopics(Collections.singleton(SERVICE_TEST_TOPIC))
      .values()
      .get(SERVICE_TEST_TOPIC)
      .get()
      .name(), SERVICE_TEST_TOPIC);
}