Search in sources :

Example 1 with AlterConfigOp

use of org.apache.kafka.clients.admin.AlterConfigOp in project cruise-control by linkedin.

the class KafkaCruiseControlUtils method maybeUpdateTopicConfig.

/**
 * Update topic configurations with the desired configs specified in the given topicToUpdateConfigs.
 *
 * @param adminClient The adminClient to send describeConfigs and incrementalAlterConfigs requests.
 * @param topicToUpdateConfigs Existing topic to update selected configs if needed -- cannot be {@code null}.
 * @return {@code true} if the request is completed successfully, {@code false} if there are any exceptions.
 */
public static boolean maybeUpdateTopicConfig(AdminClient adminClient, NewTopic topicToUpdateConfigs) {
    String topicName = topicToUpdateConfigs.name();
    // Retrieve topic config to check if it needs an update.
    ConfigResource topicResource = new ConfigResource(ConfigResource.Type.TOPIC, topicName);
    DescribeConfigsResult describeConfigsResult = adminClient.describeConfigs(Collections.singleton(topicResource));
    Config topicConfig;
    try {
        topicConfig = describeConfigsResult.values().get(topicResource).get(CLIENT_REQUEST_TIMEOUT_MS, TimeUnit.MILLISECONDS);
    } catch (InterruptedException | ExecutionException | TimeoutException e) {
        LOG.warn("Config check for topic {} failed due to failure to describe its configs.", topicName, e);
        return false;
    }
    // Update configs if needed.
    Map<String, String> desiredConfig = topicToUpdateConfigs.configs();
    if (desiredConfig != null) {
        Set<AlterConfigOp> alterConfigOps = new HashSet<>();
        maybeUpdateConfig(alterConfigOps, desiredConfig, topicConfig);
        if (!alterConfigOps.isEmpty()) {
            AlterConfigsResult alterConfigsResult = adminClient.incrementalAlterConfigs(Collections.singletonMap(topicResource, alterConfigOps));
            try {
                alterConfigsResult.values().get(topicResource).get(CLIENT_REQUEST_TIMEOUT_MS, TimeUnit.MILLISECONDS);
            } catch (InterruptedException | ExecutionException | TimeoutException e) {
                LOG.warn("Config change for topic {} failed.", topicName, e);
                return false;
            }
        }
    }
    return true;
}
Also used : KafkaCruiseControlConfig(com.linkedin.kafka.cruisecontrol.config.KafkaCruiseControlConfig) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) AnalyzerConfig(com.linkedin.kafka.cruisecontrol.config.constants.AnalyzerConfig) Config(org.apache.kafka.clients.admin.Config) AdminClientConfig(org.apache.kafka.clients.admin.AdminClientConfig) AbstractConfig(org.apache.kafka.common.config.AbstractConfig) ExecutorConfig(com.linkedin.kafka.cruisecontrol.config.constants.ExecutorConfig) DescribeConfigsResult(org.apache.kafka.clients.admin.DescribeConfigsResult) AlterConfigOp(org.apache.kafka.clients.admin.AlterConfigOp) ConfigResource(org.apache.kafka.common.config.ConfigResource) ExecutionException(java.util.concurrent.ExecutionException) AlterConfigsResult(org.apache.kafka.clients.admin.AlterConfigsResult) TimeoutException(java.util.concurrent.TimeoutException) HashSet(java.util.HashSet)

Example 2 with AlterConfigOp

use of org.apache.kafka.clients.admin.AlterConfigOp in project cruise-control by linkedin.

the class CruiseControlMetricsReporter method maybeUpdateTopicConfig.

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<>();
        Map<String, String> configsToSet = new HashMap<>();
        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);
    }
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) KafkaConfig(kafka.server.KafkaConfig) TopicConfig(org.apache.kafka.common.config.TopicConfig) Config(org.apache.kafka.clients.admin.Config) LogConfig(kafka.log.LogConfig) ProducerConfig(org.apache.kafka.clients.producer.ProducerConfig) DescribeConfigsResult(org.apache.kafka.clients.admin.DescribeConfigsResult) AlterConfigOp(org.apache.kafka.clients.admin.AlterConfigOp) ConfigResource(org.apache.kafka.common.config.ConfigResource) AlterConfigsResult(org.apache.kafka.clients.admin.AlterConfigsResult) ExecutionException(java.util.concurrent.ExecutionException) HashSet(java.util.HashSet) TimeoutException(java.util.concurrent.TimeoutException)

Example 3 with AlterConfigOp

use of org.apache.kafka.clients.admin.AlterConfigOp in project cruise-control by linkedin.

the class CruiseControlMetricsUtils method maybeUpdateConfig.

/**
 * Create a config altering operation if config's current value does not equal to target value.
 * @param configsToAlter Set of config altering operations to be applied.
 * @param configsToSet Configs to set.
 * @param currentConfig Current value of the config.
 */
public static void maybeUpdateConfig(Set<AlterConfigOp> configsToAlter, Map<String, String> configsToSet, Config currentConfig) {
    for (Map.Entry<String, String> entry : configsToSet.entrySet()) {
        String configName = entry.getKey();
        String targetConfigValue = entry.getValue();
        if (currentConfig.get(configName) == null || !currentConfig.get(configName).value().equals(targetConfigValue)) {
            configsToAlter.add(new AlterConfigOp(new ConfigEntry(configName, targetConfigValue), AlterConfigOp.OpType.SET));
        }
    }
}
Also used : ConfigEntry(org.apache.kafka.clients.admin.ConfigEntry) AlterConfigOp(org.apache.kafka.clients.admin.AlterConfigOp) Map(java.util.Map)

Example 4 with AlterConfigOp

use of org.apache.kafka.clients.admin.AlterConfigOp in project cruise-control by linkedin.

the class KafkaCruiseControlUtils method maybeUpdateConfig.

/**
 * Add config altering operations to the given configs to alter for configs that differ between current and desired.
 *
 * @param configsToAlter A set of config altering operations to be populated.
 * @param desiredConfig Desired config value by name.
 * @param currentConfig Current config.
 */
private static void maybeUpdateConfig(Set<AlterConfigOp> configsToAlter, Map<String, String> desiredConfig, Config currentConfig) {
    for (Map.Entry<String, String> entry : desiredConfig.entrySet()) {
        String configName = entry.getKey();
        String targetConfigValue = entry.getValue();
        ConfigEntry currentConfigEntry = currentConfig.get(configName);
        if (currentConfigEntry == null || !currentConfigEntry.value().equals(targetConfigValue)) {
            configsToAlter.add(new AlterConfigOp(new ConfigEntry(configName, targetConfigValue), AlterConfigOp.OpType.SET));
        }
    }
}
Also used : ConfigEntry(org.apache.kafka.clients.admin.ConfigEntry) AlterConfigOp(org.apache.kafka.clients.admin.AlterConfigOp) Map(java.util.Map) HashMap(java.util.HashMap)

Example 5 with AlterConfigOp

use of org.apache.kafka.clients.admin.AlterConfigOp in project cruise-control by linkedin.

the class SamplingUtilsTest method testMaybeUpdateTopicConfig.

@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 = 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 = maybeUpdateTopicConfig(adminClient, topicToUpdateConfigs);
    EasyMock.verify(adminClient, describeConfigsResult, describedConfigsFuture, topicConfig, alterConfigsResult);
    assertTrue(updateTopicConfig);
}
Also used : KafkaFuture(org.apache.kafka.common.KafkaFuture) Config(org.apache.kafka.clients.admin.Config) KafkaCruiseControlUtils.maybeUpdateTopicConfig(com.linkedin.kafka.cruisecontrol.KafkaCruiseControlUtils.maybeUpdateTopicConfig) DescribeConfigsResult(org.apache.kafka.clients.admin.DescribeConfigsResult) AlterConfigOp(org.apache.kafka.clients.admin.AlterConfigOp) ConfigResource(org.apache.kafka.common.config.ConfigResource) ConfigEntry(org.apache.kafka.clients.admin.ConfigEntry) NewTopic(org.apache.kafka.clients.admin.NewTopic) AlterConfigsResult(org.apache.kafka.clients.admin.AlterConfigsResult) AdminClient(org.apache.kafka.clients.admin.AdminClient) Test(org.junit.Test)

Aggregations

AlterConfigOp (org.apache.kafka.clients.admin.AlterConfigOp)5 AlterConfigsResult (org.apache.kafka.clients.admin.AlterConfigsResult)3 Config (org.apache.kafka.clients.admin.Config)3 ConfigEntry (org.apache.kafka.clients.admin.ConfigEntry)3 DescribeConfigsResult (org.apache.kafka.clients.admin.DescribeConfigsResult)3 ConfigResource (org.apache.kafka.common.config.ConfigResource)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 KafkaCruiseControlUtils.maybeUpdateTopicConfig (com.linkedin.kafka.cruisecontrol.KafkaCruiseControlUtils.maybeUpdateTopicConfig)1 KafkaCruiseControlConfig (com.linkedin.kafka.cruisecontrol.config.KafkaCruiseControlConfig)1 AnalyzerConfig (com.linkedin.kafka.cruisecontrol.config.constants.AnalyzerConfig)1 ExecutorConfig (com.linkedin.kafka.cruisecontrol.config.constants.ExecutorConfig)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 LogConfig (kafka.log.LogConfig)1 KafkaConfig (kafka.server.KafkaConfig)1 AdminClient (org.apache.kafka.clients.admin.AdminClient)1 AdminClientConfig (org.apache.kafka.clients.admin.AdminClientConfig)1