jeanouii commented on code in PR #1654:
URL: https://github.com/apache/activemq/pull/1654#discussion_r2800175557
##########
activemq-broker/src/main/java/org/apache/activemq/util/IOHelper.java:
##########
@@ -28,20 +28,128 @@
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;
+import java.util.Queue;
import java.util.Stack;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* Collection of File and Folder utility methods.
*/
public final class IOHelper {
+ private static final Logger LOG = LoggerFactory.getLogger(IOHelper.class);
+
protected static final int MAX_DIR_NAME_LENGTH;
protected static final int MAX_FILE_NAME_LENGTH;
private static final int DEFAULT_BUFFER_SIZE = 4096;
+ private static final boolean IS_WINDOWS = System.getProperty("os.name",
"").toLowerCase().contains("win");
+
+ // Async cleanup for files that couldn't be deleted immediately (Windows
file locking)
+ private static final Queue<File> PENDING_DELETES = new
ConcurrentLinkedQueue<>();
+ private static final ScheduledExecutorService CLEANUP_EXECUTOR;
+
+ static {
+ MAX_DIR_NAME_LENGTH = Integer.getInteger("MaximumDirNameLength", 200);
+ MAX_FILE_NAME_LENGTH = Integer.getInteger("MaximumFileNameLength", 64);
+
+ // Only start cleanup thread on Windows where file locking is an issue
+ if (IS_WINDOWS) {
+ CLEANUP_EXECUTOR = Executors.newSingleThreadScheduledExecutor(r ->
{
+ final Thread t = new Thread(r, "IOHelper-AsyncCleanup");
+ t.setDaemon(true);
+ return t;
+ });
+
CLEANUP_EXECUTOR.scheduleWithFixedDelay(IOHelper::processAsyncDeletes, 5, 5,
TimeUnit.SECONDS);
+ } else {
+ CLEANUP_EXECUTOR = null;
+ }
+ }
private IOHelper() {
}
+ /**
+ * Process pending async deletes. Called periodically on Windows.
+ */
+ private static void processAsyncDeletes() {
+ int processed = 0;
+ File file;
+ while ((file = PENDING_DELETES.poll()) != null && processed < 100) {
+ processed++;
+ if (file.exists()) {
+ if (file.delete()) {
+ LOG.debug("Async cleanup: deleted {}", file);
+ } else {
+ // Still locked, re-queue for later
+ PENDING_DELETES.offer(file);
+ }
+ }
+ }
+ }
+
+ /**
+ * Schedule a file for async deletion. Used when immediate deletion fails
on Windows.
+ * The file will be deleted in the background when it becomes unlocked.
+ */
+ public static void scheduleAsyncDelete(File file) {
+ if (file != null && file.exists()) {
+ if (IS_WINDOWS && CLEANUP_EXECUTOR != null) {
+ PENDING_DELETES.offer(file);
+ LOG.debug("Scheduled async delete for: {}", file);
+ } else {
+ // On non-Windows, just use deleteOnExit
+ file.deleteOnExit();
+ }
+ }
Review Comment:
Can we test it in both places?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information, visit: https://activemq.apache.org/contact