Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.KafkaException;
import org.apache.kafka.common.config.SaslConfigs;
import org.apache.kafka.common.errors.AuthorizationException;
import org.apache.kafka.common.errors.OutOfOrderSequenceException;
import org.apache.kafka.common.errors.ProducerFencedException;
import org.apache.kafka.common.serialization.StringSerializer;
import org.apache.shenyu.common.utils.GsonUtils;
import org.apache.shenyu.common.utils.JsonUtils;
Expand Down Expand Up @@ -96,25 +93,19 @@ public boolean initClient0(@NonNull final KafkaLogCollectConfig.KafkaLogConfig c
.format("org.apache.kafka.common.security.scram.ScramLoginModule required username=\"{0}\" password=\"{1}\";",
config.getUserName(), config.getPassWord()));
}
producer = new KafkaProducer<>(props);
ProducerRecord<String, String> record = new ProducerRecord<>(this.topic, StringSerializer.class.getName(), StringSerializer.class.getName());
try {
producer.send(record);
LOG.info("init kafkaLogCollectClient success");
} catch (ProducerFencedException | OutOfOrderSequenceException | AuthorizationException e) {
// We can't recover from these exceptions, so our only option is to close the producer and exit.
LOG.error("Init kafkaLogCollectClient error, We can't recover from these exceptions, so our only option is to close the producer and exit", e);
producer.close();
return false;
producer = new KafkaProducer<>(props);
producer.partitionsFor(this.topic);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion (non-blocking): this call blocks until metadata arrives or max.block.ms elapses (60 s by default), and initClient0 is reached from LoggingKafkaPluginDataHandler#doRefreshConfig, i.e. the config-sync path - an unreachable broker would park it for up to a minute per refresh.

The previous send() was not better here (KafkaProducer#send calls waitOnMetadata with the same budget), so this is not a regression, just worth bounding now that fetching metadata is the whole point of the probe. Since kafka-clients 3.9.2 has no partitionsFor(String, Duration) overload (verified with javap against the exact version this reactor depends on), the only lever is:

props.put(ProducerConfig.MAX_BLOCK_MS_CONFIG, 5_000L);

Also worth considering: when the topic does not exist and auto-creation is disabled, partitionsFor simply returns an empty list instead of raising, so a mistyped topic still reports a successful init.

LOG.info("kafka topic metadata fetched successfully");
return true;
} catch (KafkaException e) {
// For all other exceptions, just abort the transaction and try again.
LOG.error(
"init kafkaLogCollectClient error,Exceptions other than ProducerFencedException or OutOfOrderSequenceException or AuthorizationException"
+ ", just abort the transaction and try again", e);
producer.close();
LOG.error("Failed to initialize kafka producer", e);
if (Objects.nonNull(producer)) {
producer.close();
producer = null;
}
return false;
}
return true;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,22 @@
package org.apache.shenyu.plugin.logging.kafka.kafka;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.common.errors.TimeoutException;
import org.apache.shenyu.common.dto.PluginData;
import org.apache.shenyu.common.utils.GsonUtils;
import org.apache.shenyu.plugin.logging.common.entity.ShenyuRequestLog;
import org.apache.shenyu.plugin.logging.kafka.client.KafkaLogCollectClient;
import org.apache.shenyu.plugin.logging.kafka.config.KafkaLogCollectConfig;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.mockito.MockedConstruction;

import java.lang.reflect.Field;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.mockConstruction;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
* The Test Case For KafkaLogCollectClient.
Expand All @@ -58,14 +60,23 @@ public void setUp() {
}

@Test
@Disabled
public void testInitClient() throws NoSuchFieldException, IllegalAccessException {
public void testInitClientDoesNotSendRecord() {
try (MockedConstruction<KafkaProducer> construction = mockConstruction(KafkaProducer.class)) {
kafkaLogCollectClient.initClient(globalLogConfig);
Field field = kafkaLogCollectClient.getClass().getDeclaredField("topic");
field.setAccessible(true);
Assertions.assertEquals(field.get(kafkaLogCollectClient), "shenyu-access-logging");
kafkaLogCollectClient.close();
Assertions.assertTrue(kafkaLogCollectClient.initClient0(globalLogConfig));
Assertions.assertEquals(1, construction.constructed().size());
verify(construction.constructed().get(0)).partitionsFor("shenyu-access-logging");
verify(construction.constructed().get(0), never()).send(any());
}
kafkaLogCollectClient.close0();
}

@Test
public void testInitClientFailsWhenTopicMetadataIsUnavailable() {
try (MockedConstruction<KafkaProducer> construction = mockConstruction(KafkaProducer.class,
(mock, context) -> when(mock.partitionsFor("shenyu-access-logging")).thenThrow(new TimeoutException("metadata unavailable")))) {
Assertions.assertFalse(kafkaLogCollectClient.initClient0(globalLogConfig));
verify(construction.constructed().get(0)).close();
verify(construction.constructed().get(0), never()).send(any());
}
}
}
Loading