Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Dubbo-7178] Fix 'java.io.FileNotFoundException' when 'dump.directory' not exists #7178

Merged
merged 2 commits into from
Feb 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
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;

/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -104,7 +108,7 @@ private void dumpJStack() {

ExecutorService pool = Executors.newSingleThreadExecutor();
pool.execute(() -> {
String dumpPath = url.getParameter(DUMP_DIRECTORY, System.getProperty("user.home"));
String dumpPath = getDumpPath();

SimpleDateFormat sdf;

Expand Down Expand Up @@ -134,4 +138,21 @@ private void dumpJStack() {

}

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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -44,4 +44,60 @@ public void run() {
Thread.sleep(1000);

}

@Test
public void jStackDumpTest_dumpDirectoryNotExists_cannotBeCreatedTakeUserHome() throws InterruptedException {
final String dumpDirectory = dumpDirectoryCannotBeCreated();

URL url = URL.valueOf("dubbo://admin:[email protected]: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:[email protected]: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);
}
}