Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C#] FasterLog Commit Optimization (v2) #587

Merged
merged 38 commits into from
Nov 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
cbbf309
Checkpoint work
tli2 Oct 9, 2021
82ac15d
Add notion of manual commit
tli2 Oct 10, 2021
fb863a2
fix
tli2 Oct 10, 2021
543f81e
Merge branch 'master' into log-cookie
tli2 Oct 10, 2021
aaf0f3b
Recovery of commit cookie
tli2 Oct 10, 2021
1e2e8b9
alternative design of log-cookie
tli2 Oct 12, 2021
759e70b
Implementation improvement
tli2 Oct 14, 2021
24fe876
code review comments
tli2 Oct 18, 2021
cd40b11
Merge branch 'master' into log-cookie-alt
tli2 Oct 19, 2021
e406a49
Cleanup + testing
tli2 Oct 21, 2021
9e0a1a3
Merge branch 'master' into log-cookie-alt
tli2 Oct 21, 2021
32cf7ea
Add management utility for LogCommitManager
tli2 Oct 21, 2021
617f0cf
Handle race on manual commit code path where not the latest tail is w…
tli2 Oct 23, 2021
cc5b862
Rough sketch of new commit code path
tli2 Oct 24, 2021
a987f0f
save work
tli2 Oct 25, 2021
c21aa07
Prototype now works
tli2 Oct 26, 2021
23e329e
fix to truncate log
tli2 Oct 27, 2021
94b12c7
Add condition to only scan log in fastCommitMode
tli2 Oct 27, 2021
eca8ef3
Rough draft to allow for 1. elided commit record when fastCommit turn…
tli2 Oct 28, 2021
ab3be95
checkpoint changes
tli2 Oct 28, 2021
52d1bfd
fix bug with readonly log allocator initialization
tli2 Oct 29, 2021
ed3ea9e
disable fast commit temporarily for CI
tli2 Oct 29, 2021
710ade6
fix MLSD and mem device fast commit behavior
tli2 Oct 31, 2021
d841835
fix AzureStorageDevice exception type and remove self-termination on …
tli2 Oct 31, 2021
76f4cec
test case on concurrent inserts
tli2 Nov 1, 2021
a354b70
nits and test fixes
tli2 Nov 3, 2021
80356fb
Merge branch 'v2' of https://github.com/microsoft/FASTER into fastcommit
tli2 Nov 3, 2021
6d73d34
Re-enable epoch protection on CommitInternal
tli2 Nov 4, 2021
dcb9e04
Try to identify issue on CI
tli2 Nov 4, 2021
26f09ff
fix Linux bug?
tli2 Nov 4, 2021
86056a5
Remove debugging
tli2 Nov 4, 2021
f0bf05e
Ensure that full flush list error callbacks do not break FlushedUntil…
tli2 Nov 7, 2021
5e03355
Fix unclean shutdown of stress test
tli2 Nov 7, 2021
b60b96b
Merge branch 'commit-fix' into fastcommit
tli2 Nov 8, 2021
b5e726d
update device log test to scan until tail instead
tli2 Nov 8, 2021
dc6729f
change error code used for full flush list to better distinguish from…
tli2 Nov 11, 2021
1fb0144
nits
tli2 Nov 11, 2021
5e74ddf
Minor updates.
badrishc Nov 13, 2021
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
2 changes: 1 addition & 1 deletion cs/samples/FasterLogPubSub/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ static async Task CommitterAsync(FasterLog log, CancellationToken cancellationTo

Console.WriteLine("Committing...");

await log.CommitAsync(cancellationToken);
await log.CommitAsync(token: cancellationToken);
}
}
catch (OperationCanceledException) { }
Expand Down
6 changes: 5 additions & 1 deletion cs/src/core/Allocator/AllocatorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1762,8 +1762,12 @@ public void AsyncFlushPages(long fromAddress, long untilAddress)
}
else
{
// Because we are invoking the callback away from the usual codepath, need to externally
// ensure that flush address are updated in order
while (FlushedUntilAddress < asyncResult.fromAddress) Thread.Yield();
// Could not add to pending flush list, treat as a failed write
AsyncFlushPageCallback(1, 0, asyncResult);
// Use a special errorCode to convey that this is not from a syscall
AsyncFlushPageCallback(16000, 0, asyncResult);
}
}
else
Expand Down
33 changes: 28 additions & 5 deletions cs/src/core/Allocator/WorkQueueLIFO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using System;
using System.Collections.Concurrent;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -12,18 +14,30 @@ namespace FASTER.core
/// Shared work queue that ensures one worker at any given time. Uses LIFO ordering of work.
/// </summary>
/// <typeparam name="T"></typeparam>
class WorkQueueLIFO<T>
class WorkQueueLIFO<T> : IDisposable
{
const int kMaxQueueSize = 1 << 30;
readonly ConcurrentStack<T> _queue;
readonly Action<T> _work;
int _count;
private int _count;
private bool _disposed;

public WorkQueueLIFO(Action<T> work)
{
_queue = new ConcurrentStack<T>();
_work = work;
_count = 0;
_disposed = false;
}

public void Dispose()
{
_disposed = true;
// All future enqueue requests will no longer perform work after _disposed is set to true.
while (_count != 0)
Thread.Yield();
// After this point, any previous work must have completed. Even if another enqueue request manipulates the
// count field, they are guaranteed to see disposed and not enqueue any actual work.
}

/// <summary>
Expand All @@ -32,16 +46,24 @@ public WorkQueueLIFO(Action<T> work)
/// </summary>
/// <param name="work">Work to enqueue</param>
/// <param name="asTask">Process work as separate task</param>
public void EnqueueAndTryWork(T work, bool asTask)
/// <returns> whether the enqueue is successful. Enqueuing into a disposed WorkQueue will fail and the task will not be performed</returns>>
public bool EnqueueAndTryWork(T work, bool asTask)
{
Interlocked.Increment(ref _count);
if (_disposed)
{
// Remove self from count in case Dispose() is actively waiting for completion
Interlocked.Decrement(ref _count);
return false;
}

_queue.Push(work);

// Try to take over work queue processing if needed
while (true)
{
int count = _count;
if (count >= kMaxQueueSize) return;
if (count >= kMaxQueueSize) return true;
if (Interlocked.CompareExchange(ref _count, count + kMaxQueueSize, count) == count)
break;
}
Expand All @@ -50,6 +72,7 @@ public void EnqueueAndTryWork(T work, bool asTask)
_ = Task.Run(() => ProcessQueue());
else
ProcessQueue();
return true;
}

private void ProcessQueue()
Expand All @@ -59,12 +82,12 @@ private void ProcessQueue()
{
while (_queue.TryPop(out var workItem))
{
Interlocked.Decrement(ref _count);
try
{
_work(workItem);
}
catch { }
Interlocked.Decrement(ref _count);
}

int count = _count;
Expand Down
2 changes: 1 addition & 1 deletion cs/src/core/Epochs/LightEpoch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,8 @@ public void BumpCurrentEpoch(Action onDrain)
i = 0;
if (++j == 500)
{
// Spin until there is a free entry in the drain list
j = 0;
Debug.WriteLine("Delay finding a free entry in the drain list");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ public class DeviceLogCommitCheckpointManager : ILogCommitManager, ICheckpointMa
private IDevice singleLogCommitDevice;
private bool _disposed;

/// <summary>
/// Next commit number
/// </summary>
private long commitNum;

/// <summary>
/// Track historical commits for automatic purging
/// </summary>
Expand All @@ -52,7 +47,6 @@ public DeviceLogCommitCheckpointManager(INamedDeviceFactory deviceFactory, IChec
this.deviceFactory = deviceFactory;
this.checkpointNamingScheme = checkpointNamingScheme;

this.commitNum = 0;
this.semaphore = new SemaphoreSlim(0);

this.overwriteLogCommits = overwriteLogCommits;
Expand Down Expand Up @@ -99,9 +93,12 @@ public DeviceLogCommitCheckpointManager(INamedDeviceFactory deviceFactory, strin
#region ILogCommitManager

/// <inheritdoc />
public unsafe void Commit(long beginAddress, long untilAddress, byte[] commitMetadata)
public bool PreciseCommitNumRecoverySupport() => !overwriteLogCommits;

/// <inheritdoc />
public unsafe void Commit(long beginAddress, long untilAddress, byte[] commitMetadata, long commitNum)
{
var device = NextCommitDevice();
var device = NextCommitDevice(commitNum);

if (device == null) return;

Expand Down Expand Up @@ -135,6 +132,21 @@ public IEnumerable<long> ListCommits()
return deviceFactory.ListContents(checkpointNamingScheme.FasterLogCommitBasePath()).Select(e => checkpointNamingScheme.CommitNumber(e)).OrderByDescending(e => e);
}

/// <inheritdoc />
public void RemoveCommit(long commitNum)
{
if (overwriteLogCommits)
throw new FasterException("removing commit by commit num is not supported when overwriting log commits");
deviceFactory.Delete(checkpointNamingScheme.FasterLogCommitMetadata(commitNum));
}

/// <inheritdoc />
public void RemoveAllCommits()
{
foreach (var commitNum in ListCommits())
RemoveCommit(commitNum);
}

/// <inheritdoc />
public byte[] GetCommitMetadata(long commitNum)
{
Expand All @@ -156,7 +168,6 @@ public byte[] GetCommitMetadata(long commitNum)
else
{
device = deviceFactory.Get(checkpointNamingScheme.FasterLogCommitMetadata(commitNum));
this.commitNum = commitNum + 1;
}
if (device == null) return null;

Expand All @@ -176,14 +187,14 @@ public byte[] GetCommitMetadata(long commitNum)
return new Span<byte>(body).Slice(sizeof(int)).ToArray();
}

private IDevice NextCommitDevice()
private IDevice NextCommitDevice(long commitNum)
{
if (overwriteLogCommits)
{
if (_disposed) return null;
if (singleLogCommitDevice == null)
{
singleLogCommitDevice = deviceFactory.Get(checkpointNamingScheme.FasterLogCommitMetadata(commitNum));
singleLogCommitDevice = deviceFactory.Get(checkpointNamingScheme.FasterLogCommitMetadata(0));
if (_disposed)
{
singleLogCommitDevice?.Dispose();
Expand All @@ -194,11 +205,11 @@ private IDevice NextCommitDevice()
return singleLogCommitDevice;
}

return deviceFactory.Get(checkpointNamingScheme.FasterLogCommitMetadata(commitNum++));
var result = deviceFactory.Get(checkpointNamingScheme.FasterLogCommitMetadata(commitNum));
return result;
}
#endregion



#region ICheckpointManager
/// <inheritdoc />
public unsafe void CommitIndexCheckpoint(Guid indexToken, byte[] commitMetadata)
Expand Down
Loading