From d92b4ff05f9645ff285b1233bfac228ce64e6f8e Mon Sep 17 00:00:00 2001 From: Daemonxiao <735462752@qq.com> Date: Mon, 20 Jun 2022 13:43:05 +0800 Subject: [PATCH 1/4] Fixed a hang bug when TLS is turned off Signed-off-by: Daemonxiao <735462752@qq.com> --- src/main/java/org/tikv/common/util/ChannelFactory.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/tikv/common/util/ChannelFactory.java b/src/main/java/org/tikv/common/util/ChannelFactory.java index 20840af7eb5..77fd2fefc60 100644 --- a/src/main/java/org/tikv/common/util/ChannelFactory.java +++ b/src/main/java/org/tikv/common/util/ChannelFactory.java @@ -64,7 +64,7 @@ public class ChannelFactory implements AutoCloseable { private final AtomicReference sslContextBuilder = new AtomicReference<>(); - private final ScheduledExecutorService recycler = Executors.newSingleThreadScheduledExecutor(); + private final ScheduledExecutorService recycler; private final ReadWriteLock lock = new ReentrantReadWriteLock(); @@ -209,6 +209,7 @@ public ChannelFactory( this.idleTimeout = idleTimeout; this.certWatcher = null; this.certContext = null; + this.recycler = null; this.connRecycleTime = 0; } @@ -229,6 +230,7 @@ public ChannelFactory( this.connRecycleTime = connRecycleTime; this.certContext = new OpenSslContext(trustCertCollectionFilePath, keyCertChainFilePath, keyFilePath); + this.recycler = Executors.newSingleThreadScheduledExecutor(); File trustCert = new File(trustCertCollectionFilePath); File keyCert = new File(keyCertChainFilePath); @@ -261,6 +263,7 @@ public ChannelFactory( this.idleTimeout = idleTimeout; this.connRecycleTime = connRecycleTime; this.certContext = new JksContext(jksKeyPath, jksKeyPassword, jksTrustPath, jksTrustPassword); + this.recycler = Executors.newSingleThreadScheduledExecutor(); File jksKey = new File(jksKeyPath); File jksTrust = new File(jksTrustPath); @@ -361,7 +364,7 @@ public void close() { connPool.clear(); if (certContext != null) { - recycler.shutdown(); + recycler.shutdownNow(); if (certWatcher != null) { certWatcher.close(); } From c1dee04bb59f0a17ec9c5fa68512393be54d27d4 Mon Sep 17 00:00:00 2001 From: Daemonxiao <735462752@qq.com> Date: Mon, 20 Jun 2022 19:43:50 +0800 Subject: [PATCH 2/4] Update ChannelFactory Signed-off-by: Daemonxiao <735462752@qq.com> Co-authored-by: iosmanthus --- .../java/org/tikv/common/util/ChannelFactory.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/tikv/common/util/ChannelFactory.java b/src/main/java/org/tikv/common/util/ChannelFactory.java index 77fd2fefc60..effda5cf467 100644 --- a/src/main/java/org/tikv/common/util/ChannelFactory.java +++ b/src/main/java/org/tikv/common/util/ChannelFactory.java @@ -363,11 +363,12 @@ public void close() { } connPool.clear(); - if (certContext != null) { - recycler.shutdownNow(); - if (certWatcher != null) { - certWatcher.close(); - } + if (recycler != null) { + recycler.shutdown(); + } + + if (certWatcher != null) { + certWatcher.close(); } } } From ba6e092ccc65073f6b6f3ce7ba1d51325c1adb8b Mon Sep 17 00:00:00 2001 From: Daemonxiao <735462752@qq.com> Date: Tue, 21 Jun 2022 17:45:28 +0800 Subject: [PATCH 3/4] Update ChannelFactoryTest Signed-off-by: Daemonxiao <735462752@qq.com> Co-authored-by: iosmanthus --- src/test/java/org/tikv/common/ChannelFactoryTest.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/test/java/org/tikv/common/ChannelFactoryTest.java b/src/test/java/org/tikv/common/ChannelFactoryTest.java index ce071a69653..0add1da77e1 100644 --- a/src/test/java/org/tikv/common/ChannelFactoryTest.java +++ b/src/test/java/org/tikv/common/ChannelFactoryTest.java @@ -55,15 +55,16 @@ public void testCertWatcher() throws InterruptedException { File a = new File(caPath); File b = new File(clientCertPath); File c = new File(clientKeyPath); - new CertWatcher(2, ImmutableList.of(a, b, c), () -> changed.set(true)); - Thread.sleep(5000); - assertTrue(changed.get()); + try(CertWatcher certWatcher = new CertWatcher(2, ImmutableList.of(a, b, c), () -> changed.set(true))){ + Thread.sleep(5000); + assertTrue(changed.get()); + } } @Test public void testCertWatcherWithExceptionTask() throws InterruptedException { AtomicInteger timesOfReloadTask = new AtomicInteger(0); - new CertWatcher( + CertWatcher certWatcher = new CertWatcher( 1, ImmutableList.of(new File(caPath), new File(clientCertPath), new File(clientKeyPath)), () -> { @@ -73,6 +74,7 @@ public void testCertWatcherWithExceptionTask() throws InterruptedException { }); Thread.sleep(5000); + certWatcher.close(); assertTrue(timesOfReloadTask.get() > 1); } From 7742a6d3770b4b1b71c2e446265f6b0234f98681 Mon Sep 17 00:00:00 2001 From: Daemonxiao <735462752@qq.com> Date: Tue, 21 Jun 2022 18:03:00 +0800 Subject: [PATCH 4/4] fix fmt Signed-off-by: Daemonxiao <735462752@qq.com> Co-authored-by: iosmanthus --- .../org/tikv/common/ChannelFactoryTest.java | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/test/java/org/tikv/common/ChannelFactoryTest.java b/src/test/java/org/tikv/common/ChannelFactoryTest.java index 0add1da77e1..19155841598 100644 --- a/src/test/java/org/tikv/common/ChannelFactoryTest.java +++ b/src/test/java/org/tikv/common/ChannelFactoryTest.java @@ -55,7 +55,8 @@ public void testCertWatcher() throws InterruptedException { File a = new File(caPath); File b = new File(clientCertPath); File c = new File(clientKeyPath); - try(CertWatcher certWatcher = new CertWatcher(2, ImmutableList.of(a, b, c), () -> changed.set(true))){ + try (CertWatcher certWatcher = + new CertWatcher(2, ImmutableList.of(a, b, c), () -> changed.set(true))) { Thread.sleep(5000); assertTrue(changed.get()); } @@ -64,14 +65,15 @@ public void testCertWatcher() throws InterruptedException { @Test public void testCertWatcherWithExceptionTask() throws InterruptedException { AtomicInteger timesOfReloadTask = new AtomicInteger(0); - CertWatcher certWatcher = 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"); - }); + CertWatcher certWatcher = + 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); certWatcher.close();