You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by li...@apache.org on 2021/02/20 02:51:42 UTC

[dubbo] branch master updated: [Dubbo-7178] Fix 'java.io.FileNotFoundException' when 'dump.directory' not exists (#7178)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 1124e64  [Dubbo-7178] Fix 'java.io.FileNotFoundException' when 'dump.directory' not exists (#7178)
1124e64 is described below

commit 1124e64ef74da39b72dad7a5431843a111d13e42
Author: zenuo <ze...@protonmail.com>
AuthorDate: Sat Feb 20 10:51:00 2021 +0800

    [Dubbo-7178] Fix 'java.io.FileNotFoundException' when 'dump.directory' not exists (#7178)
---
 .../threadpool/support/AbortPolicyWithReport.java  | 23 ++++++++-
 .../support/AbortPolicyWithReportTest.java         | 58 +++++++++++++++++++++-
 2 files changed, 79 insertions(+), 2 deletions(-)

diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/support/AbortPolicyWithReport.java b/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/support/AbortPolicyWithReport.java
index c6865eb..6347e14 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/support/AbortPolicyWithReport.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/support/AbortPolicyWithReport.java
@@ -31,8 +31,10 @@ import org.apache.dubbo.common.logger.Logger;
 import org.apache.dubbo.common.logger.LoggerFactory;
 import org.apache.dubbo.common.threadpool.event.ThreadPoolExhaustedEvent;
 import org.apache.dubbo.common.utils.JVMUtil;
+import org.apache.dubbo.common.utils.StringUtils;
 import org.apache.dubbo.event.EventDispatcher;
 
+import static java.lang.String.format;
 import static org.apache.dubbo.common.constants.CommonConstants.DUMP_DIRECTORY;
 
 /**
@@ -61,6 +63,8 @@ public class AbortPolicyWithReport extends ThreadPoolExecutor.AbortPolicy {
 
     private static Semaphore guard = new Semaphore(1);
 
+    private static final String USER_HOME = System.getProperty("user.home");
+
     public AbortPolicyWithReport(String threadName, URL url) {
         this.threadName = threadName;
         this.url = url;
@@ -104,7 +108,7 @@ public class AbortPolicyWithReport extends ThreadPoolExecutor.AbortPolicy {
 
         ExecutorService pool = Executors.newSingleThreadExecutor();
         pool.execute(() -> {
-            String dumpPath = url.getParameter(DUMP_DIRECTORY, System.getProperty("user.home"));
+            String dumpPath = getDumpPath();
 
             SimpleDateFormat sdf;
 
@@ -134,4 +138,21 @@ public class AbortPolicyWithReport extends ThreadPoolExecutor.AbortPolicy {
 
     }
 
+    private String getDumpPath() {
+        final String dumpPath = url.getParameter(DUMP_DIRECTORY);
+        if (StringUtils.isEmpty(dumpPath)) {
+            return USER_HOME;
+        }
+        final File dumpDirectory = new File(dumpPath);
+        if (!dumpDirectory.exists()) {
+            if (dumpDirectory.mkdirs()) {
+                logger.info(format("Dubbo dump directory[%s] created", dumpDirectory.getAbsolutePath()));
+            } else {
+                logger.warn(format("Dubbo dump directory[%s] can't be created, use the 'user.home'[%s]",
+                        dumpDirectory.getAbsolutePath(), USER_HOME));
+                return USER_HOME;
+            }
+        }
+        return dumpPath;
+    }
 }
diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/support/AbortPolicyWithReportTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/support/AbortPolicyWithReportTest.java
index ed737ef..178c73a 100644
--- a/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/support/AbortPolicyWithReportTest.java
+++ b/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/support/AbortPolicyWithReportTest.java
@@ -17,9 +17,9 @@
 package org.apache.dubbo.common.threadpool.support;
 
 import org.apache.dubbo.common.URL;
-import org.apache.dubbo.common.threadpool.support.AbortPolicyWithReport;
 import org.junit.jupiter.api.Test;
 
+import java.util.UUID;
 import java.util.concurrent.Executors;
 import java.util.concurrent.RejectedExecutionException;
 import java.util.concurrent.ThreadPoolExecutor;
@@ -44,4 +44,60 @@ public class AbortPolicyWithReportTest {
         Thread.sleep(1000);
 
     }
+
+    @Test
+    public void jStackDumpTest_dumpDirectoryNotExists_cannotBeCreatedTakeUserHome() throws InterruptedException {
+        final String dumpDirectory = dumpDirectoryCannotBeCreated();
+
+        URL url = URL.valueOf("dubbo://admin:hello1234@10.20.130.230:20880/context/path?dump.directory="
+                + dumpDirectory
+                + "&version=1.0.0&application=morgan&noValue");
+        AbortPolicyWithReport abortPolicyWithReport = new AbortPolicyWithReport("Test", url);
+
+        try {
+            abortPolicyWithReport.rejectedExecution(new Runnable() {
+                @Override
+                public void run() {
+                    System.out.println("hello");
+                }
+            }, (ThreadPoolExecutor) Executors.newFixedThreadPool(1));
+        } catch (RejectedExecutionException rj) {
+            // ignore
+        }
+
+        Thread.sleep(1000);
+    }
+
+    private String dumpDirectoryCannotBeCreated() {
+        final String os = System.getProperty("os.name").toLowerCase();
+        if (os.contains("win")) {
+            // "con" is one of Windows reserved names, https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file
+            return "con";
+        } else {
+            return "/dev/full/" + UUID.randomUUID().toString();
+        }
+    }
+
+    @Test
+    public void jStackDumpTest_dumpDirectoryNotExists_canBeCreated() throws InterruptedException {
+        final String dumpDirectory = UUID.randomUUID().toString();
+
+        URL url = URL.valueOf("dubbo://admin:hello1234@10.20.130.230:20880/context/path?dump.directory="
+                + dumpDirectory
+                + "&version=1.0.0&application=morgan&noValue");
+        AbortPolicyWithReport abortPolicyWithReport = new AbortPolicyWithReport("Test", url);
+
+        try {
+            abortPolicyWithReport.rejectedExecution(new Runnable() {
+                @Override
+                public void run() {
+                    System.out.println("hello");
+                }
+            }, (ThreadPoolExecutor) Executors.newFixedThreadPool(1));
+        } catch (RejectedExecutionException rj) {
+            // ignore
+        }
+
+        Thread.sleep(1000);
+    }
 }
\ No newline at end of file