From b0c557fbfdbc1451f24b51c3246a34d61ce2d4bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Armando=20Garc=C3=ADa=20Sancio?= Date: Mon, 7 Aug 2023 14:52:17 -0700 Subject: [PATCH 1/2] KAFKA-15312; Force channel before atomic file move On ext4 file systems we have seen snapshots with zero-length files. This is possible if the file is closed and moved before forcing the channel to write to disk. --- .../java/org/apache/kafka/snapshot/FileRawSnapshotWriter.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/raft/src/main/java/org/apache/kafka/snapshot/FileRawSnapshotWriter.java b/raft/src/main/java/org/apache/kafka/snapshot/FileRawSnapshotWriter.java index 535c176c7280c..4fdbebda58aba 100644 --- a/raft/src/main/java/org/apache/kafka/snapshot/FileRawSnapshotWriter.java +++ b/raft/src/main/java/org/apache/kafka/snapshot/FileRawSnapshotWriter.java @@ -112,6 +112,9 @@ public void freeze() { checkIfFrozen("Freeze"); frozenSize = channel.size(); + // force the channel to write to the file system before closing, this guarantees that the file has the data + // on disk before preforming the atomic file move + channel.force(true); channel.close(); if (!tempSnapshotPath.toFile().setReadOnly()) { From 83ab0c867a2fdcaa59a8e223cfdeb51351150988 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Armando=20Garc=C3=ADa=20Sancio?= Date: Tue, 8 Aug 2023 07:08:40 -0700 Subject: [PATCH 2/2] KAFKA-15312; Fix comment --- .../java/org/apache/kafka/snapshot/FileRawSnapshotWriter.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/raft/src/main/java/org/apache/kafka/snapshot/FileRawSnapshotWriter.java b/raft/src/main/java/org/apache/kafka/snapshot/FileRawSnapshotWriter.java index 4fdbebda58aba..4dc1fe7da03d0 100644 --- a/raft/src/main/java/org/apache/kafka/snapshot/FileRawSnapshotWriter.java +++ b/raft/src/main/java/org/apache/kafka/snapshot/FileRawSnapshotWriter.java @@ -112,8 +112,8 @@ public void freeze() { checkIfFrozen("Freeze"); frozenSize = channel.size(); - // force the channel to write to the file system before closing, this guarantees that the file has the data - // on disk before preforming the atomic file move + // force the channel to write to the file system before closing, to make sure that the file has the data + // on disk before performing the atomic file move channel.force(true); channel.close();