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

The following examples show how to use org.apache.kafka.clients.admin.AlterConfigsResult. 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: 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 #2
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 #3
Source File: KafkaAdminClientImpl.java    From vertx-kafka-client with Apache License 2.0 6 votes vote down vote up
@Override
public Future<Void> alterConfigs(Map<ConfigResource, Config> configs) {
  ContextInternal ctx = (ContextInternal) vertx.getOrCreateContext();
  Promise<Void> promise = ctx.promise();

  AlterConfigsResult alterConfigsResult = this.adminClient.alterConfigs(Helper.toConfigMaps(configs));
  alterConfigsResult.all().whenComplete((v, ex) -> {

    if (ex == null) {
      promise.complete();
    } else {
      promise.fail(ex);
    }
  });
  return promise.future();
}
 
Example #4
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 #5
Source File: SamplingUtilsTest.java    From cruise-control with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test
public void testMaybeUpdateTopicConfig() throws InterruptedException, ExecutionException, TimeoutException {
  AdminClient adminClient = EasyMock.createMock(AdminClient.class);
  DescribeConfigsResult describeConfigsResult = EasyMock.createMock(DescribeConfigsResult.class);
  KafkaFuture<Config> describedConfigsFuture = EasyMock.createMock(KafkaFuture.class);
  Config topicConfig = EasyMock.createMock(Config.class);
  AlterConfigsResult alterConfigsResult = EasyMock.createMock(AlterConfigsResult.class);
  Set<AlterConfigOp> alterConfigOps = Collections.singleton(new AlterConfigOp(
      new ConfigEntry(RetentionMsProp(), Long.toString(MOCK_DESIRED_RETENTION_MS)), AlterConfigOp.OpType.SET));
  Map<ConfigResource, KafkaFuture<Config>> describeConfigsValues = Collections.singletonMap(MOCK_TOPIC_RESOURCE,
                                                                                            describedConfigsFuture);
  Map<ConfigResource, KafkaFuture<Void>> alterConfigsValues = Collections.singletonMap(MOCK_TOPIC_RESOURCE,
                                                                                       EasyMock.createMock(KafkaFuture.class));

  NewTopic topicToUpdateConfigs = SamplingUtils.wrapTopic(MOCK_TOPIC, MOCK_PARTITION_COUNT, MOCK_REPLICATION_FACTOR, MOCK_DESIRED_RETENTION_MS);
  EasyMock.expect(adminClient.describeConfigs(EasyMock.eq(Collections.singleton(MOCK_TOPIC_RESOURCE)))).andReturn(describeConfigsResult);
  EasyMock.expect(describeConfigsResult.values()).andReturn(describeConfigsValues);
  EasyMock.expect(describedConfigsFuture.get(CLIENT_REQUEST_TIMEOUT_MS, TimeUnit.MILLISECONDS)).andReturn(topicConfig);
  EasyMock.expect(topicConfig.get(EasyMock.eq(CleanupPolicyProp()))).andReturn(new ConfigEntry(CleanupPolicyProp(),
                                                                                               DEFAULT_CLEANUP_POLICY));
  EasyMock.expect(topicConfig.get(EasyMock.eq(RetentionMsProp()))).andReturn(new ConfigEntry(RetentionMsProp(),
                                                                                             MOCK_CURRENT_RETENTION_MS));
  EasyMock.expect(adminClient.incrementalAlterConfigs(EasyMock.eq(Collections.singletonMap(MOCK_TOPIC_RESOURCE,
                                                                                           alterConfigOps))))
          .andReturn(alterConfigsResult);
  EasyMock.expect(alterConfigsResult.values()).andReturn(alterConfigsValues);
  EasyMock.replay(adminClient, describeConfigsResult, describedConfigsFuture, topicConfig, alterConfigsResult);


  boolean updateTopicConfig = SamplingUtils.maybeUpdateTopicConfig(adminClient, topicToUpdateConfigs);
  EasyMock.verify(adminClient, describeConfigsResult, describedConfigsFuture, topicConfig, alterConfigsResult);
  assertTrue(updateTopicConfig);
}
 
Example #6
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 #7
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 #8
Source File: KafkaTopicClientImplTest.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 4 votes vote down vote up
private static AlterConfigsResult alterTopicConfigResponse() {
  final AlterConfigsResult response = mock(AlterConfigsResult.class);
  expect(response.all()).andReturn(KafkaFuture.completedFuture(null));
  replay(response);
  return response;
}
 
Example #9
Source File: KafkaTopicClientImplTest.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 4 votes vote down vote up
private static AlterConfigsResult alterTopicConfigResponse(final Exception cause) {
  final AlterConfigsResult response = mock(AlterConfigsResult.class);
  expect(response.all()).andReturn(failedFuture(cause));
  replay(response);
  return response;
}