8216565: Specifying the same path creates a new directory in JFR.configure
authorcito
Sat, 26 Jan 2019 15:47:50 +0900
changeset 58240 046533575954
parent 58238 3386b9a8ef4d
child 58241 33de7752835c
8216565: Specifying the same path creates a new directory in JFR.configure Reviewed-by: ysuenaga, egahlin
src/jdk.jfr/share/classes/jdk/jfr/internal/Repository.java
src/jdk.jfr/share/classes/jdk/jfr/internal/SecuritySupport.java
test/jdk/jdk/jfr/jcmd/TestJcmdConfigure.java
--- a/src/jdk.jfr/share/classes/jdk/jfr/internal/Repository.java	Thu Sep 19 23:38:50 2019 +0200
+++ b/src/jdk.jfr/share/classes/jdk/jfr/internal/Repository.java	Sat Jan 26 15:47:50 2019 +0900
@@ -56,6 +56,11 @@
     }
 
     public synchronized void setBasePath(SafePath baseLocation) throws Exception {
+
+        if(baseLocation.equals(this.baseLocation)) {
+            Logger.log(LogTag.JFR, LogLevel.INFO, "Same base repository path " + baseLocation.toString() + " is set");
+            return;
+        }
         // Probe to see if repository can be created, needed for fail fast
         // during JVM startup or JFR.configure
         this.repository = createRepository(baseLocation);
--- a/src/jdk.jfr/share/classes/jdk/jfr/internal/SecuritySupport.java	Thu Sep 19 23:38:50 2019 +0200
+++ b/src/jdk.jfr/share/classes/jdk/jfr/internal/SecuritySupport.java	Sat Jan 26 15:47:50 2019 +0900
@@ -171,6 +171,19 @@
         public String toString() {
             return text;
         }
+
+        @Override
+        public boolean equals(Object other) {
+            if(other != null && other instanceof SafePath){
+                return this.toPath().equals(((SafePath) other).toPath());
+            }
+            return false;
+        }
+
+        @Override
+        public int hashCode() {
+            return this.toPath().hashCode();
+        }
     }
 
     private interface RunnableWithCheckedException {
--- a/test/jdk/jdk/jfr/jcmd/TestJcmdConfigure.java	Thu Sep 19 23:38:50 2019 +0200
+++ b/test/jdk/jdk/jfr/jcmd/TestJcmdConfigure.java	Sat Jan 26 15:47:50 2019 +0900
@@ -26,9 +26,12 @@
 package jdk.jfr.jcmd;
 
 import java.nio.file.Files;
+import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.List;
 
+import jdk.jfr.internal.Repository;
+import jdk.jfr.internal.SecuritySupport.SafePath;
 import jdk.jfr.internal.Options;
 import jdk.test.lib.Asserts;
 import jdk.test.lib.Utils;
@@ -40,7 +43,7 @@
  * @requires vm.hasJFR
  * @library /test/lib /test/jdk
  * @modules jdk.jfr/jdk.jfr.internal
- * @run main/othervm jdk.jfr.jcmd.TestJcmdConfigure
+ * @run main/othervm -Xlog:jfr=info:file=jfr_info.txt jdk.jfr.jcmd.TestJcmdConfigure
  */
 public class TestJcmdConfigure {
 
@@ -53,6 +56,13 @@
     private static final String SAMPLE_THREADS = "samplethreads";
     private static final String UNSUPPORTED_OPTION = "unsupportedoption";
 
+    private static final String REPOSITORYPATH_1 = "./repo1";
+    private static final String REPOSITORYPATH_2 = "./repo2";
+
+    private static final String REPOSITORYPATH_SETTING_1 = "repositorypath="+REPOSITORYPATH_1;
+    private static final String REPOSITORYPATH_SETTING_2 = "repositorypath="+REPOSITORYPATH_2;
+    private static final String JFR_UNIFIED_LOG_FILE = "jfr_info.txt";
+
     public static void main(String[] args) throws Exception {
         //
         // Simple sanity tests against what is available in Java,
@@ -76,6 +86,8 @@
         testNegative(UNSUPPORTED_OPTION, 100000);
         testNegative(MAX_CHUNK_SIZE, -500);
 
+        testRepository();
+
         if (!testExceptions.isEmpty()) {
             for (Exception e : testExceptions) {
                 System.out.println("Error: " + e.getMessage());
@@ -118,4 +130,28 @@
             default: throw new RuntimeException("Unknown option " + name);
         }
     }
+
+    private static void testRepository(){
+        final String findWhat = "[info][jfr] Same base repository path " + REPOSITORYPATH_1 + " is set";
+
+        try {
+            JcmdHelper.jcmd("JFR.configure", REPOSITORYPATH_SETTING_1);
+            SafePath initialPath = Repository.getRepository().getRepositoryPath();
+
+            JcmdHelper.jcmd("JFR.configure", REPOSITORYPATH_SETTING_1);
+            SafePath samePath = Repository.getRepository().getRepositoryPath();
+            Asserts.assertTrue(samePath.equals(initialPath));
+
+            List<String> lines = Files.readAllLines(Paths.get(JFR_UNIFIED_LOG_FILE));
+            Asserts.assertTrue(lines.stream().anyMatch(l->l.contains(findWhat)));
+
+            JcmdHelper.jcmd("JFR.configure", REPOSITORYPATH_SETTING_2);
+            SafePath changedPath = Repository.getRepository().getRepositoryPath();
+
+            Asserts.assertFalse(changedPath.equals(initialPath));
+
+        } catch(Exception e) {
+            testExceptions.add(e);
+        }
+    }
 }