Skip to content

Commit

Permalink
Trying to auto-create topics, when using admin client for topic valid…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
onukristo committed Jan 2, 2024
1 parent 23c474e commit 8757c2f
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 31 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.28.0] - 2023-12-20

### Changed

- Configuration options related to topic validation.
- Trying to auto-create topics, when using admin client for topic validation.

### Removed

- Unused `debugEnabled` property.

## [0.27.0] - 2023-12-20

### Changed
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=0.27.0
version=0.28.0
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import lombok.experimental.UtilityClass;

/*
Mainly used to add verbose log to investigate specfic flaky tests.
Mainly used to add verbose log to investigate specific flaky tests.
*/
@UtilityClass
public class Debug {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,15 @@ public void afterPropertiesSet() {
public void preValidateAll() {
final var topics = tkmsProperties.getTopics();

final var semaphore = new Semaphore(tkmsProperties.getAdminClientTopicsValidationConcurrency());
final var semaphore = new Semaphore(tkmsProperties.getTopicValidation().getValidationConcurrency());
final var failures = new AtomicInteger();
final var countDownLatch = new CountDownLatch(topics.size());
final var startTimeEpochMs = System.currentTimeMillis();

for (var topic : topics) {
topicsValidatedDuringInitializationOrNotified.put(topic, Boolean.TRUE);
final var timeoutMs = tkmsProperties.getInternals().getTopicPreValidationTimeout().toMillis() - System.currentTimeMillis() + startTimeEpochMs;
final var timeoutMs =
tkmsProperties.getTopicValidation().getTopicPreValidationTimeout().toMillis() - System.currentTimeMillis() + startTimeEpochMs;
if (ExceptionUtils.doUnchecked(() -> semaphore.tryAcquire(timeoutMs, TimeUnit.MILLISECONDS))) {
/*
We are validating one by one, to get the proper error messages from Kafka.
Expand All @@ -113,7 +114,8 @@ public void preValidateAll() {
}
}

final var timeoutMs = tkmsProperties.getInternals().getTopicPreValidationTimeout().toMillis() - System.currentTimeMillis() + startTimeEpochMs;
final var timeoutMs =
tkmsProperties.getTopicValidation().getTopicPreValidationTimeout().toMillis() - System.currentTimeMillis() + startTimeEpochMs;

if (!ExceptionUtils.doUnchecked(() -> countDownLatch.await(timeoutMs, TimeUnit.MILLISECONDS))) {
tkmsKafkaProducerProvider.closeKafkaProducerForTopicValidation();
Expand All @@ -128,7 +130,7 @@ public void preValidateAll() {

@Override
public void validate(TkmsShardPartition shardPartition, String topic, Integer partition) {
if (tkmsProperties.isUseAdminClientForTopicsValidation()) {
if (tkmsProperties.getTopicValidation().isUseAdminClient()) {
validateUsingAdmin(shardPartition, topic, partition);
} else {
validateUsingProducer(topic);
Expand Down Expand Up @@ -188,6 +190,27 @@ protected void validateUsingProducer(String topic) {
}

protected FetchTopicDescriptionResponse fetchTopicDescription(FetchTopicDescriptionRequest request) {
final var result = fetchTopicDescription0(request);

if (result.getThrowable() != null
&& result.getThrowable() instanceof UnknownTopicOrPartitionException
&& tkmsProperties.getTopicValidation().isTryToAutoCreateTopic()) {
final var topic = request.getTopic();
try {
validateUsingProducer(topic);

log.info("Succeeded in auto creating topic `{}`", topic);

return fetchTopicDescription0(request);
} catch (Throwable t) {
log.warn("Trying to auto create topic `{}` failed.", topic, t);
}
}

return result;
}

protected FetchTopicDescriptionResponse fetchTopicDescription0(FetchTopicDescriptionRequest request) {
final var topic = request.getTopic();
TopicDescription topicDescription = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ public void afterPropertiesSet() {
*/
private Map<NotificationType, NotificationLevel> notificationLevels = new HashMap<>();

/**
* Provides more metrics at performance penalty.
*/
private boolean debugEnabled;

/**
* The default number of partitions in a shard.
*
Expand Down Expand Up @@ -206,21 +201,9 @@ public void afterPropertiesSet() {
@LegacyResolvedValue
private List<String> topics = new ArrayList<>();

/**
* Uses AdminClient to validate topics.
*
* <p>AdminClient allows us to also check if topics have suitable ACLs.
*
* <p>Experimental option.
*
* <p>May be the default in the future.
*/
private boolean useAdminClientForTopicsValidation = false;

/**
* How many topics validations are we doing in parallel, during the initialization of Tkms.
*/
private int adminClientTopicsValidationConcurrency = 10;
@Valid
@jakarta.validation.Valid
private TopicValidation topicValidation = new TopicValidation();

@Valid
@jakarta.validation.Valid
Expand Down Expand Up @@ -549,10 +532,34 @@ public static class Internals {
*/
private Duration flushInterruptionDuration = Duration.ofSeconds(30);

}

@Data
@Accessors(chain = true)
public static class TopicValidation {

/**
* How long do we wait for topics to get pre-validated.
*/
private Duration topicPreValidationTimeout = Duration.ofMinutes(1);

/**
* Uses AdminClient to validate topics.
*
* <p>AdminClient allows us to also check if topics have suitable ACLs.
*
* <p>Experimental option.
*
* <p>May be the default in the future.
*/
private boolean useAdminClient = false;

/**
* How many topics validations are we doing in parallel, during the initialization of Tkms.
*/
private int validationConcurrency = 10;

private boolean tryToAutoCreateTopic = true;
}

public enum NotificationLevel {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void cleanup() {
((TransactionalKafkaMessageSender) transactionalKafkaMessageSender).setTkmsDaoProvider(tkmsDaoProvider);
tkmsProperties.setDeferMessageRegistrationUntilCommit(false);
tkmsProperties.setValidateSerialization(false);
tkmsProperties.setUseAdminClientForTopicsValidation(false);
tkmsProperties.getTopicValidation().setUseAdminClient(false);
}

protected void setupConfig(boolean deferUntilCommit) {
Expand Down Expand Up @@ -484,7 +484,7 @@ private static Stream<Arguments> unknownTopicsMatrix() {
void sendingToUnknownTopicWillBePreventedWhenTopicAutoCreationIsDisabled(boolean deferUntilCommit, boolean useAdminClient) {
try {
setupConfig(deferUntilCommit);
tkmsProperties.setUseAdminClientForTopicsValidation(useAdminClient);
tkmsProperties.getTopicValidation().setUseAdminClient(useAdminClient);

var expectedMessage =
useAdminClient ? "Topic 'NotExistingTopic' does not exist." : "Topic NotExistingTopic not present in metadata after";
Expand Down
6 changes: 3 additions & 3 deletions tw-tkms-starter/src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ tw-tkms:
ms: 7
earliest-visible-messages:
enabled: true
debug-enabled: true
compression:
algorithm: random
min-size: 10
Expand Down Expand Up @@ -80,10 +79,11 @@ spring:
tw-tkms:
database-dialect: POSTGRES
delete-batch-sizes: "51, 11, 5, 1"
use-admin-client-for-topics-validation: true
topic-validation:
use-admin-client: true

tw-tkms-test:
test-topic: TestTopicPostgres
test-topic: TeestTopicPostgres

---

Expand Down

0 comments on commit 8757c2f

Please sign in to comment.