Skip to content

Commit 7c9fc8f

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 7c9fc8f

File tree

6 files changed

+61
-9
lines changed

6 files changed

+61
-9
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: 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: 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/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: 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)