Skip to content

Commit b28445c

Browse files
committed
Avoid UUID.randomUUID() in 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 35444ab commit b28445c

File tree

5 files changed

+61
-12
lines changed

5 files changed

+61
-12
lines changed

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public class FileSystemOptions {
4848
private boolean classPathResolvingEnabled = DEFAULT_CLASS_PATH_RESOLVING_ENABLED;
4949
private boolean fileCachingEnabled = DEFAULT_FILE_CACHING_ENABLED;
5050
private String fileCacheDir = DEFAULT_FILE_CACHING_DIR;
51+
private String exactFileCacheDir;
5152

5253
/**
5354
* Default constructor
@@ -128,7 +129,7 @@ public FileSystemOptions setFileCachingEnabled(boolean fileCachingEnabled) {
128129
}
129130

130131
/**
131-
* @return the configured file cache dir
132+
* @return the base name of the configured file cache dir. Vert.x will append a random value to this when determining the effective value
132133
*/
133134
public String getFileCacheDir() {
134135
return this.fileCacheDir;
@@ -147,13 +148,34 @@ public FileSystemOptions setFileCacheDir(String fileCacheDir) {
147148
return this;
148149
}
149150

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

151172
@Override
152173
public String toString() {
153174
return "FileSystemOptions{" +
154175
"classPathResolvingEnabled=" + classPathResolvingEnabled +
155176
", fileCachingEnabled=" + fileCachingEnabled +
156177
", fileCacheDir=" + fileCacheDir +
178+
", exactFileCacheDir=" + exactFileCacheDir +
157179
'}';
158180
}
159181
}

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

Lines changed: 23 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) : effectiveCacheDir(fileCacheDir);
5048
// Create the cache directory
5149
try {
5250
if (Utils.isWindows()) {
@@ -63,6 +61,25 @@ static File setupCacheDir(String fileCacheDir) {
6361
return cacheDir;
6462
}
6563

64+
private static File effectiveCacheDir(String fileCacheDir) {
65+
for(int i = 0; i < 10; i++) {
66+
try {
67+
// attempt to create a file using a really quick random value
68+
String cacheDirName = fileCacheDir + "-" + System.nanoTime();
69+
File file = new File(cacheDirName);
70+
Files.createDirectories(file.toPath());
71+
return file;
72+
} catch(FileAlreadyExistsException ignore) {
73+
} catch (IOException ignore) {
74+
// hope that the fallback will work
75+
break;
76+
}
77+
}
78+
79+
String cacheDirName = fileCacheDir + "-" + UUID.randomUUID();
80+
return new File(cacheDirName);
81+
}
82+
6683
private Thread shutdownHook;
6784
private File cacheDir;
6885

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/main/java/io/vertx/core/impl/deployment/DefaultDeploymentManager.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ public DefaultDeploymentManager(VertxImpl vertx) {
3636
}
3737

3838
private String generateDeploymentID() {
39-
return UUID.randomUUID().toString();
39+
if (vertx.isClustered() && vertx.haManager()!=null) {
40+
// in this case we need a globally unique id
41+
return UUID.randomUUID().toString();
42+
}
43+
// in the default case we want to generate the ID as fast as possible
44+
return Long.valueOf(new Random().nextLong()).toString();
4045
}
4146

4247
public Future<Void> undeploy(String deploymentID) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -477,12 +477,12 @@ public void testGetTheCacheDirWithoutHacks() {
477477
if (cacheDir != null) {
478478
assertTrue(cacheDir.startsWith(cacheBaseDir + "-"));
479479
// strip the remaining
480-
String uuid = cacheDir.substring(cacheBaseDir.length() + 1);
480+
String val = cacheDir.substring(cacheBaseDir.length() + 1);
481481
try {
482-
UUID.fromString(uuid);
482+
Long.parseLong(val);
483483
// OK
484484
} catch (Exception e) {
485-
fail("Expected a UUID");
485+
fail("Expected a Long");
486486
}
487487
}
488488
}

0 commit comments

Comments
 (0)