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

Add MsSql Locking Provider #3283

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
14 changes: 14 additions & 0 deletions Brighter.sln
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Paramore.Brighter.Locking.A
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Paramore.Brighter.Locking.DynamoDB", "src\Paramore.Brighter.Locking.DynamoDB\Paramore.Brighter.Locking.DynamoDB.csproj", "{CBF99394-E332-439B-8632-ABDE06F6E343}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Paramore.Brighter.Locking.MsSql", "src\Paramore.Brighter.Locking.MsSql\Paramore.Brighter.Locking.MsSql.csproj", "{9EB2566B-1115-4E32-916B-222A74FA20B4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -1991,6 +1993,18 @@ Global
{CBF99394-E332-439B-8632-ABDE06F6E343}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{CBF99394-E332-439B-8632-ABDE06F6E343}.Release|x86.ActiveCfg = Release|Any CPU
{CBF99394-E332-439B-8632-ABDE06F6E343}.Release|x86.Build.0 = Release|Any CPU
{9EB2566B-1115-4E32-916B-222A74FA20B4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9EB2566B-1115-4E32-916B-222A74FA20B4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9EB2566B-1115-4E32-916B-222A74FA20B4}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{9EB2566B-1115-4E32-916B-222A74FA20B4}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{9EB2566B-1115-4E32-916B-222A74FA20B4}.Debug|x86.ActiveCfg = Debug|Any CPU
{9EB2566B-1115-4E32-916B-222A74FA20B4}.Debug|x86.Build.0 = Debug|Any CPU
{9EB2566B-1115-4E32-916B-222A74FA20B4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9EB2566B-1115-4E32-916B-222A74FA20B4}.Release|Any CPU.Build.0 = Release|Any CPU
{9EB2566B-1115-4E32-916B-222A74FA20B4}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{9EB2566B-1115-4E32-916B-222A74FA20B4}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{9EB2566B-1115-4E32-916B-222A74FA20B4}.Release|x86.ActiveCfg = Release|Any CPU
{9EB2566B-1115-4E32-916B-222A74FA20B4}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
40 changes: 20 additions & 20 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
<CentralPackageTransitivePinningEnabled>false</CentralPackageTransitivePinningEnabled>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="AWSSDK.DynamoDBv2" Version="3.7.302.15" />
<PackageVersion Include="AWSSDK.Extensions.NETCore.Setup" Version="3.7.300" />
<PackageVersion Include="AWSSDK.S3" Version="3.7.307.15" />
<PackageVersion Include="AWSSDK.SecurityToken" Version="3.7.300.75" />
<PackageVersion Include="AWSSDK.SimpleNotificationService" Version="3.7.301.22" />
<PackageVersion Include="AWSSDK.SQS" Version="3.7.300.74" />
<PackageVersion Include="Azure.Identity" Version="1.11.0" />
<PackageVersion Include="Azure.Messaging.ServiceBus" Version="7.17.5" />
<PackageVersion Include="Azure.Storage.Blobs" Version="12.19.1" />
<PackageVersion Include="AWSSDK.DynamoDBv2" Version="3.7.400.11" />
<PackageVersion Include="AWSSDK.Extensions.NETCore.Setup" Version="3.7.301" />
<PackageVersion Include="AWSSDK.S3" Version="3.7.402.2" />
<PackageVersion Include="AWSSDK.SecurityToken" Version="3.7.400.11" />
<PackageVersion Include="AWSSDK.SimpleNotificationService" Version="3.7.400.11" />
<PackageVersion Include="AWSSDK.SQS" Version="3.7.400.11" />
<PackageVersion Include="Azure.Identity" Version="1.12.0" />
<PackageVersion Include="Azure.Messaging.ServiceBus" Version="7.18.1" />
<PackageVersion Include="Azure.Storage.Blobs" Version="12.21.2" />
<PackageVersion Include="Confluent.Kafka" Version="2.3.0" />
<PackageVersion Include="Confluent.SchemaRegistry" Version="2.3.0" />
<PackageVersion Include="Confluent.SchemaRegistry.Serdes.Json" Version="2.3.0" />
Expand All @@ -31,13 +31,13 @@
<PackageVersion Include="FluentMigrator.Runner" Version="5.2.0" />
<PackageVersion Include="MessagePack" Version="2.5.140" />
<PackageVersion Include="Microsoft.Data.SqlClient" Version="5.2.0" />
<PackageVersion Include="Microsoft.Data.Sqlite" Version="8.0.4" />
<PackageVersion Include="Microsoft.Data.Sqlite.Core" Version="8.0.4" />
<PackageVersion Include="Microsoft.Data.Sqlite" Version="8.0.8" />
<PackageVersion Include="Microsoft.Data.Sqlite.Core" Version="8.0.8" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.1" />
<PackageVersion Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1" />
<PackageVersion Include="Microsoft.Extensions.Diagnostics.HealthChecks.Abstractions" Version="8.0.4" />
<PackageVersion Include="Microsoft.Extensions.Diagnostics.HealthChecks.Abstractions" Version="8.0.8" />
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Http" Version="8.0.0" />
Expand Down Expand Up @@ -76,9 +76,9 @@
<PackageVersion Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageVersion Include="System.Diagnostics.DiagnosticSource" Version="8.0.1" />
<PackageVersion Include="System.Reflection.TypeExtensions" Version="4.7.0" />
<PackageVersion Include="System.Text.Json" Version="8.0.3" />
<PackageVersion Include="System.Text.Json" Version="8.0.4" />
<PackageVersion Include="xunit" Version="2.7.1" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.8">
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageVersion>
Expand Down Expand Up @@ -111,12 +111,12 @@
<PackageVersion Include="Pomelo.EntityFrameworkCore.MySql" Version="7.0.0" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="8.0.4" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.4" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.4" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.4" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="8.0.4" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.4" />
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="8.0.8" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.8" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.8" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.8" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="8.0.8" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.8" />
<PackageVersion Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.2" />
</ItemGroup>
<ItemGroup>
Expand Down
134 changes: 134 additions & 0 deletions src/Paramore.Brighter.Locking.MsSql/MsSqlLockingProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#region Licence

/* The MIT License (MIT)
Copyright © 2021 Ian Cooper <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. */

#endregion

using System.Collections.Concurrent;
using System.Data;
using System.Data.Common;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Logging;
using Paramore.Brighter.Logging;
using Paramore.Brighter.MsSql;

namespace Paramore.Brighter.Locking.MsSql;

/// <summary>
/// The Microsoft Sql Server Locking Provider
/// </summary>
/// <param name="connectionProvider">The Sql Server connection Provider</param>
public class MsSqlLockingProvider(IMsSqlConnectionProvider connectionProvider) : IDistributedLock, IAsyncDisposable
{
private readonly ConcurrentDictionary<string, DbConnection> _connections = new();

private readonly ILogger _logger = ApplicationLogging.CreateLogger<MsSqlLockingProvider>();
/// <summary>
/// Attempt to obtain a lock on a resource
/// </summary>
/// <param name="resource">The name of the resource to Lock</param>
/// <param name="cancellationToken">The Cancellation Token</param>
/// <returns>The id of the lock that has been acquired or null if no lock was able to be acquired</returns>
public async Task<string?> ObtainLockAsync(string resource, CancellationToken cancellationToken)
{
if (_connections.ContainsKey(resource))
{
return null;
}

var connection = await connectionProvider.GetConnectionAsync(cancellationToken);
if (connection.State != ConnectionState.Open)
await connection.OpenAsync(cancellationToken);

await using var command = connection.CreateCommand();
command.CommandText = MsSqlLockingQueries.ObtainLockQuery;
command.Parameters.Add(new SqlParameter("@Resource", SqlDbType.NVarChar, 255));
command.Parameters["@Resource"].Value = resource;
command.Parameters.Add(new SqlParameter("@LockTimeout", SqlDbType.Int));
command.Parameters["@LockTimeout"].Value = 0;

var result = (await command.ExecuteScalarAsync(cancellationToken)) ?? -999;

var resultCode = (int)result;

_logger.LogInformation("Attempt to obtain lock returned: {MsSqlLockResult}", GetLockStatusCode(resultCode));

if (resultCode < 0)
return null;

_connections.TryAdd(resource, connection);

return resource;
}

/// <summary>
/// Release a lock
/// </summary>
/// <param name="resource">The name of the resource to Lock</param>
/// <param name="lockId">The lock Id that was provided when the lock was obtained</param>
/// <param name="cancellationToken"></param>
/// <returns>Awaitable Task</returns>
public async Task ReleaseLockAsync(string resource, string lockId, CancellationToken cancellationToken)
{
if (!_connections.TryRemove(resource, out var connection))
{
return;
}

await using var command = connection.CreateCommand();
command.CommandText = MsSqlLockingQueries.ReleaseLockQuery;
command.Parameters.Add(new SqlParameter("@Resource", SqlDbType.NVarChar, 255));
command.Parameters["@Resource"].Value = resource;
await command.ExecuteNonQueryAsync(cancellationToken);

await connection.CloseAsync();
await connection.DisposeAsync();
}

/// <summary>
/// Convert Status code to messages
/// Doc: https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-getapplock-transact-sql?view=sql-server-ver16#return-code-values
/// </summary>
/// <param name="code">Status Code</param>
/// <returns>Status Message</returns>
private string GetLockStatusCode(int code)
=> code switch
{
0 => "The lock was successfully granted synchronously.",
1 => "The lock was granted successfully after waiting for other incompatible locks to be released.",
-1 => "The lock request timed out.",
-2 => "The lock request was canceled.",
-3 => "The lock request was chosen as a deadlock victim.",
_ => "Indicates a parameter validation or other call error."
};

/// <summary>
/// Dispose Locking Provider
/// </summary>
public async ValueTask DisposeAsync()
{
foreach (var connection in _connections)
{
await connection.Value.DisposeAsync();
}
}
}
18 changes: 18 additions & 0 deletions src/Paramore.Brighter.Locking.MsSql/MsSqlLockingQueries.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Paramore.Brighter.Locking.MsSql;

public static class MsSqlLockingQueries
{
public const string ObtainLockQuery = "declare @result int; " +
"Exec @result = sp_getapplock " +
"@DbPrincipal = 'dbo' " +
",@Resource = @Resource" +
",@LockMode = 'Exclusive'" +
",@LockTimeout = @LockTimeout" +
",@LockOwner = 'Session'; " +
"Select @result";

public const string ReleaseLockQuery = "EXEC sp_releaseapplock " +
"@Resource = @Resource " +
",@DbPrincipal = 'dbo' " +
",@LockOwner = 'Session';";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Authors>Paul Reardon</Authors>
<Description>A Locking Provider for Microsoft SQL Server</Description>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Paramore.Brighter.MsSql\Paramore.Brighter.MsSql.csproj" />
<ProjectReference Include="..\Paramore.Brighter\Paramore.Brighter.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Data.SqlClient" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using System;
using System.Data;
using System.Data.Common;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Logging.Abstractions;
using Paramore.Brighter.Locking.MsSql;
using Paramore.Brighter.MsSql;
using Xunit;

namespace Paramore.Brighter.MSSQL.Tests.LockingProvider;

[Trait("Category", "MSSQL")]
public class MsSqlLockingProviderTests
{
private readonly MsSqlTestHelper _msSqlTestHelper;

public MsSqlLockingProviderTests()
{
_msSqlTestHelper = new MsSqlTestHelper();
_msSqlTestHelper.SetupMessageDb();
}


[Fact]
public async Task GivenAMsSqlLockingProvider_WhenLockIsCalled_LockCanBeObtainedAndThenReleased()
{
var provider = new MsSqlLockingProvider(_msSqlTestHelper.ConnectionProvider);
var resource = "Sweeper";

var result = await provider.ObtainLockAsync(resource, CancellationToken.None);

Assert.NotEmpty(result);
Assert.Equal(resource, result);

await provider.ReleaseLockAsync(resource, result, CancellationToken.None);
}

[Fact]
public async Task GivenTwoLockingProviders_WhenLockIsCalledOnBoth_OneFailsUntilTheFirstLockIsReleased()
{
var provider1 = new MsSqlLockingProvider(_msSqlTestHelper.ConnectionProvider);
var provider2 = new MsSqlLockingProvider(_msSqlTestHelper.ConnectionProvider);
var resource = "Sweeper";

var firstLock = await provider1.ObtainLockAsync(resource, CancellationToken.None);
var secondLock = await provider2.ObtainLockAsync(resource, CancellationToken.None);

Assert.NotEmpty(firstLock);
Assert.Null(secondLock);

await provider1.ReleaseLockAsync(resource, firstLock, CancellationToken.None);
var secondLockAttemptTwo = await provider2.ObtainLockAsync(resource, CancellationToken.None);

Assert.NotEmpty(secondLockAttemptTwo);
}

[Fact]
public async Task GivenAnExistingLock_WhenConnectionDies_LockIsReleased()
{
var resource = Guid.NewGuid().ToString();
var connection = await ObtainLockForManualDisposal(resource);

var provider1 = new MsSqlLockingProvider(_msSqlTestHelper.ConnectionProvider);

var lockAttempt = await provider1.ObtainLockAsync(resource, CancellationToken.None);

// Ensure Lock was not obtained
Assert.Null(lockAttempt);

await connection.DisposeAsync();

var lockAttemptTwo = await provider1.ObtainLockAsync(resource, CancellationToken.None);

// Ensure Lock was Obtained
Assert.False(string.IsNullOrEmpty(lockAttemptTwo));
}

private async Task<DbConnection> ObtainLockForManualDisposal(string resource)
{
var connectionProvider = _msSqlTestHelper.ConnectionProvider;
var connection = await connectionProvider.GetConnectionAsync(CancellationToken.None);
await connection.OpenAsync();
var command = connection.CreateCommand();
command.CommandText = MsSqlLockingQueries.ObtainLockQuery;
command.Parameters.Add(new SqlParameter("@Resource", SqlDbType.NVarChar, 255));
command.Parameters["@Resource"].Value = resource;
command.Parameters.Add(new SqlParameter("@LockTimeout", SqlDbType.Int));
command.Parameters["@LockTimeout"].Value = 0;

var respone = await command.ExecuteScalarAsync(CancellationToken.None);

//Assert Lock was successful
int.TryParse(respone.ToString(), out var responseCode);
Assert.True(responseCode >= 0);

return connection;
}
}
2 changes: 2 additions & 0 deletions tests/Paramore.Brighter.MSSQL.Tests/MsSqlTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class MsSqlTestHelper
private SqlSettings _sqlSettings;
private IMsSqlConnectionProvider _connectionProvider;
private IMsSqlConnectionProvider _masterConnectionProvider;

public IMsSqlConnectionProvider ConnectionProvider { get => _connectionProvider; }

private const string _queueDDL = @"CREATE TABLE [dbo].[{0}](
[Id][bigint] IDENTITY(1, 1) NOT NULL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

<ItemGroup>
<ProjectReference Include="..\..\src\Paramore.Brighter.Inbox.MsSql\Paramore.Brighter.Inbox.MsSql.csproj" />
<ProjectReference Include="..\..\src\Paramore.Brighter.Locking.MsSql\Paramore.Brighter.Locking.MsSql.csproj" />
<ProjectReference Include="..\..\src\Paramore.Brighter.MessagingGateway.MsSql\Paramore.Brighter.MessagingGateway.MsSql.csproj" />
<ProjectReference Include="..\..\src\Paramore.Brighter.Outbox.MsSql\Paramore.Brighter.Outbox.MsSql.csproj" />
</ItemGroup>
Expand Down
Loading