pinxiong commented on a change in pull request #9274:
URL: https://github.com/apache/dubbo/pull/9274#discussion_r750211255
##########
File path:
dubbo-test/dubbo-test-check/src/main/java/org/apache/dubbo/test/check/registrycenter/initializer/DownloadZookeeperInitializer.java
##########
@@ -46,46 +48,105 @@
private static final String ZOOKEEPER_BINARY_URL_FORMAT =
"https://archive.apache.org/dist/zookeeper/zookeeper-%s/" +
ZOOKEEPER_FILE_NAME_FORMAT;
/**
- * The temporary directory name
+ * The temporary directory.
*/
- private static final String TEMPORARY_DIRECTORY_NAME =
"dubbo-mocked-zookeeper";
+ private static final String TEMPORARY_DIRECTORY = "zookeeper";
+
+ /**
+ * The timeout when download zookeeper binary archive file.
+ */
+ private static final int CONNECT_TIMEOUT = 30 * 1000;
+
+ /**
+ * The timeout when read the input stream to save in target path.
+ */
+ private static final int READ_TIMEOUT = 10 * 1000;
+
+ /**
+ * Returns {@code true} if the file exists with the given file path,
otherwise {@code false}.
+ *
+ * @param filePath the file path to check.
+ */
+ private boolean checkFile(Path filePath) {
+ return Files.exists(filePath) && filePath.toFile().isFile();
+ }
@Override
protected void doInitialize(ZookeeperContext context) throws
DubboTestException {
+ // checks the zookeeper binary file exists or not
+ if (checkFile(context.getSourceFile())) {
+ return;
+ }
String zookeeperFileName = String.format(ZOOKEEPER_FILE_NAME_FORMAT,
context.getVersion());
+ Path temporaryFilePath;
try {
-
context.setSourceFile(Paths.get(Files.createTempDirectory("").getParent().toString(),
- TEMPORARY_DIRECTORY_NAME,
- zookeeperFileName));
+ temporaryFilePath =
Paths.get(Files.createTempDirectory("").getParent().toString(),
+ TEMPORARY_DIRECTORY,
+ zookeeperFileName);
} catch (IOException e) {
- throw new RuntimeException(String.format("Cannot create the
temporary directory, related directory:%s/%s",
- TEMPORARY_DIRECTORY_NAME, zookeeperFileName), e);
- }
- // check if the zookeeper binary file exists
- if (context.getSourceFile() != null &&
context.getSourceFile().toFile().isFile()) {
- return;
+ throw new RuntimeException(String.format("Cannot create the
temporary directory, file path: %s", TEMPORARY_DIRECTORY), e);
}
+
// create the temporary directory path.
+ try {
+ Files.createDirectories(temporaryFilePath.getParent());
+ } catch (IOException e) {
+ throw new RuntimeException(String.format("Failed to create the
temporary directory to save zookeeper binary file, file path:%s",
temporaryFilePath.getParent()), e);
+ }
+
+ // download zookeeper binary file in temporary directory.
+ String zookeeperBinaryUrl = String.format(ZOOKEEPER_BINARY_URL_FORMAT,
context.getVersion(), context.getVersion());
+ try {
+ logger.info("It is beginning to download the zookeeper binary
archive, it will take several minutes..." +
+ "\nThe zookeeper binary archive file will be download from " +
zookeeperBinaryUrl + "," +
+ "\nwhich will be saved in " + temporaryFilePath.toString() +
"," +
+ "\nalso it will be renamed to 'apache-zookeeper-bin.tar.gz'
and moved into {project.dir}.tmp/zookeeper directory.\n");
+ URL url = new URL(zookeeperBinaryUrl);
+ HttpURLConnection connection = (HttpURLConnection)
url.openConnection();
+ // set timeout when download the zookeeper binary archive file.
+ connection.setConnectTimeout(CONNECT_TIMEOUT);
+ // set timeout when read downloaded input stream to save in
temporary file path.
+ connection.setReadTimeout(READ_TIMEOUT);
+ connection.setRequestMethod("GET");
+ // use cache first
+ connection.setUseCaches(true);
+ // only read input stream from HttpURLConnection
+ connection.setDoInput(true);
+ connection.connect();
+ InputStream inputStream = connection.getInputStream();
+ Files.copy(inputStream, temporaryFilePath,
StandardCopyOption.REPLACE_EXISTING);
+ } catch (Exception e) {
+ throw new RuntimeException(String.format("Download zookeeper
binary archive failed, download url:%s, file path:%s." +
+ "\nOr you can do something to avoid this problem as
below:" +
+ "\n1. Download zookeeper binary archive manually
regardless of the version" +
+ "\n2. Rename the downloaded file named
'apache-zookeeper-{version}-bin.tar.gz' to 'apache-zookeeper-bin.tar.gz'" +
+ "\n3. Put the renamed file in {project.dir}.tmp/zookeeper
directory, you maybe need to create the directory if necessary.\n",
Review comment:
> {project.dir}/.tmp/zookeeper
I want to see that the path .tmp/zookeeper under current project
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]