From 5eb9604156abc374aa41d844371da0a6947b6ca3 Mon Sep 17 00:00:00 2001 From: sakno Date: Mon, 8 Jul 2024 11:20:34 +0300 Subject: [PATCH] Use random path for temp snapshot --- .../MemoryBasedStateMachine.SnapshotBuilder.cs | 14 +++++++++++--- .../Consensus/Raft/MemoryBasedStateMachine.cs | 2 +- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/cluster/DotNext.Net.Cluster/Net/Cluster/Consensus/Raft/MemoryBasedStateMachine.SnapshotBuilder.cs b/src/cluster/DotNext.Net.Cluster/Net/Cluster/Consensus/Raft/MemoryBasedStateMachine.SnapshotBuilder.cs index ead42a118..a1172e5aa 100644 --- a/src/cluster/DotNext.Net.Cluster/Net/Cluster/Consensus/Raft/MemoryBasedStateMachine.SnapshotBuilder.cs +++ b/src/cluster/DotNext.Net.Cluster/Net/Cluster/Consensus/Raft/MemoryBasedStateMachine.SnapshotBuilder.cs @@ -18,13 +18,21 @@ public partial class MemoryBasedStateMachine internal sealed class Snapshot : ConcurrentStorageAccess { private new const string FileName = "snapshot"; - private const string TempFileName = "snapshot.new"; - internal Snapshot(DirectoryInfo location, int bufferSize, in BufferManager manager, int readersCount, WriteMode writeMode, bool tempSnapshot = false, long initialSize = 0L) - : base(Path.Combine(location.FullName, tempSnapshot ? TempFileName : FileName), 0, bufferSize, manager.BufferAllocator, readersCount, writeMode, initialSize) + internal Snapshot(DirectoryInfo? location, int bufferSize, in BufferManager manager, int readersCount, WriteMode writeMode, long initialSize = 0L) + : base(GetPath(location), 0, bufferSize, manager.BufferAllocator, readersCount, writeMode, initialSize) { } + private static string GetPath(DirectoryInfo? location) + { + var (directory, fileName) = location is null + ? (Path.GetTempPath(), Path.GetRandomFileName()) + : (location.FullName, FileName); + + return Path.Combine(directory, fileName); + } + internal async ValueTask WriteAsync(TEntry entry, CancellationToken token = default) where TEntry : notnull, IRaftLogEntry { diff --git a/src/cluster/DotNext.Net.Cluster/Net/Cluster/Consensus/Raft/MemoryBasedStateMachine.cs b/src/cluster/DotNext.Net.Cluster/Net/Cluster/Consensus/Raft/MemoryBasedStateMachine.cs index 7db8178dc..1861db2c6 100644 --- a/src/cluster/DotNext.Net.Cluster/Net/Cluster/Consensus/Raft/MemoryBasedStateMachine.cs +++ b/src/cluster/DotNext.Net.Cluster/Net/Cluster/Consensus/Raft/MemoryBasedStateMachine.cs @@ -211,7 +211,7 @@ private protected sealed override async ValueTask InstallSnapshotAsync