test: validate grpc data connections with local docker and opcua cli

This commit is contained in:
Joseph Doherty
2026-11-17 22:01:26 -05:00
parent 1df29a3124
commit 29fec57fac
194 changed files with 23327 additions and 740 deletions
@@ -335,6 +335,10 @@ public static class DataProtoMapper
v.Remove = new W.HandleChanges();
v.Remove.Items.AddRange(r.Items.Select(EncodeHandle));
break;
case ChallengeSubscriptionFreshness c:
v.FreshnessChallenge = new W.HandleChanges();
v.FreshnessChallenge.Items.AddRange(c.Items.Select(EncodeHandle));
break;
default:
throw new ArgumentException("Unsupported command.");
}
@@ -353,6 +357,7 @@ public static class DataProtoMapper
W.SubscriptionCommand.CommandOneofCase.Add => x.Add.Items.Count,
W.SubscriptionCommand.CommandOneofCase.Modify => x.Modify.Items.Count,
W.SubscriptionCommand.CommandOneofCase.Remove => x.Remove.Items.Count,
W.SubscriptionCommand.CommandOneofCase.FreshnessChallenge => x.FreshnessChallenge.Items.Count,
_ => 0,
};
Preflight(x, count, limits);
@@ -372,6 +377,10 @@ public static class DataProtoMapper
|| handle.StreamEpoch != x.StreamEpoch
)
throw new ArgumentException("Removal handle belongs to another stream.");
if (x.FreshnessChallenge is { } challenge)
foreach (var handle in challenge.Items)
if (handle.SubscriptionId != x.SubscriptionId || handle.StreamEpoch != x.StreamEpoch)
throw new ArgumentException("Challenge handle belongs to another stream.");
var t = TimeSpan.FromTicks(x.AcknowledgementTimeoutTicks);
return x.CommandCase switch
{
@@ -406,6 +415,9 @@ public static class DataProtoMapper
t,
x.Remove.Items.Select(DecodeHandle).ToArray()
),
W.SubscriptionCommand.CommandOneofCase.FreshnessChallenge => new ChallengeSubscriptionFreshness(
x.SubscriptionId, x.StreamEpoch, x.CommandId, t,
x.FreshnessChallenge.Items.Select(DecodeHandle).ToArray()),
_ => throw new ArgumentException("Missing command."),
};
}
@@ -488,7 +500,8 @@ public static class DataProtoMapper
serving,
update.Version
);
items.Add(new DataSubscriptionValue(handle, sample, ageBounds, readiness));
items.Add(new DataSubscriptionValue(handle, sample, ageBounds, readiness,
subscriptionAgeReadiness && update.HasFreshnessChallengeId && update.FreshnessChallengeId.Length <= 32 ? update.FreshnessChallengeId : null));
}
return new DataSubscriptionValues(
frame.SubscriptionId,
@@ -533,6 +546,7 @@ public static class DataProtoMapper
ValidateSubscriptionAgeReadiness(item);
if (item.AgeBounds.Lower is { } lower) update.OriginAgeTicks = lower.Ticks;
update.Readiness = (int)item.Readiness;
if (item.FreshnessChallengeId is { } challenge) update.FreshnessChallengeId = challenge;
}
result.Values.Items.Add(update);
}
@@ -573,6 +587,7 @@ public static class DataProtoMapper
ValidateSubscriptionAgeReadiness(item);
if (item.AgeBounds.Lower is { } lower) update.OriginAgeTicks = lower.Ticks;
update.Readiness = (int)item.Readiness;
if (item.FreshnessChallengeId is { } challenge) update.FreshnessChallengeId = challenge;
}
if (item.Sample.StatusCode != first.StatusCode)
update.StatusCode = item.Sample.StatusCode;
@@ -649,7 +664,8 @@ public static class DataProtoMapper
throw new ArgumentException("Unexpected serving identity.");
var (ageBounds, readiness) = DecodeSubscriptionAgeReadiness(subscriptionAgeReadiness,
item.HasOriginAgeTicks, item.OriginAgeTicks, item.HasReadiness, item.Readiness);
items.Add(new(handle, DecodeSample(item.Sample, maxElements, maxBytes), ageBounds, readiness));
items.Add(new(handle, DecodeSample(item.Sample, maxElements, maxBytes), ageBounds, readiness,
subscriptionAgeReadiness && item.HasFreshnessChallengeId && item.FreshnessChallengeId.Length <= 32 ? item.FreshnessChallengeId : null));
}
return new(
frame.SubscriptionId,
@@ -995,6 +1011,8 @@ public static class DataProtoMapper
SubscriptionAgeReadiness = x.SubscriptionAgeReadiness,
ResponseServingIdentity = x.ResponseServingIdentity,
WriteServingGenerationEnforced = x.WriteServingGenerationEnforced,
SubscriptionFreshnessChallenges = x.SubscriptionFreshnessChallenges,
MetadataItemLeases = x.MetadataItemLeases,
Limits = new W.Limits
{
MaxPageItems = l.MaxPageItems,
@@ -1061,7 +1079,9 @@ public static class DataProtoMapper
x.ClusterId,
x.SubscriptionAgeReadiness,
x.ResponseServingIdentity,
x.WriteServingGenerationEnforced
x.WriteServingGenerationEnforced,
x.SubscriptionFreshnessChallenges,
x.MetadataItemLeases
);
result.Validate();
return result;
@@ -1190,6 +1210,9 @@ public static class DataProtoMapper
public static W.MetadataResult EncodeMetadataResult(DataItemResult<DataMetadata> item)
{
DataContractValidation.Required(item.Status);
if (item.Lease is { } lease && (item.Status != DataItemStatus.Good || item.Value is null ||
lease.CatalogEpoch <= 0 || lease.AuthorityEpoch <= 0 || lease.Duration <= TimeSpan.Zero))
throw new ArgumentException("A metadata lease requires a successful item and valid authority.");
var result = new W.MetadataResult
{
CorrelationId = item.CorrelationId,
@@ -1199,6 +1222,9 @@ public static class DataProtoMapper
result.Value = EncodeMetadata(item.Value);
if (item.Diagnostic is not null)
result.Diagnostic = item.Diagnostic;
if (item.Lease is { } itemLease)
result.Lease = new W.Lease { CatalogEpoch = itemLease.CatalogEpoch, AuthorityEpoch = itemLease.AuthorityEpoch,
DurationTicks = itemLease.Duration.Ticks };
return result;
}
@@ -1210,11 +1236,17 @@ public static class DataProtoMapper
Preflight(item, 1, limits);
var status = (DataItemStatus)item.Status;
DataContractValidation.Required(status);
MetadataLease? lease = item.Lease is { } itemLease
? new(itemLease.CatalogEpoch, itemLease.AuthorityEpoch, TimeSpan.FromTicks(itemLease.DurationTicks)) : null;
if (lease is not null && (status != DataItemStatus.Good || item.Value is null ||
lease.CatalogEpoch <= 0 || lease.AuthorityEpoch <= 0 || lease.Duration <= TimeSpan.Zero))
throw new ArgumentException("A metadata lease requires a successful item and valid authority.");
return new(
item.CorrelationId,
status,
item.Value is null ? null : DecodeMetadata(item.Value),
item.HasDiagnostic ? item.Diagnostic : null
item.HasDiagnostic ? item.Diagnostic : null,
lease
);
}
@@ -30,4 +30,8 @@ public sealed record DataConnectionDiagnosticsSnapshot(
long? WriteFailedCount = null,
long? WriteUnconfirmedCount = null,
TimeSpan? SampleAgeUpperBound = null,
long? CoalescedCount = null);
long? CoalescedCount = null,
long? CompactFrameCount = null,
long? FallbackFrameCount = null,
long? SerializedValueFrameBytes = null,
string? AccountingInstanceId = null);
@@ -8,6 +8,8 @@ public static class DataConnectionProtoContract
public const int ProtocolMajor = 1;
public const int ProtocolMinor = 1;
public const int CompactCodecVersion = 1;
/// <summary>Optional, per-subscription client opt-in to a supported compact codec.</summary>
public const string CompactCodecHeader = "x-data-compact-codec";
public static void Validate()
{
@@ -114,6 +114,11 @@ message CapabilitiesResponse {
bool subscription_age_readiness = 11;
bool response_serving_identity = 12;
bool write_serving_generation_enforced = 13;
// The server accepts freshness_challenge and captures each stamped age after
// admitting the identified challenge. Absence or false leaves stream age unbounded.
bool subscription_freshness_challenges = 14;
// Good GetMetadata results may include an exact-item authority lease.
bool metadata_item_leases = 15;
}
message Metadata {
View view = 1;
@@ -159,6 +164,8 @@ message MetadataResult {
int32 status = 2;
Metadata value = 3;
optional string diagnostic = 4;
// Valid only for this authorized item and this response's serving identity.
Lease lease = 5;
}
message MetadataResponse { repeated MetadataResult items = 1; Serving serving = 2; }
message ReadItem {
@@ -227,6 +234,9 @@ message SubscriptionCommand {
ItemChanges add = 6;
ItemChanges modify = 7;
HandleChanges remove = 8;
// Subscribe-authorized bounded current snapshot of these active item revisions.
// command_id is a per-stream strictly increasing freshness sequence.
HandleChanges freshness_challenge = 9;
}
}
message Open {
@@ -265,6 +275,9 @@ message ValueUpdate {
// Age since normalized origin-cache ingress, not sensor measurement age.
optional int64 origin_age_ticks = 3;
optional int32 readiness = 4;
// The admitted challenge observed before origin_age_ticks was computed.
// The value and this identifier must remain coupled through queued encoding.
optional string freshness_challenge_id = 5;
}
message Values {
bool is_snapshot = 1;
@@ -283,6 +296,8 @@ message CompactUpdate {
// Age since normalized origin-cache ingress, not sensor measurement age.
optional int64 origin_age_ticks = 8;
optional int32 readiness = 9;
// Same causal witness semantics as ValueUpdate.freshness_challenge_id.
optional string freshness_challenge_id = 10;
}
message CompactValues {
bool is_snapshot = 1;
@@ -21,7 +21,7 @@ public sealed class DataConnection
public string SecretReference { get; set; } = "";
public string SecretScopeClusterId { get; set; } = "";
public DataOperations AllowedOperations { get; set; }
public int TimeoutMilliseconds { get; set; } = 5000;
public int TimeoutMilliseconds { get; set; } = 30000;
public int MaxBatchItems { get; set; } = 1000;
public int MaxPageItems { get; set; } = 500;
public int MaxMessageBytes { get; set; } = 4 * 1024 * 1024;
@@ -247,7 +247,8 @@ public sealed record DataLimits(int MaxPageItems, int MaxBatchItems, int MaxMess
public sealed record DataCapabilities(int ProtocolMajor, int ProtocolMinor, ServingIdentity Serving, DataReadiness Readiness,
DataOperations Operations, IReadOnlySet<DataValueKind> ValueKinds, DataLimits Limits, bool CompactBatches,
int CompactCodecVersion, string ClusterId, bool SubscriptionAgeReadiness = false, bool ResponseServingIdentity = false,
bool WriteServingGenerationEnforced = false)
bool WriteServingGenerationEnforced = false, bool SubscriptionFreshnessChallenges = false,
bool MetadataItemLeases = false)
{
public void Validate()
{
@@ -310,6 +311,11 @@ public sealed record DataMetadata(DataView View, string BrowseName, string NodeK
}
}
}
public sealed record DataItemResult<T>(string CorrelationId, DataItemStatus Status, T? Value, string? Diagnostic);
/// <summary>
/// A per-item result. A metadata lease certifies only this returned item; its local
/// request-start timestamp uses the connection's clock and is never serialized.
/// </summary>
public sealed record DataItemResult<T>(string CorrelationId, DataItemStatus Status, T? Value, string? Diagnostic,
MetadataLease? Lease = null, long? LeaseRequestStartedTimestamp = null);
public sealed record DataBrowseBranch(DataView Parent, DataItemStatus Status, IReadOnlyList<DataMetadata> Items,
bool Complete, string? Continuation, MetadataLease? Lease, ServingIdentity? Serving = null);
@@ -10,10 +10,11 @@ public sealed record BrowseRequest(DataView Parent, string? Filter, string? Cont
public sealed record MetadataRequest(IReadOnlyList<DataView> Views, DataBudget Budget);
public sealed record ReadItem(string CorrelationId, DataView View, TimeSpan? MaximumAge);
public sealed record ReadRequest(IReadOnlyList<ReadItem> Items, DataBudget Budget);
/// <summary>Sample is owned by the recipient. Status/age/readiness are distinct from original sample quality.</summary>
/// <summary>Sample is owned by the recipient. Status/age/readiness are distinct from original sample quality.
/// FreshnessObservedTimestamp, when present, is local to the connection clock and is never serialized.</summary>
public sealed record ReadResult(string CorrelationId, DataItemStatus Status, DataReadiness Readiness,
TimeSpan? Age, DataSample? Sample, string? Diagnostic, IDisposable? ResourceLease = null,
DataAgeBounds AgeBounds = default) : IDisposable
DataAgeBounds AgeBounds = default, long? FreshnessObservedTimestamp = null) : IDisposable
{
public void Dispose()
{
@@ -95,6 +96,9 @@ public sealed record ModifySubscriptionItems(string SubscriptionId, long StreamE
IReadOnlyList<DataSubscriptionItem> Items) : DataSubscriptionCommand(SubscriptionId, StreamEpoch, CommandId, AcknowledgementTimeout);
public sealed record RemoveSubscriptionItems(string SubscriptionId, long StreamEpoch, string CommandId, TimeSpan AcknowledgementTimeout,
IReadOnlyList<DataItemHandle> Items) : DataSubscriptionCommand(SubscriptionId, StreamEpoch, CommandId, AcknowledgementTimeout);
/// <summary>Requests a bounded, Subscribe-authorized current-value refresh and starts a freshness witness.</summary>
public sealed record ChallengeSubscriptionFreshness(string SubscriptionId, long StreamEpoch, string CommandId, TimeSpan AcknowledgementTimeout,
IReadOnlyList<DataItemHandle> Items) : DataSubscriptionCommand(SubscriptionId, StreamEpoch, CommandId, AcknowledgementTimeout);
public sealed record DataSubscriptionItemAck(string ClientItemId, DataItemStatus Status, DataItemHandle? Handle,
DataSubscriptionSettings? RevisedSettings, string? Diagnostic);
public abstract record DataSubscriptionFrame(string SubscriptionId, long StreamEpoch, long Sequence);
@@ -121,9 +125,14 @@ public sealed record DataSubscriptionValues(string SubscriptionId, long StreamEp
finally { ResourceLease?.Dispose(); }
}
}
/// <summary>Age bounds describe normalized origin-cache ingress age, independent of wall timestamps.</summary>
/// <summary>
/// Age bounds describe normalized origin-cache ingress age, independent of wall timestamps.
/// A finite bound calculated by a remote adapter is anchored to its own monotonic clock at
/// <c>FreshnessObservedTimestamp</c>; the local observation timestamp is never sent on the wire.
/// </summary>
public sealed record DataSubscriptionValue(DataItemHandle Handle, DataSample Sample,
DataAgeBounds AgeBounds = default, DataReadiness Readiness = DataReadiness.Unspecified);
DataAgeBounds AgeBounds = default, DataReadiness Readiness = DataReadiness.Unspecified, string? FreshnessChallengeId = null,
long? FreshnessObservedTimestamp = null);
/// <summary>Commands and frames are ordered per stream. Dispose cancels the stream and releases demand.</summary>
public interface IDataSubscription : IAsyncDisposable
@@ -131,6 +140,12 @@ public interface IDataSubscription : IAsyncDisposable
Task SendAsync(DataSubscriptionCommand command, CancellationToken cancellationToken);
IAsyncEnumerable<DataSubscriptionFrame> ReadAllAsync(CancellationToken cancellationToken);
}
/// <summary>True only when this stream negotiated causal freshness challenges.</summary>
public interface IDataSubscriptionFreshnessSupport
{
bool SupportsFreshnessChallenges { get; }
int FreshnessMaxItems { get; }
}
/// <summary>Immutable serving identity captured when the subscription was established.</summary>
public interface IDataSubscriptionProvenance
{
@@ -17,7 +17,7 @@ public interface IProjectedSamplePublicationProof
/// <summary>An imported sample retaining original ingress age through local actor and SDK queues.</summary>
public sealed record ProjectedValueWrite(DataRealm Realm, string LocalIdentifier, long ProjectionGeneration,
ServingIdentity Serving, DataValueSnapshot Snapshot, DataAgeBounds OriginAgeBounds, long ReceivedTimestamp,
IProjectedSamplePublicationProof Proof, bool IsCompleteSnapshot = false);
IProjectedSamplePublicationProof Proof, bool IsCompleteSnapshot = false, long? SourceVersion = null);
/// <summary>Applies imported values only after checking their captured proof at the actual SDK boundary.</summary>
public interface IProjectedValueSink
@@ -18,6 +18,16 @@ public interface IAcknowledgedAcquisitionSource
Action<AcquisitionBatch> onAcquired, CancellationToken cancellationToken);
}
/// <summary>
/// An acknowledged acquisition that can replace the ordinary baseline subscription.
/// The returned handle produces the same initial and subsequent native value and status
/// change events as <see cref="ISubscribable.SubscribeAsync"/>, while the acquisition
/// callback reports every physical acquisition, including unchanged successful reads.
/// A driver must explicitly implement this capability only when both feeds describe the
/// same physical subscription and preserve their ordinary handle and ownership semantics.
/// </summary>
public interface IAcknowledgedBaselineAcquisitionSource : IAcknowledgedAcquisitionSource { }
/// <summary>Complete physical interest and revision fence for one producer incarnation.</summary>
public sealed record AcquisitionSubscriptionRequest(IReadOnlyList<string> FullReferences,
TimeSpan RequestedSamplingInterval, long DesiredRevision, long ProducerIncarnation);