Skip to content

Commit de8664c

Browse files
authored
Use links when possible when installing test cluster modules (elastic#121067) (elastic#121081)
When we install modules into test clusters we do a full copy instead of links. This both eats up more IO and disk space unnecessarily.
1 parent cfddc26 commit de8664c

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

test/test-clusters/src/main/java/org/elasticsearch/test/cluster/local/AbstractLocalClusterFactory.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -328,15 +328,7 @@ private void initializeWorkingDirectory(boolean preserveWorkingDirectory) {
328328
IOUtils.deleteWithRetry(distributionDir);
329329
}
330330

331-
try {
332-
IOUtils.syncWithLinks(distributionDescriptor.getDistributionDir(), distributionDir);
333-
} catch (IOUtils.LinkCreationException e) {
334-
// Note does not work for network drives, e.g. Vagrant
335-
LOGGER.info("Failed to create working dir using hard links. Falling back to copy", e);
336-
// ensure we get a clean copy
337-
IOUtils.deleteWithRetry(distributionDir);
338-
IOUtils.syncWithCopy(distributionDescriptor.getDistributionDir(), distributionDir);
339-
}
331+
IOUtils.syncMaybeWithLinks(distributionDescriptor.getDistributionDir(), distributionDir);
340332
}
341333
Files.createDirectories(repoDir);
342334
Files.createDirectories(dataDir);
@@ -708,7 +700,8 @@ private void installModule(String moduleName, DefaultPluginInstallSpec installSp
708700

709701
});
710702

711-
IOUtils.syncWithCopy(modulePath, destination);
703+
IOUtils.syncMaybeWithLinks(modulePath, destination);
704+
712705
try {
713706
if (installSpec.entitlementsOverride != null) {
714707
Path entitlementsFile = modulePath.resolve(ENTITLEMENT_POLICY_YAML);

test/test-clusters/src/main/java/org/elasticsearch/test/cluster/util/IOUtils.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
package org.elasticsearch.test.cluster.util;
1111

12+
import org.apache.logging.log4j.LogManager;
13+
import org.apache.logging.log4j.Logger;
14+
1215
import java.io.File;
1316
import java.io.IOException;
1417
import java.io.UncheckedIOException;
@@ -20,6 +23,7 @@
2023
import java.util.stream.Stream;
2124

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

@@ -51,6 +55,30 @@ public static void uncheckedDeleteWithRetry(Path path) {
5155
}
5256
}
5357

58+
/**
59+
* Attempts to do a copy via linking, falling back to a normal copy if an exception is encountered.
60+
*
61+
* @see #syncWithLinks(Path, Path)
62+
* @see #syncWithCopy(Path, Path)
63+
* @param sourceRoot where to copy from
64+
* @param destinationRoot destination to link to
65+
*/
66+
public static void syncMaybeWithLinks(Path sourceRoot, Path destinationRoot) {
67+
try {
68+
syncWithLinks(sourceRoot, destinationRoot);
69+
} catch (LinkCreationException e) {
70+
// Note does not work for network drives, e.g. Vagrant
71+
LOGGER.info("Failed to sync using hard links. Falling back to copy.", e);
72+
// ensure we get a clean copy
73+
try {
74+
deleteWithRetry(destinationRoot);
75+
} catch (IOException ex) {
76+
throw new UncheckedIOException(ex);
77+
}
78+
syncWithCopy(sourceRoot, destinationRoot);
79+
}
80+
}
81+
5482
/**
5583
* Does the equivalent of `cp -lr` and `chmod -r a-w` to save space and improve speed.
5684
* We remove write permissions to make sure files are note mistakenly edited ( e.x. the config file ) and changes

0 commit comments

Comments
 (0)