From 92fbec07d3c342f434564f6c1278440db85aea87 Mon Sep 17 00:00:00 2001 From: John Lambert Date: Wed, 4 Sep 2024 12:45:07 -0400 Subject: [PATCH] Add a lock signature --- .../Configuration/IMachineBuilderExtensions.cs | 2 +- .../src/Serval.Machine.Shared/Models/Lock.cs | 1 + .../Services/DistributedReaderWriterLock.cs | 15 ++++++++++++++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Machine/src/Serval.Machine.Shared/Configuration/IMachineBuilderExtensions.cs b/src/Machine/src/Serval.Machine.Shared/Configuration/IMachineBuilderExtensions.cs index d67afb90..a4287105 100644 --- a/src/Machine/src/Serval.Machine.Shared/Configuration/IMachineBuilderExtensions.cs +++ b/src/Machine/src/Serval.Machine.Shared/Configuration/IMachineBuilderExtensions.cs @@ -268,7 +268,7 @@ await c.Indexes.CreateOrUpdateAsync( ); } ); - o.AddRepository("locks"); + o.AddRepository("locks", mapSetup: m => m.SetIgnoreExtraElements(true)); o.AddRepository( "train_segment_pairs", init: c => diff --git a/src/Machine/src/Serval.Machine.Shared/Models/Lock.cs b/src/Machine/src/Serval.Machine.Shared/Models/Lock.cs index 39ceae87..582ae5ac 100644 --- a/src/Machine/src/Serval.Machine.Shared/Models/Lock.cs +++ b/src/Machine/src/Serval.Machine.Shared/Models/Lock.cs @@ -5,4 +5,5 @@ public record Lock public required string Id { get; init; } public DateTime? ExpiresAt { get; init; } public required string HostId { get; init; } + public string CallingSignature { get; init; } = ""; } diff --git a/src/Machine/src/Serval.Machine.Shared/Services/DistributedReaderWriterLock.cs b/src/Machine/src/Serval.Machine.Shared/Services/DistributedReaderWriterLock.cs index 9c05ab5b..0e6a6395 100644 --- a/src/Machine/src/Serval.Machine.Shared/Services/DistributedReaderWriterLock.cs +++ b/src/Machine/src/Serval.Machine.Shared/Services/DistributedReaderWriterLock.cs @@ -51,11 +51,24 @@ public async Task WriterLockAsync( { lifetime ??= _defaultLifetime; string lockId = _idGenerator.GenerateId(); + StackTrace stackTrace = new StackTrace(); + string callingSignature = + $"Filename: {stackTrace.GetFrame(1)?.GetFileName()}, Method: {stackTrace.GetFrame(1)?.GetMethod()?.Name}"; + if (!await TryAcquireWriterLock(lockId, lifetime, cancellationToken)) { await _locks.UpdateAsync( _id, - u => u.Add(rwl => rwl.WriterQueue, new Lock { Id = lockId, HostId = _hostId }), + u => + u.Add( + rwl => rwl.WriterQueue, + new Lock + { + Id = lockId, + HostId = _hostId, + CallingSignature = callingSignature + } + ), cancellationToken: cancellationToken ); try