|
|
|
@@ -0,0 +1,134 @@
|
|
|
|
|
using System.Data.Common;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using JobTrackerApi.Controllers;
|
|
|
|
|
using JobTrackerApi.Data;
|
|
|
|
|
using JobTrackerApi.Models;
|
|
|
|
|
using JobTrackerApi.Services;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.EntityFrameworkCore.Diagnostics;
|
|
|
|
|
using MySqlConnector;
|
|
|
|
|
using Xunit;
|
|
|
|
|
using Xunit.Abstractions;
|
|
|
|
|
|
|
|
|
|
namespace JobTrackerApi.MariaDb.Tests;
|
|
|
|
|
|
|
|
|
|
[CollectionDefinition("Rules MariaDB measurements", DisableParallelization = true)]
|
|
|
|
|
public sealed class RulesMariaDbMeasurementCollection;
|
|
|
|
|
|
|
|
|
|
[Collection("Rules MariaDB measurements")]
|
|
|
|
|
public sealed class RulesFreshnessMariaDbTests(ITestOutputHelper output)
|
|
|
|
|
{
|
|
|
|
|
private const string Owner = "rules-owner";
|
|
|
|
|
private const string Other = "rules-other";
|
|
|
|
|
private const string Fallback = "rules-fallback";
|
|
|
|
|
private const int Repetitions = 100;
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Timed_out_save_preserves_committed_rules_and_retry_reaches_existing_readers()
|
|
|
|
|
{
|
|
|
|
|
await using var database = await MariaDbDatabase.CreateAsync();
|
|
|
|
|
await Seed(database);
|
|
|
|
|
await using var ownerReader = database.Context(Owner);
|
|
|
|
|
await using var otherReader = database.Context(Other);
|
|
|
|
|
Assert.Equal(30, (await RulesEngine.GetSettings(ownerReader, default)).AppliedFollowUpDays);
|
|
|
|
|
Assert.Equal(45, (await RulesEngine.GetSettings(otherReader, default)).AppliedFollowUpDays);
|
|
|
|
|
|
|
|
|
|
await using var lockOwner = database.Context(Owner);
|
|
|
|
|
await using var transaction = await lockOwner.Database.BeginTransactionAsync();
|
|
|
|
|
var locked = await lockOwner.UserRuleSettings.SingleAsync();
|
|
|
|
|
locked.AppliedFollowUpDays = 31;
|
|
|
|
|
await lockOwner.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
await using var failedWriter = database.Context(Owner);
|
|
|
|
|
await failedWriter.Database.OpenConnectionAsync();
|
|
|
|
|
await failedWriter.Database.ExecuteSqlRawAsync("SET SESSION innodb_lock_wait_timeout = 1");
|
|
|
|
|
var started = Stopwatch.GetTimestamp();
|
|
|
|
|
var error = await Assert.ThrowsAsync<InvalidOperationException>(
|
|
|
|
|
() => new RulesController(failedWriter).Update(new RuleSettings { AppliedFollowUpDays = 1 }, default));
|
|
|
|
|
var update = Assert.IsType<DbUpdateException>(error.InnerException);
|
|
|
|
|
Assert.Equal(1205, Assert.IsType<MySqlException>(update.InnerException).Number);
|
|
|
|
|
var timeout = Stopwatch.GetElapsedTime(started);
|
|
|
|
|
|
|
|
|
|
Assert.Equal(1, failedWriter.UserRuleSettings.Local.Single().AppliedFollowUpDays);
|
|
|
|
|
Assert.Equal(30, (await RulesEngine.GetSettings(failedWriter, default)).AppliedFollowUpDays);
|
|
|
|
|
Assert.Equal(30, (await RulesEngine.GetSettings(ownerReader, default)).AppliedFollowUpDays);
|
|
|
|
|
Assert.Equal(45, (await RulesEngine.GetSettings(otherReader, default)).AppliedFollowUpDays);
|
|
|
|
|
await transaction.RollbackAsync();
|
|
|
|
|
|
|
|
|
|
await using var retry = database.Context(Owner);
|
|
|
|
|
started = Stopwatch.GetTimestamp();
|
|
|
|
|
Assert.IsType<NoContentResult>(
|
|
|
|
|
await new RulesController(retry).Update(new RuleSettings { AppliedFollowUpDays = 1 }, default));
|
|
|
|
|
var retryDuration = Stopwatch.GetElapsedTime(started);
|
|
|
|
|
Assert.Equal(1, (await RulesEngine.GetSettings(ownerReader, default)).AppliedFollowUpDays);
|
|
|
|
|
Assert.Equal(45, (await RulesEngine.GetSettings(otherReader, default)).AppliedFollowUpDays);
|
|
|
|
|
output.WriteLine($"MariaDB {database.ActualVersion} lock timeout {timeout.TotalMilliseconds:F1} ms; retry {retryDuration.TotalMilliseconds:F1} ms.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Committed_owner_and_fallback_reads_measure_queries_and_latency_with_same_workload()
|
|
|
|
|
{
|
|
|
|
|
await using var database = await MariaDbDatabase.CreateAsync();
|
|
|
|
|
await Seed(database);
|
|
|
|
|
var ownerQueries = new QueryCounter();
|
|
|
|
|
var fallbackQueries = new QueryCounter();
|
|
|
|
|
await using var owner = database.Context(Owner, ownerQueries);
|
|
|
|
|
await using var fallback = database.Context(Fallback, fallbackQueries);
|
|
|
|
|
|
|
|
|
|
Assert.Equal(30, (await RulesEngine.GetSettings(owner, default)).AppliedFollowUpDays);
|
|
|
|
|
Assert.Equal(60, (await RulesEngine.GetSettings(fallback, default)).AppliedFollowUpDays);
|
|
|
|
|
ownerQueries.Count = fallbackQueries.Count = 0;
|
|
|
|
|
double[] ownerTimes = new double[Repetitions];
|
|
|
|
|
double[] fallbackTimes = new double[Repetitions];
|
|
|
|
|
|
|
|
|
|
for (var index = 0; index < Repetitions; index++)
|
|
|
|
|
{
|
|
|
|
|
ownerTimes[index] = await MeasureRead(owner, 30);
|
|
|
|
|
fallbackTimes[index] = await MeasureRead(fallback, 60);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Assert.Equal(Repetitions, ownerQueries.Count);
|
|
|
|
|
Assert.Equal(Repetitions * 2, fallbackQueries.Count);
|
|
|
|
|
Array.Sort(ownerTimes);
|
|
|
|
|
Array.Sort(fallbackTimes);
|
|
|
|
|
output.WriteLine(FormattableString.Invariant(
|
|
|
|
|
$"MariaDB {database.ActualVersion}; {Repetitions} reads per path. Owner {ownerQueries.Count} commands, p50/p95 {ownerTimes[Repetitions / 2 - 1]:F3}/{ownerTimes[Repetitions * 95 / 100 - 1]:F3} ms. Fallback {fallbackQueries.Count} commands, p50/p95 {fallbackTimes[Repetitions / 2 - 1]:F3}/{fallbackTimes[Repetitions * 95 / 100 - 1]:F3} ms."));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static async Task<double> MeasureRead(JobTrackerContext db, int expected)
|
|
|
|
|
{
|
|
|
|
|
var start = Stopwatch.GetTimestamp();
|
|
|
|
|
var settings = await RulesEngine.GetSettings(db, default);
|
|
|
|
|
var elapsed = Stopwatch.GetElapsedTime(start).TotalMilliseconds;
|
|
|
|
|
Assert.Equal(expected, settings.AppliedFollowUpDays);
|
|
|
|
|
return elapsed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static async Task Seed(MariaDbDatabase database)
|
|
|
|
|
{
|
|
|
|
|
await using var db = database.Context();
|
|
|
|
|
db.Users.AddRange(
|
|
|
|
|
new ApplicationUser { Id = Owner, UserName = Owner },
|
|
|
|
|
new ApplicationUser { Id = Other, UserName = Other },
|
|
|
|
|
new ApplicationUser { Id = Fallback, UserName = Fallback });
|
|
|
|
|
(await db.RuleSettings.SingleAsync()).AppliedFollowUpDays = 60;
|
|
|
|
|
db.UserRuleSettings.AddRange(
|
|
|
|
|
new UserRuleSettings { OwnerUserId = Owner, AppliedFollowUpDays = 30 },
|
|
|
|
|
new UserRuleSettings { OwnerUserId = Other, AppliedFollowUpDays = 45 });
|
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private sealed class QueryCounter : DbCommandInterceptor
|
|
|
|
|
{
|
|
|
|
|
public int Count { get; set; }
|
|
|
|
|
|
|
|
|
|
public override ValueTask<InterceptionResult<DbDataReader>> ReaderExecutingAsync(
|
|
|
|
|
DbCommand command, CommandEventData eventData, InterceptionResult<DbDataReader> result,
|
|
|
|
|
CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
|
|
|
|
Count++;
|
|
|
|
|
return base.ReaderExecutingAsync(command, eventData, result, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|