From b665e3ca685548652187b48d18f4bb60819fdf1f Mon Sep 17 00:00:00 2001 From: linghengqian Date: Mon, 18 Sep 2023 20:33:50 +0800 Subject: [PATCH] Update dependencies to avoid CVE --- .../scheduler/fixture/EmbedTestingServer.java | 88 ++++++++++++-- .../src/main/release-docs/LICENSE | 58 ++++----- .../zookeeper/fixture/EmbedTestingServer.java | 104 +++++++++++++--- .../lite/internal/server/ServerService.java | 2 +- .../lite/fixture/EmbedTestingServer.java | 105 +++++++++++++--- .../AbstractEmbedZookeeperBaseTest.java | 93 ++++++++++++-- .../boot/job/fixture/EmbedTestingServer.java | 115 +++++++++++++----- .../EmbedZookeeperTestExecutionListener.java | 110 +++++++++++++---- pom.xml | 105 +++++----------- 9 files changed, 565 insertions(+), 215 deletions(-) diff --git a/elasticjob-cloud/elasticjob-cloud-scheduler/src/test/java/org/apache/shardingsphere/elasticjob/cloud/scheduler/fixture/EmbedTestingServer.java b/elasticjob-cloud/elasticjob-cloud-scheduler/src/test/java/org/apache/shardingsphere/elasticjob/cloud/scheduler/fixture/EmbedTestingServer.java index 2cb9e6bc73..5b15150b46 100755 --- a/elasticjob-cloud/elasticjob-cloud-scheduler/src/test/java/org/apache/shardingsphere/elasticjob/cloud/scheduler/fixture/EmbedTestingServer.java +++ b/elasticjob-cloud/elasticjob-cloud-scheduler/src/test/java/org/apache/shardingsphere/elasticjob/cloud/scheduler/fixture/EmbedTestingServer.java @@ -19,43 +19,111 @@ import lombok.AccessLevel; import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.framework.imps.CuratorFrameworkState; +import org.apache.curator.retry.ExponentialBackoffRetry; import org.apache.curator.test.TestingServer; -import org.apache.shardingsphere.elasticjob.reg.exception.RegExceptionHandler; +import org.apache.zookeeper.KeeperException; -import java.io.File; import java.io.IOException; +import java.util.Collection; +import java.util.concurrent.TimeUnit; @NoArgsConstructor(access = AccessLevel.PRIVATE) +@Slf4j public final class EmbedTestingServer { private static final int PORT = 10181; - + private static volatile TestingServer testingServer; - + + private static final Object INIT_LOCK = new Object(); + /** - * Start the embed server. + * Start embed zookeeper server. */ public static void start() { if (null != testingServer) { + log.info("Embed zookeeper server already exists 1, on {}", testingServer.getConnectString()); return; } + log.info("Starting embed zookeeper server..."); + synchronized (INIT_LOCK) { + if (null != testingServer) { + log.info("Embed zookeeper server already exists 2, on {}", testingServer.getConnectString()); + return; + } + start0(); + waitTestingServerReady(); + } + } + + private static void start0() { try { - testingServer = new TestingServer(PORT, new File(String.format("target/test_zk_data/%s/", System.nanoTime()))); + testingServer = new TestingServer(PORT, true); // CHECKSTYLE:OFF } catch (final Exception ex) { // CHECKSTYLE:ON - RegExceptionHandler.handleException(ex); + if (!isIgnoredException(ex)) { + throw new RuntimeException(ex); + } else { + log.warn("Start embed zookeeper server got exception: {}", ex.getMessage()); + } } finally { Runtime.getRuntime().addShutdownHook(new Thread(() -> { try { testingServer.close(); - } catch (final IOException ex) { - RegExceptionHandler.handleException(ex); + } catch (final IOException ignored) { } + log.info("Close embed zookeeper server done"); })); } } - + + private static void waitTestingServerReady() { + int maxRetries = 60; + try (CuratorFramework client = buildCuratorClient()) { + client.start(); + int round = 0; + while (round < maxRetries) { + try { + if (client.getZookeeperClient().isConnected()) { + log.info("client is connected"); + break; + } + if (client.blockUntilConnected(500, TimeUnit.MILLISECONDS)) { + CuratorFrameworkState state = client.getState(); + Collection childrenKeys = client.getChildren().forPath("/"); + log.info("TestingServer connected, state={}, childrenKeys={}", state, childrenKeys); + break; + } + // CHECKSTYLE:OFF + } catch (final Exception ignored) { + // CHECKSTYLE:ON + } + ++round; + } + } + } + + private static CuratorFramework buildCuratorClient() { + CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder(); + int retryIntervalMilliseconds = 500; + int maxRetries = 3; + builder.connectString(getConnectionString()) + .retryPolicy(new ExponentialBackoffRetry(retryIntervalMilliseconds, maxRetries, retryIntervalMilliseconds * maxRetries)) + .namespace("test"); + builder.sessionTimeoutMs(60 * 1000); + builder.connectionTimeoutMs(500); + return builder.build(); + } + + private static boolean isIgnoredException(final Throwable cause) { + return cause instanceof KeeperException.ConnectionLossException || cause instanceof KeeperException.NoNodeException || cause instanceof KeeperException.NodeExistsException; + } + /** * Get the connection string. * diff --git a/elasticjob-distribution/elasticjob-cloud-scheduler-distribution/src/main/release-docs/LICENSE b/elasticjob-distribution/elasticjob-cloud-scheduler-distribution/src/main/release-docs/LICENSE index e7e7090f64..a32daa0182 100644 --- a/elasticjob-distribution/elasticjob-cloud-scheduler-distribution/src/main/release-docs/LICENSE +++ b/elasticjob-distribution/elasticjob-cloud-scheduler-distribution/src/main/release-docs/LICENSE @@ -216,44 +216,44 @@ The following components are provided under the Apache License. See project link The text of each license is the standard Apache 2.0 license. audience-annotations 0.5.0: https://github.com/apache/yetus, Apache 2.0 - commons-codec 1.10: https://github.com/apache/commons-codec, Apache 2.0 - commons-dbcp2 2.9.0: https://github.com/apache/commons-dbcp, Apache 2.0 + commons-codec 1.16.0: https://github.com/apache/commons-codec, Apache 2.0 + commons-dbcp2 2.11.1: https://github.com/apache/commons-dbcp, Apache 2.0 commons-exec 1.3: http://commons.apache.org/proper/commons-exec, Apache 2.0 commons-lang 2.6: https://github.com/apache/commons-lang, Apache 2.0 commons-lang3 3.4: https://github.com/apache/commons-lang, Apache 2.0 commons-logging 1.2: https://github.com/apache/commons-logging, Apache 2.0 commons-pool2 2.8.1: https://github.com/apache/commons-pool, Apache 2.0 - curator-client 5.1.0: https://github.com/apache/curator, Apache 2.0 - curator-framework 5.1.0: https://github.com/apache/curator, Apache 2.0 - curator-recipes 5.1.0: https://github.com/apache/curator, Apache 2.0 + curator-client 5.5.0: https://github.com/apache/curator, Apache 2.0 + curator-framework 5.5.0: https://github.com/apache/curator, Apache 2.0 + curator-recipes 5.5.0: https://github.com/apache/curator, Apache 2.0 error_prone_annotations 2.3.4: https://github.com/google/error-prone, Apache 2.0 failureaccess 1.0.1:https://github.com/google/guava, Apache 2.0 - fenzo-core 0.11.1: https://github.com/Netflix/Fenzo, Apache 2.0 - gson 2.6.1: https://github.com/google/gson, Apache 2.0 - guava 29.0-jre: https://github.com/google/guava, Apache 2.0 + fenzo-core 1.0.1: https://github.com/Netflix/Fenzo, Apache 2.0 + gson 2.10.1: https://github.com/google/gson, Apache 2.0 + guava 30.0-jre: https://github.com/google/guava, Apache 2.0 HikariCP-java7 2.4.13: https://github.com/brettwooldridge/HikariCP, Apache 2.0 - httpclient 4.5.13: https://github.com/apache/httpcomponents-client, Apache 2.0 - httpcore 4.4.13: https://github.com/apache/httpcomponents-core, Apache 2.0 + httpclient 4.5.14: https://github.com/apache/httpcomponents-client, Apache 2.0 + httpcore 4.4.16: https://github.com/apache/httpcomponents-core, Apache 2.0 jackson-annotations 2.4.0: https://github.com/FasterXML/jackson-annotations, Apache 2.0 jackson-core 2.4.5: https://github.com/FasterXML/jackson-core, Apache 2.0 jackson-databind 2.4.5: https://github.com/FasterXML/jackson-core, Apache 2.0 listenablefuture 9999.0-empty-to-avoid-conflict-with-guava:https://github.com/google/guava, Apache 2.0 log4j 1.2.17: http://logging.apache.org/log4j/1.2/, Apache 2.0 - log4j-over-slf4j 1.7.7: https://github.com/qos-ch/slf4j, Apache 2.0 - mesos 1.1.0: http://mesos.apache.org/, Apache 2.0 - netty-buffer 4.1.45.Final: https://github.com/netty, Apache 2.0 - netty-codec 4.1.45.Final: https://github.com/netty, Apache 2.0 - netty-codec-http 4.1.45.Final: https://github.com/netty, Apache 2.0 - netty-common 4.1.45.Final: https://github.com/netty, Apache 2.0 - netty-handler 4.1.45.Final: https://github.com/netty, Apache 2.0 - netty-resolver 4.1.45.Final: https://github.com/netty, Apache 2.0 - netty-transport 4.1.45.Final: https://github.com/netty, Apache 2.0 - netty-transport-native-epoll 4.1.45.Final: https://github.com/netty, Apache 2.0 - netty-transport-native-unix-common 4.1.45.Final: https://github.com/netty, Apache 2.0 + log4j-over-slf4j 1.7.36: https://github.com/qos-ch/slf4j, Apache 2.0 + mesos 1.11.0: http://mesos.apache.org/, Apache 2.0 + netty-buffer 4.1.97.Final: https://github.com/netty, Apache 2.0 + netty-codec 4.1.97.Final: https://github.com/netty, Apache 2.0 + netty-codec-http 4.1.97.Final: https://github.com/netty, Apache 2.0 + netty-common 4.1.97.Final: https://github.com/netty, Apache 2.0 + netty-handler 4.1.97.Final: https://github.com/netty, Apache 2.0 + netty-resolver 4.1.97.Final: https://github.com/netty, Apache 2.0 + netty-transport 4.1.97.Final: https://github.com/netty, Apache 2.0 + netty-transport-native-epoll 4.1.97.Final: https://github.com/netty, Apache 2.0 + netty-transport-native-unix-common 4.1.97.Final: https://github.com/netty, Apache 2.0 quartz 2.3.2: https://github.com/quartz-scheduler/quartz, Apache 2.0 - snakeyaml 1.26: http://www.snakeyaml.org, Apache 2.0 - zookeeper 3.6.0: https://github.com/apache/zookeeper, Apache 2.0 - zookeeper-jute 3.6.0: https://github.com/apache/zookeeper, Apache 2.0 + snakeyaml 2.0: https://bitbucket.org/snakeyaml/snakeyaml/src, Apache 2.0 + zookeeper 3.9.0: https://github.com/apache/zookeeper, Apache 2.0 + zookeeper-jute 3.9.0: https://github.com/apache/zookeeper, Apache 2.0 ======================================================================== EPL licenses @@ -264,8 +264,8 @@ The text of each license is also included at licenses/LICENSE-[project].txt. jakarta.annotation-api 1.3.5: https://github.com/eclipse-ee4j/common-annotations-api, EPL 2.0 jakarta.el 3.0.3: https://github.com/eclipse-ee4j/el-ri, EPL 2.0 - logback-classic 1.2.3: https://github.com/qos-ch/logback, EPL 1.0 - logback-core 1.2.3: https://github.com/qos-ch/logback, EPL 1.0 + logback-classic 1.2.12: https://github.com/qos-ch/logback, EPL 1.0 + logback-core 1.2.12: https://github.com/qos-ch/logback, EPL 1.0 mchange-commons-java 0.2.15: https://github.com/swaldman/mchange-commons-java/tree/mchange-commons-java-0.2.15, EPL 1.0 ======================================================================== @@ -276,6 +276,6 @@ The following components are provided under the MIT License. See project link fo The text of each license is also included at licenses/LICENSE-[project].txt. checker-qual 2.11.1: https://github.com/typetools/checker-framework, MIT - jcl-over-slf4j 1.7.7: https://github.com/qos-ch/slf4j, MIT - jul-to-slf4j 1.7.7: https://github.com/qos-ch/slf4j, MIT - slf4j-api 1.7.7: https://github.com/qos-ch/slf4j, MIT + jcl-over-slf4j 1.7.36: https://github.com/qos-ch/slf4j, MIT + jul-to-slf4j 1.7.36: https://github.com/qos-ch/slf4j, MIT + slf4j-api 1.7.36: https://github.com/qos-ch/slf4j, MIT diff --git a/elasticjob-infra/elasticjob-registry-center/elasticjob-regitry-center-provider/elasticjob-registry-center-zookeeper-curator/src/test/java/org/apache/shardingsphere/elasticjob/reg/zookeeper/fixture/EmbedTestingServer.java b/elasticjob-infra/elasticjob-registry-center/elasticjob-regitry-center-provider/elasticjob-registry-center-zookeeper-curator/src/test/java/org/apache/shardingsphere/elasticjob/reg/zookeeper/fixture/EmbedTestingServer.java index b8349344b0..fac6876979 100644 --- a/elasticjob-infra/elasticjob-registry-center/elasticjob-regitry-center-provider/elasticjob-registry-center-zookeeper-curator/src/test/java/org/apache/shardingsphere/elasticjob/reg/zookeeper/fixture/EmbedTestingServer.java +++ b/elasticjob-infra/elasticjob-registry-center/elasticjob-regitry-center-provider/elasticjob-registry-center-zookeeper-curator/src/test/java/org/apache/shardingsphere/elasticjob/reg/zookeeper/fixture/EmbedTestingServer.java @@ -19,50 +19,118 @@ import lombok.AccessLevel; import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.framework.imps.CuratorFrameworkState; +import org.apache.curator.retry.ExponentialBackoffRetry; import org.apache.curator.test.TestingServer; -import org.apache.shardingsphere.elasticjob.reg.exception.RegExceptionHandler; +import org.apache.zookeeper.KeeperException; -import java.io.File; import java.io.IOException; +import java.util.Collection; +import java.util.concurrent.TimeUnit; +@Slf4j @NoArgsConstructor(access = AccessLevel.PRIVATE) public final class EmbedTestingServer { private static final int PORT = 9181; - + private static volatile TestingServer testingServer; - - /** - * Get the connection string. - * - * @return connection string - */ - public static String getConnectionString() { - return "localhost:" + PORT; - } - + + private static final Object INIT_LOCK = new Object(); + /** - * Start the server. + * Start embed zookeeper server. */ public static void start() { if (null != testingServer) { + log.info("Embed zookeeper server already exists 1, on {}", testingServer.getConnectString()); return; } + log.info("Starting embed zookeeper server..."); + synchronized (INIT_LOCK) { + if (null != testingServer) { + log.info("Embed zookeeper server already exists 2, on {}", testingServer.getConnectString()); + return; + } + start0(); + waitTestingServerReady(); + } + } + + private static void start0() { try { - testingServer = new TestingServer(PORT, new File(String.format("target/test_zk_data/%s/", System.nanoTime()))); + testingServer = new TestingServer(PORT, true); // CHECKSTYLE:OFF } catch (final Exception ex) { // CHECKSTYLE:ON - RegExceptionHandler.handleException(ex); + if (!isIgnoredException(ex)) { + throw new RuntimeException(ex); + } else { + log.warn("Start embed zookeeper server got exception: {}", ex.getMessage()); + } } finally { Runtime.getRuntime().addShutdownHook(new Thread(() -> { try { testingServer.close(); - } catch (final IOException ex) { - RegExceptionHandler.handleException(ex); + } catch (final IOException ignored) { } + log.info("Close embed zookeeper server done"); })); } } + + private static void waitTestingServerReady() { + int maxRetries = 60; + try (CuratorFramework client = buildCuratorClient()) { + client.start(); + int round = 0; + while (round < maxRetries) { + try { + if (client.getZookeeperClient().isConnected()) { + log.info("client is connected"); + break; + } + if (client.blockUntilConnected(500, TimeUnit.MILLISECONDS)) { + CuratorFrameworkState state = client.getState(); + Collection childrenKeys = client.getChildren().forPath("/"); + log.info("TestingServer connected, state={}, childrenKeys={}", state, childrenKeys); + break; + } + // CHECKSTYLE:OFF + } catch (final Exception ignored) { + // CHECKSTYLE:ON + } + ++round; + } + } + } + + private static CuratorFramework buildCuratorClient() { + CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder(); + int retryIntervalMilliseconds = 500; + int maxRetries = 3; + builder.connectString(getConnectionString()) + .retryPolicy(new ExponentialBackoffRetry(retryIntervalMilliseconds, maxRetries, retryIntervalMilliseconds * maxRetries)) + .namespace("test"); + builder.sessionTimeoutMs(60 * 1000); + builder.connectionTimeoutMs(500); + return builder.build(); + } + + private static boolean isIgnoredException(final Throwable cause) { + return cause instanceof KeeperException.ConnectionLossException || cause instanceof KeeperException.NoNodeException || cause instanceof KeeperException.NodeExistsException; + } + + /** + * Get the connection string. + * + * @return connection string + */ + public static String getConnectionString() { + return "localhost:" + PORT; + } } diff --git a/elasticjob-lite/elasticjob-lite-core/src/main/java/org/apache/shardingsphere/elasticjob/lite/internal/server/ServerService.java b/elasticjob-lite/elasticjob-lite-core/src/main/java/org/apache/shardingsphere/elasticjob/lite/internal/server/ServerService.java index 011e4c17c8..f9dd30fbb3 100644 --- a/elasticjob-lite/elasticjob-lite-core/src/main/java/org/apache/shardingsphere/elasticjob/lite/internal/server/ServerService.java +++ b/elasticjob-lite/elasticjob-lite-core/src/main/java/org/apache/shardingsphere/elasticjob/lite/internal/server/ServerService.java @@ -18,7 +18,7 @@ package org.apache.shardingsphere.elasticjob.lite.internal.server; import com.google.common.base.Strings; -import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang3.StringUtils; import org.apache.shardingsphere.elasticjob.lite.internal.instance.InstanceNode; import org.apache.shardingsphere.elasticjob.lite.internal.schedule.JobRegistry; import org.apache.shardingsphere.elasticjob.lite.internal.storage.JobNodeStorage; diff --git a/elasticjob-lite/elasticjob-lite-core/src/test/java/org/apache/shardingsphere/elasticjob/lite/fixture/EmbedTestingServer.java b/elasticjob-lite/elasticjob-lite-core/src/test/java/org/apache/shardingsphere/elasticjob/lite/fixture/EmbedTestingServer.java index 838d2ec3df..7e4d6613be 100644 --- a/elasticjob-lite/elasticjob-lite-core/src/test/java/org/apache/shardingsphere/elasticjob/lite/fixture/EmbedTestingServer.java +++ b/elasticjob-lite/elasticjob-lite-core/src/test/java/org/apache/shardingsphere/elasticjob/lite/fixture/EmbedTestingServer.java @@ -19,50 +19,117 @@ import lombok.AccessLevel; import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.framework.imps.CuratorFrameworkState; +import org.apache.curator.retry.ExponentialBackoffRetry; import org.apache.curator.test.TestingServer; -import org.apache.shardingsphere.elasticjob.reg.exception.RegExceptionHandler; +import org.apache.zookeeper.KeeperException; -import java.io.File; import java.io.IOException; +import java.util.Collection; +import java.util.concurrent.TimeUnit; @NoArgsConstructor(access = AccessLevel.PRIVATE) +@Slf4j public final class EmbedTestingServer { private static final int PORT = 7181; - + private static volatile TestingServer testingServer; - - /** - * Get the connection string. - * - * @return connection string - */ - public static String getConnectionString() { - return "localhost:" + PORT; - } - + + private static final Object INIT_LOCK = new Object(); + /** - * Start the server. + * Start embed zookeeper server. */ public static void start() { if (null != testingServer) { + log.info("Embed zookeeper server already exists 1, on {}", testingServer.getConnectString()); return; } + log.info("Starting embed zookeeper server..."); + synchronized (INIT_LOCK) { + if (null != testingServer) { + log.info("Embed zookeeper server already exists 2, on {}", testingServer.getConnectString()); + return; + } + start0(); + waitTestingServerReady(); + } + } + + private static void start0() { try { - testingServer = new TestingServer(PORT, new File(String.format("target/test_zk_data/%s/", System.nanoTime()))); + testingServer = new TestingServer(PORT, true); // CHECKSTYLE:OFF } catch (final Exception ex) { // CHECKSTYLE:ON - RegExceptionHandler.handleException(ex); + if (!isIgnoredException(ex)) { + throw new RuntimeException(ex); + } else { + log.warn("Start embed zookeeper server got exception: {}", ex.getMessage()); + } } finally { Runtime.getRuntime().addShutdownHook(new Thread(() -> { try { testingServer.close(); - } catch (final IOException ex) { - RegExceptionHandler.handleException(ex); + } catch (final IOException ignored) { } + log.info("Close embed zookeeper server done"); })); } } -} + private static void waitTestingServerReady() { + int maxRetries = 60; + try (CuratorFramework client = buildCuratorClient()) { + client.start(); + int round = 0; + while (round < maxRetries) { + try { + if (client.getZookeeperClient().isConnected()) { + log.info("client is connected"); + break; + } + if (client.blockUntilConnected(500, TimeUnit.MILLISECONDS)) { + CuratorFrameworkState state = client.getState(); + Collection childrenKeys = client.getChildren().forPath("/"); + log.info("TestingServer connected, state={}, childrenKeys={}", state, childrenKeys); + break; + } + // CHECKSTYLE:OFF + } catch (final Exception ignored) { + // CHECKSTYLE:ON + } + ++round; + } + } + } + + private static CuratorFramework buildCuratorClient() { + CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder(); + int retryIntervalMilliseconds = 500; + int maxRetries = 3; + builder.connectString(getConnectionString()) + .retryPolicy(new ExponentialBackoffRetry(retryIntervalMilliseconds, maxRetries, retryIntervalMilliseconds * maxRetries)) + .namespace("test"); + builder.sessionTimeoutMs(60 * 1000); + builder.connectionTimeoutMs(500); + return builder.build(); + } + + private static boolean isIgnoredException(final Throwable cause) { + return cause instanceof KeeperException.ConnectionLossException || cause instanceof KeeperException.NoNodeException || cause instanceof KeeperException.NodeExistsException; + } + + /** + * Get the connection string. + * + * @return connection string + */ + public static String getConnectionString() { + return "localhost:" + PORT; + } +} diff --git a/elasticjob-lite/elasticjob-lite-lifecycle/src/test/java/org/apache/shardingsphere/elasticjob/lite/lifecycle/AbstractEmbedZookeeperBaseTest.java b/elasticjob-lite/elasticjob-lite-lifecycle/src/test/java/org/apache/shardingsphere/elasticjob/lite/lifecycle/AbstractEmbedZookeeperBaseTest.java index cf5b37263c..150c851edb 100644 --- a/elasticjob-lite/elasticjob-lite-lifecycle/src/test/java/org/apache/shardingsphere/elasticjob/lite/lifecycle/AbstractEmbedZookeeperBaseTest.java +++ b/elasticjob-lite/elasticjob-lite-lifecycle/src/test/java/org/apache/shardingsphere/elasticjob/lite/lifecycle/AbstractEmbedZookeeperBaseTest.java @@ -17,45 +17,116 @@ package org.apache.shardingsphere.elasticjob.lite.lifecycle; +import lombok.extern.slf4j.Slf4j; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.framework.imps.CuratorFrameworkState; +import org.apache.curator.retry.ExponentialBackoffRetry; import org.apache.curator.test.TestingServer; -import org.apache.shardingsphere.elasticjob.reg.exception.RegExceptionHandler; +import org.apache.zookeeper.KeeperException; import org.junit.jupiter.api.BeforeAll; -import java.io.File; import java.io.IOException; +import java.util.Collection; +import java.util.concurrent.TimeUnit; +@Slf4j public abstract class AbstractEmbedZookeeperBaseTest { - + private static final int PORT = 8181; - + private static volatile TestingServer testingServer; - + + private static final Object INIT_LOCK = new Object(); + @BeforeAll public static void setUp() { startEmbedTestingServer(); } - + + /** + * Start embed zookeeper server. + */ private static void startEmbedTestingServer() { if (null != testingServer) { + log.info("Embed zookeeper server already exists 1, on {}", testingServer.getConnectString()); return; } + log.info("Starting embed zookeeper server..."); + synchronized (INIT_LOCK) { + if (null != testingServer) { + log.info("Embed zookeeper server already exists 2, on {}", testingServer.getConnectString()); + return; + } + start0(); + waitTestingServerReady(); + } + } + + private static void start0() { try { - testingServer = new TestingServer(PORT, new File(String.format("target/test_zk_data/%s/", System.nanoTime()))); + testingServer = new TestingServer(PORT, true); // CHECKSTYLE:OFF } catch (final Exception ex) { // CHECKSTYLE:ON - RegExceptionHandler.handleException(ex); + if (!isIgnoredException(ex)) { + throw new RuntimeException(ex); + } else { + log.warn("Start embed zookeeper server got exception: {}", ex.getMessage()); + } } finally { Runtime.getRuntime().addShutdownHook(new Thread(() -> { try { testingServer.close(); - } catch (final IOException ex) { - RegExceptionHandler.handleException(ex); + } catch (final IOException ignored) { } + log.info("Close embed zookeeper server done"); })); } } - + + private static void waitTestingServerReady() { + int maxRetries = 60; + try (CuratorFramework client = buildCuratorClient()) { + client.start(); + int round = 0; + while (round < maxRetries) { + try { + if (client.getZookeeperClient().isConnected()) { + log.info("client is connected"); + break; + } + if (client.blockUntilConnected(500, TimeUnit.MILLISECONDS)) { + CuratorFrameworkState state = client.getState(); + Collection childrenKeys = client.getChildren().forPath("/"); + log.info("TestingServer connected, state={}, childrenKeys={}", state, childrenKeys); + break; + } + // CHECKSTYLE:OFF + } catch (final Exception ignored) { + // CHECKSTYLE:ON + } + ++round; + } + } + } + + private static CuratorFramework buildCuratorClient() { + CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder(); + int retryIntervalMilliseconds = 500; + int maxRetries = 3; + builder.connectString(getConnectionString()) + .retryPolicy(new ExponentialBackoffRetry(retryIntervalMilliseconds, maxRetries, retryIntervalMilliseconds * maxRetries)) + .namespace("test"); + builder.sessionTimeoutMs(60 * 1000); + builder.connectionTimeoutMs(500); + return builder.build(); + } + + private static boolean isIgnoredException(final Throwable cause) { + return cause instanceof KeeperException.ConnectionLossException || cause instanceof KeeperException.NoNodeException || cause instanceof KeeperException.NodeExistsException; + } + /** * Get the connection string. * diff --git a/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/boot/job/fixture/EmbedTestingServer.java b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/boot/job/fixture/EmbedTestingServer.java index c1f4e16e16..8c862435a1 100644 --- a/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/boot/job/fixture/EmbedTestingServer.java +++ b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/boot/job/fixture/EmbedTestingServer.java @@ -19,68 +19,117 @@ import lombok.AccessLevel; import lombok.NoArgsConstructor; -import org.apache.curator.CuratorZookeeperClient; +import lombok.extern.slf4j.Slf4j; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.framework.imps.CuratorFrameworkState; import org.apache.curator.retry.ExponentialBackoffRetry; import org.apache.curator.test.TestingServer; -import org.apache.shardingsphere.elasticjob.reg.exception.RegExceptionHandler; -import org.awaitility.Awaitility; -import org.hamcrest.Matchers; +import org.apache.zookeeper.KeeperException; import java.io.IOException; +import java.util.Collection; import java.util.concurrent.TimeUnit; -import static org.hamcrest.MatcherAssert.assertThat; - @NoArgsConstructor(access = AccessLevel.PRIVATE) +@Slf4j public final class EmbedTestingServer { - + private static final int PORT = 18181; - + private static volatile TestingServer testingServer; - - /** - * Get the connection string. - * - * @return connection string - */ - public static String getConnectionString() { - return "localhost:" + PORT; - } - + + private static final Object INIT_LOCK = new Object(); + /** - * Start the server. + * Start embed zookeeper server. */ public static void start() { if (null != testingServer) { + log.info("Embed zookeeper server already exists 1, on {}", testingServer.getConnectString()); return; } + log.info("Starting embed zookeeper server..."); + synchronized (INIT_LOCK) { + if (null != testingServer) { + log.info("Embed zookeeper server already exists 2, on {}", testingServer.getConnectString()); + return; + } + start0(); + waitTestingServerReady(); + } + } + + private static void start0() { try { testingServer = new TestingServer(PORT, true); // CHECKSTYLE:OFF } catch (final Exception ex) { // CHECKSTYLE:ON - RegExceptionHandler.handleException(ex); + if (!isIgnoredException(ex)) { + throw new RuntimeException(ex); + } else { + log.warn("Start embed zookeeper server got exception: {}", ex.getMessage()); + } } finally { Runtime.getRuntime().addShutdownHook(new Thread(() -> { try { testingServer.close(); - } catch (final IOException ex) { - RegExceptionHandler.handleException(ex); + } catch (final IOException ignored) { } + log.info("Close embed zookeeper server done"); })); } - try (CuratorZookeeperClient client = new CuratorZookeeperClient(getConnectionString(), - 60 * 1000, 500, null, - new ExponentialBackoffRetry(500, 3, 500 * 3))) { + } + + private static void waitTestingServerReady() { + int maxRetries = 60; + try (CuratorFramework client = buildCuratorClient()) { client.start(); - Awaitility.await() - .atLeast(100L, TimeUnit.MILLISECONDS) - .atMost(500 * 60L, TimeUnit.MILLISECONDS) - .untilAsserted(() -> assertThat(client.isConnected(), Matchers.is(true))); - // CHECKSTYLE:OFF - } catch (Exception e) { - // CHECKSTYLE:ON - throw new RuntimeException(e); + int round = 0; + while (round < maxRetries) { + try { + if (client.getZookeeperClient().isConnected()) { + log.info("client is connected"); + break; + } + if (client.blockUntilConnected(500, TimeUnit.MILLISECONDS)) { + CuratorFrameworkState state = client.getState(); + Collection childrenKeys = client.getChildren().forPath("/"); + log.info("TestingServer connected, state={}, childrenKeys={}", state, childrenKeys); + break; + } + // CHECKSTYLE:OFF + } catch (final Exception ignored) { + // CHECKSTYLE:ON + } + ++round; + } } } + + private static CuratorFramework buildCuratorClient() { + CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder(); + int retryIntervalMilliseconds = 500; + int maxRetries = 3; + builder.connectString(getConnectionString()) + .retryPolicy(new ExponentialBackoffRetry(retryIntervalMilliseconds, maxRetries, retryIntervalMilliseconds * maxRetries)) + .namespace("test"); + builder.sessionTimeoutMs(60 * 1000); + builder.connectionTimeoutMs(500); + return builder.build(); + } + + private static boolean isIgnoredException(final Throwable cause) { + return cause instanceof KeeperException.ConnectionLossException || cause instanceof KeeperException.NoNodeException || cause instanceof KeeperException.NodeExistsException; + } + + /** + * Get the connection string. + * + * @return connection string + */ + public static String getConnectionString() { + return "localhost:" + PORT; + } } diff --git a/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-namespace/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/namespace/test/EmbedZookeeperTestExecutionListener.java b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-namespace/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/namespace/test/EmbedZookeeperTestExecutionListener.java index 1e8ff5988c..9b29da7d11 100644 --- a/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-namespace/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/namespace/test/EmbedZookeeperTestExecutionListener.java +++ b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-namespace/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/namespace/test/EmbedZookeeperTestExecutionListener.java @@ -17,60 +17,126 @@ package org.apache.shardingsphere.elasticjob.lite.spring.namespace.test; -import org.apache.curator.CuratorZookeeperClient; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.framework.imps.CuratorFrameworkState; import org.apache.curator.retry.ExponentialBackoffRetry; import org.apache.curator.test.TestingServer; -import org.apache.shardingsphere.elasticjob.reg.exception.RegExceptionHandler; -import org.awaitility.Awaitility; -import org.hamcrest.Matchers; +import org.apache.zookeeper.KeeperException; import org.springframework.test.context.TestContext; import org.springframework.test.context.support.AbstractTestExecutionListener; import java.io.IOException; +import java.util.Collection; import java.util.concurrent.TimeUnit; -import static org.hamcrest.MatcherAssert.assertThat; - +@NoArgsConstructor(access = AccessLevel.PRIVATE) +@Slf4j public final class EmbedZookeeperTestExecutionListener extends AbstractTestExecutionListener { - + + private static final int PORT = 3181; + private static volatile TestingServer testingServer; + + private static final Object INIT_LOCK = new Object(); @Override public void beforeTestClass(final TestContext testContext) { startEmbedTestingServer(); } + /** + * Start embed zookeeper server. + */ private static void startEmbedTestingServer() { if (null != testingServer) { + log.info("Embed zookeeper server already exists 1, on {}", testingServer.getConnectString()); return; } + log.info("Starting embed zookeeper server..."); + synchronized (INIT_LOCK) { + if (null != testingServer) { + log.info("Embed zookeeper server already exists 2, on {}", testingServer.getConnectString()); + return; + } + start0(); + waitTestingServerReady(); + } + } + + private static void start0() { try { - testingServer = new TestingServer(3181, true); + testingServer = new TestingServer(PORT, true); // CHECKSTYLE:OFF } catch (final Exception ex) { // CHECKSTYLE:ON - RegExceptionHandler.handleException(ex); + if (!isIgnoredException(ex)) { + throw new RuntimeException(ex); + } else { + log.warn("Start embed zookeeper server got exception: {}", ex.getMessage()); + } } finally { Runtime.getRuntime().addShutdownHook(new Thread(() -> { try { testingServer.close(); - } catch (final IOException ex) { - RegExceptionHandler.handleException(ex); + } catch (final IOException ignored) { } + log.info("Close embed zookeeper server done"); })); } - try (CuratorZookeeperClient client = new CuratorZookeeperClient(testingServer.getConnectString(), - 60 * 1000, 500, null, - new ExponentialBackoffRetry(500, 3, 500 * 3))) { + } + + private static void waitTestingServerReady() { + int maxRetries = 60; + try (CuratorFramework client = buildCuratorClient()) { client.start(); - Awaitility.await() - .atLeast(100L, TimeUnit.MILLISECONDS) - .atMost(500 * 60L, TimeUnit.MILLISECONDS) - .untilAsserted(() -> assertThat(client.isConnected(), Matchers.is(true))); - // CHECKSTYLE:OFF - } catch (Exception e) { - // CHECKSTYLE:ON - throw new RuntimeException(e); + int round = 0; + while (round < maxRetries) { + try { + if (client.getZookeeperClient().isConnected()) { + log.info("client is connected"); + break; + } + if (client.blockUntilConnected(500, TimeUnit.MILLISECONDS)) { + CuratorFrameworkState state = client.getState(); + Collection childrenKeys = client.getChildren().forPath("/"); + log.info("TestingServer connected, state={}, childrenKeys={}", state, childrenKeys); + break; + } + // CHECKSTYLE:OFF + } catch (final Exception ignored) { + // CHECKSTYLE:ON + } + ++round; + } } } + + private static CuratorFramework buildCuratorClient() { + CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder(); + int retryIntervalMilliseconds = 500; + int maxRetries = 3; + builder.connectString(getConnectionString()) + .retryPolicy(new ExponentialBackoffRetry(retryIntervalMilliseconds, maxRetries, retryIntervalMilliseconds * maxRetries)) + .namespace("test"); + builder.sessionTimeoutMs(60 * 1000); + builder.connectionTimeoutMs(500); + return builder.build(); + } + + private static boolean isIgnoredException(final Throwable cause) { + return cause instanceof KeeperException.ConnectionLossException || cause instanceof KeeperException.NoNodeException || cause instanceof KeeperException.NodeExistsException; + } + + /** + * Get the connection string. + * + * @return connection string + */ + public static String getConnectionString() { + return "localhost:" + PORT; + } } diff --git a/pom.xml b/pom.xml index 59f63a4a76..514053b7f0 100644 --- a/pom.xml +++ b/pom.xml @@ -46,26 +46,27 @@ UTF-8 zh_CN - 29.0-jre + 30.0-jre 3.4 2.3.2 - 5.1.0 + 3.9.0 + 5.5.0 1.18.24 1.9.1 - 1.7.7 - 1.2.3 - 1.10 + 1.7.36 + 1.2.12 + 1.16.0 1.3 - 4.5.13 - 4.4.13 + 4.5.14 + 4.4.16 2.0 - 2.6.1 - 4.1.59.Final - 1.1.0 - 0.11.1 - - 2.9.0 - 2.8.1 + 2.10.1 + 4.1.97.Final + 1.11.0 + 1.0.1 + + 2.10.0 + 2.11.1 3.4.2 1.6.0 @@ -227,46 +228,6 @@ pom import - - io.netty - netty-buffer - ${netty.version} - - - io.netty - netty-codec - ${netty.version} - - - io.netty - netty-common - ${netty.version} - - - io.netty - netty-handler - ${netty.version} - - - io.netty - netty-resolver - ${netty.version} - - - io.netty - netty-transport - ${netty.version} - - - io.netty - netty-transport-native-epoll - ${netty.version} - - - io.netty - netty-transport-native-unix-common - ${netty.version} - org.apache.mesos @@ -318,24 +279,6 @@ ${hamcrest.version} test - - org.mockito - mockito-core - ${mockito.version} - test - - - org.mockito - mockito-inline - ${mockito.version} - test - - - org.mockito - mockito-junit-jupiter - ${mockito.version} - test - org.aspectj aspectjweaver @@ -355,6 +298,24 @@ pom import + + org.mockito + mockito-bom + ${mockito.version} + pom + import + + + org.mockito + mockito-inline + ${mockito.version} + test + + + org.apache.zookeeper + zookeeper + ${zookeeper.version} +