cfd403710e
Every HistoryUpdate a SQL backend applies now leaves a modification trail
(prior value, principal, Part 11 update type, modification time), and
HistoryReadRaw with IsReadModified=true serves it as HistoryModifiedData.
- Core: HistoryUpdateContext threaded into all three IHistorianDataUpdater
methods; IHistorianDataSource.ReadModifiedAsync (default NotSupported =>
BadHistoryOperationUnsupported on Gateway/Null); HistoryModified* contracts.
- SqlServer: schema v2 (002_sample_modified.sql, RequiredVersion=2, docs copy);
trail written by OUTPUT ... INTO on the same MERGE/DELETE statement, so a
failed update leaves no audit row; retention purges SampleModified per tag on
the source-time cutoff; ReadModifiedAsync keyed on (SourceTimeUtc, ModifiedId).
- QuestDB: append-only {prefix}sample_modified (TTL like samples), trail row
written BEFORE the change (orphan-on-crash, never an untracked change);
existence reads now carry the prior value; ReadModifiedAsync.
- Node manager: modified reads dispatch + page via the #669 continuation store
(HistoryContinuationState gains HistoryCursorKind + LastModifiedId), gated by
the existing HistoryRead decision; session principal / key id passed to the
updater. IsDeleteModified stays unsupported.
- Client.CLI: historyread --modified.
- Tests: node-manager dispatch/paging/cursor binding/principal threading,
over-the-wire HistoryModifiedData, shared ModifiedValueConformance on both
backends, SqlServer atomicity (forced trail failure => no change) + v1->v2
upgrade + retention trail purge.
- Docs: Historian.md (Modified-value history section, schema v2 upgrade note),
security.md, Client.CLI.md, CLAUDE.md.
174 lines
10 KiB
C#
174 lines
10 KiB
C#
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
|
using ZB.MOM.WW.OtOpcUa.Driver.Historian.Sql.Common;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Driver.Historian.Conformance;
|
|
|
|
/// <summary>History update/delete result codes and their effect on storage.</summary>
|
|
public abstract class UpdateConformance : ConformanceBase
|
|
{
|
|
private const uint UncertainStatus = HistorianStatusCodes.UncertainDataSubNormal;
|
|
|
|
// Deliberately NOT millisecond (or microsecond) aligned: sub-resolution ticks must survive; stored times are B.Round(T(n)).
|
|
private static readonly DateTime T0 = new(2026, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
|
private static DateTime T(int n) => T0.AddTicks(1234567L * n + 3);
|
|
|
|
private static HistorizationValue V(DateTime? t, double d, uint status = Good) =>
|
|
new(t, HistorianValue.FromDouble(d), status);
|
|
|
|
private static readonly CancellationToken Ct = CancellationToken.None;
|
|
|
|
[Fact]
|
|
public async Task Insert_new_is_GoodEntryInserted_and_existing_is_BadEntryExists_in_order()
|
|
{
|
|
var u = B.CreateUpdater();
|
|
var r1 = await u.UpdateDataAsync("t", HistoryUpdateMode.Insert, [V(T(1), 1), V(T(2), 2)], ConformanceData.Editor, Ct);
|
|
r1.ShouldBe([HistorianStatusCodes.GoodEntryInserted, HistorianStatusCodes.GoodEntryInserted]);
|
|
// Mixed: T(2) exists, T(3) new, T(1) exists - out of timestamp order on purpose.
|
|
var r2 = await u.UpdateDataAsync("t", HistoryUpdateMode.Insert, [V(T(3), 3), V(T(2), 99), V(T(1), 98)], ConformanceData.Editor, Ct);
|
|
r2.ShouldBe([HistorianStatusCodes.GoodEntryInserted, HistorianStatusCodes.BadEntryExists, HistorianStatusCodes.BadEntryExists]);
|
|
var rows = await B.ReadBackAsync("t");
|
|
rows.Select(x => (double?)x.V.Number).ShouldBe([1d, 2d, 3d]); // existing values untouched
|
|
rows.Select(x => x.T).ShouldBe([B.Round(T(1)), B.Round(T(2)), B.Round(T(3))]); // sub-ms ticks preserved
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Replace_existing_is_GoodEntryReplaced_with_new_value_and_status_missing_is_BadNoEntryExists()
|
|
{
|
|
var u = B.CreateUpdater();
|
|
await u.UpdateDataAsync("t", HistoryUpdateMode.Insert, [V(T(1), 1), V(T(2), 2)], ConformanceData.Editor, Ct);
|
|
var r = await u.UpdateDataAsync("t", HistoryUpdateMode.Replace,
|
|
[V(T(2), 22, UncertainStatus), V(T(5), 5), V(T(1), 11)], ConformanceData.Editor, Ct);
|
|
r.ShouldBe([HistorianStatusCodes.GoodEntryReplaced, HistorianStatusCodes.BadNoEntryExists, HistorianStatusCodes.GoodEntryReplaced]);
|
|
var rows = await B.ReadBackAsync("t");
|
|
rows.Count.ShouldBe(2); // T(5) was not created
|
|
rows[0].V.Number.ShouldBe(11d);
|
|
rows[0].Status.ShouldBe(Good);
|
|
rows[1].V.Number.ShouldBe(22d);
|
|
rows[1].Status.ShouldBe(UncertainStatus); // replaced value visible with its new status
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Update_upserts_inserted_or_replaced_per_item()
|
|
{
|
|
var u = B.CreateUpdater();
|
|
await u.UpdateDataAsync("t", HistoryUpdateMode.Insert, [V(T(1), 1)], ConformanceData.Editor, Ct);
|
|
var r = await u.UpdateDataAsync("t", HistoryUpdateMode.Update, [V(T(2), 2), V(T(1), 10)], ConformanceData.Editor, Ct);
|
|
r.ShouldBe([HistorianStatusCodes.GoodEntryInserted, HistorianStatusCodes.GoodEntryReplaced]);
|
|
(await B.ReadBackAsync("t")).Select(x => (double?)x.V.Number).ShouldBe([10d, 2d]);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Unknown_tag_Insert_and_Update_create_it_but_Replace_and_deletes_do_not()
|
|
{
|
|
var u = B.CreateUpdater();
|
|
(await u.UpdateDataAsync("rep", HistoryUpdateMode.Replace, [V(T(1), 1)], ConformanceData.Editor, Ct))
|
|
.ShouldBe([HistorianStatusCodes.BadNoEntryExists]);
|
|
(await u.DeleteRawAsync("del", T(0), T(9), ConformanceData.Editor, Ct)).ShouldBe(HistorianStatusCodes.BadNoData);
|
|
(await u.DeleteAtTimeAsync("del2", [T(1)], ConformanceData.Editor, Ct)).ShouldBe([HistorianStatusCodes.BadNoEntryExists]);
|
|
foreach (var name in new[] { "rep", "del", "del2" })
|
|
(await B.TagExistsAsync(name)).ShouldBeFalse(name); // none of those created a tag
|
|
|
|
(await u.UpdateDataAsync("ins", HistoryUpdateMode.Insert, [V(T(1), 1)], ConformanceData.Editor, Ct)).ShouldBe([HistorianStatusCodes.GoodEntryInserted]);
|
|
(await u.UpdateDataAsync("upd", HistoryUpdateMode.Update, [V(T(1), 1)], ConformanceData.Editor, Ct)).ShouldBe([HistorianStatusCodes.GoodEntryInserted]);
|
|
(await B.TagExistsAsync("ins")).ShouldBeTrue();
|
|
(await B.TagExistsAsync("upd")).ShouldBeTrue();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Null_and_MinValue_timestamps_are_BadInvalidTimestamp_and_others_proceed()
|
|
{
|
|
var u = B.CreateUpdater();
|
|
var r = await u.UpdateDataAsync("t", HistoryUpdateMode.Insert,
|
|
[V(null, 1), V(T(1), 2), V(DateTime.MinValue, 3)], ConformanceData.Editor, Ct);
|
|
r.ShouldBe([HistorianStatusCodes.BadInvalidTimestamp, HistorianStatusCodes.GoodEntryInserted, HistorianStatusCodes.BadInvalidTimestamp]);
|
|
(await B.ReadBackAsync("t")).Count.ShouldBe(1);
|
|
|
|
// All-invalid request: no DB work, no tag created, still one code per item.
|
|
var u2 = B.CreateUpdater();
|
|
await u2.UpdateDataAsync("seed", HistoryUpdateMode.Insert, [V(T(1), 1)], ConformanceData.Editor, Ct); // bootstraps the schema
|
|
(await u2.UpdateDataAsync("x", HistoryUpdateMode.Insert, [V(null, 1)], ConformanceData.Editor, Ct)).ShouldBe([HistorianStatusCodes.BadInvalidTimestamp]);
|
|
(await B.TagExistsAsync("seed")).ShouldBeTrue();
|
|
(await B.TagExistsAsync("x")).ShouldBeFalse(); // "x" was never created
|
|
}
|
|
|
|
[Fact]
|
|
public async Task NaN_and_Infinity_are_rejected_with_BadInvalidArgument_and_others_proceed()
|
|
{
|
|
var u = B.CreateUpdater();
|
|
var r = await u.UpdateDataAsync("t", HistoryUpdateMode.Insert,
|
|
[V(T(1), double.NaN), V(T(2), 2), V(T(3), double.PositiveInfinity), V(T(4), double.NegativeInfinity)], ConformanceData.Editor, Ct);
|
|
r.ShouldBe([HistorianStatusCodes.BadInvalidArgument, HistorianStatusCodes.GoodEntryInserted,
|
|
HistorianStatusCodes.BadInvalidArgument, HistorianStatusCodes.BadInvalidArgument]);
|
|
(await B.ReadBackAsync("t")).Select(x => (double?)x.V.Number).ShouldBe([2d]);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Duplicate_timestamp_in_one_Insert_request_second_is_BadEntryExists_first_wins()
|
|
{
|
|
var u = B.CreateUpdater();
|
|
var r = await u.UpdateDataAsync("t", HistoryUpdateMode.Insert, [V(T(1), 1), V(T(2), 2), V(T(1), 99)], ConformanceData.Editor, Ct);
|
|
r.ShouldBe([HistorianStatusCodes.GoodEntryInserted, HistorianStatusCodes.GoodEntryInserted, HistorianStatusCodes.BadEntryExists]);
|
|
(await B.ReadBackAsync("t")).Select(x => (double?)x.V.Number).ShouldBe([1d, 2d]);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Duplicate_timestamp_in_one_Replace_or_Update_request_last_wins_and_all_share_the_code()
|
|
{
|
|
var u = B.CreateUpdater();
|
|
await u.UpdateDataAsync("t", HistoryUpdateMode.Insert, [V(T(1), 1)], ConformanceData.Editor, Ct);
|
|
var rep = await u.UpdateDataAsync("t", HistoryUpdateMode.Replace, [V(T(1), 5), V(T(1), 6)], ConformanceData.Editor, Ct);
|
|
rep.ShouldBe([HistorianStatusCodes.GoodEntryReplaced, HistorianStatusCodes.GoodEntryReplaced]);
|
|
(await B.ReadBackAsync("t")).Single().V.Number.ShouldBe(6d);
|
|
|
|
var upd = await u.UpdateDataAsync("t", HistoryUpdateMode.Update, [V(T(7), 7), V(T(7), 8)], ConformanceData.Editor, Ct);
|
|
upd.ShouldBe([HistorianStatusCodes.GoodEntryInserted, HistorianStatusCodes.GoodEntryInserted]);
|
|
(await B.ReadBackAsync("t")).Last().V.Number.ShouldBe(8d);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Replace_and_Update_can_change_the_value_kind_and_clear_stale_columns()
|
|
{
|
|
var u = B.CreateUpdater();
|
|
await u.UpdateDataAsync("t", HistoryUpdateMode.Insert, [new HistorizationValue(T(1), HistorianValue.FromDouble(1.5), Good)], ConformanceData.Editor, Ct);
|
|
(await u.UpdateDataAsync("t", HistoryUpdateMode.Replace,
|
|
[new HistorizationValue(T(1), HistorianValue.FromString("hi"), Good)], ConformanceData.Editor, Ct)).ShouldBe([HistorianStatusCodes.GoodEntryReplaced]);
|
|
var rows = await B.ReadBackAsync("t");
|
|
rows.Single().V.ShouldBe(HistorianValue.FromString("hi")); // Number cleared (0), not left at 1.5
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DeleteRaw_is_half_open_Good_when_rows_removed_BadNoData_when_none()
|
|
{
|
|
var u = B.CreateUpdater();
|
|
await u.UpdateDataAsync("t", HistoryUpdateMode.Insert, [V(T(1), 1), V(T(2), 2), V(T(3), 3)], ConformanceData.Editor, Ct);
|
|
// [T(1), T(3)) removes T(1) and T(2); T(3) (the end) survives.
|
|
(await u.DeleteRawAsync("t", T(1), T(3), ConformanceData.Editor, Ct)).ShouldBe(Good);
|
|
(await B.ReadBackAsync("t")).Select(x => x.T).ShouldBe([B.Round(T(3))]);
|
|
(await u.DeleteRawAsync("t", T(1), T(3), ConformanceData.Editor, Ct)).ShouldBe(HistorianStatusCodes.BadNoData);
|
|
// sub-ms boundary: a start one resolution unit after T(3) excludes it.
|
|
(await u.DeleteRawAsync("t", T(3).AddTicks(B.ResolutionTicks), T(3).AddTicks(100 * B.ResolutionTicks), ConformanceData.Editor, Ct)).ShouldBe(HistorianStatusCodes.BadNoData);
|
|
(await B.ReadBackAsync("t")).Count.ShouldBe(1);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DeleteAtTime_reports_per_time_in_order_with_exact_tick_matching()
|
|
{
|
|
var u = B.CreateUpdater();
|
|
await u.UpdateDataAsync("t", HistoryUpdateMode.Insert, [V(T(1), 1), V(T(2), 2), V(T(3), 3)], ConformanceData.Editor, Ct);
|
|
var r = await u.DeleteAtTimeAsync("t", [T(3), T(2).AddTicks(B.ResolutionTicks) /* near miss */, T(1), DateTime.MinValue], ConformanceData.Editor, Ct);
|
|
r.ShouldBe([Good, HistorianStatusCodes.BadNoEntryExists, Good, HistorianStatusCodes.BadInvalidTimestamp]);
|
|
(await B.ReadBackAsync("t")).Select(x => x.T).ShouldBe([B.Round(T(2))]);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DeleteAtTime_duplicate_time_first_reports_Good_later_BadNoEntryExists()
|
|
{
|
|
var u = B.CreateUpdater();
|
|
await u.UpdateDataAsync("t", HistoryUpdateMode.Insert, [V(T(1), 1)], ConformanceData.Editor, Ct);
|
|
(await u.DeleteAtTimeAsync("t", [T(1), T(1)], ConformanceData.Editor, Ct)).ShouldBe([Good, HistorianStatusCodes.BadNoEntryExists]);
|
|
(await B.ReadBackAsync("t")).ShouldBeEmpty();
|
|
}
|
|
}
|