Skip to content

Commit c749aaa

Browse files
committed
Avoid UUID.randomUUID() in file system related startup code
This is done because bootstrapping the plumbing needed by the JDK to produce a UUID value is expensive, it thus doesn't make sense to pay this cost when the property isn't actually needed
1 parent dacd689 commit c749aaa

File tree

5 files changed

+62
-8
lines changed

5 files changed

+62
-8
lines changed

vertx-core/src/main/generated/io/vertx/core/file/FileSystemOptionsConverter.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ static void fromJson(Iterable<java.util.Map.Entry<String, Object>> json, FileSys
2929
obj.setFileCacheDir((String)member.getValue());
3030
}
3131
break;
32+
case "exactFileCacheDir":
33+
if (member.getValue() instanceof String) {
34+
obj.setExactFileCacheDir((String)member.getValue());
35+
}
36+
break;
3237
}
3338
}
3439
}
@@ -43,5 +48,8 @@ static void toJson(FileSystemOptions obj, java.util.Map<String, Object> json) {
4348
if (obj.getFileCacheDir() != null) {
4449
json.put("fileCacheDir", obj.getFileCacheDir());
4550
}
51+
if (obj.getExactFileCacheDir() != null) {
52+
json.put("exactFileCacheDir", obj.getExactFileCacheDir());
53+
}
4654
}
4755
}

vertx-core/src/main/java/io/vertx/core/file/FileSystemOptions.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,17 @@ public class FileSystemOptions {
4545
*/
4646
public static final String DEFAULT_FILE_CACHING_DIR = SysProps.FILE_CACHE_DIR.get();
4747

48+
/**
49+
* The default exact file caching dir, which is {@code null} as {@link FileSystemOptions#setExactFileCacheDir}
50+
* is meant to be used by advanced integrations that can guarantee on their own that the cache dir
51+
* will be unique
52+
*/
53+
public static final String DEFAULT_EXACT_FILE_CACHING_DIR = null;
54+
4855
private boolean classPathResolvingEnabled = DEFAULT_CLASS_PATH_RESOLVING_ENABLED;
4956
private boolean fileCachingEnabled = DEFAULT_FILE_CACHING_ENABLED;
5057
private String fileCacheDir = DEFAULT_FILE_CACHING_DIR;
58+
private String exactFileCacheDir = DEFAULT_EXACT_FILE_CACHING_DIR;
5159

5260
/**
5361
* Default constructor
@@ -128,7 +136,7 @@ public FileSystemOptions setFileCachingEnabled(boolean fileCachingEnabled) {
128136
}
129137

130138
/**
131-
* @return the configured file cache dir
139+
* @return the base name of the configured file cache dir. Vert.x will append a random value to this when determining the effective value
132140
*/
133141
public String getFileCacheDir() {
134142
return this.fileCacheDir;
@@ -147,13 +155,34 @@ public FileSystemOptions setFileCacheDir(String fileCacheDir) {
147155
return this;
148156
}
149157

158+
/**
159+
* @return the configured exact file cache dir to be used as is
160+
*/
161+
public String getExactFileCacheDir() {
162+
return this.exactFileCacheDir;
163+
}
164+
165+
/**
166+
* When vert.x reads a file that is packaged with the application it gets
167+
* extracted to this directory first and subsequent reads will use the extracted
168+
* file to get better IO performance.
169+
*
170+
* @param exactFileCacheDir the value
171+
* @return a reference to this, so the API can be used fluently
172+
*/
173+
public FileSystemOptions setExactFileCacheDir(String exactFileCacheDir) {
174+
this.exactFileCacheDir = exactFileCacheDir;
175+
return this;
176+
}
177+
150178

151179
@Override
152180
public String toString() {
153181
return "FileSystemOptions{" +
154182
"classPathResolvingEnabled=" + classPathResolvingEnabled +
155183
", fileCachingEnabled=" + fileCachingEnabled +
156184
", fileCacheDir=" + fileCacheDir +
185+
", exactFileCacheDir=" + exactFileCacheDir +
157186
'}';
158187
}
159188
}

vertx-core/src/main/java/io/vertx/core/file/impl/FileCache.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.io.InputStream;
1919
import java.nio.file.FileAlreadyExistsException;
2020
import java.nio.file.Files;
21-
import java.nio.file.Path;
2221
import java.nio.file.StandardCopyOption;
2322
import java.nio.file.attribute.PosixFilePermission;
2423
import java.nio.file.attribute.PosixFilePermissions;
@@ -27,8 +26,8 @@
2726

2827
public class FileCache {
2928

30-
static FileCache setupCache(String fileCacheDir) {
31-
FileCache cache = new FileCache(setupCacheDir(fileCacheDir));
29+
static FileCache setupCache(String fileCacheDir, boolean isEffectiveValue) {
30+
FileCache cache = new FileCache(setupCacheDir(fileCacheDir, isEffectiveValue));
3231
// Add shutdown hook to delete on exit
3332
cache.registerShutdownHook();
3433
return cache;
@@ -37,16 +36,15 @@ static FileCache setupCache(String fileCacheDir) {
3736
/**
3837
* Prepares the cache directory to be used in the application.
3938
*/
40-
static File setupCacheDir(String fileCacheDir) {
39+
static File setupCacheDir(String fileCacheDir, boolean isEffectiveValue) {
4140
// ensure that the argument doesn't end with separator
4241
if (fileCacheDir.endsWith(File.separator)) {
4342
fileCacheDir = fileCacheDir.substring(0, fileCacheDir.length() - File.separator.length());
4443
}
4544

4645
// the cacheDir will be suffixed a unique id to avoid eavesdropping from other processes/users
4746
// also this ensures that if process A deletes cacheDir, it won't affect process B
48-
String cacheDirName = fileCacheDir + "-" + UUID.randomUUID();
49-
File cacheDir = new File(cacheDirName);
47+
File cacheDir = isEffectiveValue ? new File(fileCacheDir) : new File(fileCacheDir + "-" + UUID.randomUUID());
5048
// Create the cache directory
5149
try {
5250
if (Utils.isWindows()) {

vertx-core/src/main/java/io/vertx/core/file/impl/FileResolverImpl.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,12 @@ public FileResolverImpl(FileSystemOptions fileSystemOptions) {
6868
enableCPResolving = fileSystemOptions.isClassPathResolvingEnabled();
6969

7070
if (enableCPResolving) {
71-
cache = FileCache.setupCache(fileSystemOptions.getFileCacheDir());
71+
String exactFileCacheDir = fileSystemOptions.getExactFileCacheDir();
72+
if (exactFileCacheDir != null) {
73+
cache = FileCache.setupCache(exactFileCacheDir, true);
74+
} else {
75+
cache = FileCache.setupCache(fileSystemOptions.getFileCacheDir(), false);
76+
}
7277
} else {
7378
cache = null;
7479
}

vertx-core/src/test/java/io/vertx/tests/file/FileResolverTestBase.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import io.vertx.core.internal.VertxInternal;
2323
import io.vertx.test.core.VertxTestBase;
2424
import io.vertx.test.http.HttpTestBase;
25+
import java.nio.file.Path;
26+
import java.nio.file.Paths;
2527
import org.junit.Assert;
2628
import org.junit.Assume;
2729
import org.junit.Test;
@@ -486,4 +488,16 @@ public void testGetTheCacheDirWithoutHacks() {
486488
}
487489
}
488490
}
491+
492+
@Test
493+
public void testGetTheExactCacheDirWithoutHacks() {
494+
String cacheDir = new FileResolverImpl(new FileSystemOptions().setExactFileCacheDir(cacheBaseDir + "-exact")).cacheDir();
495+
if (cacheDir != null) {
496+
System.out.println(cacheDir);
497+
assertTrue(cacheDir.startsWith(cacheBaseDir + "-"));
498+
// strip the remaining
499+
String remaining = cacheDir.substring(cacheBaseDir.length() + 1);
500+
assertEquals(remaining, "exact");
501+
}
502+
}
489503
}

0 commit comments

Comments
 (0)