Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid UUID.randomUUID() in startup code #5450

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -48,6 +48,7 @@ public class FileSystemOptions {
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 constructor
Expand Down Expand Up @@ -128,7 +129,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 +148,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 @@ -36,7 +36,12 @@ 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();
}
// in the default case we want to generate the ID as fast as possible
return Long.valueOf(new Random().nextLong()).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");
}
}
}
Loading