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;
}
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);
}
}
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));
}
}
}
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));
}
}
}
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);
}
Aggregations