Skip to content

Commit 4c6bd9d

Browse files
authored
[C#] FasterLog CommitAsync wait if no new commit done (#753)
1 parent 6feb670 commit 4c6bd9d

File tree

1 file changed

+47
-13
lines changed

1 file changed

+47
-13
lines changed

cs/src/core/FasterLog/FasterLog.cs

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -931,14 +931,29 @@ public bool CommitStrongly(out long commitTail, out long actualCommitNum, bool s
931931
public async ValueTask CommitAsync(CancellationToken token = default)
932932
{
933933
token.ThrowIfCancellationRequested();
934+
935+
// Take a lower-bound of the content of this commit in case our request is filtered but we need to wait
936+
var tail = TailAddress;
937+
var lastCommit = commitNum;
938+
934939
var task = CommitTask;
935-
if (!CommitInternal(out var tailAddress, out var actualCommitNum, true, null, -1, null))
936-
return;
940+
var success = CommitInternal(out var actualTail, out var actualCommitNum, true, null, -1, null);
937941

938-
while (CommittedUntilAddress < tailAddress || persistedCommitNum < actualCommitNum)
942+
if (success)
939943
{
940-
var linkedCommitInfo = await task.WithCancellationAsync(token).ConfigureAwait(false);
941-
task = linkedCommitInfo.NextTask;
944+
while (CommittedUntilAddress < actualTail || persistedCommitNum < actualCommitNum)
945+
{
946+
var linkedCommitInfo = await task.WithCancellationAsync(token).ConfigureAwait(false);
947+
task = linkedCommitInfo.NextTask;
948+
}
949+
}
950+
else
951+
{
952+
while (CommittedUntilAddress < tail || persistedCommitNum < lastCommit)
953+
{
954+
var linkedCommitInfo = await task.WithCancellationAsync(token).ConfigureAwait(false);
955+
task = linkedCommitInfo.NextTask;
956+
}
942957
}
943958
}
944959

@@ -951,18 +966,37 @@ public async ValueTask CommitAsync(CancellationToken token = default)
951966
public async ValueTask<Task<LinkedCommitInfo>> CommitAsync(Task<LinkedCommitInfo> prevCommitTask, CancellationToken token = default)
952967
{
953968
token.ThrowIfCancellationRequested();
969+
970+
// Take a lower-bound of the content of this commit in case our request is filtered but we need to spin
971+
var tail = TailAddress;
972+
var lastCommit = commitNum;
973+
954974
if (prevCommitTask == null) prevCommitTask = CommitTask;
955975

956-
if (!CommitInternal(out var tailAddress, out var actualCommitNum, true, null, -1, null))
957-
return prevCommitTask;
976+
var success = CommitInternal(out var actualTail, out var actualCommitNum, true, null, -1, null);
958977

959-
while (CommittedUntilAddress < tailAddress || persistedCommitNum < actualCommitNum)
978+
979+
if (success)
960980
{
961-
var linkedCommitInfo = await prevCommitTask.WithCancellationAsync(token).ConfigureAwait(false);
962-
if (linkedCommitInfo.CommitInfo.UntilAddress < tailAddress || persistedCommitNum < actualCommitNum)
963-
prevCommitTask = linkedCommitInfo.NextTask;
964-
else
965-
return linkedCommitInfo.NextTask;
981+
while (CommittedUntilAddress < actualTail || persistedCommitNum < actualCommitNum)
982+
{
983+
var linkedCommitInfo = await prevCommitTask.WithCancellationAsync(token).ConfigureAwait(false);
984+
if (linkedCommitInfo.CommitInfo.UntilAddress < actualTail || persistedCommitNum < actualCommitNum)
985+
prevCommitTask = linkedCommitInfo.NextTask;
986+
else
987+
return linkedCommitInfo.NextTask;
988+
}
989+
}
990+
else
991+
{
992+
while (CommittedUntilAddress < tail || persistedCommitNum < lastCommit)
993+
{
994+
var linkedCommitInfo = await prevCommitTask.WithCancellationAsync(token).ConfigureAwait(false);
995+
if (linkedCommitInfo.CommitInfo.UntilAddress < actualTail || persistedCommitNum < actualCommitNum)
996+
prevCommitTask = linkedCommitInfo.NextTask;
997+
else
998+
return linkedCommitInfo.NextTask;
999+
}
9661000
}
9671001

9681002
return prevCommitTask;

0 commit comments

Comments
 (0)