04e3969071
AUD-5: PurgeChannelOlderThanAsync binds @channel as varchar(32) and the model gains IX_AuditLog_Channel_Occurred (Category, OccurredAtUtc). The migration + partition-aligned SQL script land in the WP's final migration commit (AuditLogChannelOccurredIndex). sqlcmd -I, SQL_Latin1_General_CP1_CI_AS, scratch DB with 1M AuditLog rows (6 channels, clustered (OccurredAtUtc, EventId), ScriptTrace override 30 d), steady-state per-channel tick (nothing left to purge): before (nvarchar, old indexes): 5,858 logical reads, 124 ms after (varchar + new index): 6 logical reads, 0 ms new index but nvarchar param: 4,747 logical reads, 114 ms (binding matters) SEL-1: site_events idx_events_instance -> idx_events_instance_timestamp (instance_id, timestamp), dropped/rebuilt in place by SiteEventLogSchema.Apply like #123; the storage-cap size model now charges 4 x timestamp per row. sqlite3, 2.7M rows / 20 instances, instance-filtered 501-row page: before: SEARCH idx_events_instance + USE TEMP B-TREE FOR ORDER BY; 396 ms after: SEARCH idx_events_instance_timestamp (temp b-tree for id tiebreak only); 0.7 ms
125 lines
5.9 KiB
C#
125 lines
5.9 KiB
C#
using System.Data;
|
|
using System.Data.Common;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using Microsoft.Data.SqlClient;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Repositories;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Tests.Repositories;
|
|
|
|
public class AuditPurgeBudgetTests
|
|
{
|
|
/// <summary>Verifies that a backlogged channel purge stops at the row budget, including a partial final batch.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task BackloggedChannel_StopsAtBudgetIncludingPartialFinalBatch()
|
|
{
|
|
using var connection = new BackloggedConnection();
|
|
await using var context = new ScadaBridgeDbContext(new DbContextOptionsBuilder<ScadaBridgeDbContext>()
|
|
.UseSqlServer(connection).Options);
|
|
var deleted = await new AuditLogRepository(context).PurgeChannelOlderThanAsync(
|
|
"ScriptTrace", DateTime.UtcNow, batchSize: 3, maxRows: 5);
|
|
Assert.Equal(5, deleted);
|
|
Assert.Equal(new[] { 3, 2 }, connection.RequestedBatches);
|
|
Assert.Equal(ConnectionState.Closed, connection.State);
|
|
}
|
|
|
|
/// <summary>
|
|
/// AUD-5 (WP6 6.5): the channel is bound as varchar(32) to match the Category column — an
|
|
/// nvarchar parameter makes SQL Server convert the column under a SQL collation and rules
|
|
/// out a seek on IX_AuditLog_Channel_Occurred.
|
|
/// </summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
[Fact]
|
|
public async Task ChannelParameter_IsBoundAsVarchar32()
|
|
{
|
|
using var connection = new BackloggedConnection();
|
|
await using var context = new ScadaBridgeDbContext(new DbContextOptionsBuilder<ScadaBridgeDbContext>()
|
|
.UseSqlServer(connection).Options);
|
|
await new AuditLogRepository(context).PurgeChannelOlderThanAsync(
|
|
"ScriptTrace", DateTime.UtcNow, batchSize: 3, maxRows: 3);
|
|
var channel = Assert.Single(connection.ChannelParameters);
|
|
Assert.Equal(SqlDbType.VarChar, channel.Type);
|
|
Assert.Equal(32, channel.Size);
|
|
}
|
|
|
|
private sealed class BackloggedConnection : DbConnection
|
|
{
|
|
/// <summary>Gets the batch sizes requested by the purge, in order.</summary>
|
|
public List<int> RequestedBatches { get; } = new();
|
|
/// <summary>Gets the SQL type and size of each bound <c>@channel</c> parameter, in order.</summary>
|
|
public List<(SqlDbType Type, int Size)> ChannelParameters { get; } = new();
|
|
private ConnectionState _state;
|
|
/// <inheritdoc />
|
|
[AllowNull] public override string ConnectionString { get; set; } = "Server=localhost;Database=test";
|
|
/// <inheritdoc />
|
|
public override string Database => "test";
|
|
/// <inheritdoc />
|
|
public override string DataSource => "localhost";
|
|
/// <inheritdoc />
|
|
public override string ServerVersion => "16.0";
|
|
/// <inheritdoc />
|
|
public override ConnectionState State => _state;
|
|
/// <inheritdoc />
|
|
public override void Open() => _state = ConnectionState.Open;
|
|
/// <inheritdoc />
|
|
public override void Close() => _state = ConnectionState.Closed;
|
|
/// <inheritdoc />
|
|
public override void ChangeDatabase(string databaseName) => throw new NotSupportedException();
|
|
/// <inheritdoc />
|
|
protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel) => throw new NotSupportedException();
|
|
/// <inheritdoc />
|
|
protected override DbCommand CreateDbCommand() => new BackloggedCommand(this);
|
|
}
|
|
|
|
private sealed class BackloggedCommand(BackloggedConnection owner) : DbCommand
|
|
{
|
|
private readonly SqlCommand _parameters = new();
|
|
/// <inheritdoc />
|
|
[AllowNull] public override string CommandText { get; set; } = "";
|
|
/// <inheritdoc />
|
|
public override int CommandTimeout { get; set; }
|
|
/// <inheritdoc />
|
|
public override CommandType CommandType { get; set; }
|
|
/// <inheritdoc />
|
|
public override bool DesignTimeVisible { get; set; }
|
|
/// <inheritdoc />
|
|
public override UpdateRowSource UpdatedRowSource { get; set; }
|
|
/// <inheritdoc />
|
|
protected override DbConnection? DbConnection { get; set; }
|
|
/// <inheritdoc />
|
|
protected override DbTransaction? DbTransaction { get; set; }
|
|
/// <inheritdoc />
|
|
protected override DbParameterCollection DbParameterCollection => _parameters.Parameters;
|
|
/// <inheritdoc />
|
|
protected override DbParameter CreateDbParameter() => new SqlParameter();
|
|
/// <inheritdoc />
|
|
public override int ExecuteNonQuery()
|
|
{
|
|
// A server with more eligible rows than any tick is allowed to delete.
|
|
// Fail rapidly if the repository loops indefinitely, without a timer-based test.
|
|
if (owner.RequestedBatches.Count >= 10)
|
|
throw new InvalidOperationException("Purge did not yield despite exhausting its row budget.");
|
|
var requested = (int)_parameters.Parameters["@batch"].Value;
|
|
owner.RequestedBatches.Add(requested);
|
|
var channel = _parameters.Parameters["@channel"];
|
|
owner.ChannelParameters.Add((channel.SqlDbType, channel.Size));
|
|
return requested;
|
|
}
|
|
/// <inheritdoc />
|
|
public override object? ExecuteScalar() => throw new NotSupportedException();
|
|
/// <inheritdoc />
|
|
protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior) => throw new NotSupportedException();
|
|
/// <inheritdoc />
|
|
public override void Cancel() { }
|
|
/// <inheritdoc />
|
|
public override void Prepare() { }
|
|
/// <inheritdoc />
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
if (disposing) _parameters.Dispose();
|
|
base.Dispose(disposing);
|
|
}
|
|
}
|
|
}
|