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

jojochuang 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 c4b180dde8e HDDS-17358. Closing the OM DB during a compaction DAG 
prune batch stalls and can crash the OM when the pruner reads the freed DB 
(#11705)
c4b180dde8e is described below

commit c4b180dde8ebe3d4bd07e26c81a67f1bff526fd6
Author: Siyao Meng <[email protected]>
AuthorDate: Thu Oct 29 23:46:03 2026 -0700

    HDDS-17358. Closing the OM DB during a compaction DAG prune batch stalls 
and can crash the OM when the pruner reads the freed DB (#11705)
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .../ozone/rocksdiff/RocksDBCheckpointDiffer.java   | 30 ++++++---
 .../rocksdiff/TestRocksDBCheckpointDiffer.java     | 71 ++++++++++++++++++++++
 2 files changed, 92 insertions(+), 9 deletions(-)

diff --git 
a/hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/rocksdiff/RocksDBCheckpointDiffer.java
 
b/hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/rocksdiff/RocksDBCheckpointDiffer.java
index 470722e3bf5..0566ee10ffd 100644
--- 
a/hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/rocksdiff/RocksDBCheckpointDiffer.java
+++ 
b/hadoop-hdds/rocksdb-checkpoint-differ/src/main/java/org/apache/ozone/rocksdiff/RocksDBCheckpointDiffer.java
@@ -340,16 +340,18 @@ private void createSstBackUpDir() {
   public void close() {
     if (!closed) {
       synchronized (this) {
-        if (!closed) {
-          closed = true;
-          if (scheduler != null) {
-            LOG.info("Shutting down {}.", DAG_PRUNING_SERVICE_NAME);
-            scheduler.close();
-          }
-          if (sstFilePruningMetrics != null) {
-            sstFilePruningMetrics.unRegister();
-          }
+        if (closed) {
+          return;
         }
+        closed = true;
+      }
+      // Not under the lock: a task waiting for it must be able to get it, see 
closed and stop.
+      if (scheduler != null) {
+        LOG.info("Shutting down {}.", DAG_PRUNING_SERVICE_NAME);
+        scheduler.close();
+      }
+      if (sstFilePruningMetrics != null) {
+        sstFilePruningMetrics.unRegister();
       }
     }
   }
@@ -1123,6 +1125,9 @@ public void pruneOlderSnapshotsWithCompactionHistory() {
    * older than the maximum allowed in the compaction DAG.
    */
   private synchronized Pair<Set<String>, List<byte[]>> getOlderFileNodes() {
+    if (closed) {
+      return Pair.of(Collections.emptySet(), Collections.emptyList());
+    }
     long compactionLogPruneStartTime = System.currentTimeMillis();
     Set<String> compactionNodes = new HashSet<>();
     List<byte[]> keysToRemove = new ArrayList<>();
@@ -1156,6 +1161,9 @@ private synchronized Pair<Set<String>, List<byte[]>> 
getOlderFileNodes() {
 
   private synchronized void removeKeyFromCompactionLogTable(
       List<byte[]> keysToRemove) {
+    if (closed) {
+      return;
+    }
     try {
       for (byte[] key: keysToRemove) {
         activeRocksDB.get().delete(compactionLogTableCFHandle, key);
@@ -1340,6 +1348,10 @@ public void pruneSstFileValues() {
         CompactionLogEntry compactionLogEntry;
         // Get the compaction log entry.
         synchronized (this) {
+          // close() waits for this task only up to a timeout, then the DB is 
freed.
+          if (closed) {
+            break;
+          }
           try {
             compactionLogEntry = 
CompactionLogEntry.getCodec().fromPersistedFormat(
                 activeRocksDB.get().get(compactionLogTableCFHandle, 
compactionLogEntryKey));
diff --git 
a/hadoop-hdds/rocksdb-checkpoint-differ/src/test/java/org/apache/ozone/rocksdiff/TestRocksDBCheckpointDiffer.java
 
b/hadoop-hdds/rocksdb-checkpoint-differ/src/test/java/org/apache/ozone/rocksdiff/TestRocksDBCheckpointDiffer.java
index 506fa9ea6aa..d646fbec7bf 100644
--- 
a/hadoop-hdds/rocksdb-checkpoint-differ/src/test/java/org/apache/ozone/rocksdiff/TestRocksDBCheckpointDiffer.java
+++ 
b/hadoop-hdds/rocksdb-checkpoint-differ/src/test/java/org/apache/ozone/rocksdiff/TestRocksDBCheckpointDiffer.java
@@ -43,6 +43,7 @@
 import static org.junit.jupiter.api.Assertions.fail;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.anyLong;
 import static org.mockito.ArgumentMatchers.anyString;
 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.doAnswer;
@@ -54,6 +55,7 @@
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.graph.MutableGraph;
+import com.google.common.util.concurrent.Uninterruptibles;
 import java.io.File;
 import java.io.FileWriter;
 import java.io.IOException;
@@ -76,6 +78,7 @@
 import java.util.TreeMap;
 import java.util.UUID;
 import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
@@ -89,10 +92,12 @@
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 import org.apache.commons.lang3.RandomStringUtils;
+import org.apache.commons.lang3.reflect.FieldUtils;
 import org.apache.commons.lang3.tuple.Pair;
 import org.apache.hadoop.hdds.StringUtils;
 import org.apache.hadoop.hdds.conf.ConfigurationSource;
 import org.apache.hadoop.hdds.utils.IOUtils;
+import org.apache.hadoop.hdds.utils.Scheduler;
 import org.apache.hadoop.hdds.utils.db.CodecBuffer;
 import org.apache.hadoop.hdds.utils.db.ManagedRawSSTFileIterator;
 import org.apache.hadoop.hdds.utils.db.ManagedRawSSTFileReader;
@@ -1603,6 +1608,72 @@ public void 
testPruneSstFilesRetainsBackupFilesWhenCompactionDagIsEmpty() throws
     }
   }
 
+  /**
+   * A prune task can still be running when close() returns, because the 
scheduler wait is bounded, and RDBStore
+   * frees the DB right after close(). Such a task must not use the DB any 
more.
+   */
+  @Test
+  public void testPruneTasksDoNotUseDbAfterClose() throws Exception {
+    long oldCompactionTime = System.currentTimeMillis() - MINUTES.toMillis(20);
+    byte[] prunedKey = rocksDBCheckpointDiffer.addToCompactionLogTable(new 
CompactionLogEntry(1, oldCompactionTime,
+        Collections.singletonList(new CompactionFileInfo("000011", 
"/vol/bucket/a", "/vol/bucket/z", "keyTable")),
+        Collections.singletonList(new CompactionFileInfo("000012", 
"/vol/bucket/a", "/vol/bucket/z", "keyTable")),
+        null));
+    rocksDBCheckpointDiffer.pruneOlderSnapshotsWithCompactionHistory();
+    assertThat(activeRocksDB.get().get(compactionLogTableCFHandle, 
prunedKey)).isNull();
+
+    byte[] key = rocksDBCheckpointDiffer.addToCompactionLogTable(new 
CompactionLogEntry(2, oldCompactionTime,
+        Collections.singletonList(new CompactionFileInfo("000013", 
"/vol/bucket/a", "/vol/bucket/z", "keyTable")),
+        Collections.singletonList(new CompactionFileInfo("000014", 
"/vol/bucket/a", "/vol/bucket/z", "keyTable")),
+        null));
+    rocksDBCheckpointDiffer.loadAllCompactionLogs();
+    SSTFilePruningMetrics pruningMetrics = 
rocksDBCheckpointDiffer.getPruningMetrics();
+    assertThat(pruningMetrics.getPruneQueueSize()).isEqualTo(1L);
+
+    rocksDBCheckpointDiffer.close();
+    rocksDBCheckpointDiffer.pruneSstFileValues();
+    rocksDBCheckpointDiffer.pruneOlderSnapshotsWithCompactionHistory();
+
+    assertThat(activeRocksDB.get().get(compactionLogTableCFHandle, 
key)).isNotNull();
+    assertThat(pruningMetrics.getPruneQueueSize()).isEqualTo(1L);
+  }
+
+  /**
+   * A prune task that waits for the differ lock while close() runs cannot be 
interrupted, so close() must not hold
+   * that lock while it waits for the tasks to end.
+   */
+  @Test
+  public void testCloseDoesNotWaitForTaskBlockedOnDifferLock(@TempDir Path 
dir) throws Exception {
+    ConfigurationSource config = mock(ConfigurationSource.class);
+    
when(config.getTimeDuration(eq(OZONE_OM_SNAPSHOT_COMPACTION_DAG_PRUNE_DAEMON_RUN_INTERVAL),
 anyLong(), any()))
+        .thenReturn(TimeUnit.HOURS.toMillis(1));
+    RocksDBCheckpointDiffer differ = new 
RocksDBCheckpointDiffer(dir.toString(), SST_BACK_UP_DIR_NAME,
+        COMPACTION_LOG_DIR_NAME, dir.resolve("db").toString(), config, 
readLock -> () -> { });
+    Scheduler scheduler = (Scheduler) FieldUtils.readField(differ, 
"scheduler", true);
+
+    CountDownLatch taskStarted = new CountDownLatch(1);
+    CountDownLatch closing = new CountDownLatch(1);
+    CountDownLatch taskDone = new CountDownLatch(1);
+    // Stands in for a prune task that reaches synchronized (this) between two 
entries while close() runs.
+    scheduler.schedule(() -> {
+      taskStarted.countDown();
+      Uninterruptibles.awaitUninterruptibly(closing);
+      synchronized (differ) {
+        taskDone.countDown();
+      }
+    }, 0, TimeUnit.MILLISECONDS);
+    assertThat(taskStarted.await(10, TimeUnit.SECONDS)).isTrue();
+
+    Thread closer = new Thread(differ::close);
+    closer.start();
+    GenericTestUtils.waitFor(scheduler::isClosed, 10, 10000);
+    closing.countDown();
+    closer.join(TimeUnit.SECONDS.toMillis(10));
+
+    assertThat(closer.isAlive()).as("close() still waiting").isFalse();
+    assertThat(taskDone.getCount()).isZero();
+  }
+
   /**
    * Test cases for testGetSSTDiffListWithoutDB.
    */


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

Reply via email to