This is an automated email from the ASF dual-hosted git repository.

ivandika3 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ozone.git


The following commit(s) were added to refs/heads/master by this push:
     new ba2e5cfa545 HDDS-17380. Fix deadlock when 
BackgroundContainerDataScanner shutdown during volume failure (#11729)
ba2e5cfa545 is described below

commit ba2e5cfa54543ce73bcd659f37737e2dc52da8be
Author: Ivan Andika <[email protected]>
AuthorDate: Sun Oct 25 14:27:53 2026 +0800

    HDDS-17380. Fix deadlock when BackgroundContainerDataScanner shutdown 
during volume failure (#11729)
    
    Generated-by: GPT 6.1 Sol
---
 .../AbstractBackgroundContainerScanner.java        | 24 ++++---
 .../ozoneimpl/BackgroundContainerDataScanner.java  |  4 +-
 .../TestBackgroundContainerDataScanner.java        | 74 ++++++++++++++++++++++
 3 files changed, 90 insertions(+), 12 deletions(-)

diff --git 
a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/ozoneimpl/AbstractBackgroundContainerScanner.java
 
b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/ozoneimpl/AbstractBackgroundContainerScanner.java
index 72662bd4571..14bd6c67e41 100644
--- 
a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/ozoneimpl/AbstractBackgroundContainerScanner.java
+++ 
b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/ozoneimpl/AbstractBackgroundContainerScanner.java
@@ -140,19 +140,23 @@ public final void handleRemainingSleep(long 
remainingSleep) {
   }
 
   /**
-   * Shutdown the current container scanning thread.
-   * If the thread is already being shutdown, the call will block until the
-   * shutdown completes.
+   * Requests shutdown and waits for the scanning thread to exit, unless 
called by the scanning thread itself.
+   * Concurrent callers also wait for shutdown to complete, unless interrupted.
+   *
+   * <p>Do not hold this object's monitor while calling this method: the 
scanning thread may also call shutdown.
    */
-  public synchronized void shutdown() {
+  public void shutdown() {
     if (stopping.compareAndSet(false, true)) {
       scannerThread.interrupt();
-      try {
-        scannerThread.join();
-      } catch (InterruptedException ex) {
-        LOG.warn("Unexpected exception while stopping data scanner.", ex);
-        Thread.currentThread().interrupt();
-      }
+    }
+    if (Thread.currentThread() == scannerThread) {
+      return;
+    }
+    try {
+      scannerThread.join();
+    } catch (InterruptedException ex) {
+      LOG.warn("Unexpected exception while stopping data scanner.", ex);
+      Thread.currentThread().interrupt();
     }
   }
 
diff --git 
a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/ozoneimpl/BackgroundContainerDataScanner.java
 
b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/ozoneimpl/BackgroundContainerDataScanner.java
index 8b1da664159..6bfcd50e6f3 100644
--- 
a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/ozoneimpl/BackgroundContainerDataScanner.java
+++ 
b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/ozoneimpl/BackgroundContainerDataScanner.java
@@ -77,11 +77,11 @@ public Iterator<Container<?>> getContainerIterator() {
   }
 
   @Override
-  public synchronized void shutdown() {
+  public void shutdown() {
     shutdown("");
   }
 
-  private synchronized void shutdown(String reason) {
+  private void shutdown(String reason) {
     String shutdownMessage = String.format(NAME_FORMAT, volume) + " is " +
         "shutting down. " + reason;
     LOG.info(shutdownMessage);
diff --git 
a/hadoop-hdds/container-service/src/test/java/org/apache/hadoop/ozone/container/ozoneimpl/TestBackgroundContainerDataScanner.java
 
b/hadoop-hdds/container-service/src/test/java/org/apache/hadoop/ozone/container/ozoneimpl/TestBackgroundContainerDataScanner.java
index 50968c49d49..0cc5334220a 100644
--- 
a/hadoop-hdds/container-service/src/test/java/org/apache/hadoop/ozone/container/ozoneimpl/TestBackgroundContainerDataScanner.java
+++ 
b/hadoop-hdds/container-service/src/test/java/org/apache/hadoop/ozone/container/ozoneimpl/TestBackgroundContainerDataScanner.java
@@ -17,6 +17,7 @@
 
 package org.apache.hadoop.ozone.container.ozoneimpl;
 
+import static 
com.google.common.util.concurrent.Uninterruptibles.awaitUninterruptibly;
 import static 
org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos.ContainerDataProto.State.UNHEALTHY;
 import static 
org.apache.hadoop.ozone.container.common.ContainerTestUtils.getHealthyDataScanResult;
 import static 
org.apache.hadoop.ozone.container.common.ContainerTestUtils.getHealthyMetadataScanResult;
@@ -26,6 +27,7 @@
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.mockito.ArgumentMatchers.anyLong;
 import static org.mockito.Mockito.any;
@@ -52,6 +54,7 @@
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
 import org.apache.hadoop.hdds.conf.OzoneConfiguration;
 import org.apache.hadoop.hdds.utils.db.Table;
 import org.apache.hadoop.hdfs.util.Canceler;
@@ -287,6 +290,77 @@ public void testShutdownDuringScan() throws Exception {
     verifyContainerMarkedUnhealthy(healthy, never());
   }
 
+  @Test
+  public void testShutdownDuringVolumeFailureDoesNotDeadlock() throws 
Exception {
+    CountDownLatch checkingVolume = new CountDownLatch(1);
+    CountDownLatch reportVolumeFailure = new CountDownLatch(1);
+    when(vol.isFailed()).thenAnswer(invocation -> {
+      checkingVolume.countDown();
+      awaitUninterruptibly(reportVolumeFailure);
+      return true;
+    });
+    Thread shutdownThread = new Thread(scanner::shutdown, "scanner-shutdown");
+    shutdownThread.setDaemon(true);
+    try {
+      scanner.start();
+      assertTrue(checkingVolume.await(5, TimeUnit.SECONDS));
+      shutdownThread.start();
+      // Let shutdown begin waiting for the worker before the worker detects 
the failed volume.
+      GenericTestUtils.waitFor(() -> shutdownThread.getState() == 
Thread.State.WAITING, 10, 5000);
+      reportVolumeFailure.countDown();
+
+      shutdownThread.join(5000);
+      assertFalse(shutdownThread.isAlive(), "Shutdown must not deadlock with 
volume failure detection");
+      assertFalse(scanner.isAlive(), "Shutdown must wait for the scanner to 
exit");
+    } finally {
+      reportVolumeFailure.countDown();
+      shutdownThread.interrupt();
+      shutdownThread.join(5000);
+      GenericTestUtils.waitFor(() -> !scanner.isAlive(), 10, 5000);
+    }
+  }
+
+  @Test
+  public void testConcurrentShutdownWaitsForScanner() throws Exception {
+    CountDownLatch scanning = new CountDownLatch(1);
+    CountDownLatch interrupted = new CountDownLatch(1);
+    CountDownLatch finishScan = new CountDownLatch(1);
+    when(healthy.scanData(any(), any())).thenAnswer(invocation -> {
+      scanning.countDown();
+      try {
+        finishScan.await();
+      } catch (InterruptedException ex) {
+        interrupted.countDown();
+        awaitUninterruptibly(finishScan);
+      }
+      return getHealthyDataScanResult();
+    });
+    ExecutorService shutdownExecutor = Executors.newFixedThreadPool(2);
+    try {
+      scanner.start();
+      assertTrue(scanning.await(5, TimeUnit.SECONDS));
+      Future<?> firstShutdown = shutdownExecutor.submit(scanner::shutdown);
+      assertTrue(interrupted.await(5, TimeUnit.SECONDS));
+      CountDownLatch secondShutdownStarted = new CountDownLatch(1);
+      Future<?> secondShutdown = shutdownExecutor.submit(() -> {
+        secondShutdownStarted.countDown();
+        scanner.shutdown();
+      });
+      assertTrue(secondShutdownStarted.await(5, TimeUnit.SECONDS));
+      assertThrows(TimeoutException.class, () -> secondShutdown.get(100, 
TimeUnit.MILLISECONDS));
+
+      finishScan.countDown();
+      firstShutdown.get(5, TimeUnit.SECONDS);
+      secondShutdown.get(5, TimeUnit.SECONDS);
+      assertFalse(scanner.isAlive());
+    } finally {
+      finishScan.countDown();
+      shutdownExecutor.shutdownNow();
+      assertTrue(shutdownExecutor.awaitTermination(5, TimeUnit.SECONDS));
+      GenericTestUtils.waitFor(() -> !scanner.isAlive(), 10, 5000);
+    }
+  }
+
   /**
    * Scanner shuts down cleanly when volume failure is detected mid-iteration.
    */


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to