HTHou commented on code in PR #19287:
URL: https://github.com/apache/iotdb/pull/19287#discussion_r4581674917


##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/checkpoint/CheckpointManager.java:
##########
@@ -250,17 +232,62 @@ public void fsyncCheckpointFile() {
     }
   }
 
-  private boolean tryRollingLogWriter() throws IOException {
-    if (currentLogWriter.size() < 
config.getCheckpointFileSizeThresholdInByte()) {
-      return false;
+  private void rollLogWriterIfNeeded() throws IOException {
+    long size = currentLogWriter.size();
+    // A global snapshot can exceed the threshold by itself. Wait for an 
incremental checkpoint.
+    if (size <= initialCheckpointSize || size < 
config.getCheckpointFileSizeThresholdInByte()) {
+      return;
     }
-    currentLogWriter.close();
-    currentCheckPointFileVersion++;
+    long nextVersion = currentCheckPointFileVersion + 1;
     File nextLogFile =
         SystemFileFactory.INSTANCE.getFile(
-            logDirectory, 
CheckpointFileUtils.getLogFileName(currentCheckPointFileVersion));
-    currentLogWriter = new CheckpointWriter(nextLogFile);
-    return true;
+            logDirectory, CheckpointFileUtils.getLogFileName(nextVersion));
+    ILogWriter oldWriter = currentLogWriter;
+    currentLogWriter = initializeLogWriter(nextLogFile, false);
+    currentCheckPointFileVersion = nextVersion;
+    initialCheckpointSize = currentLogWriter.size();
+    oldWriter.close();
+    Files.deleteIfExists(oldWriter.getLogFile().toPath());
+  }
+
+  private CheckpointWriter initializeLogWriter(File file, boolean append) 
throws IOException {
+    // The .tmp suffix keeps a candidate out of checkpoint recovery, even 
after a crash.
+    Path candidate =
+        append
+            ? null
+            : Files.createTempFile(

Review Comment:
   **[P3] Remove abandoned checkpoint candidates after successful recovery.** A 
process exit before publication leaves this `.tmp` file in the WAL directory. 
With `IoTConsensus`, successful recovery reuses the directory and only removes 
files matching the final checkpoint name, so these candidates accumulate across 
crashes. I verified that the candidate is still there after the WAL node is 
registered again. Can we remove files matching the candidate naming scheme 
after recovery succeeds, before registering the writer?



##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/io/CheckpointWriter.java:
##########
@@ -23,10 +23,34 @@
 
 import java.io.File;
 import java.io.IOException;
+import java.nio.file.FileAlreadyExistsException;
+import java.nio.file.Files;
+import java.nio.file.LinkOption;
+import java.nio.file.Path;
+import java.nio.file.StandardCopyOption;
 
 /** CheckpointWriter writes the binary {@link Checkpoint} into .checkpoint 
file. */
 public class CheckpointWriter extends LogWriter {
+  private File currentFile;
+
   public CheckpointWriter(File logFile) throws IOException {
     super(logFile, WALFormats.writeVersion(WALFileVersion.V2));
+    currentFile = logFile;
+  }
+
+  /** Publishes a forced checkpoint in the same directory, keeping its writer 
open for appends. */
+  public void publish(File targetFile) throws IOException {
+    Path target = targetFile.toPath();
+    // The manager owns the directory. Check for a stale target since 
ATOMIC_MOVE may replace it.
+    if (Files.exists(target, LinkOption.NOFOLLOW_LINKS)) {
+      throw new FileAlreadyExistsException(target.toString());
+    }
+    Files.move(currentFile.toPath(), target, StandardCopyOption.ATOMIC_MOVE);

Review Comment:
   **[P2] Make the published directory entry durable before switching 
writers.** The file is forced under its temporary name, but `ATOMIC_MOVE` does 
not guarantee the directory entry is on disk. After a power loss the final name 
can be missing. Even if the previous checkpoint survives, it cannot replay 
mappings appended to the new writer after the switch. Please make the directory 
update durable before switching writers or deleting the previous checkpoint, 
and handle sync failure without leaving a newer selectable checkpoint while 
continuing to append to the old one. See [Linux 
`fsync(2)`](https://man7.org/linux/man-pages/man2/fsync.2.html#DESCRIPTION).



##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/recover/CheckpointRecoverUtils.java:
##########
@@ -43,19 +44,20 @@ public static CheckpointInfo recoverMemTableInfo(File 
logDirectory) {
     // desc sort by version id
     CheckpointFileUtils.descSortByVersionId(checkpointFiles);
     // find last valid .checkpoint file and load checkpoints from it
-    long maxMemTableId = 0;
-    List<Checkpoint> checkpoints = null;
     for (File checkpointFile : checkpointFiles) {
       CheckpointReader reader = new CheckpointReader(checkpointFile);
-      maxMemTableId = reader.getMaxMemTableId();
-      checkpoints = reader.getCheckpoints();
-      if (!checkpoints.isEmpty()) {
-        break;
+      List<Checkpoint> checkpoints = reader.getCheckpoints();
+      // A file left by an older failed roll may have incremental records 
without a snapshot.
+      if (!checkpoints.isEmpty()

Review Comment:
   **[P1] Replay the intact incremental records from legacy rolls.** The old 
writer could fail to write `GLOBAL_MEMORY_TABLE_INFO` to `_1.checkpoint`, then 
successfully append `CREATE_MEMORY_TABLE` for memtable 2 and force its WAL. 
Skipping all of `_1` loses the mapping, even when `_0` still exists, because 
`_0` predates memtable 2. I verified that the base recovery code replays this 
write both with and without `_0`, while this PR skips it in both cases. Apply 
the readable `CREATE` and `FLUSH` records in version order after the last valid 
snapshot, and preserve recoverable records when no snapshot survives.



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