forked from ravendb/ravendb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RavenDB-22294 Fix include cmpXchg & atomic-guard
- Loading branch information
Showing
20 changed files
with
640 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
using Sparrow.Json; | ||
|
||
namespace Raven.Client.Util; | ||
|
||
public static class ClusterWideTransactionHelper | ||
{ | ||
public static bool IsAtomicGuardKey(string id, out string docId) | ||
{ | ||
if (IsAtomicGuardKey(id)) | ||
{ | ||
docId = null; | ||
return false; | ||
} | ||
|
||
docId = ExtractDocumentIdFromAtomicGuard(id); | ||
return true; | ||
} | ||
|
||
public static bool IsAtomicGuardKey(string key) => key.StartsWith(Constants.CompareExchange.RvnAtomicPrefix, StringComparison.OrdinalIgnoreCase); | ||
|
||
public static string GetAtomicGuardKey(string docId) => Constants.CompareExchange.RvnAtomicPrefix + docId; | ||
|
||
public static string ExtractDocumentIdFromAtomicGuard(string key) => key.Substring(Constants.CompareExchange.RvnAtomicPrefix.Length); | ||
} |
48 changes: 48 additions & 0 deletions
48
src/Raven.Server/Documents/ClusterTransactionIndexWaiter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Sparrow.Server; | ||
using Sparrow.Utils; | ||
|
||
namespace Raven.Server.Documents; | ||
|
||
public class ClusterTransactionIndexWaiter | ||
{ | ||
private long _lastCompletedIndex; | ||
private readonly AsyncManualResetEvent _notifiedListeners = new AsyncManualResetEvent(); | ||
|
||
public long LastIndex => Interlocked.Read(ref _lastCompletedIndex); | ||
|
||
public void SetAndNotifyListenersIfHigher(long newIndex) | ||
{ | ||
if (ThreadingHelper.InterlockedExchangeMax(ref _lastCompletedIndex, newIndex)) | ||
{ | ||
_notifiedListeners.SetAndResetAtomically(); | ||
} | ||
} | ||
|
||
public async Task WaitAsync(long index, CancellationToken token) | ||
{ | ||
while (true) | ||
{ | ||
Task waitAsync = _notifiedListeners.WaitAsync(token); | ||
long lastIndex = LastIndex; | ||
if (index <= lastIndex) | ||
break; | ||
|
||
try | ||
{ | ||
await waitAsync; | ||
} | ||
catch (TaskCanceledException) | ||
{ | ||
ThrowCanceledException(index, lastIndex); | ||
} | ||
} | ||
} | ||
|
||
private static void ThrowCanceledException(long index, long lastModifiedIndex) | ||
{ | ||
throw new OperationCanceledException($"Cancelled while waiting to get an index notification for {index}. lastModifiedIndex {lastModifiedIndex}"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.