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

The following examples show how to use org.apache.kafka.clients.admin.Config. 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: Utils.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
public static TopicMetadata getTopicMetadata(Topic kubeTopic) {
    List<Node> nodes = new ArrayList<>();
    for (int nodeId = 0; nodeId < kubeTopic.getNumReplicas(); nodeId++) {
        nodes.add(new Node(nodeId, "localhost", 9092 + nodeId));
    }
    List<TopicPartitionInfo> partitions = new ArrayList<>();
    for (int partitionId = 0; partitionId < kubeTopic.getNumPartitions(); partitionId++) {
        partitions.add(new TopicPartitionInfo(partitionId, nodes.get(0), nodes, nodes));
    }
    List<ConfigEntry> configs = new ArrayList<>();
    for (Map.Entry<String, String> entry: kubeTopic.getConfig().entrySet()) {
        configs.add(new ConfigEntry(entry.getKey(), entry.getValue()));
    }

    return new TopicMetadata(new TopicDescription(kubeTopic.getTopicName().toString(), false,
            partitions), new Config(configs));
}
 
Example #3
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 #4
Source File: TopicOperatorMockTest.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
private Config waitUntilTopicInKafka(String topicName, Predicate<Config> p) {
    ConfigResource configResource = new ConfigResource(ConfigResource.Type.TOPIC, topicName);
    AtomicReference<Config> ref = new AtomicReference<>();
    waitFor("Creation of topic " + topicName, 1_000, 60_000, () -> {
        try {
            Map<ConfigResource, Config> descriptionMap = adminClient.describeConfigs(asList(configResource)).all().get();
            Config desc = descriptionMap.get(configResource);
            if (p.test(desc)) {
                ref.set(desc);
                return true;
            }
            return false;
        } catch (Exception e) {
            return false;
        }
    });
    return ref.get();
}
 
Example #5
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 #6
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 #7
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 #8
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 #9
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 #10
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 #11
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 #12
Source File: KafkaHighLevelAdminClient.java    From kafdrop with Apache License 2.0 6 votes vote down vote up
Map<String, Config> describeTopicConfigs(Set<String> topicNames) {
  final var resources = topicNames.stream()
      .map(topic -> new ConfigResource(Type.TOPIC, topic))
      .collect(Collectors.toList());
  final var result = adminClient.describeConfigs(resources);
  final Map<String, Config> configsByTopic;
  try {
    final var allConfigs = result.all().get();
    configsByTopic = new HashMap<>(allConfigs.size(), 1f);
    for (var entry : allConfigs.entrySet()) {
      configsByTopic.put(entry.getKey().name(), entry.getValue());
    }
  } catch (InterruptedException | ExecutionException e) {
    if (e.getCause() instanceof UnsupportedVersionException) {
      return Map.of();
    } else if (e.getCause() instanceof TopicAuthorizationException) {
      printAcls();
    }
    throw new KafkaAdminClientException(e);
  }
  return configsByTopic;
}
 
Example #13
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 #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: TopicServiceImpl.java    From kafka-helmsman with MIT License 6 votes vote down vote up
/**
 * Transform a TopicDescription instance to ConfiguredTopic instance.
 *
 * @param td  an instance of TopicDescription
 * @param ktc a topic config future
 * @return an instance of ConfiguredTopic
 */
static ConfiguredTopic configuredTopic(TopicDescription td, KafkaFuture<Config> ktc) {
  int partitions = td.partitions().size();
  short replication = (short) td.partitions().iterator().next().replicas().size();
  try {
    Config tc = ktc.get();
    Map<String, String> configMap = tc
        .entries()
        .stream()
        .filter(TopicServiceImpl::isNonDefault)
        .collect(toMap(ConfigEntry::name, ConfigEntry::value));
    return new ConfiguredTopic(td.name(), partitions, replication, configMap);
  } catch (InterruptedException | ExecutionException e) {
    // TODO: FA-10109: Improve exception handling
    throw new RuntimeException(e);
  }
}
 
Example #16
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 #17
Source File: KafkaMetricsServiceImpl.java    From kafka-eagle with Apache License 2.0 5 votes vote down vote up
private String addTopicConfig(String clusterAlias, AdminClient adminClient, String topic, ConfigEntry configEntry) {
	try {
		String describeTopicConfigs = describeTopicConfig(clusterAlias, topic);
		JSONObject object = JSON.parseObject(describeTopicConfigs).getJSONObject("config");
		if (object.containsKey(configEntry.name())) {
			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)));
			}
		}
		configEntrys.add(configEntry);
		Map<ConfigResource, Config> configs = new HashMap<>();
		ConfigResource configRes = new ConfigResource(Type.TOPIC, topic);
		Config config = new Config(configEntrys);
		configs.put(configRes, config);
		AlterConfigsResult alterConfig = adminClient.alterConfigs(configs);
		alterConfig.all().get();
		return KConstants.Topic.SUCCESS;
	} catch (Exception e) {
		e.printStackTrace();
		LOG.error("Add topic[" + topic + "] config has error, msg is " + e.getMessage());
		return e.getMessage();
	}
}
 
Example #18
Source File: TopologyBuilderAdminClient.java    From kafka-topology-builder with MIT License 5 votes vote down vote up
private Config getActualTopicConfig(String topic)
    throws ExecutionException, InterruptedException {
  ConfigResource resource = new ConfigResource(Type.TOPIC, topic);
  Collection<ConfigResource> resources = Collections.singletonList(resource);

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

  return configs.get(resource);
}
 
Example #19
Source File: TopicSerialization.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
/**
 * Return a singleton map from the topic {@link ConfigResource} for the given topic,
 * to the {@link Config} of the given topic.
 * @return
 */
public static Map<ConfigResource, Config> toTopicConfig(Topic topic) {
    List<ConfigEntry> entries = new ArrayList<>(topic.getConfig().size());

    for (Map.Entry<String, String> entry : topic.getConfig().entrySet()) {
        ConfigEntry configEntry = new ConfigEntry(entry.getKey(), entry.getValue());
        entries.add(configEntry);
    }

    return Collections.singletonMap(
            new ConfigResource(ConfigResource.Type.TOPIC, topic.getTopicName().toString()),
            new Config(entries));
}
 
Example #20
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 #21
Source File: KafkaImplTest.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Test
public void testTopicMetadataDescribeTopicNotFound(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.ofLeft(mock(Config.class))));

    KafkaImpl impl = new KafkaImpl(admin, vertx);
    impl.topicMetadata(new TopicName("test")).onComplete(testContext.succeeding(topicMetadata -> testContext.verify(() -> {
        assertNull(topicMetadata);
        testContext.completeNow();
    })));
}
 
Example #22
Source File: TopologyBuilderAdminClient.java    From kafka-topology-builder with MIT License 5 votes vote down vote up
/**
 * Find cluster inter protocol version, used to determine the minimum level of Api compatibility
 *
 * @return String, the current Kafka Protocol version
 */
private String findKafkaVersion() throws IOException {
  ConfigResource resource = new ConfigResource(Type.BROKER, "inter.broker.protocol.version");
  String kafkaVersion = "";
  try {
    Map<ConfigResource, Config> configs =
        adminClient.describeConfigs(Collections.singletonList(resource)).all().get();
    kafkaVersion =
        configs.get(resource).get("inter.broker.protocol.version").value().split("-")[0];
  } catch (ExecutionException | InterruptedException e) {
    LOGGER.error(e);
    throw new IOException(e);
  }
  return kafkaVersion;
}
 
Example #23
Source File: KafkaImpl.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public Future<Void> updateTopicConfig(Topic topic) {
    Map<ConfigResource, Config> configs = TopicSerialization.toTopicConfig(topic);
    KafkaFuture<Void> future = adminClient.alterConfigs(configs).values().get(configs.keySet().iterator().next());
    return mapFuture(future);
}
 
Example #24
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 #25
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 #26
Source File: KafkaAvailability.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
private Future<Boolean> canRollBroker(Future<Collection<TopicDescription>> descriptions, int podId) {
    Future<Set<TopicDescription>> topicsOnGivenBroker = descriptions
            .compose(topicDescriptions -> {
                log.debug("Got {} topic descriptions", topicDescriptions.size());
                return Future.succeededFuture(groupTopicsByBroker(topicDescriptions, podId));
            }).recover(error -> {
                log.warn(error);
                return Future.failedFuture(error);
            });

    // 4. Get topic configs (for those on $broker)
    Future<Map<String, Config>> topicConfigsOnGivenBroker = topicsOnGivenBroker
            .compose(td -> topicConfigs(td.stream().map(t -> t.name()).collect(Collectors.toSet())));

    // 5. join
    return topicConfigsOnGivenBroker.map(topicNameToConfig -> {
        Collection<TopicDescription> tds = topicsOnGivenBroker.result();
        boolean canRoll = tds.stream().noneMatch(
            td -> wouldAffectAvailability(podId, topicNameToConfig, td));
        if (!canRoll) {
            log.debug("Restart pod {} would remove it from ISR, stalling producers with acks=all", podId);
        }
        return canRoll;
    }).recover(error -> {
        log.warn("Error determining whether it is safe to restart pod {}", podId, error);
        return Future.failedFuture(error);
    });
}
 
Example #27
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 #28
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;
}
 
Example #29
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 #30
Source File: TopicServiceImplTest.java    From kafka-helmsman with MIT License 5 votes vote down vote up
@Test
public void testListExisting() {
  Cluster cluster = createCluster(1);
  TopicPartitionInfo tp = new TopicPartitionInfo(0, cluster.nodeById(0), cluster.nodes(), Collections.emptyList());
  ConfigEntry configEntry = new ConfigEntry("k", "v");
  KafkaFuture<Config> kfc = KafkaFuture.completedFuture(new Config(Collections.singletonList(configEntry)));
  Set<String> topicNames = new HashSet<>(Arrays.asList("a", "b", "_c"));
  Map<String, TopicDescription> tds = new HashMap<String, TopicDescription>() {
    {
      put("a", new TopicDescription("a", false, Collections.singletonList(tp)));
      put("b", new TopicDescription("b", false, Collections.singletonList(tp)));
      put("c", new TopicDescription("_c", false, Collections.singletonList(tp)));
    }
  };
  Map<ConfigResource, KafkaFuture<Config>> configs = new HashMap<ConfigResource, KafkaFuture<Config>>() {
    {
      put(new ConfigResource(TOPIC, "a"), kfc);
      put(new ConfigResource(TOPIC, "b"), kfc);
      put(new ConfigResource(TOPIC, "_c"), kfc);
    }
  };

  TopicService service = new TopicServiceImpl(adminClient, true);
  ListTopicsResult listTopicsResult = mock(ListTopicsResult.class);
  DescribeTopicsResult describeTopicsResult = mock(DescribeTopicsResult.class);
  DescribeConfigsResult describeConfigsResult = mock(DescribeConfigsResult.class);

  when(describeTopicsResult.all()).thenReturn(KafkaFuture.completedFuture(tds));
  when(listTopicsResult.names()).thenReturn(KafkaFuture.completedFuture(topicNames));
  when(describeConfigsResult.values()).thenReturn(configs);
  when(adminClient.listTopics(any(ListTopicsOptions.class))).thenReturn(listTopicsResult);
  when(adminClient.describeTopics(topicNames)).thenReturn(describeTopicsResult);
  when(adminClient.describeConfigs(any(Collection.class))).thenReturn(describeConfigsResult);

  Map<String, ConfiguredTopic> actual = service.listExisting(true);
  Assert.assertEquals(2, actual.size());
  Assert.assertEquals(new HashSet<>(Arrays.asList("a", "b")), actual.keySet());
}