-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add opt-in to terminate non-empty topics matching props
- Loading branch information
Showing
6 changed files
with
124 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/java/io/statnett/k3a/topicterminator/strategy/AndOperation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package io.statnett.k3a.topicterminator.strategy; | ||
|
||
import org.apache.kafka.clients.admin.AdminClient; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
|
||
public class AndOperation implements ReservedTopic { | ||
|
||
private final ReservedTopic[] parts; | ||
|
||
public AndOperation(ReservedTopic... parts) { | ||
this.parts = parts; | ||
} | ||
|
||
@Override | ||
public Set<String> filter(AdminClient client, Set<String> topicNames) throws ExecutionException, InterruptedException { | ||
if (parts.length == 0) { | ||
return topicNames; | ||
} | ||
|
||
HashSet<String> reserved = new HashSet<>(); | ||
for (ReservedTopic reservedTopic : parts) { | ||
reserved.addAll(reservedTopic.filter(client, new HashSet<>(topicNames))); | ||
} | ||
topicNames.retainAll(reserved); | ||
return topicNames; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/io/statnett/k3a/topicterminator/strategy/ReservedIfTopicNotMatchingProps.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package io.statnett.k3a.topicterminator.strategy; | ||
|
||
import org.apache.kafka.clients.admin.AdminClient; | ||
import org.apache.kafka.clients.admin.ConfigEntry; | ||
import org.apache.kafka.common.config.ConfigResource; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.stream.Collectors; | ||
|
||
import static java.util.Collections.emptySet; | ||
import static java.util.Collections.unmodifiableSet; | ||
|
||
public class ReservedIfTopicNotMatchingProps implements ReservedTopic { | ||
private final Set<Map.Entry<String, String>> matchingProps; | ||
|
||
public ReservedIfTopicNotMatchingProps(Map<String, String> matchingProps) { | ||
if (matchingProps == null) { | ||
this.matchingProps = emptySet(); | ||
} else { | ||
this.matchingProps = unmodifiableSet(matchingProps.entrySet()); | ||
} | ||
} | ||
|
||
@Override | ||
public Set<String> filter(AdminClient client, Set<String> topicNames) throws ExecutionException, InterruptedException { | ||
if (matchingProps.isEmpty()) { | ||
return emptySet(); | ||
} | ||
|
||
List<ConfigResource> topicResources = topicNames.stream() | ||
.map(t -> new ConfigResource(ConfigResource.Type.TOPIC, t)) | ||
.toList(); | ||
|
||
Map<String, Map<String, String>> topicProps = client.describeConfigs(topicResources).all().get().entrySet().stream() | ||
.collect(Collectors.toMap( | ||
entry -> entry.getKey().name(), | ||
entry -> entry.getValue().entries().stream().collect( | ||
Collectors.toMap(ConfigEntry::name, ConfigEntry::value) | ||
) | ||
) | ||
); | ||
|
||
return topicNames.stream() | ||
.filter(t -> topicProps.get(t).entrySet().containsAll(matchingProps)) | ||
.collect(Collectors.toSet()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
app: | ||
dry-run: false | ||
blessed-topics: ^blessed.*,topic-foo | ||
non-empty-topics-matching-props: | ||
cleanup.policy: compact |