C#: store replace on Windows tries POSIX-semantics rename first (store-windows-cs) (#152)

This commit is contained in:
Libby 2026-11-13 00:38:07 +00:00
commit a85c63712b
5 changed files with 466 additions and 46 deletions

View file

@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
@ -364,11 +365,11 @@ public sealed class CultMeshSingleFileStoreTests
[Test]
public void ACacheOpeningDuringMeshWritesNeverSeesAnEmptyStore()
{
// Windows replaces with ReplaceFile, which leaves a moment with nothing at the path for a lock-free reader: 1,722 empty
// stores in 17,313 opens at b1ff2dcb. That is the store's replace, not Mesh's, and it is the same for a cache flush beside a
// cache open; the fork is in the cut report.
if (OperatingSystem.IsWindows())
Assert.Ignore("Windows File.Replace is not atomic to a lock-free reader; see cut report fork mesh-reader-windows-replace-window.");
// Windows replaces with a POSIX-semantics rename first (StoreReplace), which makes a lock-free reader finding the path
// absent rare, not impossible: a stalled rename was measured leaving it absent for 5-47 ms on Windows. So on Windows a
// brief not-found (a read throwing, or a cache opening onto an absent path) is tolerated and reported; elsewhere rename
// is atomic and none is allowed.
var windows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
var path = Path.Combine(_root, "race.cc");
CultMesh.WriteSingleFileDocument(path, Key, Publication());
@ -386,7 +387,7 @@ public sealed class CultMeshSingleFileStoreTests
Volatile.Write(ref stop, 1);
});
int opens = 0, empty = 0, reads = 0;
int opens = 0, empty = 0, reads = 0, absent = 0;
var clock = Stopwatch.StartNew();
while (Volatile.Read(ref stop) == 0 && clock.Elapsed < TimeSpan.FromSeconds(60))
{
@ -397,6 +398,10 @@ public sealed class CultMeshSingleFileStoreTests
CultMesh.ReadSingleFileDocumentPayload(path, Key, SchemaId());
reads++;
}
catch (Exception ex) when (windows && (ex is FileNotFoundException || ex is DirectoryNotFoundException))
{
absent++;
}
catch (Exception ex)
{
errors.Enqueue("read: " + ex.GetType().Name + ": " + ex.Message);
@ -404,9 +409,10 @@ public sealed class CultMeshSingleFileStoreTests
}
writer.Wait(TimeSpan.FromSeconds(60)).Should().BeTrue();
TestContext.Out.WriteLine($"mesh writes={writes} cache opens={opens} empty stores={empty} mesh reads ok={reads} errors={errors.Count}");
TestContext.Out.WriteLine($"mesh writes={writes} cache opens={opens} empty stores={empty} not-found reads={absent} mesh reads ok={reads} errors={errors.Count}");
errors.Take(3).Should().BeEmpty();
empty.Should().Be(0, "a cache opened in the window where nothing was at the path");
if (!windows)
empty.Should().Be(0, "a cache opened in the window where nothing was at the path");
opens.Should().BeGreaterThan(0);
}