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


##########
bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/BufferedChannel.java:
##########
@@ -229,10 +251,19 @@ public void flushAndForceWriteIfRegularFlush(boolean 
forceMetadata) throws IOExc
      * @throws IOException if the write fails.
      */
     public synchronized void flush() throws IOException {
+        checkWritable();
         ByteBuffer toWrite = writeBuffer.internalNioBuffer(0, 
writeBuffer.writerIndex());
-        do {
-            fileChannel.write(toWrite);
-        } while (toWrite.hasRemaining());
+        try {
+            while (toWrite.hasRemaining()) {
+                int written = fileChannel.write(toWrite);
+                if (written <= 0) {
+                    throw new IOException("Unable to make progress while 
flushing buffered channel");
+                }
+            }
+        } catch (IOException e) {
+            markWriteFailure(e);

Review Comment:
   Test gap: nothing in the PR's tests would notice this `markWriteFailure(e)` 
being removed. I deleted just this line and all 15 `BufferedChannelTest` tests 
still passed. The existing poisoning tests 
(`testPartialFlushFailurePoisons...`) trigger the failure through `write()`, 
whose own catch block marks the channel; a failure of a direct `flush()` or 
`flushAndForceWrite()` (the calls the sync thread and log rotation make) is 
never exercised.
   
   Without the poisoning here, a second `flush()` after a part-way failure 
succeeds and rewrites the whole buffer from index 0, which is exactly the 
duplicate-bytes situation this PR is meant to prevent. Tests that fail without 
this line and pass with it, using the existing 
`PartialFailingFileChannel(delegate, 8)`:
   ```java
   logChannel.write(Unpooled.wrappedBuffer(new byte[12]));          // buffered 
only, no internal flush
   assertThrows(IOException.class, () -> logChannel.flush());       // partial 
write, then failure
   IOException second = assertThrows(IOException.class, () -> 
logChannel.flush());   // fails without the line: nothing thrown
   assertTrue(second.getMessage().contains("failed state"));
   ```
   The same with `flushAndForceWrite(false)`, and a control with a healthy 
channel (flush, write, flush) both pass on this head.



##########
bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/EntryLogManagerBase.java:
##########
@@ -118,18 +128,53 @@ List<BufferedLogChannel> getRotatedLogChannels() {
     }
 
     @Override
-    public void flush() throws IOException {
+    public void setFatalErrorListener(LedgerDirsListener fatalErrorListener) {
+        this.fatalErrorListener = fatalErrorListener != null ? 
fatalErrorListener : NOOP_FATAL_ERROR_LISTENER;
+    }
+
+    void notifyFatalEntryLogWriteFailure(String message, Throwable cause) {
+        log.error().exception(cause).log(message);
+        fatalErrorListener.fatalError();
+        for (LedgerDirsListener listener : ledgerDirsManager.getListeners()) {
+            if (listener != fatalErrorListener) {
+                listener.fatalError();
+            }
+        }
+    }
+
+    @Override
+    public synchronized void flush() throws IOException {

Review Comment:
   This makes `flush()` hold the manager monitor for the whole flush, including 
the fsync, and the monitor is also what 
`EntryLogManagerForSingleEntryLog.addEntry` (`synchronized`) and 
`getCurrentLogForLedgerForAddEntry` (`synchronized`) use. Before this PR 
`flush()` was not `synchronized`, so the `force()` in `flushCurrentLogs()` ran 
outside the monitor, and `BufferedChannel.forceWrite` is written so that writes 
can continue while a force is in progress. With this change every `addEntry` on 
the single-entry-log manager waits for the fsync to finish.
   
   Reproduced on this head: put a spy channel in the manager whose 
`flushAndForceWrite` waits (simulating a slow fsync), start `manager.flush()` 
on one thread, then call `entryLogger.addEntry(...)` from another:
   ```
   testAdvAddEntryIsNotBlockedWhileFlushIsInFsync
     addEntry was blocked behind a flush that is waiting in fsync     (3 of 3 
runs, 3 s wait)
   ```
   The control without a flush in progress passes, and removing only the 
`synchronized` keyword here makes the test pass. This affects 
`InterleavedLedgerStorage`/`SortedLedgerStorage` foreground writes and GC 
compaction writes. I did not measure `DbLedgerStorage`, whose foreground adds 
go to the write cache. `EntryLogManagerForEntryLogPerLedger` inherits this 
`flush()` and takes the same monitor in `setCurrentLogForLedgerAndAddToRotate`, 
so log rotation there would wait too (from reading the code, not tested).
   
   I think the lock is there to make the current-to-rotated hand-off atomic 
with flush snapshots. That only needs the monitor to take the snapshot of the 
current and rotated channels; the flush and fsync themselves can run outside it 
(the code already does this for `rotatedLogChannels` in 
`EntryLogManagerForSingleEntryLog.flushRotatedLogs`).



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