feat(grpc-data): H2 consumer history client and deployed-connection history

GrpcDataConnection implements IDataHistoryService (explicitly; ReadHistoryAsync/ReleaseHistoryAsync avoid an overload
clash with ReadAsync): the capabilities probe always asks for history, other calls send it only when the selected
endpoint advertised it; per-call deadline min(remaining, HistoryTimeoutMilliseconds); FAILED_PRECONDITION/UNIMPLEMENTED
=> BadHistoryOperationUnsupported; chunks assembled within the request budget (BadTooManyOperations past it); page
tokens wrapped with the issuing endpoint + serving incarnation so a resumed read never moves; release forwarded. The
deployment lease gates history on the row's HistoryRead bit and carries HistoryTimeout. Decoded capabilities drop
HistoryRead without the history field. X0 tests that used bit 32 as the unknown bit now use a still-reserved bit.
This commit is contained in:
Joseph Doherty
2026-11-23 20:57:35 -05:00
parent d0756bb78e
commit b36c8d8716
10 changed files with 614 additions and 31 deletions
@@ -24,7 +24,7 @@ public sealed class DataConnectionDeploymentRuntime : IAsyncDisposable
{
private sealed record Spec(string Id, long Revision, DataTarget Target, Uri[] Endpoints, HashSet<string> EligibleNodes,
string Secret, DataOperations Operations, TimeSpan Timeout, int Batch, int Page, int MessageBytes,
int Streams, int Monitored, long PendingBytes);
int Streams, int Monitored, long PendingBytes, TimeSpan HistoryTimeout = default);
private sealed record Desired(long ScopeVersion, string ArtifactRevision, long Generation, RedundancyMode ConsumerMode,
IReadOnlyDictionary<string, Spec> Specs, IReadOnlyDictionary<string, string> Allowlist);
private sealed class Entry(Spec spec, long generation, GrpcDataConnection connection, CancellationToken token,
@@ -192,7 +192,8 @@ public sealed class DataConnectionDeploymentRuntime : IAsyncDisposable
throw new ArgumentException("Cluster target has no eligible nodes.", nameof(snapshot));
var spec = new Spec(item.DataConnectionId, item.Revision, target, endpoints, eligible, item.SecretReference,
operations, TimeSpan.FromMilliseconds(item.TimeoutMilliseconds), item.MaxBatchItems,
item.MaxPageItems, item.MaxMessageBytes, item.MaxStreams, item.MaxMonitoredItems, item.MaxPendingBytes);
item.MaxPageItems, item.MaxMessageBytes, item.MaxStreams, item.MaxMonitoredItems, item.MaxPendingBytes,
TimeSpan.FromMilliseconds(item.HistoryTimeoutMilliseconds > 0 ? item.HistoryTimeoutMilliseconds : 30000));
if (!specs.TryAdd(spec.Id, spec) || !allow.TryAdd(spec.Id, spec.Secret))
throw new ArgumentException("Duplicate connection identity.", nameof(snapshot));
}
@@ -243,7 +244,7 @@ public sealed class DataConnectionDeploymentRuntime : IAsyncDisposable
a.Endpoints.SequenceEqual(b.Endpoints) && a.EligibleNodes.SetEquals(b.EligibleNodes) &&
string.Equals(a.Secret, b.Secret, StringComparison.Ordinal) && a.Operations == b.Operations && a.Timeout == b.Timeout &&
a.Batch == b.Batch && a.Page == b.Page && a.MessageBytes == b.MessageBytes && a.Streams == b.Streams &&
a.Monitored == b.Monitored && a.PendingBytes == b.PendingBytes;
a.Monitored == b.Monitored && a.PendingBytes == b.PendingBytes && a.HistoryTimeout == b.HistoryTimeout;
private bool MayAcquire() { try { return _consumerMayAcquire(); } catch (Exception) { return false; } }
private DataConnectionConsumerRoleSnapshot? CaptureRole(RedundancyMode mode)
{ try { return _consumerRoleSnapshot?.Invoke(mode); } catch (Exception) { return null; } }
@@ -428,7 +429,8 @@ public sealed class DataConnectionDeploymentRuntime : IAsyncDisposable
{
var connection = new GrpcDataConnection(spec.Id, spec.Target, spec.Endpoints, _security, scope,
spec.MessageBytes, spec.Endpoints.Length, _clock, spec.EligibleNodes,
spec.Streams, spec.Monitored, maxPendingCommandBytes: spec.PendingBytes, enableCompactBatches: _useCompactBatches);
spec.Streams, spec.Monitored, maxPendingCommandBytes: spec.PendingBytes, enableCompactBatches: _useCompactBatches)
{ HistoryTimeout = spec.HistoryTimeout };
_entries.Add(spec.Id, new Entry(spec, desired.Generation, connection, scope.CancellationToken, desired.ConsumerMode, role) { Ready = true });
}
catch (Exception) { scope.Dispose(); retryNeeded = true; }
@@ -509,8 +511,40 @@ public sealed class DataConnectionDeploymentRuntime : IAsyncDisposable
}
/// <summary>A non-owning handle with the deployed operation and resource policy.</summary>
public sealed class DataConnectionDeploymentLease : IDataConnection, IDisposable
public sealed class DataConnectionDeploymentLease : IDataConnection, IDataHistoryService, IDisposable
{
/// <summary>
/// H2 remote history through the deployed connection: needs the connection row's HistoryRead bit (masked to the
/// understood set) and an inner connection that implements history; otherwise every item is unsupported.
/// </summary>
public async Task<IReadOnlyList<DataHistoryReadResult>> ReadHistoryAsync(DataHistoryReadRequest request, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(request);
Check(DataOperations.HistoryRead);
if (request.Items.Count > _batch) throw new ArgumentOutOfRangeException(nameof(request));
if (_inner is not IDataHistoryService history)
return request.Items.Select(x => new DataHistoryReadResult(x.CorrelationId, GrpcDataConnection.BadHistoryOperationUnsupported, [], null)).ToArray();
// The deadline is the connection's HistoryTimeout (applied per call by GrpcDataConnection), not the unary timeout,
// and MaxBytes is the caller's chunk-assembly budget, so only the item count is clamped here.
request.Budget.Validate();
var value = await history.ReadAsync(request with { Budget = request.Budget with { MaxItems = Math.Min(request.Budget.MaxItems, _batch) } },
cancellationToken).ConfigureAwait(false);
try { Check(DataOperations.HistoryRead); return value; }
catch { foreach (var item in value) item.Dispose(); throw; }
}
Task<IReadOnlyList<DataHistoryReadResult>> IDataHistoryService.ReadAsync(DataHistoryReadRequest request, CancellationToken cancellationToken) =>
ReadHistoryAsync(request, cancellationToken);
Task IDataHistoryService.ReleaseAsync(IReadOnlyList<byte[]> continuations, CancellationToken cancellationToken) =>
ReleaseHistoryAsync(continuations, cancellationToken);
/// <summary>Releases continuations (best effort).</summary>
public Task ReleaseHistoryAsync(IReadOnlyList<byte[]> continuations, CancellationToken cancellationToken)
{
Check(DataOperations.HistoryRead);
return _inner is IDataHistoryService history ? history.ReleaseAsync(continuations, cancellationToken) : Task.CompletedTask;
}
private readonly IDataConnection _inner;
private readonly CancellationToken _scope;
private readonly DataOperations _allowed;