Skip to content

Commit

Permalink
Use links when possible when installing test cluster modules
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-vieira committed Jan 28, 2025
1 parent b2cc9d9 commit def6394
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -350,15 +350,7 @@ private void initializeWorkingDirectory(boolean preserveWorkingDirectory) {
IOUtils.deleteWithRetry(distributionDir);
}

try {
IOUtils.syncWithLinks(distributionDescriptor.getDistributionDir(), distributionDir);
} catch (IOUtils.LinkCreationException e) {
// Note does not work for network drives, e.g. Vagrant
LOGGER.info("Failed to create working dir using hard links. Falling back to copy", e);
// ensure we get a clean copy
IOUtils.deleteWithRetry(distributionDir);
IOUtils.syncWithCopy(distributionDescriptor.getDistributionDir(), distributionDir);
}
IOUtils.syncMaybeWithLinks(distributionDescriptor.getDistributionDir(), distributionDir);
}
Files.createDirectories(repoDir);
Files.createDirectories(dataDir);
Expand Down Expand Up @@ -773,7 +765,8 @@ private void installModule(String moduleName, DefaultPluginInstallSpec installSp

});

IOUtils.syncWithCopy(modulePath, destination);
IOUtils.syncMaybeWithLinks(modulePath, destination);

try {
if (installSpec.entitlementsOverride != null) {
Path entitlementsFile = modulePath.resolve(ENTITLEMENT_POLICY_YAML);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

package org.elasticsearch.test.cluster.util;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
Expand All @@ -20,6 +23,7 @@
import java.util.stream.Stream;

public final class IOUtils {
private static final Logger LOGGER = LogManager.getLogger(IOUtils.class);
private static final int RETRY_DELETE_MILLIS = OS.current() == OS.WINDOWS ? 500 : 0;
private static final int MAX_RETRY_DELETE_TIMES = OS.current() == OS.WINDOWS ? 15 : 0;

Expand Down Expand Up @@ -51,6 +55,30 @@ public static void uncheckedDeleteWithRetry(Path path) {
}
}

/**
* Attempts to do a copy via linking, calling back to a normal copy if an exception is encountered.
*
* @see #syncWithLinks(Path, Path)
* @see #syncWithCopy(Path, Path)
* @param sourceRoot where to copy from
* @param destinationRoot destination to link to
*/
public static void syncMaybeWithLinks(Path sourceRoot, Path destinationRoot) {
try {
syncWithLinks(sourceRoot, destinationRoot);
} catch (LinkCreationException e) {
// Note does not work for network drives, e.g. Vagrant
LOGGER.info("Failed to sync using hard links. Falling back to copy.", e);
// ensure we get a clean copy
try {
IOUtils.deleteWithRetry(destinationRoot);
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
IOUtils.syncWithCopy(sourceRoot, destinationRoot);
}
}

/**
* Does the equivalent of `cp -lr` and `chmod -r a-w` to save space and improve speed.
* We remove write permissions to make sure files are note mistakenly edited ( e.x. the config file ) and changes
Expand Down

0 comments on commit def6394

Please sign in to comment.