use of org.apache.kafka.common.config.ConfigResource in project kafka by apache.
the class ConfigurationsDelta method replay.
public void replay(ConfigRecord record) {
ConfigResource resource = new ConfigResource(Type.forId(record.resourceType()), record.resourceName());
ConfigurationImage configImage = image.resourceData().getOrDefault(resource, ConfigurationImage.EMPTY);
ConfigurationDelta delta = changes.computeIfAbsent(resource, __ -> new ConfigurationDelta(configImage));
delta.replay(record);
}
use of org.apache.kafka.common.config.ConfigResource in project kafka by apache.
the class QuorumControllerTest method testConfigResourceExistenceChecker.
@Test
public void testConfigResourceExistenceChecker() throws Throwable {
try (LocalLogManagerTestEnv logEnv = new LocalLogManagerTestEnv(3, Optional.empty())) {
try (QuorumControllerTestEnv controlEnv = new QuorumControllerTestEnv(logEnv, b -> b.setConfigDefs(CONFIGS))) {
QuorumController active = controlEnv.activeController();
registerBrokers(active, 5);
active.createTopics(new CreateTopicsRequestData().setTopics(new CreatableTopicCollection(Collections.singleton(new CreatableTopic().setName("foo").setReplicationFactor((short) 3).setNumPartitions(1)).iterator()))).get();
ConfigResourceExistenceChecker checker = active.new ConfigResourceExistenceChecker();
// A ConfigResource with type=BROKER and name=(empty string) represents
// the default broker resource. It is used to set cluster configs.
checker.accept(new ConfigResource(BROKER, ""));
// Broker 3 exists, so we can set a configuration for it.
checker.accept(new ConfigResource(BROKER, "3"));
// Broker 10 does not exist, so this should throw an exception.
assertThrows(BrokerIdNotRegisteredException.class, () -> checker.accept(new ConfigResource(BROKER, "10")));
// Topic foo exists, so we can set a configuration for it.
checker.accept(new ConfigResource(TOPIC, "foo"));
// Topic bar does not exist, so this should throw an exception.
assertThrows(UnknownTopicOrPartitionException.class, () -> checker.accept(new ConfigResource(TOPIC, "bar")));
}
}
}
use of org.apache.kafka.common.config.ConfigResource in project kafka by apache.
the class ReplicationControlManager method createTopics.
ControllerResult<CreateTopicsResponseData> createTopics(CreateTopicsRequestData request) {
Map<String, ApiError> topicErrors = new HashMap<>();
List<ApiMessageAndVersion> records = new ArrayList<>();
// Check the topic names.
validateNewTopicNames(topicErrors, request.topics());
// Identify topics that already exist and mark them with the appropriate error
request.topics().stream().filter(creatableTopic -> topicsByName.containsKey(creatableTopic.name())).forEach(t -> topicErrors.put(t.name(), new ApiError(Errors.TOPIC_ALREADY_EXISTS, "Topic '" + t.name() + "' already exists.")));
// Verify that the configurations for the new topics are OK, and figure out what
// ConfigRecords should be created.
Map<ConfigResource, Map<String, Entry<OpType, String>>> configChanges = computeConfigChanges(topicErrors, request.topics());
ControllerResult<Map<ConfigResource, ApiError>> configResult = configurationControl.incrementalAlterConfigs(configChanges, NO_OP_EXISTENCE_CHECKER);
for (Entry<ConfigResource, ApiError> entry : configResult.response().entrySet()) {
if (entry.getValue().isFailure()) {
topicErrors.put(entry.getKey().name(), entry.getValue());
}
}
records.addAll(configResult.records());
// Try to create whatever topics are needed.
Map<String, CreatableTopicResult> successes = new HashMap<>();
for (CreatableTopic topic : request.topics()) {
if (topicErrors.containsKey(topic.name()))
continue;
ApiError error;
try {
error = createTopic(topic, records, successes);
} catch (ApiException e) {
error = ApiError.fromThrowable(e);
}
if (error.isFailure()) {
topicErrors.put(topic.name(), error);
}
}
// Create responses for all topics.
CreateTopicsResponseData data = new CreateTopicsResponseData();
StringBuilder resultsBuilder = new StringBuilder();
String resultsPrefix = "";
for (CreatableTopic topic : request.topics()) {
ApiError error = topicErrors.get(topic.name());
if (error != null) {
data.topics().add(new CreatableTopicResult().setName(topic.name()).setErrorCode(error.error().code()).setErrorMessage(error.message()));
resultsBuilder.append(resultsPrefix).append(topic).append(": ").append(error.error()).append(" (").append(error.message()).append(")");
resultsPrefix = ", ";
continue;
}
CreatableTopicResult result = successes.get(topic.name());
data.topics().add(result);
resultsBuilder.append(resultsPrefix).append(topic).append(": ").append("SUCCESS");
resultsPrefix = ", ";
}
if (request.validateOnly()) {
log.info("Validate-only CreateTopics result(s): {}", resultsBuilder.toString());
return ControllerResult.atomicOf(Collections.emptyList(), data);
} else {
log.info("CreateTopics result(s): {}", resultsBuilder.toString());
return ControllerResult.atomicOf(records, data);
}
}
use of org.apache.kafka.common.config.ConfigResource in project kafka by apache.
the class MockController method incrementalAlterConfigs.
@Override
public CompletableFuture<Map<ConfigResource, ApiError>> incrementalAlterConfigs(Map<ConfigResource, Map<String, Entry<AlterConfigOp.OpType, String>>> configChanges, boolean validateOnly) {
Map<ConfigResource, ApiError> results = new HashMap<>();
for (Entry<ConfigResource, Map<String, Entry<AlterConfigOp.OpType, String>>> entry : configChanges.entrySet()) {
ConfigResource resource = entry.getKey();
results.put(resource, incrementalAlterResource(resource, entry.getValue(), validateOnly));
}
CompletableFuture<Map<ConfigResource, ApiError>> future = new CompletableFuture<>();
future.complete(results);
return future;
}
Aggregations