fix(grpc-data): H2 review 9 - upper bounds on the history limits (validator + AdminUI max)

This commit is contained in:
Joseph Doherty
2026-11-23 21:54:38 -05:00
parent 6a621423f4
commit 82d34b84a8
3 changed files with 46 additions and 8 deletions
@@ -17,6 +17,18 @@ public static class DataConnectionValidator
private const DataOperations KnownOperations = DataOperationSets.V1 | DataOperations.HistoryRead;
private static readonly StringComparer Ids = StringComparer.OrdinalIgnoreCase;
// H2 upper bounds: keep derived capacities (sessions x reads x batch, remainder budgets) far from int overflow.
/// <summary>Largest <c>DataConnection.HistoryTimeoutMilliseconds</c> (1 h).</summary>
public const int MaxHistoryTimeoutMilliseconds = 3_600_000;
/// <summary>Largest <c>DataExport.MaxConcurrentHistoryReads</c>.</summary>
public const int MaxConcurrentHistoryReadsLimit = 64;
/// <summary>Largest <c>DataExport.MaxHistoryValuesPerPage</c>.</summary>
public const int MaxHistoryValuesPerPageLimit = 1_000_000;
/// <summary>Largest <c>DataExport.MaxHistoryResponseBytes</c> (64 MiB).</summary>
public const int MaxHistoryResponseBytesLimit = 64 * 1024 * 1024;
/// <summary>Largest <c>RemoteNamespaceMapping.MaxHistoryAssemblyBytesPerRequest</c> (1 GiB).</summary>
public const int MaxHistoryAssemblyBytesLimit = 1024 * 1024 * 1024;
/// <summary>Authoring/deploy-gate validation: an operation bit outside the known set is an error.</summary>
public static IReadOnlyList<ValidationError> Validate(DraftSnapshot draft) => Validate(draft, deployed: false);
@@ -124,7 +136,7 @@ public static class DataConnectionValidator
Add("DataConnectionOperationsInvalid", id, "Connection operations must be a supported nonempty subset.");
if (connection.TimeoutMilliseconds <= 0 || connection.MaxBatchItems <= 0 || connection.MaxPageItems <= 0 ||
connection.MaxMessageBytes <= 0 || connection.MaxStreams <= 0 || connection.MaxMonitoredItems <= 0 || connection.MaxPendingBytes <= 0 ||
connection.HistoryTimeoutMilliseconds <= 0)
connection.HistoryTimeoutMilliseconds <= 0 || connection.HistoryTimeoutMilliseconds > MaxHistoryTimeoutMilliseconds)
Add("DataConnectionLimitInvalid", id, "Connection limits must be positive.");
}
@@ -277,7 +289,7 @@ public static class DataConnectionValidator
Add("DataMappingOperationsInvalid", id, "Mapping operations must be permitted by the connection and write-through policy.");
if (mapping.MaxProjectionNodes <= 0 || mapping.MaxProjectionDepth <= 0 || mapping.MetadataLeaseMilliseconds <= 0 ||
mapping.StaleTimeoutMilliseconds <= 0 || mapping.SamplingIntervalMilliseconds <= 0 || mapping.PublishingIntervalMilliseconds <= 0 ||
mapping.MaxHistoryAssemblyBytesPerRequest <= 0)
mapping.MaxHistoryAssemblyBytesPerRequest <= 0 || mapping.MaxHistoryAssemblyBytesPerRequest > MaxHistoryAssemblyBytesLimit)
Add("DataMappingLimitInvalid", id, "Mapping limits and intervals must be positive and the metadata lease bounded.");
}
foreach (var export in draft.DataExports.Where(x => x.Enabled))
@@ -289,7 +301,9 @@ public static class DataConnectionValidator
Add("DataExportRootInvalid", id, "Export requires a named principal and an origin-local root in its realm.");
if (!ValidOps(export.AllowedOperations)) Add("DataExportOperationsInvalid", id, "Export operations must be a supported nonempty subset.");
if (export.MaxBatchItems <= 0 || export.MaxPageItems <= 0 || export.MaxMonitoredItems <= 0 || export.MaxStreams <= 0 ||
export.MaxConcurrentHistoryReads <= 0 || export.MaxHistoryValuesPerPage <= 0 || export.MaxHistoryResponseBytes <= 0)
export.MaxConcurrentHistoryReads <= 0 || export.MaxConcurrentHistoryReads > MaxConcurrentHistoryReadsLimit ||
export.MaxHistoryValuesPerPage <= 0 || export.MaxHistoryValuesPerPage > MaxHistoryValuesPerPageLimit ||
export.MaxHistoryResponseBytes <= 0 || export.MaxHistoryResponseBytes > MaxHistoryResponseBytesLimit)
Add("DataExportLimitInvalid", id, "Export limits must be positive.");
}
return errors;
@@ -73,7 +73,7 @@ else
<label class="form-label" for="secret-ref">Secret reference</label><input id="secret-ref" class="form-control mb-2" placeholder="secret:data/…" @bind="c.SecretReference" /><div class="form-text">Reference only; never enter a key or secret value.</div>
<fieldset><legend class="fs-6">Consumer nodes</legend>@foreach (var n in Nodes) { <label class="me-3"><input type="checkbox" checked="@c.ConsumerNodeIds.Contains(n.NodeId)" @onchange="e => Toggle(c.ConsumerNodeIds, n.NodeId, (bool)e.Value!)" /> @n.NodeId</label> }</fieldset>
@Operations(c.AllowedOperations, v => c.AllowedOperations = v)
<label class="form-label" for="history-timeout">History call timeout (ms)</label><input id="history-timeout" type="number" min="1" class="form-control mb-2" @bind="c.HistoryTimeoutMilliseconds" /><div class="form-text">Each remote HistoryRead call is bounded by the caller's remaining deadline and this.</div>
<label class="form-label" for="history-timeout">History call timeout (ms)</label><input id="history-timeout" type="number" min="1" max="@DataConnectionValidator.MaxHistoryTimeoutMilliseconds" class="form-control mb-2" @bind="c.HistoryTimeoutMilliseconds" /><div class="form-text">Each remote HistoryRead call is bounded by the caller's remaining deadline and this.</div>
<label><input type="checkbox" @bind="c.Enabled" /> Enabled in next deployment</label>
@Actions(c, c.DataConnectionRowId != Guid.Empty)
</div></section>
@@ -87,9 +87,9 @@ else
<label class="form-label" for="export-realm">Realm</label><select id="export-realm" class="form-select mb-2" @bind="e.Realm"><option value="@DataRealm.Raw">Raw</option><option value="@DataRealm.Uns">UNS</option></select>
<label class="form-label" for="export-root">Local export root</label><select id="export-root" class="form-select mb-2" value="@(e.RootKind + "|" + e.RootEntityId)" @onchange="args => SetExportRoot(e, args.Value?.ToString())"><option value="">Choose a root</option>@foreach (var p in ExportRoots(e.Realm)) { <option value="@p.Value">@p.Label</option> }</select>
@Operations(e.AllowedOperations, v => e.AllowedOperations = v)
<label class="form-label" for="export-history-reads">Concurrent history reads per peer</label><input id="export-history-reads" type="number" min="1" class="form-control mb-2" @bind="e.MaxConcurrentHistoryReads" />
<label class="form-label" for="export-history-page">History values per page</label><input id="export-history-page" type="number" min="1" class="form-control mb-2" @bind="e.MaxHistoryValuesPerPage" />
<label class="form-label" for="export-history-bytes">History response bytes</label><input id="export-history-bytes" type="number" min="1" class="form-control mb-2" @bind="e.MaxHistoryResponseBytes" /><div class="form-text">Larger answers are sent in bounded chunks.</div>
<label class="form-label" for="export-history-reads">Concurrent history reads per peer</label><input id="export-history-reads" type="number" min="1" max="@DataConnectionValidator.MaxConcurrentHistoryReadsLimit" class="form-control mb-2" @bind="e.MaxConcurrentHistoryReads" />
<label class="form-label" for="export-history-page">History values per page</label><input id="export-history-page" type="number" min="1" max="@DataConnectionValidator.MaxHistoryValuesPerPageLimit" class="form-control mb-2" @bind="e.MaxHistoryValuesPerPage" />
<label class="form-label" for="export-history-bytes">History response bytes</label><input id="export-history-bytes" type="number" min="1" max="@DataConnectionValidator.MaxHistoryResponseBytesLimit" class="form-control mb-2" @bind="e.MaxHistoryResponseBytes" /><div class="form-text">Larger answers are sent in bounded chunks.</div>
<label><input type="checkbox" @bind="e.Enabled" /> Enabled in next deployment</label>@Actions(e, e.DataExportRowId != Guid.Empty)
</div></section>
}
@@ -107,7 +107,7 @@ else
<label class="form-label" for="identifier">Remote canonical identifier</label><input id="identifier" class="form-control mb-2" @bind="m.RemoteIdentifier" />
<fieldset><legend class="fs-6">Consumer nodes</legend>@foreach (var n in Nodes) { <label class="me-3"><input type="checkbox" checked="@m.ConsumerNodeIds.Contains(n.NodeId)" @onchange="e => Toggle(m.ConsumerNodeIds, n.NodeId, (bool)e.Value!)" /> @n.NodeId</label> }</fieldset>
@Operations(m.AllowedOperations, v => m.AllowedOperations = v)
<label class="form-label" for="mapping-history-bytes">History assembly bytes per request</label><input id="mapping-history-bytes" type="number" min="1" class="form-control mb-2" @bind="m.MaxHistoryAssemblyBytesPerRequest" />
<label class="form-label" for="mapping-history-bytes">History assembly bytes per request</label><input id="mapping-history-bytes" type="number" min="1" max="@DataConnectionValidator.MaxHistoryAssemblyBytesLimit" class="form-control mb-2" @bind="m.MaxHistoryAssemblyBytesPerRequest" />
<div><label><input type="checkbox" @bind="m.WriteThrough" /> Allow write-through</label></div><label><input type="checkbox" @bind="m.Enabled" /> Enabled in next deployment</label>@Actions(m, m.RemoteNamespaceMappingRowId != Guid.Empty)
</div></section>
}
@@ -66,6 +66,30 @@ public sealed class GrpcDataHistoryConfigurationTests
static (string, string) Set(Action set, string code, string context) { set(); return (code, context); }
}
[Fact]
public void History_limits_have_upper_bounds_so_derived_capacities_cannot_overflow()
{
DataConnectionValidator.MaxHistoryTimeoutMilliseconds.ShouldBe(3_600_000);
DataConnectionValidator.MaxConcurrentHistoryReadsLimit.ShouldBe(64);
DataConnectionValidator.MaxHistoryValuesPerPageLimit.ShouldBe(1_000_000);
DataConnectionValidator.MaxHistoryResponseBytesLimit.ShouldBe(64 * 1024 * 1024);
DataConnectionValidator.MaxHistoryAssemblyBytesLimit.ShouldBe(1024 * 1024 * 1024);
foreach (var (set, code) in new (Action<DraftSnapshot>, string)[]
{
(d => d.DataConnections[0].HistoryTimeoutMilliseconds = 3_600_001, "DataConnectionLimitInvalid"),
(d => d.DataExports[0].MaxConcurrentHistoryReads = 65, "DataExportLimitInvalid"),
(d => d.DataExports[0].MaxHistoryValuesPerPage = 1_000_001, "DataExportLimitInvalid"),
(d => d.DataExports[0].MaxHistoryResponseBytes = 64 * 1024 * 1024 + 1, "DataExportLimitInvalid"),
(d => d.RemoteNamespaceMappings[0].MaxHistoryAssemblyBytesPerRequest = int.MaxValue, "DataMappingLimitInvalid"),
})
{
var draft = GrpcDataReservedOperationAuthoringTests.Snapshot(DataOperations.Read | DataOperations.HistoryRead);
set(draft);
DataConnectionValidator.Validate(draft).ShouldContain(x => x.Code == code);
DataConnectionValidator.ValidateDeployed(draft).ShouldContain(x => x.Code == code);
}
}
[Fact]
public void Limits_ride_the_artifact_so_the_trust_scope_revision_ignores_the_history_timeout()
{