Skip to content

Commit

Permalink
Merge pull request #1995 from preardon/issue/failureToImplicitClear
Browse files Browse the repository at this point in the history
Fix Threading issue on Implicit Clear
  • Loading branch information
preardon authored Feb 21, 2022
2 parents c714674 + bd3fbb3 commit 2efd443
Show file tree
Hide file tree
Showing 30 changed files with 485 additions and 125 deletions.
40 changes: 32 additions & 8 deletions Brighter.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.28922.388
# Visual Studio Version 17
VisualStudioVersion = 17.0.32112.339
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{235DE1F1-E71B-4817-8E27-3B34FF006E4C}"
EndProject
Expand Down Expand Up @@ -223,17 +223,17 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GreetingsWorker", "samples\
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Paramore.Brighter.PostgreSql", "src\Paramore.Brighter.PostgreSql\Paramore.Brighter.PostgreSql.csproj", "{08E6D0F8-B6CE-454F-8761-77731D99F743}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Paramore.Brighter.PostgreSql.EntityFrameworkCore", "src\Paramore.Brighter.PostgreSql.EntityFrameworkCore\Paramore.Brighter.PostgreSql.EntityFrameworkCore.csproj", "{AA85493A-4120-4DA0-BAA5-CBF34D238A64}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Paramore.Brighter.PostgreSql.EntityFrameworkCore", "src\Paramore.Brighter.PostgreSql.EntityFrameworkCore\Paramore.Brighter.PostgreSql.EntityFrameworkCore.csproj", "{AA85493A-4120-4DA0-BAA5-CBF34D238A64}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GreetingsWeb", "samples\WebAPI_EFCore\GreetingsWeb\GreetingsWeb.csproj", "{BC4C3DF7-52C0-41EA-98DD-70E402AD5116}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GreetingsWeb", "samples\WebAPI_EFCore\GreetingsWeb\GreetingsWeb.csproj", "{BC4C3DF7-52C0-41EA-98DD-70E402AD5116}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SalutationPorts", "samples\WebAPI_EFCore\SalutationPorts\SalutationPorts.csproj", "{5A6A95C0-F82E-49DD-B4C6-98D7A765ECFF}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SalutationPorts", "samples\WebAPI_EFCore\SalutationPorts\SalutationPorts.csproj", "{5A6A95C0-F82E-49DD-B4C6-98D7A765ECFF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SalutationEntities", "samples\WebAPI_EFCore\SalutationEntities\SalutationEntities.csproj", "{F701369D-EDA3-4407-8655-6B81DD6EBCBA}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SalutationEntities", "samples\WebAPI_EFCore\SalutationEntities\SalutationEntities.csproj", "{F701369D-EDA3-4407-8655-6B81DD6EBCBA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Salutations_MySqlMigrations", "samples\WebAPI_EFCore\Salutations_MySqlMigrations\Salutations_MySqlMigrations.csproj", "{82E64F30-8D74-4E01-A974-5A78EBAD916C}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Salutations_MySqlMigrations", "samples\WebAPI_EFCore\Salutations_MySqlMigrations\Salutations_MySqlMigrations.csproj", "{82E64F30-8D74-4E01-A974-5A78EBAD916C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Salutations_SqliteMigrations", "samples\WebAPI_EFCore\Salutations_SqliteMigrations\Salutations_SqliteMigrations.csproj", "{05647D1B-87A3-4440-B468-82866B206E49}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Salutations_SqliteMigrations", "samples\WebAPI_EFCore\Salutations_SqliteMigrations\Salutations_SqliteMigrations.csproj", "{05647D1B-87A3-4440-B468-82866B206E49}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -1229,6 +1229,30 @@ Global
{93589653-2B49-4818-BE98-FE6F16EC72EC}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{93589653-2B49-4818-BE98-FE6F16EC72EC}.Release|x86.ActiveCfg = Release|Any CPU
{93589653-2B49-4818-BE98-FE6F16EC72EC}.Release|x86.Build.0 = Release|Any CPU
{08E6D0F8-B6CE-454F-8761-77731D99F743}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{08E6D0F8-B6CE-454F-8761-77731D99F743}.Debug|Any CPU.Build.0 = Debug|Any CPU
{08E6D0F8-B6CE-454F-8761-77731D99F743}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{08E6D0F8-B6CE-454F-8761-77731D99F743}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{08E6D0F8-B6CE-454F-8761-77731D99F743}.Debug|x86.ActiveCfg = Debug|Any CPU
{08E6D0F8-B6CE-454F-8761-77731D99F743}.Debug|x86.Build.0 = Debug|Any CPU
{08E6D0F8-B6CE-454F-8761-77731D99F743}.Release|Any CPU.ActiveCfg = Release|Any CPU
{08E6D0F8-B6CE-454F-8761-77731D99F743}.Release|Any CPU.Build.0 = Release|Any CPU
{08E6D0F8-B6CE-454F-8761-77731D99F743}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{08E6D0F8-B6CE-454F-8761-77731D99F743}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{08E6D0F8-B6CE-454F-8761-77731D99F743}.Release|x86.ActiveCfg = Release|Any CPU
{08E6D0F8-B6CE-454F-8761-77731D99F743}.Release|x86.Build.0 = Release|Any CPU
{AA85493A-4120-4DA0-BAA5-CBF34D238A64}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AA85493A-4120-4DA0-BAA5-CBF34D238A64}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AA85493A-4120-4DA0-BAA5-CBF34D238A64}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{AA85493A-4120-4DA0-BAA5-CBF34D238A64}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{AA85493A-4120-4DA0-BAA5-CBF34D238A64}.Debug|x86.ActiveCfg = Debug|Any CPU
{AA85493A-4120-4DA0-BAA5-CBF34D238A64}.Debug|x86.Build.0 = Debug|Any CPU
{AA85493A-4120-4DA0-BAA5-CBF34D238A64}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AA85493A-4120-4DA0-BAA5-CBF34D238A64}.Release|Any CPU.Build.0 = Release|Any CPU
{AA85493A-4120-4DA0-BAA5-CBF34D238A64}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{AA85493A-4120-4DA0-BAA5-CBF34D238A64}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{AA85493A-4120-4DA0-BAA5-CBF34D238A64}.Release|x86.ActiveCfg = Release|Any CPU
{AA85493A-4120-4DA0-BAA5-CBF34D238A64}.Release|x86.Build.0 = Release|Any CPU
{BC4C3DF7-52C0-41EA-98DD-70E402AD5116}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BC4C3DF7-52C0-41EA-98DD-70E402AD5116}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BC4C3DF7-52C0-41EA-98DD-70E402AD5116}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
Expand Down
34 changes: 23 additions & 11 deletions src/Paramore.Brighter.Extensions.Hosting/TimedOutboxSweeper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,31 @@ private void DoWork(object state)
s_logger.LogInformation("Outbox Sweeper looking for unsent messages");

var scope = _serviceScopeFactory.CreateScope();
IAmACommandProcessor commandProcessor = scope.ServiceProvider.GetService<IAmACommandProcessor>();
try
{
IAmACommandProcessor commandProcessor = scope.ServiceProvider.GetService<IAmACommandProcessor>();

var outBoxSweeper = new OutboxSweeper(
milliSecondsSinceSent: _options.MinimumMessageAge,
commandProcessor: commandProcessor,
_options.BatchSize,
_options.UseBulk);

if(_options.UseBulk)
outBoxSweeper.SweepAsync(CancellationToken.None).RunSynchronously();
else
outBoxSweeper.Sweep();
var outBoxSweeper = new OutboxSweeper(
millisecondsSinceSent: _options.MinimumMessageAge,
commandProcessor: commandProcessor,
_options.BatchSize,
_options.UseBulk);

if (_options.UseBulk)
outBoxSweeper.SweepAsyncOutbox();
else
outBoxSweeper.Sweep();
}
catch (Exception e)
{
s_logger.LogError(e, "Error while sweeping the outbox.");
throw;
}
finally
{
scope.Dispose();
}

s_logger.LogInformation("Outbox Sweeper sleeping");
}

Expand Down
35 changes: 26 additions & 9 deletions src/Paramore.Brighter/ExternalBusServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ internal class ExternalBusServices : IDisposable

internal IAmAProducerRegistry ProducerRegistry { get; set; }

private static SemaphoreSlim _clearSemaphoreToken = new SemaphoreSlim(1, 1);
private static readonly SemaphoreSlim _clearSemaphoreToken = new SemaphoreSlim(1, 1);
private static readonly SemaphoreSlim _backgroundClearSemaphoreToken = new SemaphoreSlim(1, 1);

private DateTime _lastOutStandingMessageCheckAt = DateTime.UtcNow;

//Used to checking the limit on outstanding messages for an Outbox. We throw at that point. Writes to the static bool should be made thread-safe by locking the object
private readonly object _checkOutStandingMessagesObject = new object();
private readonly object _implicitClearMessagesObject = new object();

//Uses -1 to indicate no outbox and will thus force a throw on a failed publish
private int _outStandingCount;
Expand Down Expand Up @@ -198,7 +198,7 @@ internal void ClearOutbox(int amountToClear, int minimumAge, bool useAsync, bool

private async Task BackgroundDispatchUsingSync(int amountToClear, int minimumAge)
{
if (Monitor.TryEnter(_implicitClearMessagesObject))
if (await _backgroundClearSemaphoreToken.WaitAsync(TimeSpan.Zero))
{
await _clearSemaphoreToken.WaitAsync(CancellationToken.None);
try
Expand All @@ -209,44 +209,61 @@ private async Task BackgroundDispatchUsingSync(int amountToClear, int minimumAge
Dispatch(messages);
s_logger.LogInformation("Messages have been cleared");
}
catch (Exception e)
{
s_logger.LogError(e, "Error while dispatching from outbox");
}
finally
{
_clearSemaphoreToken.Release();
Monitor.Exit(_implicitClearMessagesObject);
_backgroundClearSemaphoreToken.Release();
}

CheckOutstandingMessages();
}
else
{
s_logger.LogInformation("Skipping dispatch of messages as another thread is running");
}
}

private async Task BackgroundDispatchUsingAsync(int amountToClear, int minimumAge, bool useBulk)
{
if (Monitor.TryEnter(_implicitClearMessagesObject))

if (await _backgroundClearSemaphoreToken.WaitAsync(TimeSpan.Zero))
{
await _clearSemaphoreToken.WaitAsync(CancellationToken.None);
try
{
var messages =
await AsyncOutbox.OutstandingMessagesAsync(minimumAge, amountToClear);

s_logger.LogInformation("Found {NumberOfMessages} to clear out of amount {AmountToClear}",
messages.Count(), amountToClear);

if (useBulk)
await BulkDispatchAsync(messages, CancellationToken.None);
else
await DispatchAsync(messages, false, CancellationToken.None);

s_logger.LogInformation("Messages have been cleared");
}
catch (Exception e)
{
s_logger.LogError(e, "Error while dispatching from outbox");
}
finally
{
_clearSemaphoreToken.Release();
Monitor.Exit(_implicitClearMessagesObject);
_backgroundClearSemaphoreToken.Release();
}

CheckOutstandingMessages();
}
else
{
s_logger.LogInformation("Skipping dispatch of messages as another thread is running");
}
}

private void Dispatch(IEnumerable<Message> posts)
Expand Down Expand Up @@ -323,7 +340,7 @@ private async Task BulkDispatchAsync(IEnumerable<Message> posts, CancellationTok

foreach (var topicBatch in messagesByTopic)
{
var producer = ProducerRegistry.LookupBy(topicBatch.Key);
var producer = ProducerRegistry.LookupByOrDefault(topicBatch.Key);

if (producer is IAmABulkMessageProducerAsync bulkMessageProducer)
{
Expand Down
29 changes: 14 additions & 15 deletions src/Paramore.Brighter/OutboxSweeper.cs
Original file line number Diff line number Diff line change
@@ -1,43 +1,42 @@
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace Paramore.Brighter
namespace Paramore.Brighter
{
public class OutboxSweeper
{
private readonly int _milliSecondsSinceSent;
private readonly int _millisecondsSinceSent;
private readonly IAmACommandProcessor _commandProcessor;
private readonly int _batchSize;
private readonly bool _useBulk;

/// <summary>
/// This sweeper clears an outbox of any outstanding messages within the time interval
/// </summary>
/// <param name="milliSecondsSinceSent">How long can a message sit in the box before we attempt to resend</param>
/// <param name="millisecondsSinceSent">How long can a message sit in the box before we attempt to resend</param>
/// <param name="commandProcessor">Who should post the messages</param>
/// <param name="batchSize">The maximum number of messages to dispatch.</param>
/// <param name="useBulk">Use the producers bulk dispatch functionality.</param>
public OutboxSweeper(int milliSecondsSinceSent, IAmACommandProcessor commandProcessor, int batchSize = 100,
public OutboxSweeper(int millisecondsSinceSent, IAmACommandProcessor commandProcessor, int batchSize = 100,
bool useBulk = false)
{
_milliSecondsSinceSent = milliSecondsSinceSent;
_millisecondsSinceSent = millisecondsSinceSent;
_commandProcessor = commandProcessor;
_batchSize = batchSize;
_useBulk = useBulk;
}

/// <summary>
/// Dispatches the oldest un-dispatched messages from the outbox in a background thread.
/// </summary>
public void Sweep()
{
_commandProcessor.ClearOutbox(_batchSize, _milliSecondsSinceSent);
_commandProcessor.ClearOutbox(_batchSize, _millisecondsSinceSent);
}

public Task SweepAsync(CancellationToken cancellationToken = default)
/// <summary>
/// Dispatches the oldest un-dispatched messages from the asynchronous outbox in a background thread.
/// </summary>
public void SweepAsyncOutbox()
{
_commandProcessor.ClearAsyncOutbox(_batchSize, _milliSecondsSinceSent, _useBulk);

return Task.CompletedTask;
_commandProcessor.ClearAsyncOutbox(_batchSize, _millisecondsSinceSent, _useBulk);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@ THE SOFTWARE. */

namespace Paramore.Brighter.Core.Tests.CommandProcessors.TestDoubles
{
public class FakeMessageProducer : IAmAMessageProducerSync, IAmAMessageProducerAsync, ISupportPublishConfirmation, IAmABulkMessageProducerAsync
public class FakeMessageProducer : IAmAMessageProducerSync, IAmAMessageProducerAsync, IAmABulkMessageProducerAsync
{
public event Action<bool, Guid> OnMessagePublished;
public int MaxOutStandingMessages { get; set; } = -1;
public int MaxOutStandingCheckIntervalMilliSeconds { get; set; } = 0;

public List<Message> SentMessages = new List<Message>();
public bool MessageWasSent { get; set; }

public void Dispose() { }

public Task SendAsync(Message message)
Expand All @@ -52,7 +51,7 @@ public async IAsyncEnumerable<Guid[]> SendAsync(IEnumerable<Message> messages, [
{
foreach (var msg in messages)
{
yield return new[] {msg.Id};
yield return new[] { msg.Id };
}
MessageWasSent = true;
SentMessages.AddRange(messages);
Expand All @@ -62,13 +61,11 @@ public void Send(Message message)
{
MessageWasSent = true;
SentMessages.Add(message);
OnMessagePublished?.Invoke(true, message.Id);
}

public void SendWithDelay(Message message, int delayMilliseconds = 0)
{
Send(message);
}

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#region Licence
/* The MIT License (MIT)
Copyright © 2015 Toby Henderson <hendersont@gmail.com>
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;

namespace Paramore.Brighter.Core.Tests.CommandProcessors.TestDoubles
{
public class FakeMessageProducerWithPublishConfirmation : FakeMessageProducer, ISupportPublishConfirmation
{
public event Action<bool, Guid> OnMessagePublished;

public new void Send(Message message)
{
MessageWasSent = true;
SentMessages.Add(message);
OnMessagePublished?.Invoke(true, message.Id);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,13 @@ public Task MarkDispatchedAsync(Guid id, DateTime? dispatchedAt = null, Dictiona
return tcs.Task;
}

public Task MarkDispatchedAsync(IEnumerable<Guid> ids, DateTime? dispatchedAt = null, Dictionary<string, object> args = null,
public async Task MarkDispatchedAsync(IEnumerable<Guid> ids, DateTime? dispatchedAt = null, Dictionary<string, object> args = null,
CancellationToken cancellationToken = default(CancellationToken))
{
throw new NotImplementedException();
foreach (var id in ids)
{
await MarkDispatchedAsync(id, dispatchedAt, args, cancellationToken);
}
}

public Task<IEnumerable<Message>> OutstandingMessagesAsync(double millSecondsSinceSent, int pageSize = 100, int pageNumber = 1,
Expand Down
Loading

0 comments on commit 2efd443

Please sign in to comment.