Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
496a176
feat(journaling): support composable grain participants
ReubenBond Aug 19, 2026
072e213
test(journaling): assert participant counts per activation
ReubenBond Aug 19, 2026
924aeab
Fix journal manager write lifecycle guards
ReubenBond Aug 20, 2026
a7bfdf4
Clarify journal recovery write errors
ReubenBond Aug 20, 2026
0f9ed94
Require successful journal initialization
ReubenBond Aug 20, 2026
4124671
Add stable journal observer snapshots
ReubenBond Aug 20, 2026
809d3be
Guard journal observer registration after shutdown
ReubenBond Aug 20, 2026
acf19c3
Preserve journal manager interface compatibility
ReubenBond Aug 20, 2026
1791acd
Guard journal recovery enqueue after shutdown
ReubenBond Aug 20, 2026
50a1bda
fix(journaling): snapshot observers before recovery
ReubenBond Aug 20, 2026
aaa3b30
docs(journaling): clarify observer snapshots
ReubenBond Aug 20, 2026
9b15f70
Materialize journaling participants before initialization
ReubenBond Aug 20, 2026
6cee8c4
Align journaling observer API baseline
ReubenBond Aug 20, 2026
5517304
fix(journaling): preserve rollback interface compatibility
ReubenBond Aug 20, 2026
cb4c42d
fix(journaling): preserve retryable conflict recovery
ReubenBond Aug 22, 2026
c56baf4
fix(journaling): model pending recovery retries
ReubenBond Aug 22, 2026
96bee11
refactor(journaling): make recovery states explicit
ReubenBond Aug 22, 2026
798df20
refactor(journaling): require new interface members
ReubenBond Aug 22, 2026
686ba1c
fix(journaling): preserve observer interface compatibility
ReubenBond Aug 23, 2026
76213db
fix(journaling): preserve rollback interface compatibility
ReubenBond Aug 23, 2026
1f1d9b9
refactor(journaling): reuse grain lifecycle participation
ReubenBond Aug 23, 2026
172615d
refactor(journaling): simplify rollback lifecycle
ReubenBond Aug 24, 2026
3cb26cc
fix(journaling): preserve state manager compatibility
ReubenBond Aug 24, 2026
57e80c6
test(journaling): rely on revert default behavior
ReubenBond Aug 24, 2026
4e8eafe
refactor(journaling): require rollback support
ReubenBond Aug 24, 2026
d4b8af3
fix(journaling): clarify unsupported state operations
ReubenBond Aug 24, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Orleans.Journaling/IJournaledStateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ public interface IJournaledStateManager : IAsyncDisposable
/// <returns>A <see cref="ValueTask"/> which represents the operation.</returns>
ValueTask WriteStateAsync(CancellationToken cancellationToken);

/// <summary>
/// Discards uncommitted mutations and reloads the last durable state.
/// </summary>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A <see cref="ValueTask"/> which represents the operation.</returns>
ValueTask RevertPendingChangesAsync(CancellationToken cancellationToken);

/// <summary>
/// Resets this instance, removing any persistent state.
/// </summary>
Expand Down
196 changes: 149 additions & 47 deletions src/Orleans.Journaling/JournaledStateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,42 @@ private async Task WorkLoop()
{
if (needsRecovery)
{
await RecoverAsync(_shutdownCancellation.Token).ConfigureAwait(true);
needsRecovery = false;
CompleteRecoveryTrigger();
bool fenceOnFailure;
lock (_lock)
{
fenceOnFailure = _state is not ManagerState.Unknown;
if (fenceOnFailure)
{
_state = ManagerState.Recovering;
}
}

try
{
await RecoverAsync(_shutdownCancellation.Token).ConfigureAwait(true);
lock (_lock)
{
if (fenceOnFailure)
{
_state = ManagerState.Ready;
}
}

needsRecovery = false;
CompleteRecoveryTrigger();
}
catch
{
lock (_lock)
{
if (fenceOnFailure)
{
_state = ManagerState.Ready;
}
}

throw;
}
}

WorkItem workItem;
Expand Down Expand Up @@ -364,25 +397,8 @@ private async Task WorkLoop()
writeSequence = new ReadOnlySequence<byte>(debugPoisonBuffer, 0, debugPoisonLength);
#endif

var writeCompleted = false;
try
{
if (isSnapshot && hasBufferToConsume)
{
await AppendStorageAsync(bufferToConsume.AsReadOnlySequence(), _shutdownCancellation.Token).ConfigureAwait(true);
lock (_lock)
{
_journalWriter.Consume(bufferToConsume);
foreach (var state in _states.Values)
{
state.OnWriteCompleted();
}
}

bufferToConsume.Dispose();
hasBufferToConsume = false;
}

if (isSnapshot)
{
await ReplaceStorageAsync(writeSequence, _shutdownCancellation.Token).ConfigureAwait(true);
Expand All @@ -392,35 +408,25 @@ private async Task WorkLoop()
await AppendStorageAsync(writeSequence, _shutdownCancellation.Token).ConfigureAwait(true);
}

writeCompleted = true;
}
finally
{
try
lock (_lock)
{
if (writeCompleted)
if (hasBufferToConsume)
{
lock (_lock)
{
if (hasBufferToConsume)
{
_journalWriter.Consume(bufferToConsume);
}
}
_journalWriter.Consume(bufferToConsume);
}
}
finally
}
finally
{
committedBuffer.Dispose();
if (hasBufferToConsume && !bufferToConsumeIsCommittedBuffer)
{
committedBuffer.Dispose();
if (hasBufferToConsume && !bufferToConsumeIsCommittedBuffer)
{
bufferToConsume.Dispose();
}
bufferToConsume.Dispose();
}
#if DEBUG
debugPoisonBuffer.AsSpan(0, debugPoisonLength).Fill(0x67);
ArrayPool<byte>.Shared.Return(debugPoisonBuffer);
debugPoisonBuffer.AsSpan(0, debugPoisonLength).Fill(0x67);
ArrayPool<byte>.Shared.Return(debugPoisonBuffer);
#endif
}
}

// Notify all states that the operation completed.
Expand All @@ -437,6 +443,7 @@ private async Task WorkLoop()
}
}
}

break;
}

Expand All @@ -461,6 +468,17 @@ private async Task WorkLoop()
break;
}

case RevertPendingChangesWorkItem:
{
await RecoverAsync(_shutdownCancellation.Token).ConfigureAwait(true);
lock (_lock)
{
_state = ManagerState.Ready;
}

break;
}

case InitializeWorkItem:
{
lock (_lock)
Expand Down Expand Up @@ -528,11 +546,23 @@ private async Task WorkLoop()
Debug.Assert(recoveryTrigger is null);
recoveryTrigger = workItem;
recoveryTriggerException = exception;
lock (_lock)
{
_state = ManagerState.Recovering;
}

needsRecovery = true;
}
else
{
workItem.SetException(exception);
if (workItem is RevertPendingChangesWorkItem)
{
lock (_lock)
{
_state = ManagerState.Fenced;
}
}
}
}
finally
Expand Down Expand Up @@ -664,10 +694,12 @@ private static void AppendUpdatesOrSnapshotState(JournalBufferWriter journalWrit

public async ValueTask DeleteStateAsync(CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
Task task;
bool didEnqueue;
lock (_lock)
{
ThrowIfStateOperationsUnavailable();
task = EnqueueOrGetPendingWorkItem<DeleteStateWorkItem>(out didEnqueue);
}

Expand All @@ -679,7 +711,7 @@ public async ValueTask DeleteStateAsync(CancellationToken cancellationToken)
var startTimestamp = _shared.TimeProvider.GetTimestamp();
try
{
await task;
await task.WaitAsync(cancellationToken);
_shared.Instruments.OnStateDeleteRequest(_shared.TimeProvider.GetElapsedTime(startTimestamp), succeeded: true);
}
catch
Expand Down Expand Up @@ -842,6 +874,7 @@ public async ValueTask WriteStateAsync(CancellationToken cancellationToken)
string operation;
lock (_lock)
{
ThrowIfStateOperationsUnavailable();
var isSnapshot = _migrationSnapshotRequired || _storage.IsCompactionRequested;
operation = isSnapshot ? JournalingInstruments.OperationSnapshot : JournalingInstruments.OperationAppend;
pendingWrite = isSnapshot
Expand All @@ -867,6 +900,49 @@ public async ValueTask WriteStateAsync(CancellationToken cancellationToken)
}
}

public async ValueTask RevertPendingChangesAsync(CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
Task pendingRecovery;
bool didEnqueue;
lock (_lock)
{
_shutdownCancellation.Token.ThrowIfCancellationRequested();
if (_state is ManagerState.Unknown)
{
throw new InvalidOperationException("The journaled state manager has not been initialized.");
}

pendingRecovery = EnqueueOrGetPendingWorkItem<RevertPendingChangesWorkItem>(out didEnqueue);
_state = ManagerState.Recovering;
}

if (didEnqueue)
{
_workSignal.Signal();
}

await pendingRecovery.WaitAsync(cancellationToken);
}

private void ThrowIfStateOperationsUnavailable()
{
_shutdownCancellation.Token.ThrowIfCancellationRequested();
switch (_state)
{
case ManagerState.Unknown:
throw new InvalidOperationException("The journaled state manager has not been initialized.");
case ManagerState.Ready:
return;
case ManagerState.Recovering:
throw new InvalidOperationException("Journaled state operations are unavailable while recovery is in progress.");
case ManagerState.Fenced:
throw new InvalidOperationException("Journaled state operations are fenced because recovery failed. Call RevertPendingChangesAsync to retry recovery.");
default:
throw new UnreachableException();
}
}

private async ValueTask ReadStorageAsync(IJournalStorageConsumer consumer, CancellationToken cancellationToken)
{
var startTimestamp = _shared.TimeProvider.GetTimestamp();
Expand Down Expand Up @@ -983,11 +1059,33 @@ private void BindState(string name, uint id)

private async Task StopAsync(CancellationToken cancellationToken)
{
_shutdownCancellation.Cancel();
lock (_lock)
{
_shutdownCancellation.Cancel();
}

_workSignal.Signal();
if (_workLoop is { } task)
try
{
if (_workLoop is { } task)
{
await task.WaitAsync(cancellationToken).ConfigureAwait(ConfigureAwaitOptions.ContinueOnCapturedContext | ConfigureAwaitOptions.SuppressThrowing);
}
}
finally
{
await task.WaitAsync(cancellationToken).ConfigureAwait(ConfigureAwaitOptions.ContinueOnCapturedContext | ConfigureAwaitOptions.SuppressThrowing);
CancelQueuedWorkItems(_shutdownCancellation.Token);
}
}

private void CancelQueuedWorkItems(CancellationToken cancellationToken)
{
lock (_lock)
{
while (_workQueue.TryDequeue(out var workItem))
{
workItem.TrySetCanceled(cancellationToken);
}
}
}

Expand Down Expand Up @@ -1103,6 +1201,8 @@ private sealed class WriteSnapshotWorkItem : WorkItem;

private sealed class DeleteStateWorkItem : WorkItem;

private sealed class RevertPendingChangesWorkItem : WorkItem;

private sealed class RegisterStateWorkItem(string name) : WorkItem(name)
{
public string Name => (string)Task.AsyncState!;
Expand All @@ -1111,7 +1211,9 @@ private sealed class RegisterStateWorkItem(string name) : WorkItem(name)
private enum ManagerState : byte
{
Unknown,
Ready
Ready,
Recovering,
Fenced
}

private sealed class StateDirectory(
Expand Down
1 change: 1 addition & 0 deletions src/api/Orleans.Journaling/Orleans.Journaling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ public partial interface IJournaledStateManager : System.IAsyncDisposable
System.Threading.Tasks.ValueTask DeleteStateAsync(System.Threading.CancellationToken cancellationToken);
System.Threading.Tasks.ValueTask InitializeAsync(System.Threading.CancellationToken cancellationToken);
void RegisterState(string name, IJournaledState state);
System.Threading.Tasks.ValueTask RevertPendingChangesAsync(System.Threading.CancellationToken cancellationToken);
System.Threading.Tasks.ValueTask System.IAsyncDisposable.DisposeAsync();
bool TryGetState(string name, out IJournaledState? state);
System.Threading.Tasks.ValueTask WriteStateAsync(System.Threading.CancellationToken cancellationToken);
Expand Down
2 changes: 2 additions & 0 deletions test/Benchmarks/Journaling/DurableListJournalBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ public bool TryGetState(string name, [NotNullWhen(true)] out IJournaledState sta

public ValueTask WriteStateAsync(CancellationToken cancellationToken) => default;

public ValueTask RevertPendingChangesAsync(CancellationToken cancellationToken) => throw new NotSupportedException();

public ValueTask DeleteStateAsync(CancellationToken cancellationToken) => default;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ public bool TryGetState(string name, [NotNullWhen(true)] out IJournaledState? st

public ValueTask WriteStateAsync(CancellationToken cancellationToken) => default;

public ValueTask RevertPendingChangesAsync(CancellationToken cancellationToken) => throw new NotSupportedException();

public ValueTask DeleteStateAsync(CancellationToken cancellationToken) => default;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ public bool TryGetState(string name, [NotNullWhen(true)] out IJournaledState? st

public ValueTask WriteStateAsync(CancellationToken cancellationToken) => default;

public ValueTask RevertPendingChangesAsync(CancellationToken cancellationToken) => throw new NotSupportedException();

public ValueTask DeleteStateAsync(CancellationToken cancellationToken) => default;
}

Expand Down
Loading
Loading