Skip to content

Commit

Permalink
Avoid UUID.randomUUID() in startup code
Browse files Browse the repository at this point in the history
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
  • Loading branch information
geoand committed Jan 31, 2025
1 parent dacd689 commit 8ba99cf
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ static void fromJson(Iterable<java.util.Map.Entry<String, Object>> json, FileSys
obj.setFileCacheDir((String)member.getValue());
}
break;
case "exactFileCacheDir":
if (member.getValue() instanceof String) {
obj.setExactFileCacheDir((String)member.getValue());
}
break;
}
}
}
Expand All @@ -43,5 +48,8 @@ static void toJson(FileSystemOptions obj, java.util.Map<String, Object> json) {
if (obj.getFileCacheDir() != null) {
json.put("fileCacheDir", obj.getFileCacheDir());
}
if (obj.getExactFileCacheDir() != null) {
json.put("exactFileCacheDir", obj.getExactFileCacheDir());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,17 @@ public class FileSystemOptions {
*/
public static final String DEFAULT_FILE_CACHING_DIR = SysProps.FILE_CACHE_DIR.get();

/**
* The default exact file caching dir, which is {@code null} as {@link FileSystemOptions#setExactFileCacheDir}
* is meant to be used by advanced integrations that can guarantee on their own that the cache dir
* will be unique
*/
public static final String DEFAULT_EXACT_FILE_CACHING_DIR = null;

private boolean classPathResolvingEnabled = DEFAULT_CLASS_PATH_RESOLVING_ENABLED;
private boolean fileCachingEnabled = DEFAULT_FILE_CACHING_ENABLED;
private String fileCacheDir = DEFAULT_FILE_CACHING_DIR;
private String exactFileCacheDir = DEFAULT_EXACT_FILE_CACHING_DIR;

/**
* Default constructor
Expand Down Expand Up @@ -128,7 +136,7 @@ public FileSystemOptions setFileCachingEnabled(boolean fileCachingEnabled) {
}

/**
* @return the configured file cache dir
* @return the base name of the configured file cache dir. Vert.x will append a random value to this when determining the effective value
*/
public String getFileCacheDir() {
return this.fileCacheDir;
Expand All @@ -147,13 +155,34 @@ public FileSystemOptions setFileCacheDir(String fileCacheDir) {
return this;
}

/**
* @return the configured exact file cache dir to be used as is
*/
public String getExactFileCacheDir() {
return this.exactFileCacheDir;
}

/**
* When vert.x reads a file that is packaged with the application it gets
* extracted to this directory first and subsequent reads will use the extracted
* file to get better IO performance.
*
* @param exactFileCacheDir the value
* @return a reference to this, so the API can be used fluently
*/
public FileSystemOptions setExactFileCacheDir(String exactFileCacheDir) {
this.exactFileCacheDir = exactFileCacheDir;
return this;
}


@Override
public String toString() {
return "FileSystemOptions{" +
"classPathResolvingEnabled=" + classPathResolvingEnabled +
", fileCachingEnabled=" + fileCachingEnabled +
", fileCacheDir=" + fileCacheDir +
", exactFileCacheDir=" + exactFileCacheDir +
'}';
}
}
10 changes: 4 additions & 6 deletions vertx-core/src/main/java/io/vertx/core/file/impl/FileCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.io.InputStream;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
Expand All @@ -27,8 +26,8 @@

public class FileCache {

static FileCache setupCache(String fileCacheDir) {
FileCache cache = new FileCache(setupCacheDir(fileCacheDir));
static FileCache setupCache(String fileCacheDir, boolean isEffectiveValue) {
FileCache cache = new FileCache(setupCacheDir(fileCacheDir, isEffectiveValue));
// Add shutdown hook to delete on exit
cache.registerShutdownHook();
return cache;
Expand All @@ -37,16 +36,15 @@ static FileCache setupCache(String fileCacheDir) {
/**
* Prepares the cache directory to be used in the application.
*/
static File setupCacheDir(String fileCacheDir) {
static File setupCacheDir(String fileCacheDir, boolean isEffectiveValue) {
// ensure that the argument doesn't end with separator
if (fileCacheDir.endsWith(File.separator)) {
fileCacheDir = fileCacheDir.substring(0, fileCacheDir.length() - File.separator.length());
}

// the cacheDir will be suffixed a unique id to avoid eavesdropping from other processes/users
// also this ensures that if process A deletes cacheDir, it won't affect process B
String cacheDirName = fileCacheDir + "-" + UUID.randomUUID();
File cacheDir = new File(cacheDirName);
File cacheDir = isEffectiveValue ? new File(fileCacheDir) : new File(fileCacheDir + "-" + UUID.randomUUID());
// Create the cache directory
try {
if (Utils.isWindows()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ public FileResolverImpl(FileSystemOptions fileSystemOptions) {
enableCPResolving = fileSystemOptions.isClassPathResolvingEnabled();

if (enableCPResolving) {
cache = FileCache.setupCache(fileSystemOptions.getFileCacheDir());
String exactFileCacheDir = fileSystemOptions.getExactFileCacheDir();
if (exactFileCacheDir != null) {
cache = FileCache.setupCache(exactFileCacheDir, true);
} else {
cache = FileCache.setupCache(fileSystemOptions.getFileCacheDir(), false);
}
} else {
cache = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

/**
* @author <a href="http://tfox.org">Tim Fox</a>
Expand All @@ -27,6 +28,8 @@ public class DefaultDeploymentManager implements DeploymentManager {

public static final Logger log = LoggerFactory.getLogger(DefaultDeploymentManager.class);

private static final AtomicLong nextId = new AtomicLong();

private final VertxImpl vertx;
private final Map<String, DeploymentContext> deploying = new HashMap<>();
private final Map<String, DeploymentContext> deployments = new ConcurrentHashMap<>();
Expand All @@ -36,7 +39,11 @@ public DefaultDeploymentManager(VertxImpl vertx) {
}

private String generateDeploymentID() {
return UUID.randomUUID().toString();
if (vertx.isClustered() && vertx.haManager()!=null) {
// in this case we need a globally unique id
return UUID.randomUUID().toString();
}
return Long.valueOf(nextId.incrementAndGet()).toString();
}

public Future<Void> undeploy(String deploymentID) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import io.vertx.core.internal.VertxInternal;
import io.vertx.test.core.VertxTestBase;
import io.vertx.test.http.HttpTestBase;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Test;
Expand Down Expand Up @@ -486,4 +488,16 @@ public void testGetTheCacheDirWithoutHacks() {
}
}
}

@Test
public void testGetTheExactCacheDirWithoutHacks() {
String cacheDir = new FileResolverImpl(new FileSystemOptions().setExactFileCacheDir(cacheBaseDir + "-exact")).cacheDir();
if (cacheDir != null) {
System.out.println(cacheDir);
assertTrue(cacheDir.startsWith(cacheBaseDir + "-"));
// strip the remaining
String remaining = cacheDir.substring(cacheBaseDir.length() + 1);
assertEquals(remaining, "exact");
}
}
}

0 comments on commit 8ba99cf

Please sign in to comment.