Skip to content

Commit

Permalink
perf: Optimize the code on global zookeeper (apache#9302)
Browse files Browse the repository at this point in the history
1. Avoid compiling pattern every time
2. Avoid port already in use
3. Output all configuration information of global zookeeper instances
  • Loading branch information
pinxiong authored Nov 28, 2021
1 parent a72123a commit 98c8602
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

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;

Expand Down Expand Up @@ -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 {
Expand All @@ -77,8 +78,11 @@ private void updateConfig(ZookeeperContext context, int clientPort, int adminSer
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();
Expand All @@ -105,9 +109,29 @@ private void updateConfig(ZookeeperContext context, int clientPort, int adminSer
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,35 @@
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);
}
}

@Override
protected Pattern getPattern() {
return Pattern.compile(".*STARTED.*");
return PATTERN_STARTED;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -56,6 +61,6 @@ protected Process doProcess(ZookeeperContext context, int clientPort) throws Dub

@Override
protected Pattern getPattern() {
return Pattern.compile(".*STOPPED.*");
return PATTERN_STOPPED;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ private void awaitProcessReady(final InputStream inputStream) throws DubboTestEx
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) {
Expand All @@ -101,6 +102,8 @@ private void awaitProcessReady(final InputStream inputStream) throws DubboTestEx

/**
* Gets the pattern to check the server is ready or not.
*
* @return the pattern for checking.
*/
protected abstract Pattern getPattern();
}

0 comments on commit 98c8602

Please sign in to comment.