Skip to content

Commit f1a57fd

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 f1a57fd

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ static File setupCacheDir(String fileCacheDir) {
4545

4646
// the cacheDir will be suffixed a unique id to avoid eavesdropping from other processes/users
4747
// 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);
48+
File cacheDir = effectiveCacheDir(fileCacheDir);
5049
// Create the cache directory
5150
try {
5251
if (Utils.isWindows()) {
@@ -63,6 +62,25 @@ static File setupCacheDir(String fileCacheDir) {
6362
return cacheDir;
6463
}
6564

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

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(System.nanoTime()).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)