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
29 changes: 21 additions & 8 deletions src/main/java/org/tikv/common/util/ChannelFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,15 @@ public CertWatcher(long pollInterval, List<File> targets, Runnable onChange) {
this::tryReload, pollInterval, pollInterval, TimeUnit.SECONDS);
}

// If any execution of the task encounters an exception, subsequent executions are suppressed.
private void tryReload() {
if (needReload()) {
onChange.run();
// Add exception handling to avoid schedule stop.
try {
if (needReload()) {
onChange.run();
}
} catch (Exception e) {
logger.error("Failed to reload cert!", e);
}
}

Expand Down Expand Up @@ -180,11 +186,16 @@ public OpenSslContext(String trustPath, String chainPath, String keyPath) {
@Override
public SslContextBuilder createSslContextBuilder() {
SslContextBuilder builder = GrpcSslContexts.forClient();
if (trustPath != null) {
builder.trustManager(new File(trustPath));
}
if (chainPath != null && keyPath != null) {
builder.keyManager(new File(chainPath), new File(keyPath));
try {
if (trustPath != null) {
builder.trustManager(new File(trustPath));
}
if (chainPath != null && keyPath != null) {
builder.keyManager(new File(chainPath), new File(keyPath));
}
} catch (Exception e) {
logger.error("Failed to create ssl context builder", e);
throw new IllegalArgumentException(e);
}
return builder;
}
Expand Down Expand Up @@ -351,7 +362,9 @@ public void close() {

if (certContext != null) {
recycler.shutdown();
certWatcher.close();
if (certWatcher != null) {
certWatcher.close();
}
}
}
}
17 changes: 17 additions & 0 deletions src/test/java/org/tikv/common/ChannelFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import org.junit.Test;
import org.tikv.common.util.ChannelFactory;
Expand Down Expand Up @@ -59,6 +60,22 @@ public void testCertWatcher() throws InterruptedException {
assertTrue(changed.get());
}

@Test
public void testCertWatcherWithExceptionTask() throws InterruptedException {
AtomicInteger timesOfReloadTask = new AtomicInteger(0);
new CertWatcher(
1,
ImmutableList.of(new File(caPath), new File(clientCertPath), new File(clientKeyPath)),
() -> {
timesOfReloadTask.getAndIncrement();
touchCert();
throw new RuntimeException("Mock exception in reload task");
});

Thread.sleep(5000);
assertTrue(timesOfReloadTask.get() > 1);
}

@Test
public void testMultiThreadTlsReload() throws InterruptedException {
ChannelFactory factory = createFactory();
Expand Down