hangc0276 commented on code in PR #4837:
URL: https://github.com/apache/bookkeeper/pull/4837#discussion_r4760079776


##########
bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/storage/ldb/KeyValueStorageRocksDB.java:
##########
@@ -408,6 +412,8 @@ public void compact(byte[] firstKey, byte[] lastKey) throws 
IOException {
 
     @Override
     public void compact() throws IOException {
+        // setCanceled(false) initializes the native atomic flag before 
CompactRange receives its options copy.
+        CompactRangeOptions compactRangeOptions = new 
CompactRangeOptions().setCanceled(false);
         try {
             final long start = System.currentTimeMillis();
             final int oriRocksDBFileCount = db.getLiveFilesMetaData().size();

Review Comment:
   **Small window where `DELETE` returns `false` while a compaction is already 
counted as running.** `EntryLocationIndex` sets `isCompacting = true` before 
calling `compact()`, but `currentCompactRangeOptions` is only published at line 
427, after `getLiveFilesMetaData()` and `getRocksDBSize()`. A `DELETE` in that 
gap returns `false` and cancels nothing, while a `GET` already says the 
location is compacting. The same kind of gap exists at the end, between the 
`finally` here and `isCompacting = false` in `EntryLocationIndex`.
   
   I measured the start-up gap at about 1 ms on a 3M-entry DB (mostly thread 
start-up). With many thousands of SST files `getLiveFilesMetaData()` takes 
longer, so the gap grows. Nothing breaks, because the operator can retry, but 
moving the `synchronized (compactionLock) { currentCompactRangeOptions = ... }` 
block above these two calls closes it.



##########
bookkeeper-server/src/main/java/org/apache/bookkeeper/server/http/service/TriggerLocationCompactService.java:
##########
@@ -128,11 +130,36 @@ public HttpServiceResponse handle(HttpServiceRequest 
request) throws Exception {
             response.setBody(jsonResponse);
             response.setCode(HttpServer.StatusCode.OK);
             return response;
+        } else if (HttpServer.Method.DELETE == request.getMethod()) {

Review Comment:
   Two behaviours worth documenting here and in the HTTP docs:
   1. This only cancels a compaction that BookKeeper started through 
`KeyValueStorageRocksDB.compact()` (operator `PUT` or the GC interval). 
RocksDB's own background compactions are not touched, so a `false` result does 
not mean "no compaction I/O is happening".
   2. If `entryLocationCompactionInterval > 0`, the GC thread starts a new 
compaction at the next interval, so a cancel only stops the current run.
   
   I checked the end-to-end path against real RocksDB: the compaction ended as 
`EntryLocationIndexCompactionCancelledException`, a second cancel returned 
`false`, and the next `compact()` ran normally. I did not run the GC-triggered 
path itself; it goes through the same `compact()`.



##########
bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/storage/ldb/EntryLocationIndexCompactionCancelledException.java:
##########
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.bookkeeper.bookie.storage.ldb;
+
+import java.io.IOException;
+
+/**
+ * Signals that an operator cancelled a manually triggered entry-location 
index RocksDB compaction.

Review Comment:
   The wording "manually triggered" is too narrow. `compact()` is also called 
from `GarbageCollectorThread` (`entryLocationCompactionInterval > 0`, via 
`ledgerStorage.entryLocationCompact()`), so interval-triggered compactions are 
cancelled by the same `DELETE`. The message on line 32 has the same problem. 
Something like "BookKeeper-initiated entry-location index compaction" would 
match what the code does.



##########
bookkeeper-server/src/test/java/org/apache/bookkeeper/bookie/storage/ldb/KeyValueStorageRocksDBTest.java:
##########
@@ -36,12 +37,48 @@
 import org.rocksdb.ChecksumType;
 import org.rocksdb.ColumnFamilyDescriptor;
 import org.rocksdb.ColumnFamilyOptions;
+import org.rocksdb.CompactRangeOptions;
 import org.rocksdb.CompressionType;
 import org.rocksdb.DBOptions;
+import org.rocksdb.FlushOptions;
 import org.rocksdb.Options;
+import org.rocksdb.RocksDB;
+import org.rocksdb.RocksDBException;
+import org.rocksdb.Status;
 
 public class KeyValueStorageRocksDBTest {
 
+    @Test
+    public void testCancelledCompactRangeReturnsIncompleteStatus() throws 
Exception {
+        RocksDB.loadLibrary();
+        File tmpDir = 
Files.createTempDirectory("bk-kv-rocksdbtest-cancelled-compact").toFile();
+        try (Options options = new Options().setCreateIfMissing(true);
+                RocksDB rocksDB = RocksDB.open(options, tmpDir.toString());
+                CompactRangeOptions compactRangeOptions = new 
CompactRangeOptions().setCanceled(true)) {
+            rocksDB.put(new byte[] {1}, new byte[] {1});
+
+            RocksDBException exception = assertThrows(RocksDBException.class,
+                    () -> 
rocksDB.compactRange(rocksDB.getDefaultColumnFamily(), null, null, 
compactRangeOptions));
+            assertNotNull(exception.getStatus());
+            assertEquals(Status.Code.Incomplete, 
exception.getStatus().getCode());
+            
assertTrue(KeyValueStorageRocksDB.isCompactionCancellationStatus(exception));
+        }
+    }
+
+    @Test
+    public void testCancelCompactionWhenNoCompactionIsRunning() throws 
Exception {

Review Comment:
   The real cancel path (cancel while `compact()` is running in RocksDB) has no 
test now that the flaky one was removed; the current tests only check the 
status mapping, the idle case and the HTTP layer with mocks. I wrote an 
end-to-end test that did not flake in my runs (a few runs on macOS):
   - Populate a `KeyValueStorageRocksDB` with ~3M entries through a batch, then 
`db.sync()`.
   - Run `db.compact()` in a thread and spin on `cancelCompaction()` until it 
returns `true` (or the thread ends).
   - Assert that the thread's outcome is either normal completion or 
`EntryLocationIndexCompactionCancelledException`, never a generic `IOException`.
   - Control: a second `compact()` afterwards completes and 
`cancelCompaction()` returns `false`.
   
   The wait loop means it doesn't depend on timing. Happy to share it.



##########
site3/website/docs/admin/http.md:
##########
@@ -522,6 +522,32 @@ Currently all the HTTP endpoints could be divided into 
these 5 components:
        }
        ```
 
+3. Method: DELETE
+    * Description: Request cancellation of running manually triggered entry 
location index RocksDB compactions.

Review Comment:
   Suggest rewording "running manually triggered entry location index RocksDB 
compactions". It also cancels interval-triggered ones 
(`entryLocationCompactionInterval`), and it does not cancel RocksDB's own 
background compactions. A note that the next scheduled interval will start a 
new run would also help operators.



-- 
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]

Reply via email to