Skip to content
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
6 changes: 0 additions & 6 deletions src/ServiceControl.Monitoring.AcceptanceTests/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,3 @@

# Justification: Test project
dotnet_diagnostic.CA2007.severity = none

# Cancellation analyzer debt. These fire because Particular.Analyzers is no longer pinned to 0.9.0.
# They are scheduled work, not accepted exceptions: remove a line once this project has no
# violations of that rule left, and never add a rule back to this list.
dotnet_diagnostic.PS0003.severity = none
dotnet_diagnostic.PS0018.severity = none
28 changes: 15 additions & 13 deletions src/ServiceControl.Monitoring.AcceptanceTests/PerformanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ public async Task GetMonitoredEndpointsQueryTest(int numberOfEndpoints, int numb
var reporters =
new[]
{
BuildReporters(sendReportEvery, numberOfEntriesInReport, instances, source, (e, i) => criticalTimeStore.Store(e, i, EndpointMessageType.Unknown(i.EndpointName))),
BuildReporters(sendReportEvery, numberOfEntriesInReport, instances, source, (e, i) => processingTimeStore.Store(e, i, EndpointMessageType.Unknown(i.EndpointName))),
BuildReporters(sendReportEvery, numberOfEntriesInReport, instances, source, (e, i) => retriesStore.Store(e, i, EndpointMessageType.Unknown(i.EndpointName)))
BuildReporters(sendReportEvery, numberOfEntriesInReport, instances, (e, i) => criticalTimeStore.Store(e, i, EndpointMessageType.Unknown(i.EndpointName)), source.Token),
BuildReporters(sendReportEvery, numberOfEntriesInReport, instances, (e, i) => processingTimeStore.Store(e, i, EndpointMessageType.Unknown(i.EndpointName)), source.Token),
BuildReporters(sendReportEvery, numberOfEntriesInReport, instances, (e, i) => retriesStore.Store(e, i, EndpointMessageType.Unknown(i.EndpointName)), source.Token)
}.SelectMany(i => i).ToArray();

var histogram = CreateTimeHistogram();
Expand Down Expand Up @@ -123,9 +123,9 @@ public async Task GetMonitoredSingleEndpointQueryTest(int numberOfInstances, int
var reporters =
new[]
{
BuildReporters(sendReportEvery, numberOfEntriesInReport, instances, source, (e, i) => criticalTimeStore.Store(e, i, getter())),
BuildReporters(sendReportEvery, numberOfEntriesInReport, instances, source, (e, i) => processingTimeStore.Store(e, i, getter())),
BuildReporters(sendReportEvery, numberOfEntriesInReport, instances, source, (e, i) => retriesStore.Store(e, i, getter()))
BuildReporters(sendReportEvery, numberOfEntriesInReport, instances, (e, i) => criticalTimeStore.Store(e, i, getter()), source.Token),
BuildReporters(sendReportEvery, numberOfEntriesInReport, instances, (e, i) => processingTimeStore.Store(e, i, getter()), source.Token),
BuildReporters(sendReportEvery, numberOfEntriesInReport, instances, (e, i) => retriesStore.Store(e, i, getter()), source.Token)
}.SelectMany(i => i).ToArray();

var histogram = CreateTimeHistogram();
Expand All @@ -149,21 +149,21 @@ public async Task GetMonitoredSingleEndpointQueryTest(int numberOfInstances, int
Report("Reporters", reportFinalHistogram, TimeSpan.FromMilliseconds(20));
}

static IEnumerable<Task<LongHistogram>> BuildReporters(int sendReportEvery, int numberOfEntriesInReport, EndpointInstanceId[] instances, CancellationTokenSource source, Action<RawMessage.Entry[], EndpointInstanceId> store)
static IEnumerable<Task<LongHistogram>> BuildReporters(int sendReportEvery, int numberOfEntriesInReport, EndpointInstanceId[] instances, Action<RawMessage.Entry[], EndpointInstanceId> store, CancellationToken cancellationToken)
{
return instances
.Select(instance => StartReporter(sendReportEvery, numberOfEntriesInReport, source, instance, store))
.Select(instance => StartReporter(sendReportEvery, numberOfEntriesInReport, instance, store, cancellationToken))
.ToArray();
}

static Task<LongHistogram> StartReporter(int sendReportEvery, int numberOfEntriesInReport, CancellationTokenSource source, EndpointInstanceId instance, Action<RawMessage.Entry[], EndpointInstanceId> store)
static Task<LongHistogram> StartReporter(int sendReportEvery, int numberOfEntriesInReport, EndpointInstanceId instance, Action<RawMessage.Entry[], EndpointInstanceId> store, CancellationToken cancellationToken)
{
return Task.Run(async () =>
{
var entries = new RawMessage.Entry[numberOfEntriesInReport];
var histogram = CreateTimeHistogram();

while (source.IsCancellationRequested == false)
while (cancellationToken.IsCancellationRequested == false)
{
var now = DateTime.UtcNow;

Expand All @@ -178,7 +178,9 @@ static Task<LongHistogram> StartReporter(int sendReportEvery, int numberOfEntrie
var elapsed = Stopwatch.GetTimestamp() - start;
histogram.RecordValue(elapsed);

await Task.Delay(sendReportEvery);
// Not cancelled: the loop exits on the next check and the task has to complete
// normally, because MergeHistograms reads the histogram it returns.
await Task.Delay(sendReportEvery, CancellationToken.None);
}

return histogram;
Expand Down Expand Up @@ -247,8 +249,8 @@ public void TrackEndpointInputQueue(EndpointToQueueMapping queueToTrack)
{
}

public Task StartAsync(CancellationToken cancellationToken) => Task.CompletedTask;
public Task StartAsync(CancellationToken cancellationToken = default) => Task.CompletedTask;

public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
public Task StopAsync(CancellationToken cancellationToken = default) => Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public ServiceControlComponentBehavior(ITransportIntegration transportToUse, Act
public HttpClient HttpClient => runner.HttpClient;
public JsonSerializerOptions SerializerOptions => runner.SerializerOptions;

#pragma warning disable PS0018 // IComponentBehavior declares this without a CancellationToken
public async Task<ComponentRunner> CreateRunner(RunDescriptor run)
#pragma warning restore PS0018
{
runner = new ServiceControlComponentRunner(transportIntegration, setSettings, customConfiguration);
await runner.Initialize(run);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ class ServiceControlComponentRunner(
public HttpClient HttpClient { get; private set; }
public JsonSerializerOptions SerializerOptions => Infrastructure.SerializerOptions.Default;

public Task Initialize(RunDescriptor run) => InitializeServiceControl(run.ScenarioContext);
public Task Initialize(RunDescriptor run, CancellationToken cancellationToken = default) => InitializeServiceControl(run.ScenarioContext, cancellationToken);

async Task InitializeServiceControl(ScenarioContext context)
async Task InitializeServiceControl(ScenarioContext context, CancellationToken cancellationToken)
{
LoggerUtil.ActiveLoggers = Loggers.Test;
settings = new Settings(transportType: transportToUse.TypeName)
Expand Down Expand Up @@ -79,7 +79,7 @@ async Task InitializeServiceControl(ScenarioContext context)
using (new DiagnosticTimer($"Creating infrastructure for {settings.InstanceName}"))
{
var setupCommand = new SetupCommand();
await setupCommand.Execute(new HostArguments([]), settings);
await setupCommand.Execute(new HostArguments([]), settings, cancellationToken);
}

var configuration = new EndpointConfiguration(settings.InstanceName);
Expand Down Expand Up @@ -127,7 +127,7 @@ async Task InitializeServiceControl(ScenarioContext context)
host.UseTestRemoteIp();
host.UseServiceControlAuthentication(settings.OpenIdConnectSettings.Enabled);
host.UseServiceControlMonitoring(settings.ForwardedHeadersSettings, settings.HttpsSettings, settings.CorsSettings);
await host.StartAsync();
await host.StartAsync(cancellationToken);

HttpClient = host.Services.GetRequiredKeyedService<TestServer>(settings.InstanceName).CreateClient();
}
Expand Down
7 changes: 0 additions & 7 deletions src/ServiceControl.Monitoring/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,3 @@

# Justification: Application synchronization contexts don't require ConfigureAwait(false)
dotnet_diagnostic.CA2007.severity = none

# Cancellation analyzer debt. These fire because Particular.Analyzers is no longer pinned to 0.9.0.
# They are scheduled work, not accepted exceptions: remove a line once this project has no
# violations of that rule left, and never add a rule back to this list.
dotnet_diagnostic.PS0003.severity = none
dotnet_diagnostic.PS0018.severity = none
dotnet_diagnostic.PS0019.severity = none
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
namespace ServiceControl.Monitoring
{
using System.Threading;
using System.Threading.Tasks;

abstract class AbstractCommand
{
public abstract Task Execute(HostArguments args, Settings settings);
public abstract Task Execute(HostArguments args, Settings settings, CancellationToken cancellationToken = default);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
namespace ServiceControl.Monitoring
{
using System;
using System.Threading;
using System.Threading.Tasks;

class CommandRunner(Type commandType)
{
public async Task Execute(HostArguments args, Settings settings)
public async Task Execute(HostArguments args, Settings settings, CancellationToken cancellationToken = default)
{
var command = (AbstractCommand)Activator.CreateInstance(commandType);
await command.Execute(args, settings);
await command.Execute(args, settings, cancellationToken);
}
}
}
3 changes: 2 additions & 1 deletion src/ServiceControl.Monitoring/Hosting/Commands/RunCommand.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace ServiceControl.Monitoring
{
using System.Threading;
using System.Threading.Tasks;
using Infrastructure;
using Infrastructure.WebApi;
Expand All @@ -10,7 +11,7 @@ namespace ServiceControl.Monitoring

class RunCommand : AbstractCommand
{
public override async Task Execute(HostArguments args, Settings settings)
public override async Task Execute(HostArguments args, Settings settings, CancellationToken cancellationToken = default)
{
var endpointConfiguration = new EndpointConfiguration(settings.InstanceName);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
namespace ServiceControl.Monitoring
{
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using ServiceControl.Infrastructure;
using Transports;

class SetupCommand : AbstractCommand
{
public override Task Execute(HostArguments args, Settings settings)
public override Task Execute(HostArguments args, Settings settings, CancellationToken cancellationToken = default)
{
if (args.SkipQueueCreation)
{
Expand All @@ -18,7 +19,7 @@ public override Task Execute(HostArguments args, Settings settings)
var transportSettings = settings.ToTransportSettings();
transportSettings.ErrorQueue = settings.ErrorQueue;
var transportCustomization = TransportFactory.Create(transportSettings);
return transportCustomization.ProvisionQueues(transportSettings, []);
return transportCustomization.ProvisionQueues(transportSettings, [], cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class RemoveExpiredEndpointInstances(
{
const int IntervalInMinutes = 5;

protected override async Task ExecuteAsync(CancellationToken cancellationToken)
protected override async Task ExecuteAsync(CancellationToken cancellationToken = default)
{
logger.LogInformation($"Starting {nameof(RemoveExpiredEndpointInstances)}");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

class ReportThroughputHostedService(ILogger<ReportThroughputHostedService> logger, IMessageSession session, IEndpointMetricsApi endpointMetricsApi, Settings settings, TimeProvider timeProvider, ITransportCustomization transportCustomization) : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken cancellationToken)
protected override async Task ExecuteAsync(CancellationToken cancellationToken = default)
{
logger.LogInformation($"Starting {nameof(ReportThroughputHostedService)}");

Expand All @@ -29,7 +29,10 @@ protected override async Task ExecuteAsync(CancellationToken cancellationToken)
{
await ReportOnThroughput(serviceControlThroughputDataQueue, cancellationToken);
}
#pragma warning disable PS0019 // The filter already excludes OperationCanceledException, so cancellation
// is left to the outer handler.
catch (Exception ex) when (ex is not OperationCanceledException)
#pragma warning restore PS0019
{
if (ex.InnerException is not null and QueueNotFoundException)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class LicenseCheckHostedService(ActiveLicense activeLicense, IAsyncTimer scheduler, ILogger<LicenseCheckHostedService> logger) : IHostedService
{
public Task StartAsync(CancellationToken cancellationToken)
public Task StartAsync(CancellationToken cancellationToken = default)
{
var due = TimeSpan.FromHours(8);
timer = scheduler.Schedule(_ =>
Expand All @@ -20,7 +20,7 @@ public Task StartAsync(CancellationToken cancellationToken)
return Task.CompletedTask;
}

public Task StopAsync(CancellationToken cancellationToken) => timer.Stop(cancellationToken);
public Task StopAsync(CancellationToken cancellationToken = default) => timer.Stop(cancellationToken);

TimerJob timer;

Expand Down