From b2ed7d166cf04669a7103428b3bb656fc0141043 Mon Sep 17 00:00:00 2001 From: Ahmed Yasin Koculu Date: Thu, 4 Aug 2022 00:40:20 +0200 Subject: [PATCH] File intermediate buffers should be flushed on all flushes. --- .../Segments/Disk/CompressedFileRandomAccessDevice.cs | 6 +++--- src/ZoneTree/Segments/Disk/FileRandomAccessDevice.cs | 4 ++-- src/ZoneTree/WAL/CompressedFileStream.cs | 2 +- src/ZoneTree/WAL/FileSystemWriteAheadLog.cs | 4 ++-- src/ZoneTree/WAL/IncrementalLogAppender.cs | 10 +++++----- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/ZoneTree/Segments/Disk/CompressedFileRandomAccessDevice.cs b/src/ZoneTree/Segments/Disk/CompressedFileRandomAccessDevice.cs index 08274d8..8fd7b5f 100644 --- a/src/ZoneTree/Segments/Disk/CompressedFileRandomAccessDevice.cs +++ b/src/ZoneTree/Segments/Disk/CompressedFileRandomAccessDevice.cs @@ -139,7 +139,7 @@ private void AppendBlock(DecompressedBlock nextBlock) CompressedBlockPositions.Add(offset); CompressedBlockLengths.Add(compressedBytes.Length); FileStream.Write(compressedBytes); - FileStream.Flush(); + FileStream.Flush(true); DecompressedBlocks.TryRemove(nextBlock.BlockIndex, out _); ++NextBlockIndex; LastBlockLength = 0; @@ -219,7 +219,7 @@ public void Close() DecompressedBlocks.Clear(); if (FileStream == null) return; - FileStream.Flush(); + FileStream.Flush(true); FileStream.Dispose(); FileStream = null; if (Writable) @@ -274,7 +274,7 @@ void WriteCompressedBlockPositionsAndLengths() } bw.Write(positions.Count); bw.Write(offset); - FileStream.Flush(); + FileStream.Flush(true); } (List positions, List lengths) ReadCompressedBlockPositionsAndLengths() diff --git a/src/ZoneTree/Segments/Disk/FileRandomAccessDevice.cs b/src/ZoneTree/Segments/Disk/FileRandomAccessDevice.cs index f15e19a..28dfbc6 100644 --- a/src/ZoneTree/Segments/Disk/FileRandomAccessDevice.cs +++ b/src/ZoneTree/Segments/Disk/FileRandomAccessDevice.cs @@ -46,7 +46,7 @@ public long AppendBytesReturnPosition(byte[] bytes) { var pos = FileStream.Position; FileStream.Write(bytes); - FileStream.Flush(); + FileStream.Flush(true); return pos; } @@ -81,7 +81,7 @@ public void Close() { if (FileStream == null) return; - FileStream.Flush(); + FileStream.Flush(true); FileStream.Dispose(); FileStream = null; if (Writable) diff --git a/src/ZoneTree/WAL/CompressedFileStream.cs b/src/ZoneTree/WAL/CompressedFileStream.cs index 6f1bc41..72092ae 100644 --- a/src/ZoneTree/WAL/CompressedFileStream.cs +++ b/src/ZoneTree/WAL/CompressedFileStream.cs @@ -160,7 +160,7 @@ public void WriteTail() BinaryChunkWriter.Write(tailBlock.Length); var bytes = tailBlock.GetBytes(0, tailBlock.Length); BinaryChunkWriter.Write(bytes); - TailStream.Flush(); + TailStream.Flush(true); LastWrittenTailIndex = tailBlock.BlockIndex; LastWrittenTailLength = tailBlock.Length; } diff --git a/src/ZoneTree/WAL/FileSystemWriteAheadLog.cs b/src/ZoneTree/WAL/FileSystemWriteAheadLog.cs index a863ca3..b5185fa 100644 --- a/src/ZoneTree/WAL/FileSystemWriteAheadLog.cs +++ b/src/ZoneTree/WAL/FileSystemWriteAheadLog.cs @@ -133,7 +133,7 @@ public void Dispose() private void Flush() { if (FileStream.CanWrite) - FileStream.Flush(); + FileStream.Flush(true); } public long ReplaceWriteAheadLog(TKey[] keys, TValue[] values, bool disableBackup) @@ -147,7 +147,7 @@ public long ReplaceWriteAheadLog(TKey[] keys, TValue[] values, bool disableBacku FilePath + ".full", () => { - FileStream.Flush(); + FileStream.Flush(true); FileStream.Seek(0, SeekOrigin.Begin); var existingLength = (int)FileStream.Length; var bytes = new byte[existingLength]; diff --git a/src/ZoneTree/WAL/IncrementalLogAppender.cs b/src/ZoneTree/WAL/IncrementalLogAppender.cs index 6d2c925..b38ffb7 100644 --- a/src/ZoneTree/WAL/IncrementalLogAppender.cs +++ b/src/ZoneTree/WAL/IncrementalLogAppender.cs @@ -51,7 +51,7 @@ public static void AppendLogToTheBackupFile(string backupFile, Func getB fs.Write(BitConverter.GetBytes(fs.Length)); fs.Write(BitConverter.GetBytes(fs.Length)); fs.Write(BitConverter.GetBytes(fs.Length)); - fs.Flush(); + fs.Flush(true); } // first append the additional data. @@ -59,17 +59,17 @@ public static void AppendLogToTheBackupFile(string backupFile, Func getB fs.Seek(0, SeekOrigin.End); fs.Write(bytes); - fs.Flush(); + fs.Flush(true); // now write the file length-stamps. // what happens if a crash happens with partial write of the fs Length? // To prevent that, we write and flush the length-stamp three times with separate flushes.. fs.Position = 0; fs.Write(BitConverter.GetBytes(fs.Length)); - fs.Flush(); + fs.Flush(true); fs.Write(BitConverter.GetBytes(fs.Length)); - fs.Flush(); + fs.Flush(true); fs.Write(BitConverter.GetBytes(fs.Length)); - fs.Flush(); + fs.Flush(true); } }