Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

LUCENE-5577: Temporary folder and file management (and cleanup facilities)

Dawid Weiss2b1c7f34c475
diff --cc lucene/core/src/test/org/apache/lucene/store/TestDirectory.java
index 7e0b4d308c2,e26b66f3fa7..45a26565631
--- a/lucene/core/src/test/org/apache/lucene/store/TestDirectory.java
+++ b/lucene/core/src/test/org/apache/lucene/store/TestDirectory.java
@@@ -290,7 -291,7 +291,8 @@@ public class TestDirectory extends Luce
}
public void testFsyncDoesntCreateNewFiles() throws Exception {
-- File path = TestUtil.getTempDir("nocreate");
++ File path = createTempDir("nocreate");
++ System.out.println(path.getAbsolutePath());
Directory fsdir = new SimpleFSDirectory(path);
// write a file
diff --cc lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java
index 7ac506f6356,160470f1f18..69b6394cbe5
--- a/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java
@@@ -2079,4 -2192,156 +2192,157 @@@ public abstract class LuceneTestCase ex
return false;
}
}
+
+ /**
+ * A base location for temporary files of a given test. Helps in figuring out
+ * which tests left which files and where.
+ */
+ private static File tempDirBase;
+
+ /**
+ * Retry to create temporary file name this many times.
+ */
+ private static final int TEMP_NAME_RETRY_THRESHOLD = 9999;
+
+ /**
+ * This method is deprecated for a reason. Do not use it. Call {@link #createTempDir()}
+ * or {@link #createTempDir(String)} or {@link #createTempFile(String, String)}.
+ */
+ @Deprecated
+ public static File getTempDirBase() {
+ synchronized (LuceneTestCase.class) {
+ if (tempDirBase == null) {
+ File directory = new File(System.getProperty("tempDir", System.getProperty("java.io.tmpdir")));
+ assert directory.exists() &&
+ directory.isDirectory() &&
+ directory.canWrite();
+
+ RandomizedContext ctx = RandomizedContext.current();
+ Class<?> clazz = ctx.getTargetClass();
+ String prefix = clazz.getName();
+ prefix = prefix.replaceFirst("^org.apache.lucene.", "lucene.");
+ prefix = prefix.replaceFirst("^org.apache.solr.", "solr.");
+
+ int attempt = 0;
+ File f;
+ do {
+ if (attempt++ >= TEMP_NAME_RETRY_THRESHOLD) {
+ throw new RuntimeException(
+ "Failed to get a temporary name too many times, check your temp directory and consider manually cleaning it: "
+ + directory.getAbsolutePath());
+ }
+ f = new File(directory, prefix + "-" + ctx.getRunnerSeedAsString()
+ + "-" + String.format(Locale.ENGLISH, "%03d", attempt));
+ } while (!f.mkdirs());
+
+ tempDirBase = f;
+ registerToRemoveAfterSuite(tempDirBase);
+ }
+ }
+ return tempDirBase;
+ }
+
++
++ /**
++ */
++ public static File createTempDir() {
++ return createTempDir("tempDir");
++ }
++
+ /**
+ */
- protected static File createTempDir(String prefix) {
++ public static File createTempDir(String prefix) {
+ File base = getTempDirBase();
+
+ int attempt = 0;
+ File f;
+ do {
+ if (attempt++ >= TEMP_NAME_RETRY_THRESHOLD) {
+ throw new RuntimeException(
+ "Failed to get a temporary name too many times, check your temp directory and consider manually cleaning it: "
+ + base.getAbsolutePath());
+ }
+ f = new File(base, prefix + "-" + String.format(Locale.ENGLISH, "%03d", attempt));
+ } while (!f.mkdirs());
+
+ registerToRemoveAfterSuite(f);
+ return f;
+ }
+
+ /**
+ */
- protected static File createTempFile(String prefix, String suffix) throws IOException {
++ public static File createTempFile(String prefix, String suffix) throws IOException {
+ File base = getTempDirBase();
+
+ int attempt = 0;
+ File f;
+ do {
+ if (attempt++ >= TEMP_NAME_RETRY_THRESHOLD) {
+ throw new RuntimeException(
+ "Failed to get a temporary name too many times, check your temp directory and consider manually cleaning it: "
+ + base.getAbsolutePath());
+ }
+ f = new File(base, prefix + "-" + String.format(Locale.ENGLISH, "%03d", attempt) + suffix);
+ } while (!f.createNewFile());
+
+ registerToRemoveAfterSuite(f);
+ return f;
+ }
+
+ /**
+ */
- protected static File createTempDir() {
- return createTempDir("tempDir");
- }
-
- /**
- */
- protected static File createTempFile() throws IOException {
++ public static File createTempFile() throws IOException {
+ return createTempFile("tempFile", ".tmp");
+ }
+
+ /**
+ * A queue of temporary resources to be removed after the
+ * suite completes.
+ * @see #registerToRemoveAfterSuite(File)
+ */
+ private final static Deque<File> cleanupQueue = new ArrayDeque<File>();
+
+ /**
+ * Register temporary folder for removal after the suite completes.
+ */
+ private static void registerToRemoveAfterSuite(File f) {
+ assert f != null;
+
+ if (LuceneTestCase.LEAVE_TEMPORARY) {
+ System.err.println("INFO: Will leave temporary file: " + f.getAbsolutePath());
+ return;
+ }
+
+ synchronized (cleanupQueue) {
+ cleanupQueue.addLast(f);
+ }
+ }
+
+ private static class TemporaryFilesCleanupRule extends TestRuleAdapter {
+ @Override
+ protected void afterAlways(List<Throwable> errors) throws Throwable {
+ if (LuceneTestCase.suiteFailureMarker.wasSuccessful()) {
+ synchronized (cleanupQueue) {
+ File [] everything = new File [cleanupQueue.size()];
+ for (int i = 0; !cleanupQueue.isEmpty(); i++) {
+ everything[i] = cleanupQueue.removeLast();
+ }
+
+ // Will throw an IOException on un-removable files.
+ try {
+ TestUtil.rm(everything);
+ } catch (IOException e) {
+ Class<?> suiteClass = RandomizedContext.current().getTargetClass();
+ if (suiteClass.isAnnotationPresent(SuppressTempFileChecks.class)) {
+ System.err.println("WARNING: Leftover undeleted temporary files (bugUrl: "
+ + suiteClass.getAnnotation(SuppressTempFileChecks.class).bugUrl() + "): "
+ + e.getMessage());
+ return;
+ }
+ throw e;
+ }
+ }
+ }
+ }
+ }
}