Skip to content

Commit

Permalink
Use random path for temp snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
sakno committed Jul 8, 2024
1 parent 582035b commit 5eb9604
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<long> WriteAsync<TEntry>(TEntry entry, CancellationToken token = default)
where TEntry : notnull, IRaftLogEntry
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ private protected sealed override async ValueTask InstallSnapshotAsync<TSnapshot
// Save the snapshot into temporary file to avoid corruption caused by network connection
string tempSnapshotFile, snapshotFile = this.snapshot.FileName;
var snapshotLength = snapshot.Length.GetValueOrDefault();
using (var tempSnapshot = new Snapshot(Location, snapshotBufferSize, in bufferManager, 0, WriteMode.NoFlush, tempSnapshot: true, initialSize: snapshotLength))
using (var tempSnapshot = new Snapshot(location: null, snapshotBufferSize, in bufferManager, 0, WriteMode.NoFlush, initialSize: snapshotLength))
{
tempSnapshotFile = tempSnapshot.FileName;
snapshotLength = await tempSnapshot.WriteAsync(snapshot).ConfigureAwait(false);
Expand Down

0 comments on commit 5eb9604

Please sign in to comment.