Skip to content

Commit 29cf302

Browse files
TedHartMSbadrishc
andauthored
[C#] Fix missing Sunlock on nested CopyToTail lock (#857)
* Fix missing Sunlock on nested CopyToTail lock * Improve handling of Sealed records in Ephemeral locking --------- Co-authored-by: Badrish Chandramouli <badrishc@microsoft.com>
1 parent 44428db commit 29cf302

File tree

4 files changed

+11
-7
lines changed

4 files changed

+11
-7
lines changed

cs/src/core/Index/Common/RecordInfo.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ public bool TryLockExclusive()
141141
for (; ; Thread.Yield())
142142
{
143143
long expected_word = word;
144-
Debug.Assert(!IsClosedWord(expected_word), "Should not be X locking closed records, pt 1");
144+
if (IsClosedWord(expected_word))
145+
return false;
145146
if ((expected_word & kExclusiveLockBitMask) == 0)
146147
{
147148
if (expected_word == Interlocked.CompareExchange(ref word, expected_word | kExclusiveLockBitMask, expected_word))
@@ -198,7 +199,8 @@ public bool TryLockShared()
198199
for (; ; Thread.Yield())
199200
{
200201
long expected_word = word;
201-
Debug.Assert(!IsClosedWord(expected_word), "Should not be S locking closed records");
202+
if (IsClosedWord(expected_word))
203+
return false;
202204
if (((expected_word & kExclusiveLockBitMask) == 0) // not exclusively locked
203205
&& (expected_word & kSharedLockMaskInWord) != kSharedLockMaskInWord) // shared lock is not full
204206
{

cs/src/core/Index/FASTER/Implementation/ConditionalCopyToTail.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public unsafe partial class FasterKV<Key, Value> : FasterBase, IFasterKV<Key, Va
1212
private OperationStatus ConditionalCopyToTail<Input, Output, Context, FasterSession>(FasterSession fasterSession,
1313
ref PendingContext<Input, Output, Context> pendingContext,
1414
ref Key key, ref Input input, ref Value value, ref Output output, ref Context userContext, long lsn,
15-
ref OperationStackContext<Key, Value> stackCtx, WriteReason writeReason)
15+
ref OperationStackContext<Key, Value> stackCtx, WriteReason writeReason, bool callerHasLock = false)
1616
where FasterSession : IFasterSession<Key, Value, Input, Output, Context>
1717
{
1818
// We are called by one of ReadFromImmutable, CompactionConditionalCopyToTail, or ContinueConditionalCopyToTail, and stackCtx is set up for the first try.
@@ -22,7 +22,7 @@ private OperationStatus ConditionalCopyToTail<Input, Output, Context, FasterSess
2222
{
2323
// ConditionalCopyToTail is different in regard to locking from the usual procedures, in that if we find a source record we don't lock--we exit with success.
2424
// So we only do LockTable-based locking and only when we are about to insert at the tail.
25-
if (TryTransientSLock<Input, Output, Context, FasterSession>(fasterSession, ref key, ref stackCtx, out OperationStatus status))
25+
if (callerHasLock || TryTransientSLock<Input, Output, Context, FasterSession>(fasterSession, ref key, ref stackCtx, out OperationStatus status))
2626
{
2727
try
2828
{
@@ -32,7 +32,8 @@ private OperationStatus ConditionalCopyToTail<Input, Output, Context, FasterSess
3232
finally
3333
{
3434
stackCtx.HandleNewRecordOnException(this);
35-
TransientSUnlock<Input, Output, Context, FasterSession>(fasterSession, ref key, ref stackCtx);
35+
if (!callerHasLock)
36+
TransientSUnlock<Input, Output, Context, FasterSession>(fasterSession, ref key, ref stackCtx);
3637
}
3738
}
3839
if (!HandleImmediateRetryStatus(status, fasterSession, ref pendingContext))

cs/src/core/Index/FASTER/Implementation/ContinuePending.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ internal OperationStatus ContinuePendingRead<Input, Output, Context, FasterSessi
114114
{
115115
if (pendingContext.readCopyOptions.CopyTo == ReadCopyTo.MainLog)
116116
status = ConditionalCopyToTail(fasterSession, ref pendingContext, ref key, ref pendingContext.input.Get(), ref value, ref pendingContext.output,
117-
ref pendingContext.userContext, pendingContext.serialNum, ref stackCtx, WriteReason.CopyToTail);
117+
ref pendingContext.userContext, pendingContext.serialNum, ref stackCtx, WriteReason.CopyToTail, callerHasLock: true);
118118
else if (pendingContext.readCopyOptions.CopyTo == ReadCopyTo.ReadCache && !stackCtx.recSrc.HasReadCacheSrc
119119
&& TryCopyToReadCache(fasterSession, ref pendingContext, ref key, ref pendingContext.input.Get(), ref value, ref stackCtx))
120120
status |= OperationStatus.COPIED_RECORD_TO_READ_CACHE;

cs/src/core/Index/FASTER/Implementation/InternalRead.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,8 @@ private OperationStatus ReadFromImmutableRegion<Input, Output, Context, FasterSe
286286
if (pendingContext.readCopyOptions.CopyFrom != ReadCopyFrom.AllImmutable)
287287
return OperationStatus.SUCCESS;
288288
if (pendingContext.readCopyOptions.CopyTo == ReadCopyTo.MainLog)
289-
return ConditionalCopyToTail(fasterSession, ref pendingContext, ref key, ref input, ref recordValue, ref output, ref userContext, lsn, ref stackCtx, WriteReason.CopyToTail);
289+
return ConditionalCopyToTail(fasterSession, ref pendingContext, ref key, ref input, ref recordValue, ref output, ref userContext, lsn, ref stackCtx,
290+
WriteReason.CopyToTail, callerHasLock: true);
290291
if (pendingContext.readCopyOptions.CopyTo == ReadCopyTo.ReadCache
291292
&& TryCopyToReadCache(fasterSession, ref pendingContext, ref key, ref input, ref recordValue, ref stackCtx))
292293
return OperationStatus.SUCCESS | OperationStatus.COPIED_RECORD_TO_READ_CACHE;

0 commit comments

Comments
 (0)