-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #465 from renaissance-benchmarks/topic/issue464
Close URLClassLoader instances to enable proper cleanup on NFS
- Loading branch information
Showing
5 changed files
with
99 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
renaissance-core/src/main/java/org/renaissance/core/Cleaner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package org.renaissance.core; | ||
|
||
import java.io.Closeable; | ||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.LinkedHashSet; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
final class Cleaner { | ||
private static Set<Closeable> registeredCloseables = new LinkedHashSet<>(); | ||
private static Set<Path> registeredPaths = new LinkedHashSet<>(); | ||
|
||
private Cleaner() {} | ||
|
||
static Path deleteOnExit(Path path) { | ||
return register(registeredPaths, path); | ||
} | ||
|
||
static <T extends Closeable> T closeOnExit(T closeable) { | ||
return register(registeredCloseables, closeable); | ||
} | ||
|
||
private synchronized static <T> T register(Set<? super T> registry, T item) { | ||
if (registry == null) { | ||
throw new IllegalStateException("Shutdown in progress"); | ||
} else { | ||
registry.add(Objects.requireNonNull(item)); | ||
} | ||
|
||
return item; | ||
} | ||
|
||
private static void run() { | ||
Set<Closeable> closeables; | ||
Set<Path> paths; | ||
|
||
synchronized (Cleaner.class) { | ||
closeables = registeredCloseables; | ||
registeredCloseables = null; | ||
|
||
paths = registeredPaths; | ||
registeredPaths = null; | ||
} | ||
|
||
// Close closeables BEFORE deleting files/directories. | ||
// In particular URLClassLoader instances which keep files open. | ||
// Delete files/directories AFTER closing closeables. | ||
|
||
cleanupEach(closeables, "close", Closeable::close).clear(); | ||
cleanupEach(paths, "delete", DirUtils::deleteRecursively).clear(); | ||
} | ||
|
||
@FunctionalInterface | ||
private interface Action<T> { | ||
void accept(T object) throws IOException; | ||
} | ||
|
||
private static <T, C extends Iterable<T>> C cleanupEach(C items, String name, Action<T> action) { | ||
for (T item : items) { | ||
try { | ||
action.accept(item); | ||
} catch (IOException e) { | ||
// Just a notification. This should be rare and is not critical. | ||
System.err.format("warning: failed to %s %s on shutdown: %s\n", name, item, e); | ||
} | ||
} | ||
|
||
return items; | ||
} | ||
|
||
static { | ||
Runtime.getRuntime().addShutdownHook(new Thread(Cleaner::run)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters