Skip to content

Commit

Permalink
feat(core): allow to delete topic config (tchiotludoGH-362)
Browse files Browse the repository at this point in the history
  • Loading branch information
jnnfrmeyer authored and timonback committed Oct 24, 2023
1 parent 8214a11 commit 4ffa4d2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/main/java/org/akhq/models/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public Config(ConfigEntry entry) {
}
}

public boolean shouldResetToDefault() {
return this.getValue().isBlank();
}

private String findDescription(String name) {
String docName = name.toUpperCase().replace(".", "_") + "_DOC";

Expand Down Expand Up @@ -89,4 +93,4 @@ public enum Source {
DEFAULT_CONFIG,
UNKNOWN
}
}
}
12 changes: 9 additions & 3 deletions src/main/java/org/akhq/repositories/ConfigRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,20 @@ public void updateTopic(String clusterId, String name, List<Config> configs) thr
private void update(String clusterId, ConfigResource.Type type, String name, List<Config> configs) throws ExecutionException, InterruptedException {
List<ConfigEntry> entries = new ArrayList<>();

List<String> configNamesToReset = configs.stream()
.filter(Config::shouldResetToDefault)
.map(Config::getName)
.collect(Collectors.toList());

this.find(clusterId, type, Collections.singletonList(name))
.get(name)
.stream()
.filter(config -> config.getSource().name().startsWith("DYNAMIC_"))
.filter(config -> !configNamesToReset.contains(config.getName()))
.forEach(config -> entries.add(new ConfigEntry(config.getName(), config.getValue())));

configs
.stream()
configs.stream()
.filter(config -> !config.shouldResetToDefault())
.map(config -> new ConfigEntry(config.getName(), config.getValue()))
.forEach(entries::add);

Expand All @@ -102,4 +108,4 @@ public static List<Config> updatedConfigs(Map<String, String> request, List<Conf
.map(config -> config.withValue(request.get(configFn.apply(config))))
.collect(Collectors.toList());
}
}
}
22 changes: 20 additions & 2 deletions src/test/java/org/akhq/repositories/ConfigRepositoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
import java.util.concurrent.ExecutionException;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

class ConfigRepositoryTest extends AbstractTest {
@Inject
private ConfigRepository repository;

@Test
void updateTopic() throws ExecutionException, InterruptedException {
// write config the first time
repository.updateTopic(
KafkaTestCluster.CLUSTER_ID,
KafkaTestCluster.TOPIC_HUGE,
Expand All @@ -30,6 +32,7 @@ void updateTopic() throws ExecutionException, InterruptedException {
assertEquals("1", getConfig("file.delete.delay.ms").getValue());
assertEquals("2", getConfig("index.interval.bytes").getValue());

// update config 1
repository.updateTopic(
KafkaTestCluster.CLUSTER_ID,
KafkaTestCluster.TOPIC_HUGE,
Expand All @@ -41,7 +44,7 @@ void updateTopic() throws ExecutionException, InterruptedException {
assertEquals("3", getConfig("file.delete.delay.ms").getValue());
assertEquals("2", getConfig("index.interval.bytes").getValue());


// update config 2
repository.updateTopic(
KafkaTestCluster.CLUSTER_ID,
KafkaTestCluster.TOPIC_HUGE,
Expand All @@ -52,6 +55,21 @@ void updateTopic() throws ExecutionException, InterruptedException {

assertEquals("3", getConfig("file.delete.delay.ms").getValue());
assertEquals("4", getConfig("index.interval.bytes").getValue());

// reset config index.interval.bytes (leave config file.delete.delay.ms unchanged)
repository.updateTopic(
KafkaTestCluster.CLUSTER_ID,
KafkaTestCluster.TOPIC_HUGE,
Collections.singletonList(
new Config("index.interval.bytes", "")
)
);

Config unchangedConfig = getConfig("file.delete.delay.ms");
assertTrue(unchangedConfig.getSource().name().startsWith("DYNAMIC_"));

Config resettedConfig = getConfig("index.interval.bytes");
assertTrue(resettedConfig.getSource().name().startsWith("DEFAULT_"));
}

private Config getConfig(String name) throws ExecutionException, InterruptedException {
Expand All @@ -62,4 +80,4 @@ private Config getConfig(String name) throws ExecutionException, InterruptedExce
.findAny()
.get();
}
}
}

0 comments on commit 4ffa4d2

Please sign in to comment.