kylixs commented on a change in pull request #9274:
URL: https://github.com/apache/dubbo/pull/9274#discussion_r750218155



##########
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);

Review comment:
       > If the timeout expires before there is data available for read, a 
java.net.SocketTimeoutException is raised
   
   The read timeout will not interrupt the downloading if the file data is 
received slowly and continuously. It is more reliable to check the download 
timeout in a separate checking thread, or use the request timeout of the 
asynchronous http client :  https://stackoverflow.com/a/38273306
   
   




-- 
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]

Reply via email to