Skip to content

Commit

Permalink
KUDU-2411: Remove binary test artifact files on JVM exit
Browse files Browse the repository at this point in the history
This patch adds a JVM shutdown hook to remove the mini cluster binary
test artifact at shutdown time.

Change-Id: I51701e14eb2f2eb541c04813e74ddc02bf431c3d
Reviewed-on: http://gerrit.cloudera.org:8080/12377
Reviewed-by: Grant Henke <[email protected]>
Tested-by: Kudu Jenkins
  • Loading branch information
mpercy committed Feb 6, 2019
1 parent 0c02110 commit b2d95c6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
3 changes: 2 additions & 1 deletion java/kudu-test-utils/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ apply from: "$rootDir/gradle/shadow.gradle"

dependencies {
compile project(path: ":kudu-client")
compile libs.commonsIo
compile libs.guava
compile libs.osdetector

Expand All @@ -36,4 +37,4 @@ dependencies {
// kudu-test-utils has no public Javadoc.
javadoc {
enabled = false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,33 @@

package org.apache.kudu.test;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
* Utilities for retrieving and creating temp directories.
*/
public class TempDirUtils {

/**
* An enum to control whether a temporary directory created by
* {@link #makeTempDirectory(String, DeleteOnExit)} is recursively deleted on JVM exit,
* including the contents of the directory.
*/
public enum DeleteOnExit {
/** Do not delete the directory on exit. */
NO_DELETE_ON_EXIT,
/** Recursively delete the directory and its contents on exit. */
DELETE_RECURSIVELY_ON_EXIT,
}

private static final Logger LOG = LoggerFactory.getLogger(TempDirUtils.class);

/**
Expand All @@ -40,16 +54,43 @@ public class TempDirUtils {
*
* @param prefix a directory name to be created, in environment variable TEST_TMPDIR if defined,
* else within the java.io.tmpdir system property
* @param deleteRecursivelyOnExit whether to recursively delete the directory and all its
* contents on JVM exit
* @return temp directory as a file
* @throws IOException if a temp directory cannot be created
*/
public static File getTempDirectory(String prefix) throws IOException {
public static File makeTempDirectory(String prefix, DeleteOnExit deleteRecursivelyOnExit)
throws IOException {
String testTmpdir = System.getenv("TEST_TMPDIR");
File newDir;
if (testTmpdir != null) {
LOG.info("Using the temp directory defined by TEST_TMPDIR: " + testTmpdir);
return Files.createTempDirectory(Paths.get(testTmpdir), prefix).toFile();
newDir = Files.createTempDirectory(Paths.get(testTmpdir), prefix).toFile();
} else {
return Files.createTempDirectory(prefix).toFile();
newDir = Files.createTempDirectory(prefix).toFile();
}
if (deleteRecursivelyOnExit == DeleteOnExit.DELETE_RECURSIVELY_ON_EXIT) {
registerToRecursivelyDeleteOnShutdown(newDir.toPath());
}
return newDir;
}

/**
* Register a JVM shutdown hook to recursively delete the specified directory on JVM shutdown.
* @param path directory to delete on shutdown
*/
private static void registerToRecursivelyDeleteOnShutdown(Path path) {
final Path absPath = path.toAbsolutePath();
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
File dir = absPath.toFile();
if (!dir.exists()) return;
try {
FileUtils.deleteDirectory(dir);
} catch (IOException exc) {
LOG.warn("Unable to remove directory tree " + absPath.toString(), exc);
}
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ private static KuduBinaryInfo findBinaryLocation() {
try {
KuduBinaryJarExtractor extractor = new KuduBinaryJarExtractor();
if (extractor.isKuduBinaryJarOnClasspath()) {
String testTmpDir = TempDirUtils.getTempDirectory("kudu-binary-jar").toString();
LOG.info("Using Kudu binary jar directory: {}", testTmpDir);
return extractor.extractKuduBinaryArtifact(testTmpDir);
File testTmpDir = TempDirUtils.makeTempDirectory("kudu-binary-jar",
TempDirUtils.DeleteOnExit.DELETE_RECURSIVELY_ON_EXIT);
LOG.info("Using Kudu binary jar directory: {}", testTmpDir.getAbsolutePath());
return extractor.extractKuduBinaryArtifact(testTmpDir.getAbsolutePath());
}
} catch (IOException ex) {
LOG.warn("Unable to extract a Kudu binary jar", ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -130,7 +129,8 @@ private MiniKuduCluster(boolean enableKerberos,
// If a cluster root was not set, create a unique temp directory to use.
// The mini cluster will clean this directory up on exit.
try {
File tempRoot = TempDirUtils.getTempDirectory("mini-kudu-cluster");
File tempRoot = TempDirUtils.makeTempDirectory("mini-kudu-cluster",
TempDirUtils.DeleteOnExit.NO_DELETE_ON_EXIT);
this.clusterRoot = tempRoot.toString();
} catch (IOException ex) {
throw new RuntimeException("Could not create cluster root directory", ex);
Expand Down

0 comments on commit b2d95c6

Please sign in to comment.