This is an automated email from the ASF dual-hosted git repository.

albumenj pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.0 by this push:
     new 98c8602  perf: Optimize the code on global zookeeper (#9302)
98c8602 is described below

commit 98c86024160ea31fd450cb86fefc4a7da9208420
Author: Pin Xiong <[email protected]>
AuthorDate: Sun Nov 28 16:39:04 2021 +0800

    perf: Optimize the code on global zookeeper (#9302)
    
    1. Avoid compiling pattern every time
    2. Avoid port already in use
    3. Output all configuration information of global zookeeper instances
---
 .../initializer/ConfigZookeeperInitializer.java    | 38 ++++++++++++++++++----
 .../processor/StartZookeeperUnixProcessor.java     | 20 +++++++-----
 .../processor/StopZookeeperUnixProcessor.java      |  7 +++-
 .../processor/ZookeeperUnixProcessor.java          |  5 ++-
 4 files changed, 53 insertions(+), 17 deletions(-)

diff --git 
a/dubbo-test/dubbo-test-check/src/main/java/org/apache/dubbo/test/check/registrycenter/initializer/ConfigZookeeperInitializer.java
 
b/dubbo-test/dubbo-test-check/src/main/java/org/apache/dubbo/test/check/registrycenter/initializer/ConfigZookeeperInitializer.java
index c908649..cc50bb9 100644
--- 
a/dubbo-test/dubbo-test-check/src/main/java/org/apache/dubbo/test/check/registrycenter/initializer/ConfigZookeeperInitializer.java
+++ 
b/dubbo-test/dubbo-test-check/src/main/java/org/apache/dubbo/test/check/registrycenter/initializer/ConfigZookeeperInitializer.java
@@ -18,6 +18,7 @@ package 
org.apache.dubbo.test.check.registrycenter.initializer;
 
 import org.apache.dubbo.common.logger.Logger;
 import org.apache.dubbo.common.logger.LoggerFactory;
+import org.apache.dubbo.common.utils.NetUtils;
 import org.apache.dubbo.test.check.exception.DubboTestException;
 import org.apache.dubbo.test.check.registrycenter.context.ZookeeperContext;
 
@@ -46,16 +47,16 @@ public class ConfigZookeeperInitializer extends 
ZookeeperInitializer {
      */
     private void updateConfig(ZookeeperContext context, int clientPort, int 
adminServerPort) throws DubboTestException {
         Path zookeeperConf = 
Paths.get(context.getSourceFile().getParent().toString(),
-            String.valueOf(clientPort),
-            context.getUnpackedDirectory(),
-            "conf");
+                String.valueOf(clientPort),
+                context.getUnpackedDirectory(),
+                "conf");
         File zooSample = Paths.get(zookeeperConf.toString(), 
"zoo_sample.cfg").toFile();
-
+        int availableAdminServerPort = 
NetUtils.getAvailablePort(adminServerPort);
         Properties properties = new Properties();
         try {
             properties.load(new FileInputStream(zooSample));
             properties.setProperty("clientPort", String.valueOf(clientPort));
-            properties.setProperty("admin.serverPort", 
String.valueOf(adminServerPort));
+            properties.setProperty("admin.serverPort", 
String.valueOf(availableAdminServerPort));
             Path dataDir = Paths.get(zookeeperConf.getParent().toString(), 
"data");
             if (!Files.exists(dataDir)) {
                 try {
@@ -77,8 +78,11 @@ public class ConfigZookeeperInitializer extends 
ZookeeperInitializer {
                     throw new DubboTestException("Failed to close file", e);
                 }
             }
+            logger.info("The configuration information of zoo.cfg are as 
below,\n" +
+                    "which located in " + zooSample.getAbsolutePath() + "\n" +
+                    propertiesToString(properties));
         } catch (IOException e) {
-            throw new DubboTestException(String.format("Failed to update %s 
file", zooSample.toString()), e);
+            throw new DubboTestException(String.format("Failed to update %s 
file", zooSample), e);
         }
 
         File log4j = Paths.get(zookeeperConf.toString(), 
"log4j.properties").toFile();
@@ -105,9 +109,29 @@ public class ConfigZookeeperInitializer extends 
ZookeeperInitializer {
                     throw new DubboTestException("Failed to close file", e);
                 }
             }
+            logger.info("The configuration information of log4j.properties are 
as below,\n" +
+                    "which located in " + log4j.getAbsolutePath() + "\n" +
+                    propertiesToString(properties));
         } catch (IOException e) {
-            throw new DubboTestException(String.format("Failed to update %s 
file", zooSample.toString()), e);
+            throw new DubboTestException(String.format("Failed to update %s 
file", zooSample), e);
+        }
+    }
+
+    /**
+     * Convert the {@link Properties} instance to {@link String}.
+     *
+     * @param properties the properties to convert.
+     * @return the string converted from {@link Properties} instance.
+     */
+    private String propertiesToString(Properties properties) {
+        StringBuilder builder = new StringBuilder();
+        for (Object key : properties.keySet()) {
+            builder.append(key);
+            builder.append(": ");
+            builder.append(properties.get(key));
+            builder.append("\n");
         }
+        return builder.toString();
     }
 
     @Override
diff --git 
a/dubbo-test/dubbo-test-check/src/main/java/org/apache/dubbo/test/check/registrycenter/processor/StartZookeeperUnixProcessor.java
 
b/dubbo-test/dubbo-test-check/src/main/java/org/apache/dubbo/test/check/registrycenter/processor/StartZookeeperUnixProcessor.java
index 3e14af2..3cb4587 100644
--- 
a/dubbo-test/dubbo-test-check/src/main/java/org/apache/dubbo/test/check/registrycenter/processor/StartZookeeperUnixProcessor.java
+++ 
b/dubbo-test/dubbo-test-check/src/main/java/org/apache/dubbo/test/check/registrycenter/processor/StartZookeeperUnixProcessor.java
@@ -34,24 +34,28 @@ import java.util.regex.Pattern;
 public class StartZookeeperUnixProcessor extends ZookeeperUnixProcessor {
 
     private static final Logger logger = 
LoggerFactory.getLogger(StartZookeeperUnixProcessor.class);
+    /**
+     * The pattern for checking if zookeeper instances started.
+     */
+    private static final Pattern PATTERN_STARTED = 
Pattern.compile(".*STARTED.*");
 
     @Override
     protected Process doProcess(ZookeeperContext context, int clientPort) 
throws DubboTestException {
         logger.info(String.format("The zookeeper-%d is starting...", 
clientPort));
         List<String> commands = new ArrayList<>();
         Path zookeeperBin = 
Paths.get(context.getSourceFile().getParent().toString(),
-            String.valueOf(clientPort),
-            context.getUnpackedDirectory(),
-            "bin");
+                String.valueOf(clientPort),
+                context.getUnpackedDirectory(),
+                "bin");
         commands.add(Paths.get(zookeeperBin.toString(), "zkServer.sh")
-            .toAbsolutePath().toString());
+                .toAbsolutePath().toString());
         commands.add("start");
         commands.add(Paths.get(zookeeperBin.getParent().toString(),
-            "conf",
-            "zoo.cfg").toAbsolutePath().toString());
+                "conf",
+                "zoo.cfg").toAbsolutePath().toString());
         try {
             return new 
ProcessBuilder().directory(zookeeperBin.getParent().toFile())
-                
.command(commands).inheritIO().redirectOutput(ProcessBuilder.Redirect.PIPE).start();
+                    
.command(commands).inheritIO().redirectOutput(ProcessBuilder.Redirect.PIPE).start();
         } catch (IOException e) {
             throw new DubboTestException(String.format("Failed to start 
zookeeper-%d", clientPort), e);
         }
@@ -59,6 +63,6 @@ public class StartZookeeperUnixProcessor extends 
ZookeeperUnixProcessor {
 
     @Override
     protected Pattern getPattern() {
-        return Pattern.compile(".*STARTED.*");
+        return PATTERN_STARTED;
     }
 }
diff --git 
a/dubbo-test/dubbo-test-check/src/main/java/org/apache/dubbo/test/check/registrycenter/processor/StopZookeeperUnixProcessor.java
 
b/dubbo-test/dubbo-test-check/src/main/java/org/apache/dubbo/test/check/registrycenter/processor/StopZookeeperUnixProcessor.java
index 9b26036..452fbed 100644
--- 
a/dubbo-test/dubbo-test-check/src/main/java/org/apache/dubbo/test/check/registrycenter/processor/StopZookeeperUnixProcessor.java
+++ 
b/dubbo-test/dubbo-test-check/src/main/java/org/apache/dubbo/test/check/registrycenter/processor/StopZookeeperUnixProcessor.java
@@ -35,6 +35,11 @@ public class StopZookeeperUnixProcessor extends 
ZookeeperUnixProcessor {
 
     private static final Logger logger = 
LoggerFactory.getLogger(StopZookeeperUnixProcessor.class);
 
+    /**
+     * The pattern for checking if the zookeeper instance stopped.
+     */
+    private static final Pattern PATTERN_STOPPED = 
Pattern.compile(".*STOPPED.*");
+
     @Override
     protected Process doProcess(ZookeeperContext context, int clientPort) 
throws DubboTestException {
         logger.info(String.format("The zookeeper-%d is stopping...", 
clientPort));
@@ -56,6 +61,6 @@ public class StopZookeeperUnixProcessor extends 
ZookeeperUnixProcessor {
 
     @Override
     protected Pattern getPattern() {
-        return Pattern.compile(".*STOPPED.*");
+        return PATTERN_STOPPED;
     }
 }
diff --git 
a/dubbo-test/dubbo-test-check/src/main/java/org/apache/dubbo/test/check/registrycenter/processor/ZookeeperUnixProcessor.java
 
b/dubbo-test/dubbo-test-check/src/main/java/org/apache/dubbo/test/check/registrycenter/processor/ZookeeperUnixProcessor.java
index 0f3b5ac..c260031 100644
--- 
a/dubbo-test/dubbo-test-check/src/main/java/org/apache/dubbo/test/check/registrycenter/processor/ZookeeperUnixProcessor.java
+++ 
b/dubbo-test/dubbo-test-check/src/main/java/org/apache/dubbo/test/check/registrycenter/processor/ZookeeperUnixProcessor.java
@@ -79,8 +79,9 @@ public abstract class ZookeeperUnixProcessor implements 
Processor {
         try (final BufferedReader reader = new BufferedReader(new 
InputStreamReader(inputStream))) {
             String line;
             while ((line = reader.readLine()) != null) {
-                if (this.getPattern().matcher(line).matches())
+                if (this.getPattern().matcher(line).matches()) {
                     return;
+                }
                 log.append('\n').append(line);
             }
         } catch (IOException e) {
@@ -101,6 +102,8 @@ public abstract class ZookeeperUnixProcessor implements 
Processor {
 
     /**
      * Gets the pattern to check the server is ready or not.
+     *
+     * @return the pattern for checking.
      */
     protected abstract Pattern getPattern();
 }

Reply via email to