author | mgronlun |
Wed, 30 Oct 2019 19:43:52 +0100 | |
changeset 58863 | c16ac7a2eba4 |
parent 58240 | 046533575954 |
child 59226 | a0f39cc47387 |
permissions | -rw-r--r-- |
50113 | 1 |
/* |
58863 | 2 |
* Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved. |
50113 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package jdk.jfr.internal; |
|
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.nio.file.Path; |
|
30 |
import java.time.Instant; |
|
31 |
import java.time.LocalDateTime; |
|
32 |
import java.time.format.DateTimeFormatter; |
|
33 |
import java.util.HashSet; |
|
34 |
import java.util.Set; |
|
35 |
||
36 |
import jdk.jfr.internal.SecuritySupport.SafePath; |
|
37 |
||
38 |
public final class Repository { |
|
39 |
||
40 |
private static final int MAX_REPO_CREATION_RETRIES = 1000; |
|
41 |
private static final JVM jvm = JVM.getJVM(); |
|
42 |
private static final Repository instance = new Repository(); |
|
43 |
||
50745 | 44 |
public final static DateTimeFormatter REPO_DATE_FORMAT = DateTimeFormatter |
50113 | 45 |
.ofPattern("yyyy_MM_dd_HH_mm_ss"); |
58863 | 46 |
private static final String JFR_REPOSITORY_LOCATION_PROPERTY = "jdk.jfr.repository"; |
50113 | 47 |
|
48 |
private final Set<SafePath> cleanupDirectories = new HashSet<>(); |
|
49 |
private SafePath baseLocation; |
|
50 |
private SafePath repository; |
|
51 |
||
52 |
private Repository() { |
|
53 |
} |
|
54 |
||
55 |
public static Repository getRepository() { |
|
56 |
return instance; |
|
57 |
} |
|
58 |
||
58863 | 59 |
public synchronized void setBasePath(SafePath baseLocation) throws IOException { |
58240
046533575954
8216565: Specifying the same path creates a new directory in JFR.configure
cito
parents:
50745
diff
changeset
|
60 |
if(baseLocation.equals(this.baseLocation)) { |
046533575954
8216565: Specifying the same path creates a new directory in JFR.configure
cito
parents:
50745
diff
changeset
|
61 |
Logger.log(LogTag.JFR, LogLevel.INFO, "Same base repository path " + baseLocation.toString() + " is set"); |
046533575954
8216565: Specifying the same path creates a new directory in JFR.configure
cito
parents:
50745
diff
changeset
|
62 |
return; |
046533575954
8216565: Specifying the same path creates a new directory in JFR.configure
cito
parents:
50745
diff
changeset
|
63 |
} |
50113 | 64 |
// Probe to see if repository can be created, needed for fail fast |
65 |
// during JVM startup or JFR.configure |
|
66 |
this.repository = createRepository(baseLocation); |
|
67 |
try { |
|
68 |
// Remove so we don't "leak" repositories, if JFR is never started |
|
69 |
// and shutdown hook not added. |
|
70 |
SecuritySupport.delete(repository); |
|
71 |
} catch (IOException ioe) { |
|
72 |
Logger.log(LogTag.JFR, LogLevel.INFO, "Could not delete disk repository " + repository); |
|
73 |
} |
|
74 |
this.baseLocation = baseLocation; |
|
75 |
} |
|
76 |
||
58863 | 77 |
public synchronized void ensureRepository() throws IOException { |
50113 | 78 |
if (baseLocation == null) { |
79 |
setBasePath(SecuritySupport.JAVA_IO_TMPDIR); |
|
80 |
} |
|
81 |
} |
|
82 |
||
83 |
synchronized RepositoryChunk newChunk(Instant timestamp) { |
|
84 |
try { |
|
85 |
if (!SecuritySupport.existDirectory(repository)) { |
|
86 |
this.repository = createRepository(baseLocation); |
|
87 |
jvm.setRepositoryLocation(repository.toString()); |
|
88 |
cleanupDirectories.add(repository); |
|
89 |
} |
|
90 |
return new RepositoryChunk(repository, timestamp); |
|
91 |
} catch (Exception e) { |
|
92 |
String errorMsg = String.format("Could not create chunk in repository %s, %s", repository, e.getMessage()); |
|
93 |
Logger.log(LogTag.JFR, LogLevel.ERROR, errorMsg); |
|
94 |
jvm.abort(errorMsg); |
|
95 |
throw new InternalError("Could not abort after JFR disk creation error"); |
|
96 |
} |
|
97 |
} |
|
98 |
||
58863 | 99 |
private static SafePath createRepository(SafePath basePath) throws IOException { |
50113 | 100 |
SafePath canonicalBaseRepositoryPath = createRealBasePath(basePath); |
101 |
SafePath f = null; |
|
102 |
||
103 |
String basename = REPO_DATE_FORMAT.format(LocalDateTime.now()) + "_" + JVM.getJVM().getPid(); |
|
104 |
String name = basename; |
|
105 |
||
106 |
int i = 0; |
|
107 |
for (; i < MAX_REPO_CREATION_RETRIES; i++) { |
|
108 |
f = new SafePath(canonicalBaseRepositoryPath.toPath().resolve(name)); |
|
109 |
if (tryToUseAsRepository(f)) { |
|
110 |
break; |
|
111 |
} |
|
112 |
name = basename + "_" + i; |
|
113 |
} |
|
114 |
||
115 |
if (i == MAX_REPO_CREATION_RETRIES) { |
|
58863 | 116 |
throw new IOException("Unable to create JFR repository directory using base location (" + basePath + ")"); |
50113 | 117 |
} |
118 |
SafePath canonicalRepositoryPath = SecuritySupport.toRealPath(f); |
|
58863 | 119 |
SecuritySupport.setProperty(JFR_REPOSITORY_LOCATION_PROPERTY, canonicalRepositoryPath.toString()); |
50113 | 120 |
return canonicalRepositoryPath; |
121 |
} |
|
122 |
||
58863 | 123 |
private static SafePath createRealBasePath(SafePath safePath) throws IOException { |
50113 | 124 |
if (SecuritySupport.exists(safePath)) { |
125 |
if (!SecuritySupport.isWritable(safePath)) { |
|
126 |
throw new IOException("JFR repository directory (" + safePath.toString() + ") exists, but isn't writable"); |
|
127 |
} |
|
128 |
return SecuritySupport.toRealPath(safePath); |
|
129 |
} |
|
130 |
SafePath p = SecuritySupport.createDirectories(safePath); |
|
131 |
return SecuritySupport.toRealPath(p); |
|
132 |
} |
|
133 |
||
134 |
private static boolean tryToUseAsRepository(final SafePath path) { |
|
135 |
Path parent = path.toPath().getParent(); |
|
136 |
if (parent == null) { |
|
137 |
return false; |
|
138 |
} |
|
139 |
try { |
|
140 |
try { |
|
141 |
SecuritySupport.createDirectories(path); |
|
142 |
} catch (Exception e) { |
|
143 |
// file already existed or some other problem occurred |
|
144 |
} |
|
145 |
if (!SecuritySupport.exists(path)) { |
|
146 |
return false; |
|
147 |
} |
|
148 |
if (!SecuritySupport.isDirectory(path)) { |
|
149 |
return false; |
|
150 |
} |
|
151 |
return true; |
|
152 |
} catch (IOException io) { |
|
153 |
return false; |
|
154 |
} |
|
155 |
} |
|
156 |
||
157 |
synchronized void clear() { |
|
158 |
for (SafePath p : cleanupDirectories) { |
|
159 |
try { |
|
160 |
SecuritySupport.clearDirectory(p); |
|
161 |
Logger.log(LogTag.JFR, LogLevel.INFO, "Removed repository " + p); |
|
162 |
} catch (IOException e) { |
|
58863 | 163 |
Logger.log(LogTag.JFR, LogLevel.INFO, "Repository " + p + " could not be removed at shutdown: " + e.getMessage()); |
50113 | 164 |
} |
165 |
} |
|
166 |
} |
|
167 |
||
168 |
public synchronized SafePath getRepositoryPath() { |
|
169 |
return repository; |
|
170 |
} |
|
171 |
} |