org.apache.kafka.common.config.ConfigResource Java Examples

The following examples show how to use org.apache.kafka.common.config.ConfigResource. 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: RetentionByTopicFunctionTest.java    From data-highway with Apache License 2.0 8 votes vote down vote up
@Test
public void typical() throws Exception {
  String topic = "topic";
  Collection<String> topics = singleton(topic);
  ConfigResource configResource = new ConfigResource(TOPIC, topic);
  Config config = new Config(singleton(new ConfigEntry("retention.ms", "1")));
  KafkaFuture<Map<ConfigResource, Config>> kafkaFuture = completedFuture(singletonMap(configResource, config));

  doReturn(describeConfigsResult).when(adminClient).describeConfigs(any());
  doReturn(kafkaFuture).when(describeConfigsResult).all();

  Map<String, Duration> result = underTest.apply(topics);

  assertThat(result.size(), is(1));
  Duration retention = result.get(topic);
  assertThat(retention, is(Duration.ofMillis(1)));
}
 
Example #2
Source File: AbstractSharedKafkaTestResourceTest.java    From kafka-junit with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Simple smoke test to ensure broker running appropriate listeners.
 */
@Test
void validateListener() throws ExecutionException, InterruptedException {
    try (final AdminClient adminClient  = getKafkaTestUtils().getAdminClient()) {
        final ConfigResource broker1Resource = new ConfigResource(ConfigResource.Type.BROKER, "1");

        // Pull broker configs
        final Config configResult = adminClient
            .describeConfigs(Collections.singletonList(broker1Resource))
            .values()
            .get(broker1Resource)
            .get();

        // Check listener
        final String actualListener = configResult.get("listeners").value();
        Assertions.assertTrue(
            actualListener.contains(getExpectedListenerProtocol() + "://"),
            "Expected " + getExpectedListenerProtocol() + ":// and found: " + actualListener);

        // Check inter broker protocol
        final String actualBrokerProtocol = configResult.get("security.inter.broker.protocol").value();
        Assertions.assertEquals(getExpectedListenerProtocol(), actualBrokerProtocol, "Unexpected inter-broker protocol");
    }
}
 
Example #3
Source File: KafkaImpl.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
/**
 * Completes the returned Future on the Vertx event loop
 * with the topic config obtained from the Kafka AdminClient API.
 * The Future completes with a null result a topic with the given {@code topicName} does not exist.
 */
@Override
public Future<TopicMetadata> topicMetadata(TopicName topicName) {
    LOGGER.debug("Getting metadata for topic {}", topicName);
    ConfigResource resource = new ConfigResource(ConfigResource.Type.TOPIC, topicName.toString());
    Future<TopicDescription> topicDescriptionFuture = mapFuture(adminClient.describeTopics(
            singleton(topicName.toString())).values().get(topicName.toString()));
    Future<Config> configFuture = mapFuture(adminClient.describeConfigs(
            singleton(resource)).values().get(resource));
    return CompositeFuture.all(topicDescriptionFuture, configFuture)
    .map(compositeFuture ->
        new TopicMetadata(compositeFuture.resultAt(0), compositeFuture.resultAt(1)))
        .recover(error -> {
            if (error instanceof UnknownTopicOrPartitionException) {
                return Future.succeededFuture(null);
            } else {
                return Future.failedFuture(error);
            }
        });
}
 
Example #4
Source File: KafkaAvailability.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
private Future<Map<String, Config>> topicConfigs(Collection<String> topicNames) {
    log.debug("Getting topic configs for {} topics", topicNames.size());
    List<ConfigResource> configs = topicNames.stream()
            .map((String topicName) -> new ConfigResource(ConfigResource.Type.TOPIC, topicName))
            .collect(Collectors.toList());
    Promise<Map<String, Config>> promise = Promise.promise();
    ac.describeConfigs(configs).all().whenComplete((topicNameToConfig, error) -> {
        if (error != null) {
            promise.fail(error);
        } else {
            log.debug("Got topic configs for {} topics", topicNames.size());
            promise.complete(topicNameToConfig.entrySet().stream()
                    .collect(Collectors.toMap(
                        entry -> entry.getKey().name(),
                        entry -> entry.getValue())));
        }
    });
    return promise.future();
}
 
Example #5
Source File: AdminClientTest.java    From javabase with Apache License 2.0 6 votes vote down vote up
/**
 * describe topic‘s config
 * 
 * @param client
 */
public static void describeConfig(AdminClient client)
        throws ExecutionException, InterruptedException {
    DescribeConfigsResult ret = client.describeConfigs(
            Collections.singleton(new ConfigResource(ConfigResource.Type.TOPIC, TEST_TOPIC)));
    Map<ConfigResource, Config> configs = ret.all().get();
    for (Map.Entry<ConfigResource, Config> entry : configs.entrySet()) {
        ConfigResource key = entry.getKey();
        Config value = entry.getValue();
        System.out.println(
                String.format("Resource type: %s, resource name: %s", key.type(), key.name()));
        Collection<ConfigEntry> configEntries = value.entries();
        for (ConfigEntry each : configEntries) {
            System.out.println(each.name() + " = " + each.value());
        }
    }

}
 
Example #6
Source File: KafkaAdminClient.java    From common-kafka with Apache License 2.0 6 votes vote down vote up
/**
 * Updates the given topic's config with the {@link Properties} provided. This is not additive but a full
 * replacement
 *
 * @param topic
 *      the topic to update config for
 * @param properties
 *      the properties to assign to the topic
 * @throws IllegalArgumentException
 *      if topic is null, empty or blank, or properties is {@code null}
 * @throws AdminOperationException
 *      if there is an issue updating the topic config
 */
public void updateTopicConfig(String topic, Properties properties) {
    if (StringUtils.isBlank(topic))
        throw new IllegalArgumentException("topic cannot be null, empty or blank");
    if (properties == null)
        throw new IllegalArgumentException("properties cannot be null");

    LOG.debug("Updating topic config for topic [{}] with config [{}]", topic, properties);

    try {
        List<ConfigEntry> configEntries = new ArrayList<>();
        for (String property : properties.stringPropertyNames()) {
            configEntries.add(new ConfigEntry(property, properties.getProperty(property)));
        }

        getNewAdminClient()
            .alterConfigs(
                Collections.singletonMap(
                    new ConfigResource(ConfigResource.Type.TOPIC, topic),
                    new Config(configEntries)))
            .all()
            .get(operationTimeout, TimeUnit.MILLISECONDS);
    } catch (InterruptedException | ExecutionException | TimeoutException e) {
        throw new AdminOperationException("Unable to update configuration for topic: " + topic, e);
    }
}
 
Example #7
Source File: TopicReplicationFactorAnomalyFinder.java    From cruise-control with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Retrieve topic minISR config information if it is not cached locally.
 * @param topicsToCheck Set of topics to check.
 */
private void maybeRetrieveAndCacheTopicMinISR(Set<String> topicsToCheck) {
  Set<ConfigResource> topicResourcesToCheck = new HashSet<>(topicsToCheck.size());
  topicsToCheck.stream().filter(t -> !_cachedTopicMinISR.containsKey(t))
                        .forEach(t -> topicResourcesToCheck.add(new ConfigResource(ConfigResource.Type.TOPIC, t)));
  if (topicResourcesToCheck.isEmpty()) {
    return;
  }
  for (Map.Entry<ConfigResource, KafkaFuture<Config>> entry : _adminClient.describeConfigs(topicResourcesToCheck).values().entrySet()) {
    try {
      short topicMinISR = Short.parseShort(entry.getValue().get(DESCRIBE_TOPIC_CONFIG_TIMEOUT_MS, TimeUnit.MILLISECONDS)
                                                .get(TopicConfig.MIN_IN_SYNC_REPLICAS_CONFIG).value());
      _cachedTopicMinISR.put(entry.getKey().name(), new TopicMinISREntry(topicMinISR, System.currentTimeMillis()));
    } catch (TimeoutException | InterruptedException | ExecutionException e) {
      LOG.warn("Skip attempt to fix replication factor of topic {} due to unable to retrieve its minISR config.",
               entry.getKey().name());
    }
  }
}
 
Example #8
Source File: KafkaAdminConfigOperation.java    From kafka_book_demo with Apache License 2.0 6 votes vote down vote up
public static void alterTopicConfig() throws ExecutionException, InterruptedException {
    String brokerList =  "localhost:9092";
    String topic = "topic-admin";

    Properties props = new Properties();
    props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, brokerList);
    props.put(AdminClientConfig.REQUEST_TIMEOUT_MS_CONFIG, 30000);
    AdminClient client = AdminClient.create(props);

    ConfigResource resource =
            new ConfigResource(ConfigResource.Type.TOPIC, topic);
    ConfigEntry entry = new ConfigEntry("cleanup.policy", "compact");
    Config config = new Config(Collections.singleton(entry));
    Map<ConfigResource, Config> configs = new HashMap<>();
    configs.put(resource, config);
    AlterConfigsResult result = client.alterConfigs(configs);
    result.all().get();

    client.close();
}
 
Example #9
Source File: KafkaAdminConfigOperation.java    From kafka_book_demo with Apache License 2.0 6 votes vote down vote up
public static void describeTopicConfig() throws ExecutionException,
        InterruptedException {
    String brokerList =  "localhost:9092";
    String topic = "topic-admin";

    Properties props = new Properties();
    props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, brokerList);
    props.put(AdminClientConfig.REQUEST_TIMEOUT_MS_CONFIG, 30000);
    AdminClient client = AdminClient.create(props);

    ConfigResource resource =
            new ConfigResource(ConfigResource.Type.TOPIC, topic);
    DescribeConfigsResult result =
            client.describeConfigs(Collections.singleton(resource));
    Config config = result.all().get().get(resource);
    System.out.println(config);
    client.close();
}
 
Example #10
Source File: TopicServiceImplTest.java    From kafka-helmsman with MIT License 6 votes vote down vote up
@Test
public void testAlterConfiguration() {
  TopicService service = new TopicServiceImpl(adminClient, true);
  AlterConfigsResult result = mock(AlterConfigsResult.class);
  when(result.all()).thenReturn(KafkaFuture.completedFuture(null));
  when(adminClient.alterConfigs(any(Map.class), any(AlterConfigsOptions.class))).thenReturn(result);

  service.alterConfiguration(Collections.singletonList(
      new ConfiguredTopic("test", 3, (short) 2, Collections.singletonMap("k", "v"))));

  ArgumentCaptor<Map> alter = ArgumentCaptor.forClass(Map.class);
  ArgumentCaptor<AlterConfigsOptions> options = ArgumentCaptor.forClass(AlterConfigsOptions.class);
  verify(adminClient).alterConfigs((Map<ConfigResource, Config>) alter.capture(), options.capture());
  Assert.assertEquals(1, alter.getValue().size());
  ConfigResource expectedKey = new ConfigResource(TOPIC, "test");
  Assert.assertTrue(alter.getValue().containsKey(expectedKey));
  Assert.assertEquals("v", ((Config) alter.getValue().get(expectedKey)).get("k").value());
  Assert.assertTrue(options.getValue().shouldValidateOnly());
}
 
Example #11
Source File: TopicOperatorBaseIT.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
protected String alterTopicConfigInKafka(String topicName, String key, Function<String, String> mutator) throws InterruptedException, ExecutionException {
    // Get the topic config
    ConfigResource configResource = topicConfigResource(topicName);
    org.apache.kafka.clients.admin.Config config = getTopicConfig(configResource);

    Map<String, ConfigEntry> m = new HashMap<>();
    for (ConfigEntry entry: config.entries()) {
        if (entry.name().equals(key)
            || entry.source() != ConfigEntry.ConfigSource.DEFAULT_CONFIG
                && entry.source() != ConfigEntry.ConfigSource.STATIC_BROKER_CONFIG) {
            m.put(entry.name(), entry);
        }
    }
    final String changedValue = mutator.apply(m.get(key).value());
    m.put(key, new ConfigEntry(key, changedValue));
    LOGGER.info("Changing topic config {} to {}", key, changedValue);

    // Update the topic config
    AlterConfigsResult cgf = adminClient.alterConfigs(singletonMap(configResource,
            new org.apache.kafka.clients.admin.Config(m.values())));
    cgf.all().get();
    return changedValue;
}
 
Example #12
Source File: TopicServiceImpl.java    From kafka-helmsman with MIT License 6 votes vote down vote up
@Override
public Map<String, ConfiguredTopic> listExisting(boolean excludeInternal) {
  try {
    Set<String> topics = adminClient
        .listTopics(excludeInternal ? EXCLUDE_INTERNAL : INCLUDE_INTERNAL)
        .names().get();
    Collection<TopicDescription> topicDescriptions = adminClient.describeTopics(topics).all().get().values();

    List<ConfigResource> resources = topics
        .stream()
        .map(t -> new ConfigResource(Type.TOPIC, t))
        .collect(toList());

    Map<ConfigResource, KafkaFuture<Config>> topicConfigs = adminClient.describeConfigs(resources).values();

    return topicDescriptions
        .stream()
        .map(td -> configuredTopic(td, topicConfigs.get(new ConfigResource(Type.TOPIC, td.name()))))
        .filter(t -> !INTERNAL_TOPIC.test(t))
        .collect(toMap(ConfiguredTopic::getName, td -> td));

  } catch (InterruptedException | ExecutionException e) {
    // TODO: FA-10109: Improve exception handling
    throw new RuntimeException(e);
  }
}
 
Example #13
Source File: TopicSerializationTest.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
@Test
public void testToTopicConfig() {
    Topic topic = new Topic.Builder()
            .withTopicName("test-topic")
            .withConfigEntry("foo", "bar")
            .withNumPartitions(3)
            .withNumReplicas((short) 2)
            .withMapName("gee")
            .build();
    Map<ConfigResource, Config> config = TopicSerialization.toTopicConfig(topic);
    assertThat(config.size(), is(1));
    Map.Entry<ConfigResource, Config> c = config.entrySet().iterator().next();
    assertThat(c.getKey().type(), is(ConfigResource.Type.TOPIC));
    assertThat(c.getValue().entries().size(), is(1));
    assertThat(c.getKey().name(), is("test-topic"));
    assertThat(c.getValue().get("foo").value(), is("bar"));
}
 
Example #14
Source File: TopicManagerIT.java    From kafka-topology-builder with MIT License 6 votes vote down vote up
private void verifyTopicConfiguration(
    String topic, HashMap<String, String> config, List<String> removedConfigs)
    throws ExecutionException, InterruptedException {

  ConfigResource resource = new ConfigResource(Type.TOPIC, topic);
  Collection<ConfigResource> resources = Collections.singletonList(resource);

  Map<ConfigResource, Config> configs = kafkaAdminClient.describeConfigs(resources).all().get();

  Config topicConfig = configs.get(resource);
  Assert.assertNotNull(topicConfig);

  topicConfig
      .entries()
      .forEach(
          entry -> {
            if (!entry.isDefault()) {
              if (config.get(entry.name()) != null)
                Assert.assertEquals(config.get(entry.name()), entry.value());
              Assert.assertFalse(removedConfigs.contains(entry.name()));
            }
          });
}
 
Example #15
Source File: CruiseControlMetricsReporter.java    From cruise-control with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void maybeUpdateTopicConfig() {
  try {
    // Retrieve topic config to check and update.
    ConfigResource topicResource = new ConfigResource(ConfigResource.Type.TOPIC, _cruiseControlMetricsTopic);
    DescribeConfigsResult describeConfigsResult = _adminClient.describeConfigs(Collections.singleton(topicResource));
    Config topicConfig = describeConfigsResult.values().get(topicResource).get(CLIENT_REQUEST_TIMEOUT_MS, TimeUnit.MILLISECONDS);
    Set<AlterConfigOp> alterConfigOps = new HashSet<>(2);
    Map<String, String> configsToSet = new HashMap<>(2);
    configsToSet.put(LogConfig.RetentionMsProp(), _metricsTopic.configs().get(LogConfig.RetentionMsProp()));
    configsToSet.put(LogConfig.CleanupPolicyProp(), _metricsTopic.configs().get(LogConfig.CleanupPolicyProp()));
    maybeUpdateConfig(alterConfigOps, configsToSet, topicConfig);
    if (!alterConfigOps.isEmpty()) {
      AlterConfigsResult alterConfigsResult = _adminClient.incrementalAlterConfigs(Collections.singletonMap(topicResource, alterConfigOps));
      alterConfigsResult.values().get(topicResource).get(CLIENT_REQUEST_TIMEOUT_MS, TimeUnit.MILLISECONDS);
    }
  } catch (InterruptedException | ExecutionException | TimeoutException e) {
    LOG.warn("Unable to update config of Cruise Cruise Control metrics topic {}", _cruiseControlMetricsTopic, e);
  }
}
 
Example #16
Source File: KafkaMetricsServiceImpl.java    From kafka-eagle with Apache License 2.0 6 votes vote down vote up
private String deleteTopicConfig(String clusterAlias, AdminClient adminClient, String topic, ConfigEntry configEntry) {
	try {
		String describeTopicConfigs = describeTopicConfig(clusterAlias, topic);
		JSONObject object = JSON.parseObject(describeTopicConfigs).getJSONObject("config");
		object.remove(configEntry.name());
		List<ConfigEntry> configEntrys = new ArrayList<>();
		for (String key : KConstants.Topic.getTopicConfigKeys()) {
			if (object.containsKey(key)) {
				configEntrys.add(new ConfigEntry(key, object.getString(key)));
			}
		}
		Map<ConfigResource, Config> configs = new HashMap<>();
		ConfigResource configRes = new ConfigResource(Type.TOPIC, topic);
		Config config = new Config(configEntrys);
		configs.put(configRes, config);
		adminClient.alterConfigs(configs);
		return KConstants.Topic.SUCCESS;
	} catch (Exception e) {
		e.printStackTrace();
		LOG.error("Delete topic[" + topic + "] config has error, msg is " + e.getMessage());
		return e.getMessage();
	}
}
 
Example #17
Source File: KafkaImplTest.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
private void mockDescribeConfigs(Admin admin, Map<ConfigResource, Either<Config, Exception>> result) {
    DescribeConfigsResult describeConfigsResult = mock(DescribeConfigsResult.class);
    when(describeConfigsResult.values()).thenReturn(result.entrySet().stream().collect(toMap(
        entry -> entry.getKey(),
        entry -> {
            KafkaFutureImpl<Config> kafkaFuture = new KafkaFutureImpl<>();
            if (entry.getValue().isLeft()) {
                kafkaFuture.complete(entry.getValue().left());
            } else {
                kafkaFuture.completeExceptionally(entry.getValue().right());
            }
            return kafkaFuture;
        })));
    when(admin.describeConfigs(result.keySet())).thenReturn(describeConfigsResult);
}
 
Example #18
Source File: KafkaImplTest.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Test
public void testTopicMetadataBothFound(VertxTestContext testContext) {
    Admin admin = mock(Admin.class);
    mockDescribeTopics(admin, singletonMap("test", Either.ofLeft(mock(TopicDescription.class))));
    mockDescribeConfigs(admin, singletonMap(new ConfigResource(ConfigResource.Type.TOPIC, "test"),
            Either.ofLeft(mock(Config.class))));

    KafkaImpl impl = new KafkaImpl(admin, vertx);
    impl.topicMetadata(new TopicName("test")).onComplete(testContext.succeeding(topicMetadata -> testContext.verify(() -> {
        assertNotNull(topicMetadata);
        assertNotNull(topicMetadata.getDescription());
        assertNotNull(topicMetadata.getConfig());
        testContext.completeNow();
    })));
}
 
Example #19
Source File: KafkaImplTest.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Test
public void testTopicMetadataDescribeTimeout(VertxTestContext testContext) {
    Admin admin = mock(Admin.class);
    mockDescribeTopics(admin, singletonMap("test", Either.ofLeft(mock(TopicDescription.class))));
    mockDescribeConfigs(admin, singletonMap(new ConfigResource(ConfigResource.Type.TOPIC, "test"),
                Either.ofRight(new TimeoutException())));

    KafkaImpl impl = new KafkaImpl(admin, vertx);
    impl.topicMetadata(new TopicName("test")).onComplete(testContext.failing(error -> testContext.verify(() -> {
        assertTrue(error instanceof TimeoutException);
        testContext.completeNow();
    })));
}
 
Example #20
Source File: TopicOperatorBaseIT.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
protected void awaitTopicConfigInKafka(String topicName, String key, String expectedValue) throws InterruptedException, ExecutionException, TimeoutException {
    // Wait for that to be reflected in the kafka topic
    waitFor(() -> {
        ConfigResource configResource = topicConfigResource(topicName);
        org.apache.kafka.clients.admin.Config config = getTopicConfig(configResource);
        String retention = config.get("retention.ms").value();
        LOGGER.debug("retention of {}, waiting for 12341234", retention);
        return expectedValue.equals(retention);
    },  "Expected the topic " + topicName + " to have retention.ms=" + expectedValue + " in Kafka");
}
 
Example #21
Source File: ClientKafkaMonitor.java    From Kafdrop with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<BrokerConfigurationVO> getBrokerConfiguration(int id)
{
   ConfigResource configResource = new ConfigResource(ConfigResource.Type.BROKER, String.valueOf(id));

   DescribeConfigsResult configs = admin(client -> {
      return client.describeConfigs(
         Collections.singletonList(configResource));
   });

   return Optional.ofNullable(waitOnFuture(configs.all()).get(configResource))
      .map(config -> createBrokerConfiguration(id, config));
}
 
Example #22
Source File: ClientKafkaMonitor.java    From Kafdrop with Apache License 2.0 5 votes vote down vote up
private Map<String, Config> getTopicConfigs(Collection<String> topics)
{
   DescribeConfigsResult topicConfigs =
      admin(a -> a.describeConfigs(
         topics.stream()
            .map(topic -> new ConfigResource(ConfigResource.Type.TOPIC, topic))
            .collect(Collectors.toList())
            )
      );

   return waitOnFuture(topicConfigs.all())
      .entrySet().stream()
      .collect(Collectors.toMap(e -> e.getKey().name(), Map.Entry::getValue));
}
 
Example #23
Source File: RetentionByTopicFunction.java    From data-highway with Apache License 2.0 5 votes vote down vote up
public Map<String, Duration> apply(Collection<String> topics) {
  return Flux
      .fromIterable(topics)
      .map(name -> new ConfigResource(TOPIC, name))
      .collectList()
      .map(crs -> client.describeConfigs(crs).all())
      .map(KafkaFutures::join)
      .flatMapIterable(Map::entrySet)
      .collectMap(e -> e.getKey().name(), this::retention)
      .block();
}
 
Example #24
Source File: KafkaImplTest.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Test
public void testTopicMetadataDescribeConfigsNotFound(VertxTestContext testContext) {
    Admin admin = mock(Admin.class);
    mockDescribeTopics(admin, singletonMap("test", Either.ofLeft(mock(TopicDescription.class))));
    mockDescribeConfigs(admin, singletonMap(
            new ConfigResource(ConfigResource.Type.TOPIC, "test"),
            Either.ofRight(new UnknownTopicOrPartitionException())));

    KafkaImpl impl = new KafkaImpl(admin, vertx);
    impl.topicMetadata(new TopicName("test")).onComplete(testContext.succeeding(topicMetadata -> testContext.verify(() -> {
        assertNull(topicMetadata);
        testContext.completeNow();
    })));
}
 
Example #25
Source File: KafkaAvailabilityTest.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
void mockDescribeConfigs(Admin mockAc) {
    when(mockAc.describeConfigs(any())).thenAnswer(invocation -> {
        Collection<ConfigResource> argument = invocation.getArgument(0);
        DescribeConfigsResult dcr = mock(DescribeConfigsResult.class);
        Throwable throwable = null;
        for (ConfigResource configResource : argument) {
            throwable = describeConfigsResult.get(configResource);
            if (throwable != null) {
                break;
            }
        }
        when(dcr.values()).thenThrow(notImplemented());
        if (throwable != null) {
            when(dcr.all()).thenReturn(failedFuture(throwable));
        } else {
            Map<ConfigResource, Config> result = new HashMap<>();
            for (ConfigResource cr : argument) {
                List<ConfigEntry> entries = new ArrayList<>();
                for (Map.Entry<String, String> e : topics.get(cr.name()).configs.entrySet()) {
                    ConfigEntry ce = new ConfigEntry(e.getKey(), e.getValue());
                    entries.add(ce);
                }
                result.put(cr, new Config(entries));
            }
            when(dcr.all()).thenReturn(KafkaFuture.completedFuture(result));
        }
        return dcr;
    });
}
 
Example #26
Source File: KafkaImplTest.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Test
public void testTopicMetadataBothNotFound(VertxTestContext testContext) {
    Admin admin = mock(Admin.class);
    mockDescribeTopics(admin, singletonMap("test", Either.ofRight(new UnknownTopicOrPartitionException())));
    mockDescribeConfigs(admin, singletonMap(
            new ConfigResource(ConfigResource.Type.TOPIC, "test"),
            Either.ofRight(new UnknownTopicOrPartitionException())));

    KafkaImpl impl = new KafkaImpl(admin, vertx);
    impl.topicMetadata(new TopicName("test")).onComplete(testContext.succeeding(topicMetadata -> testContext.verify(() -> {
        assertNull(topicMetadata);
        testContext.completeNow();
    })));
}
 
Example #27
Source File: AdminClientTest.java    From javabase with Apache License 2.0 5 votes vote down vote up
/**
 * alter config for topics
 * 
 * @param client
 */
public static void alterConfigs(AdminClient client)
        throws ExecutionException, InterruptedException {
    Config topicConfig = new Config(
            Arrays.asList(new ConfigEntry("cleanup.policy", "compact")));
    client.alterConfigs(Collections.singletonMap(
            new ConfigResource(ConfigResource.Type.TOPIC, TEST_TOPIC), topicConfig)).all()
            .get();
}
 
Example #28
Source File: AbstractSharedKafkaTestResourceTest.java    From kafka-junit with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Simple smoke test to ensure broker running appropriate listeners.
 */
@Test
public void validateListener() throws ExecutionException, InterruptedException {
    try (final AdminClient adminClient  = getKafkaTestUtils().getAdminClient()) {
        final ConfigResource broker1Resource = new ConfigResource(ConfigResource.Type.BROKER, "1");

        // Pull broker configs
        final Config configResult = adminClient
            .describeConfigs(Collections.singletonList(broker1Resource))
            .values()
            .get(broker1Resource)
            .get();

        // Check listener
        final String actualListener = configResult.get("listeners").value();
        assertTrue(
            "Expected " + getExpectedListenerProtocol() + ":// and found: " + actualListener,
            actualListener.contains(getExpectedListenerProtocol() + "://")
        );

        // Check inter broker protocol
        final String actualBrokerProtocol = configResult.get("security.inter.broker.protocol").value();
        assertEquals(
            "Unexpected inter-broker protocol",
            getExpectedListenerProtocol(),
            actualBrokerProtocol
        );
    }
}
 
Example #29
Source File: KafkaOperations.java    From kafka-webview with MIT License 5 votes vote down vote up
/**
 * Modify configuration values for a specific topic.
 * @param topic The topic to modify.
 * @param configItems Map of Key to Value to modify.
 * @return boolean
 */
public TopicConfig alterTopicConfig(final String topic, final Map<String, String> configItems) {
    try {
        // Define the resource we want to modify, the topic.
        final ConfigResource configResource = new ConfigResource(ConfigResource.Type.TOPIC, topic);

        final List<ConfigEntry> configEntries = new ArrayList<>();
        for (final Map.Entry<String, String> entry : configItems.entrySet()) {
            configEntries.add(
                new ConfigEntry(entry.getKey(), entry.getValue())
            );
        }

        // Define the configuration set
        final Config config = new Config(configEntries);

        // Create the topic
        final AlterConfigsResult result = adminClient.alterConfigs(Collections.singletonMap(configResource, config));

        // Wait for the async request to process.
        result.all().get();

        // Lets return updated topic details
        return getTopicConfig(topic);
    } catch (final ExecutionException e) {
        throw handleExecutionException(e);
    } catch (final InterruptedException exception) {
        // TODO Handle this
        throw new RuntimeException(exception.getMessage(), exception);
    }
}
 
Example #30
Source File: TopicEnsure.java    From common-docker with Apache License 2.0 5 votes vote down vote up
public boolean validateTopic(TopicSpec spec, int timeOut) throws Exception {
  // Describe topic.
  DescribeTopicsResult topicDescribeResult = adminClient.describeTopics(
      Collections.singletonList(spec.name()), new DescribeTopicsOptions().timeoutMs(timeOut)
  );
  TopicDescription topic = topicDescribeResult.all().get().get(spec.name());

  // Get topic config.
  ConfigResource configResource = new ConfigResource(ConfigResource.Type.TOPIC, spec.name());
  DescribeConfigsResult configResult = adminClient.describeConfigs(
      Collections.singletonList(configResource)
  );
  Map<ConfigResource, Config> resultMap = configResult.all().get();
  Config config = resultMap.get(configResource);

  // Create actual TopicSpec.
  Map<String, String> actualConfig = new HashMap<>();
  for (Map.Entry<String, String> entry : spec.config().entrySet()) {
    ConfigEntry actualConfigEntry = config.get(entry.getKey());
    if (actualConfigEntry != null) {
      actualConfig.put(entry.getKey(), actualConfigEntry.value());
    }
  }

  TopicSpec actualSpec = new TopicSpec(
      topic.name(), topic.partitions().size(),
      topic.partitions().get(0).replicas().size(), actualConfig
  );

  boolean isTopicValid = actualSpec.equals(spec);
  if (!isTopicValid) {
    System.err.printf(
        "Invalid topic [ %s ] ! Expected %s but got %s\n", spec.name(), spec, actualSpec
    );
  }

  return isTopicValid;
}