diff --git a/CHANGELOG.md b/CHANGELOG.md index 149de64a..484e5789 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ### Added -- **A seam for value serializers.** `ICacheValueSerializer` and `CacheValueFormats` define a buffer-based serializer whose output starts with a one-byte format id, `AddValueSerializer` registers one by name, and `CachePolicy.Serializer` (`Write`, `Read`, `MigrateOnRead`) names it per policy. `AddValueSerializer` throws at the call for a name used twice on one builder or for `json`. A policy that names an unregistered or blank serializer, has an empty `Read`, or writes a format its `Read` leaves out fails with an `OptionsValidationException`, as does a serializer whose ids are outside the consumer ranges; these are checked when the policy factory is first resolved, on first cache use rather than at host start. A custom `ICachePolicyFactory` whose policy names an unregistered serializer fails the same way when the Redis provider builds its cache. Serializers the container created are disposed with it, through `DisposeAsync` when they only implement `IAsyncDisposable`, so a factory must return a new instance rather than one resolved from DI. The default stays JSON with no header, and `AddDistributedCache` ignores `Serializer`: its own policy always writes and reads JSON with no migration, so the caller's bytes never get a format header. +- **A seam for value serializers.** `ICacheValueSerializer` and `CacheValueFormats` define a buffer-based serializer whose output starts with a one-byte format id, `AddValueSerializer` registers one by name, and `CachePolicy.Serializer` (`Write`, `Read`, `MigrateOnRead`) names it per policy. `AddValueSerializer` throws at the call for a name used twice on one builder or for `json`. A policy that names an unregistered or blank serializer, has an empty `Read`, or writes a format its `Read` leaves out fails with an `OptionsValidationException`, as does a serializer whose ids are outside the consumer ranges; these are checked when the policy factory is first resolved, on first cache use rather than at host start. A custom `ICachePolicyFactory` whose policy names an unregistered serializer fails the same way when the Redis provider builds its cache. Serializers the container created are disposed with it, through `DisposeAsync` when they only implement `IAsyncDisposable`, so a factory must return a new instance rather than one resolved from DI. A policy that sets `Write` makes `RedisCache` and `RedisHashCache` write their values, hash fields and hash metadata with that serializer, and every read picks the serializer by the format id, whatever the policy writes. A value in a format no registered serializer owns is a miss, counted in `cache.serializer.unreadable` and removed only if the key still holds the bytes that were read, so a newer value written in between survives; a hash with such a field misses whole and is left in place. A value whose registered serializer throws is a logged miss and the key stays, like one that is not headered and fails to parse as JSON, because the bytes may be good for another version of the type during a rolling deploy; this departs from the design spec, which removed it, and an `OutOfMemoryException` from its payload is never swallowed, while one from a value with no header (JSON) is a logged miss as it is with no serializer registered. `RedisHashCache.TryDecodeFields` now treats any exception except `OutOfMemoryException` as a refresh, since a registered serializer can throw any type, where it caught four. A node with no serializer registered logs the `JsonException` and keeps the key. A policy passed to a single call that names an unregistered serializer throws `OptionsValidationException` before anything is written. The reader only looks for a header with the default JSON serializer or under a policy whose effective `Write` is a serializer other than `json` (`Read` does not count), so `byte[]` values stored through `RawByteSerializerProxy` or `AddDistributedCache` are never reinterpreted. A policy that sets no serializer, or only `Read`, takes the default policy's `Write`. The default stays JSON with no header, and `AddDistributedCache` ignores `Serializer`: its own policy always writes and reads JSON with no migration, so the caller's bytes never get a format header. A hash write that names a serializer leaves `_metadata_` out when the metadata is empty and nulls are not cached, and a `HashReplace` one deletes the field an earlier write left. Hash system field names are compared as bytes instead of strings. See [value serializers](docs/how-to/extending.md#value-serializers) and [rolling a serializer out](docs/concepts.md#rolling-a-serializer-out). ### Changed diff --git a/benchmarks/UiPath.Caching.Benchmarks/SerializerBenchmark.cs b/benchmarks/UiPath.Caching.Benchmarks/SerializerBenchmark.cs index e7badae2..2e336b1e 100644 --- a/benchmarks/UiPath.Caching.Benchmarks/SerializerBenchmark.cs +++ b/benchmarks/UiPath.Caching.Benchmarks/SerializerBenchmark.cs @@ -2,7 +2,13 @@ using System.Text.Json; using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Configs; +using Microsoft.Extensions.Logging.Abstractions; using StackExchange.Redis; +using UiPath.Caching.Config; +using UiPath.Caching.Policies; +using UiPath.Caching.Redis; +using UiPath.Caching.Serialization; +using UiPath.Caching.Telemetry; namespace UiPath.Caching.Benchmarks; @@ -11,12 +17,23 @@ namespace UiPath.Caching.Benchmarks; [CategoriesColumn] public class SerializerBenchmark { + private static readonly TimeSpan Expiration = TimeSpan.FromMinutes(30); + + private readonly Func> _neverRuns = _ => throw new InvalidOperationException("A hit ran its generator"); private CustomObject _obj = default!; private RedisValue _payload; + private FakeRedisConnector _redis = default!; + private RedisCache _cache = default!; + private CachePolicy? _policy; + [Params("Small", "Medium", "Large")] public string Size { get; set; } = "Medium"; + /// Whether a consumer serializer is registered, with a policy that names json so that every read looks for a format header; off is the app that never registered one. + [Params(false, true)] + public bool Registered { get; set; } + [GlobalSetup] public void Setup() { @@ -27,8 +44,33 @@ public void Setup() _ => CustomObject.RandomMedium(), }; _payload = JsonSerializer.SerializeToUtf8Bytes(_obj); + + var registry = Registered ? new ValueSerializerRegistry([("bench", new UnusedSerializer(), false)]) : ValueSerializerRegistry.Empty; + _policy = Registered ? new CachePolicy { Serializer = new SerializerPolicy { Write = "json" } } : null; + _redis = new FakeRedisConnector(); + _cache = new RedisCache( + _redis, + new SystemJsonByteSerializerProxy(), + EmptyResiliencePipelineProvider.Instance, + NullTelemetryProvider.Instance, + new RedisCacheOptions(), + new CacheOptions { AppShortName = "bench" }, + new DefaultCachePolicyFactory(null, null, valueSerializers: registry), + TimeProvider.System, + NullLogger.Instance) + { + ValueSerializers = registry, + }; + _cache.SetAsync("bench:serializer", _obj, Expiration, _policy).AsTask().GetAwaiter().GetResult(); + if (_cache.GetAsync("bench:serializer", _policy).AsTask().GetAwaiter().GetResult() is null) + { + throw new InvalidOperationException("The fake connector did not serve the read"); + } } + [GlobalCleanup] + public void Cleanup() => _redis.Dispose(); + [BenchmarkCategory("Serialize"), Benchmark(Baseline = true)] public RedisValue Serialize_String() => JsonSerializer.Serialize(_obj); @@ -48,4 +90,22 @@ public void Setup() var reader = new Utf8JsonReader(payload); return JsonSerializer.Deserialize(ref reader); } + + [BenchmarkCategory("RedisCacheGet"), Benchmark] + public ValueTask RedisCache_Get() => _cache.GetAsync("bench:serializer", _policy); + + [BenchmarkCategory("RedisCacheGetOrAdd"), Benchmark] + public ValueTask RedisCache_GetOrAdd_Hit() => _cache.GetOrAddAsync("bench:serializer", _neverRuns, Expiration, _policy); + + [BenchmarkCategory("RedisCacheSet"), Benchmark] + public ValueTask RedisCache_Set() => _cache.SetAsync("bench:serializer", _obj, Expiration, _policy); + + private sealed class UnusedSerializer : ICacheValueSerializer + { + public IReadOnlyList FormatIds { get; } = [0x10]; + + public int Serialize(T value, IBufferWriter target) => throw new NotSupportedException(); + + public T? Deserialize(int formatId, ReadOnlySequence source) => throw new NotSupportedException(); + } } diff --git a/benchmarks/UiPath.Caching.Benchmarks/ValueReadBenchmark.cs b/benchmarks/UiPath.Caching.Benchmarks/ValueReadBenchmark.cs new file mode 100644 index 00000000..8143c2ae --- /dev/null +++ b/benchmarks/UiPath.Caching.Benchmarks/ValueReadBenchmark.cs @@ -0,0 +1,87 @@ +using System.Buffers; +using System.Text.Json; +using BenchmarkDotNet.Attributes; +using StackExchange.Redis; +using UiPath.Caching.Serialization; + +namespace UiPath.Caching.Benchmarks; + +/// A single-key read of a value that carries a format header: the bytes StackExchange.Redis allocates, or a leased buffer it hands back. +[MemoryDiagnoser] +[MarkdownExporterAttribute.GitHub] +public class ValueReadBenchmark +{ + private const int FormatId = 0x10; + + private readonly string _run = Guid.NewGuid().ToString("N"); + private readonly ValueSerializerRegistry _registry = new([("bench", new JsonBody(), false)]); + + private ConnectionMultiplexer _redis = default!; + private IDatabase _database = default!; + private ValueReader _reader = default!; + private RedisKey _key; + + [Params("Small", "Medium", "Large")] + public string Size { get; set; } = "Medium"; + + [GlobalSetup] + public async Task Setup() + { + _redis = await ConnectionMultiplexer.ConnectAsync("127.0.0.1:6379,abortConnect=false"); + _database = _redis.GetDatabase(); + _reader = new ValueReader(new SystemJsonByteSerializerProxy(), _registry); + _key = $"bench:valueread:{_run}:{Size}"; + var value = Size switch + { + "Small" => CustomObject.RandomSmall(), + "Large" => CustomObject.RandomLarge(), + _ => CustomObject.RandomMedium(), + }; + var body = JsonSerializer.SerializeToUtf8Bytes(value); + var stored = new byte[body.Length + 1]; + stored[0] = FormatId; + body.CopyTo(stored, 1); + await _database.StringSetAsync(_key, stored, TimeSpan.FromMinutes(30)); + } + + [GlobalCleanup] + public async Task Cleanup() + { + await _database.KeyDeleteAsync(_key); + await _redis.CloseAsync(); + _redis.Dispose(); + } + + [Benchmark(Baseline = true)] + public async Task Bytes() + { + var value = await _database.StringGetAsync(_key); + return _reader.Read(((byte[]?)value)!).Value; + } + + [Benchmark] + public async Task Lease() + { + using var lease = await _database.StringGetLeaseAsync(_key); + var memory = lease!.Memory; + if (!ValueHeader.TryRead(memory.Span, out var formatId, out var headerLength) || !_registry.TryGetByFormat(formatId, out var serializer)) + { + throw new InvalidOperationException("unexpected value"); + } + + return serializer.Deserialize(formatId, new ReadOnlySequence(memory[headerLength..])); + } + + private sealed class JsonBody : ICacheValueSerializer + { + public IReadOnlyList FormatIds { get; } = [FormatId]; + + public int Serialize(T value, IBufferWriter target) => throw new NotSupportedException(); + + public T? Deserialize(int formatId, ReadOnlySequence source) + { + var reader = new Utf8JsonReader(source); + return JsonSerializer.Deserialize(ref reader); + } + } +} diff --git a/docs/concepts.md b/docs/concepts.md index 65d59c3e..903e5efd 100644 --- a/docs/concepts.md +++ b/docs/concepts.md @@ -490,6 +490,58 @@ See also: [how-to/resilience.md#named-policies](how-to/resilience.md#named-polic --- +## Value serializers and the format header + +Values are JSON unless a policy says otherwise; a call with no policy takes the default policy's choice. A +policy that sets `Serializer.Write` to a registered serializer writes its values in that serializer's format, behind a one-byte *format id*; JSON has no header, +and no JSON text starts with a byte a header can be. The reader looks at the first byte: a format id picks +the serializer registered for it, anything else is JSON. It does this whatever the reading policy writes, so +a key written as JSON, then MessagePack, then JSON again reads correctly each time. Hash fields and a hash's +`_metadata_` field follow the same rule; `_version_` stays a plain number and is never inspected. + +The reader looks only where it can tell a header from caller bytes, and only when a serializer is registered. +It does so when the cache's serializer is the default `SystemJsonByteSerializerProxy` itself, or when the +call's effective policy writes a serializer other than JSON: its own `Serializer.Write`, else the default +policy's, compared without regard to case. `Write = "json"` does not open it, and neither does what +`Serializer.Read` lists. A subclass, a custom `ISerializerProxy` and `RawByteSerializerProxy` (which +`AddDistributedCache` uses) keep their bytes as they are otherwise, so a stored `byte[]` that starts with +`0x02` reads back unchanged, also under a policy that writes JSON or lists other formats to read. + +A hash write that names a serializer leaves out `_metadata_` when the metadata is empty and nulls are not +cached; the entry then reads with no metadata rather than an empty dictionary, and a `HashReplace` write also +deletes the field an earlier write left, in the same transaction as the fields (a replacement too large for one command is +written in chunks and deletes it right after). The JSON default keeps writing it. Events stay JSON for every policy, and a peer that applies another node's field update decodes the field +bytes by their header like any other read, with the gate its own policy for that key sets. Where serializers are registered and +the key's policy is not known to the peer, it does not apply the update and reads the hash instead. + +### Rolling a serializer out + +Reading comes first, writing second. Every node from 3.2.0 reads every registered format; nothing writes a +new one until a policy sets `Serializer`, so set it only after every node runs 3.2.0 or later with the +serializer registered. + +A node that cannot read a format treats the value as a miss, and what happens to the key depends on why: + +- A node before 3.2.0, or a 3.2.0 node with no serializer registered, hits a `JsonException` (logged at + Warning) and keeps the key; the generator rewrites it as JSON. In a batch read one `JsonException` misses + every key of the batch, and a hash misses whole, its `_metadata_` included. +- A 3.2.0 node with a serializer registered, reading an id none of its serializers owns, removes the key and + counts it in `cache.serializer.unreadable`, but only if the key still holds the bytes it read. A newer value + another node wrote in between, or a lagging replica's stale read, survives the removal. A hash with such a + field misses whole and is left in place, still counted: removing it could drop what another node just + wrote, and removing the one field would leave the rest looking like a complete hash. +- A registered serializer that throws on a value it owns is a logged miss, and the key stays, as for JSON that + does not parse. The bytes may be good for another version of the type that other nodes still run during a + rolling deploy, and a serializer disposed at shutdown says nothing about the data. This departs from the + design spec, which removed such a value. An `OutOfMemoryException` from a registered serializer's payload is never treated as a failed read: it propagates from `Get`, `GetOrAdd` and the hash reads instead of becoming a miss that runs the generator. A value with no header (JSON) behaves as it does with no serializer registered, so an `OutOfMemoryException` there is a logged miss. + +All of it shows up as factory load and churn during the deploy, never as a wrong value. Switching a policy back +to JSON is safe at any time: new writes are JSON and the older values still read. + +See also: [how-to/extending.md#value-serializers](how-to/extending.md#value-serializers). + +--- + ## Telemetry `ICachingTelemetryProvider` is the instrumentation seam. The library never takes diff --git a/docs/how-to/extending.md b/docs/how-to/extending.md index e5b5f002..c985dd35 100644 --- a/docs/how-to/extending.md +++ b/docs/how-to/extending.md @@ -228,7 +228,8 @@ JSON `null` literal, following the contract note below. Both read back as `defau `RawByteSerializerProxy` exists because an `IDistributedCache` payload is caller bytes that the caller has already serialized: base64-ing them would cost a third again in size for no benefit, and a consumer who swaps in MessagePack should not have their session -bytes re-encoded. Neither implementation sniffs the payload — the requested type argument decides. +bytes re-encoded. Neither implementation inspects the payload — the requested type argument decides. A +format that is not JSON belongs in a [value serializer](#value-serializers), which tags what it writes. You can register `RawByteSerializerProxy` app-wide to get raw byte storage everywhere, but that *is* a wire-format change: it returns stored bytes as-is rather than base64-decoding them, so a 1.x @@ -433,6 +434,90 @@ o.RedisKeyStrategyFactory = new MyRedisKeyStrategyFactory(); // gets "dh" (or Left null it inherits the application's own `RedisCacheOptions.RedisKeyStrategyFactory`, so `AppShortName`, the separator and sharding behave as they do everywhere else. +### Value serializers + +`ICacheValueSerializer` is the seam for a format that is not JSON: MessagePack, MemoryPack, a compressing +wrapper. It writes into the buffer the library hands it, and the library puts a one-byte format id in front +of what it wrote. The reader picks the serializer by that id, not by the policy's current choice, so a +policy can switch formats at any time and the values already stored still read. + +```csharp +using System.Buffers; +using MessagePack; +using UiPath.Caching; + +public sealed class MessagePackValueSerializer(MessagePackSerializerOptions? options = null) : ICacheValueSerializer +{ + public IReadOnlyList FormatIds { get; } = [0x10]; + + public int Serialize(T value, IBufferWriter target) + { + MessagePackSerializer.Serialize(target, value, options); + return 0x10; + } + + public T? Deserialize(int formatId, ReadOnlySequence source) => + MessagePackSerializer.Deserialize(source, options); +} +``` + +Register it by name and choose it per policy: + +```csharp +services.AddCaching( + b => + { + b.AddRedis(_ => { }); + b.AddValueSerializer("msgpack", _ => new MessagePackValueSerializer()); + }, + o => o.Policies["Sessions"] = new CachePolicy { Serializer = new SerializerPolicy { Write = "msgpack" } }); +``` + +A policy without `Serializer` keeps writing JSON, exactly as before. `Write` accepts a registered name or +`json`, and a name nobody registered fails when the policies are built; a policy passed to a single call +that names an unregistered serializer throws `OptionsValidationException` on that call, before anything is +written. + +Rules a serializer follows: + +- **Format ids.** A serializer lists the ids it writes in `FormatIds`, all with the same header length. + Consumer serializers take `0x10`-`0x1E` (one byte) or `0x100`-`0x1FF` (the marker byte `0x1F`, then the low + byte). `0x01`-`0x0F`, except `0x09`, `0x0A` and `0x0D`, belong to the library. Every other first byte, + including `0x00`, JSON whitespace (`0x09`, `0x0A`, `0x0D`) and `0x20` onward, is JSON, which has no header. An + id is never reused: a leftover value written under a retired id would be misread by whatever took it over. +- **`Serialize` returns the id it wrote**, one of `FormatIds`; a serializer that picks between a plain and a + compressed id says which after it has written. +- **Never write zero bytes.** An empty payload means a cached null. +- **Never retain `source`.** `Deserialize` receives a sequence over a buffer the library owns and reclaims + when the call returns, so copy what you keep. +- **Handle null.** A single value that is null never reaches `Serialize`, but a hash field can be null while + nulls are not cached; write something `Deserialize` maps back to `default` (MessagePack's nil does). + +What a node does with a value it cannot read depends on why: + +- **An id no registered serializer owns** is data no node with this registry can read. It is a miss, the key + is removed (best effort) only if it still holds the bytes that were read, `cache.serializer.unreadable` + counts it and the generator runs. A newer value written in between survives. A node with no serializer + registered does not look for ids: it logs a `JsonException` miss and keeps the key. +- **A registered serializer that throws** is a logged miss and the key stays, as for JSON that does not parse: + the bytes may be good for another version of the type that other nodes run during a rolling deploy. + An `OutOfMemoryException` from a registered serializer's payload is never treated as a failed read; one from a value with no header (JSON) is a logged miss, as with no serializer registered. +- **A hash with such a field** misses as a whole and is left in place; an unknown id is still counted. + +Update the nodes that read before any policy writes a new format: a node that cannot read one keeps missing +and regenerating until it is updated. + +Under a policy that writes a serializer, reads of a custom app-wide `ISerializerProxy` look for a +format header, so the shared keys of such a policy must not also be written by that proxy: a byte it writes +first that happens to be a format id would be handed to the wrong reader. The gate is this. With the default +JSON proxy, every read looks for a header once a serializer is registered. With a custom proxy, a subclass or +`RawByteSerializerProxy`, a read looks only when the call's effective policy (its own `Serializer.Write`, else +the default policy's) writes a serializer other than `json`, compared without regard to case. `Write = "json"` +and `Serializer.Read` do not open it, so such a proxy's values, and what `AddDistributedCache` stores, are never +inspected under them. + +A policy that sets no `Serializer` takes the default policy's `Write`, and one that sets only `Read` does too. + ## Swapping the default factories `ICacheFactory` and `ICachePolicyFactory` each have a default DI registration set up by `AddCaching` — the concrete `CacheFactory` and `DefaultCachePolicyFactory` respectively. When you need to substitute either, use the fluent `Use*Factory` extensions on `ICachingBuilder`. They internally call `Services.Replace(...)` so the swap survives the rest of the `AddCaching` pipeline (which uses `TryAddSingleton` and would otherwise lose to the default registration). diff --git a/docs/reference/glossary.md b/docs/reference/glossary.md index 47d07ef2..174aa06f 100644 --- a/docs/reference/glossary.md +++ b/docs/reference/glossary.md @@ -26,6 +26,8 @@ Alphabetical lookup for terms used across the docs. **Event** — A message published on a topic, such as a set, a removal or a refresh of a key. Its effect on a receiving node is an [invalidation](#invalidation). +**Format id** — The byte in front of a value that a [value serializer](../how-to/extending.md#value-serializers) wrote, naming the format so any node can pick the right reader. JSON has none. Consumer ids are `0x10`-`0x1E` and `0x100`-`0x1FF`; the library keeps the rest. See [concepts.md](../concepts.md#value-serializers-and-the-format-header). + **Generated expiration** — A lifetime the generator picks for the value it returns, instead of the policy. `GetOrAddWithExpirationAsync` takes a `GeneratedValue`; a null expiration leaves it to the policy. **Generator** — The caller's function that makes a value on a miss. The `FactoryTimeout` option and the `cache.factory.timed_out` event say "factory"; the docs say generator. diff --git a/docs/reference/settings.md b/docs/reference/settings.md index 3976e452..80d02732 100644 --- a/docs/reference/settings.md +++ b/docs/reference/settings.md @@ -480,7 +480,7 @@ Nested under a `CachePolicy` entry. Field-level merged against the default polic | Property | Type | Default | Scope | Notes | |---|---|---|---|---| | `Write` | `string?` | `null` | Per-policy | A registered serializer name, or `json`; blank is rejected; `null` = inherit, and under that `json`. | -| `Read` | `string[]?` | `null` | Per-policy | The formats the policy reads, each a registered name or `json`; never empty, and including `Write`; `null` = inherit, and under that every registered format. Arrays from layered configuration merge by index, see below. | -| `MigrateOnRead` | `bool?` | `null` | Per-policy | Rewrite a value read in another format into the write format; `null` = inherit, and under that off. | +| `Read` | `string[]?` | `null` | Per-policy | The formats the policy reads, each a registered name or `json`; never empty, and including `Write`; `null` = inherit, and under that every registered format. Checked when the policies are built; it does not yet limit what is read, which is every registered format whatever it says. Arrays from layered configuration merge by index, see below. | +| `MigrateOnRead` | `bool?` | `null` | Per-policy | Rewrite a value read in another format into the write format; `null` = inherit, and under that off. Accepted and merged, but nothing reads it yet: a value is never rewritten in this release. | **`Read` and layered configuration.** A `Read` set in code replaces the default policy's list as a whole, but .NET configuration providers merge arrays by index. With a base layer of `"Read": ["a", "b"]` and an override layer of `"Read": ["json"]`, the bound list is `["json", "b"]`, not `["json"]`. An override of `"Read": []` binds as `null`, so it inherits and the policy reads every registered format. To override safely, set every index the base layer sets (or repeat the full list), or declare `Read` in one layer only. diff --git a/src/UiPath.Caching/KnownFieldNames.cs b/src/UiPath.Caching/KnownFieldNames.cs index 30953e9a..9b08a48b 100644 --- a/src/UiPath.Caching/KnownFieldNames.cs +++ b/src/UiPath.Caching/KnownFieldNames.cs @@ -33,4 +33,35 @@ field is not null /// True for the actual system field names (kept in sync with the per-field read validation). public static bool IsSystemField(string field) => field == MetadataKey || field == VersionKey; + + public static bool IsSystemField(ReadOnlySpan field) => + IsMetadata(field) || IsVersion(field); + + /// Compares the name's bytes in place, whatever the storage of the value Redis handed back, without making a string of it. + public static bool IsSystemField(RedisValue name) => + IsMetadata(name) || IsVersion(name); + + public static bool IsMetadata(ReadOnlySpan field) => + field.SequenceEqual("_metadata_"u8); + + public static bool IsMetadata(RedisValue name) => + SameBytes(name, "_metadata_"u8); + + public static bool IsVersion(ReadOnlySpan field) => + field.SequenceEqual("_version_"u8); + + public static bool IsVersion(RedisValue name) => + SameBytes(name, "_version_"u8); + + private static bool SameBytes(RedisValue name, ReadOnlySpan expected) + { + if (name.GetByteCount() != expected.Length) + { + return false; + } + + Span bytes = stackalloc byte[expected.Length]; + name.CopyTo(bytes); + return bytes.SequenceEqual(expected); + } } diff --git a/src/UiPath.Caching/MultilayerHashCache.cs b/src/UiPath.Caching/MultilayerHashCache.cs index a477f224..e744ef7d 100644 --- a/src/UiPath.Caching/MultilayerHashCache.cs +++ b/src/UiPath.Caching/MultilayerHashCache.cs @@ -683,7 +683,7 @@ internal bool ApplyPeerUpdate(RefreshMetadataState state, ICacheEntry(update.Fields, out var written)) + if (!versioned.TryDecodeFields(update.Fields, state.Refresh?.Policy, out var written)) { return false; } diff --git a/src/UiPath.Caching/Redis/IVersionedHashCache.cs b/src/UiPath.Caching/Redis/IVersionedHashCache.cs index 6132122d..ffca37fa 100644 --- a/src/UiPath.Caching/Redis/IVersionedHashCache.cs +++ b/src/UiPath.Caching/Redis/IVersionedHashCache.cs @@ -7,5 +7,5 @@ internal interface IVersionedHashCache ValueTask RemoveFieldsVersionedAsync(CacheKey cacheKey, string[] fields, CancellationToken token = default); - bool TryDecodeFields(IReadOnlyDictionary encoded, [MaybeNullWhen(false)] out Dictionary values); + bool TryDecodeFields(IReadOnlyDictionary encoded, CachePolicy? policy, [MaybeNullWhen(false)] out Dictionary values); } diff --git a/src/UiPath.Caching/Redis/RedisCache.Formats.cs b/src/UiPath.Caching/Redis/RedisCache.Formats.cs new file mode 100644 index 00000000..b573263d --- /dev/null +++ b/src/UiPath.Caching/Redis/RedisCache.Formats.cs @@ -0,0 +1,74 @@ +using UiPath.Caching.Policies; +using UiPath.Caching.Serialization; + +namespace UiPath.Caching.Redis; + +internal sealed partial class RedisCache +{ + private ValueFormat FormatFor(CachePolicy? policy, bool forWrite) => _formats.For(policy, forWrite); + + /// A read found a format no registered serializer owns: counted, and the key dropped if it still holds the bytes that were read, so the caller regenerates it. Best effort, like any remove. + private ValueTask RemoveUnreadableAsync(RedisKey redisKey, RedisValue read, int formatId, CancellationToken token) + { + TrackUnreadable(formatId, Name); + return RemoveIfUnchangedAsync(redisKey, read, token); + } + + /// Never throws what handles, so a late cancellation leaves the batch's misses as they are. + private async ValueTask RemoveUnreadableAsync(List<(RedisKey Key, RedisValue Read, int FormatId)> unreadable, TFormat format, CancellationToken token) + where TFormat : struct, IValueFormat + { + foreach (var (_, _, formatId) in unreadable) + { + TrackUnreadable(formatId, Name); + } + + try + { + await Task.WhenAll(unreadable.Select(u => RemoveIfUnchangedAsync(u.Key, u.Read, token).AsTask())).ConfigureAwait(false); + } + catch (Exception ex) when (format.Handles(ex)) + { + LogRedisCacheException(ex); + } + } + + // A transaction per key, so a key that changed fails alone and stays on its own slot; the condition runs on the master, so a lagging replica's read cannot delete a newer value. + private async ValueTask RemoveIfUnchangedAsync(RedisKey redisKey, RedisValue read, CancellationToken token) + { + bool ret = default; + token.ThrowIfCancellationRequested(); + NoteWritten(redisKey); + var operation = StartOperation(nameof(RemoveAsync)); + try + { + ret = await _write.ExecuteAsync(static (s, token) => + { + token.ThrowIfCancellationRequested(); + var transaction = s.Self.Database.CreateTransaction(asyncState: null); + // The server-side compare-and-delete needs Redis 8.4; this library supports 6. +#pragma warning disable SER301 + transaction.AddCondition(Condition.StringEqual(s.Key, s.Read)); + transaction.KeyDeleteAsync(s.Key, CommandFlags.DemandMaster).Forget(); +#pragma warning restore SER301 + return transaction.ExecuteAsync(CommandFlags.DemandMaster).AsValueTask(); + }, + (Self: this, Key: redisKey, Read: read), + default, + token).ConfigureAwait(false); + operation.Stop(); + } + catch (Exception ex) + { + operation.Stop(); + LogRedisCacheException(ex); + } + finally + { + NoteWritten(redisKey); + operation.Track(ret); + } + + return ret; + } +} diff --git a/src/UiPath.Caching/Redis/RedisCache.Result.cs b/src/UiPath.Caching/Redis/RedisCache.Result.cs index c5afeb38..dea285eb 100644 --- a/src/UiPath.Caching/Redis/RedisCache.Result.cs +++ b/src/UiPath.Caching/Redis/RedisCache.Result.cs @@ -1,3 +1,5 @@ +using UiPath.Caching.Serialization; + namespace UiPath.Caching.Redis; internal sealed partial class RedisCache : IResultCache @@ -61,11 +63,20 @@ private async ValueTask> AttributeAsync(ValueTask shared, private ValueTask> GetOrAddResultCoreAsync(CacheKey cacheKey, GeneratorRecorder recorder, TimeSpan duration, CachePolicy? policy, CancellationToken token) { NotCacheableException.ThrowIfNotCacheable(); + var format = FormatFor(policy, forWrite: true); + return format.IsDefault + ? GetOrAddResultCoreAsync(cacheKey, recorder, duration, policy, default(PlainFormat), token) + : GetOrAddResultCoreAsync(cacheKey, recorder, duration, policy, format, token); + } + + private ValueTask> GetOrAddResultCoreAsync(CacheKey cacheKey, GeneratorRecorder recorder, TimeSpan duration, CachePolicy? policy, TFormat format, CancellationToken token) + where TFormat : struct, IValueFormat + { var redisKey = ToRedisKey(cacheKey, token); - var shared = ShareGenerationAsync Recorder, TimeSpan? FactoryTimeout, TimeSpan Duration)>( + var shared = ShareGenerationAsync Recorder, TimeSpan? FactoryTimeout, TimeSpan Duration, TFormat Format)>( redisKey, - (this, cacheKey, redisKey, recorder, (policy ?? DefaultPolicy)?.FactoryTimeout, duration), - static (args, run) => args.Cache.ReadOrGenerateResultAsync(args.Key, args.RedisKey, args.Recorder, args.FactoryTimeout, args.Duration, run), + (this, cacheKey, redisKey, recorder, (policy ?? DefaultPolicy)?.FactoryTimeout, duration, format), + static (args, run) => args.Cache.ReadOrGenerateResultAsync(args.Key, args.RedisKey, args.Recorder, args.FactoryTimeout, args.Duration, args.Format, run), token, out var flight); return AttributeAsync(shared, flight, recorder, cacheKey, token); @@ -79,7 +90,8 @@ private ValueTask> GetOrAddResultCoreAsync(CacheKey cacheKey, } /// The work of the result run, which ends the run itself (see ) once the expiration to report is recorded on it. - private async ValueTask ReadOrGenerateResultAsync(CacheKey cacheKey, RedisKey redisKey, GeneratorRecorder recorder, TimeSpan? factoryTimeout, TimeSpan duration, IInFlightEnd run) + private async ValueTask ReadOrGenerateResultAsync(CacheKey cacheKey, RedisKey redisKey, GeneratorRecorder recorder, TimeSpan? factoryTimeout, TimeSpan duration, TFormat format, IInFlightEnd run) + where TFormat : struct, IValueFormat { T? ret; try @@ -87,7 +99,7 @@ private ValueTask> GetOrAddResultCoreAsync(CacheKey cacheKey, var token = run.Token; // One read of value and expiration together, shared by every caller of the key. - var entry = await GetCacheEntryInternalAsync(cacheKey, token, nameof(GetOrAddResultAsync)).ConfigureAwait(false); + var entry = await GetCacheEntryInternalAsync(cacheKey, format, token, nameof(GetOrAddResultAsync)).ConfigureAwait(false); if (entry.Found) { run.Expiration = entry.Expiration == DateTimeOffset.MaxValue ? null : entry.Expiration; @@ -98,7 +110,7 @@ private ValueTask> GetOrAddResultCoreAsync(CacheKey cacheKey, LogCacheMissed(Logged(cacheKey, redisKey, typeof(T))); ret = await RunGeneratorAsync(cacheKey, recorder.Generator, static (g, ct) => g(ct), factoryTimeout, token).ConfigureAwait(false); // Only a value that was written has an expiration to report. - if ((!IsDefault(ret) || _cacheNullValues) && await SetInternalAsync(cacheKey, redisKey, ret, duration, token).ConfigureAwait(false)) + if ((!IsDefault(ret) || _cacheNullValues) && await SetInternalAsync(cacheKey, redisKey, ret, duration, format, token).ConfigureAwait(false)) { run.Expiration = StoredDeadline(duration); } diff --git a/src/UiPath.Caching/Redis/RedisCache.cs b/src/UiPath.Caching/Redis/RedisCache.cs index 3818a58e..b099861c 100644 --- a/src/UiPath.Caching/Redis/RedisCache.cs +++ b/src/UiPath.Caching/Redis/RedisCache.cs @@ -27,6 +27,8 @@ internal sealed partial class RedisCache : RedisCacheBase, ICache, IInvalidation private readonly int _largeValueThreshold; private readonly bool _cacheNullValues; private readonly ConcurrentDictionary _generations = new(); + private ValueSerializerRegistry _valueSerializers = ValueSerializerRegistry.Empty; + private ValueFormatResolver _formats; public RedisCache( IRedisConnector redis, @@ -46,6 +48,7 @@ public RedisCache( _logReadScriptUnavailable = LogReadScriptUnavailable; _serializer = serializer; _memorySerializer = serializer as IMemorySerializerProxy; + _formats = new ValueFormatResolver(serializer, ValueSerializerRegistry.Empty, DefaultPolicy); _read = new ConnectingPipeline(redis, resiliencePipelineProvider.Get(ResiliencePipelineNames.Read)); _write = new ConnectingPipeline(redis, resiliencePipelineProvider.Get(ResiliencePipelineNames.Write)); _largeValueThreshold = cacheOptions.LargeValueThreshold; @@ -61,31 +64,44 @@ public RedisCache( public string Name => KnownCacheProviderNames.Redis; - internal ValueSerializerRegistry ValueSerializers { get; init; } = ValueSerializerRegistry.Empty; + internal ValueSerializerRegistry ValueSerializers + { + get => _valueSerializers; + init + { + _valueSerializers = value; + _formats = new ValueFormatResolver(_serializer, value, DefaultPolicy); + } + } [OverloadResolutionPriority(1)] public ValueTask GetAsync(CacheKey cacheKey, CachePolicy? policy, CancellationToken token = default) { NotCacheableException.ThrowIfNotCacheable(); - return GetAsync(ToRedisKey(cacheKey, token), token); + var format = FormatFor(policy, forWrite: false); + var redisKey = ToRedisKey(cacheKey, token); + return format.Sniff ? GetAsync(redisKey, format, token) : GetAsync(redisKey, default, token); } public ValueTask[]> GetAsync(CacheKey[] cacheKeys, CachePolicy? policy, CancellationToken token = default) { NotCacheableException.ThrowIfNotCacheable(); - return GetAsyncInternal(cacheKeys, token); + var format = FormatFor(policy, forWrite: false); + return format.Sniff ? GetAsyncInternal(cacheKeys, format, token) : GetAsyncInternal(cacheKeys, default, token); } public ValueTask> GetCacheEntryAsync(CacheKey cacheKey, CachePolicy? policy, CancellationToken token = default) { NotCacheableException.ThrowIfNotCacheable(); - return GetCacheEntryInternalAsync(cacheKey, token); + var format = FormatFor(policy, forWrite: false); + return format.Sniff ? GetCacheEntryInternalAsync(cacheKey, format, token) : GetCacheEntryInternalAsync(cacheKey, default, token); } public ValueTask>[]> GetCacheEntriesAsync(CacheKey[] cacheKeys, CachePolicy? policy, CancellationToken token = default) { NotCacheableException.ThrowIfNotCacheable(); - return GetCacheEntriesInternalAsync(cacheKeys, token); + var format = FormatFor(policy, forWrite: false); + return format.Sniff ? GetCacheEntriesInternalAsync(cacheKeys, format, token) : GetCacheEntriesInternalAsync(cacheKeys, default, token); } public ValueTask GetOrAddAsync(CacheKey cacheKey, Func> generator, CachePolicy? policy, CancellationToken token = default) @@ -146,14 +162,20 @@ public RedisCache( { NotCacheableException.ThrowIfNotCacheable(); ArgumentNullException.ThrowIfNull(generator); - return GetOrAddGeneratedAsync(cacheKey, ToRedisKey(cacheKey, token), generator, static (g, ct) => g(ct), policy, token); + var format = FormatFor(policy, forWrite: true); + return format.IsDefault + ? GetOrAddGeneratedAsync(cacheKey, ToRedisKey(cacheKey, token), generator, static (g, ct) => g(ct), policy, default(PlainFormat), token) + : GetOrAddGeneratedAsync(cacheKey, ToRedisKey(cacheKey, token), generator, static (g, ct) => g(ct), policy, format, token); } public ValueTask GetOrAddWithExpirationAsync(CacheKey cacheKey, TState state, Func>> generator, CachePolicy? policy, CancellationToken token = default) { NotCacheableException.ThrowIfNotCacheable(); ArgumentNullException.ThrowIfNull(generator); - return GetOrAddGeneratedAsync(cacheKey, ToRedisKey(cacheKey, token), state, generator, policy, token); + var format = FormatFor(policy, forWrite: true); + return format.IsDefault + ? GetOrAddGeneratedAsync(cacheKey, ToRedisKey(cacheKey, token), state, generator, policy, default(PlainFormat), token) + : GetOrAddGeneratedAsync(cacheKey, ToRedisKey(cacheKey, token), state, generator, policy, format, token); } public ValueTask RefreshAsync(CacheKey cacheKey, CachePolicy? policy, CancellationToken token = default) => @@ -177,31 +199,31 @@ public ValueTask RemoveAsync(CacheKey[] cacheKey, CancellationToken tok } public ValueTask SetAsync(CacheKey cacheKey, T? value, CachePolicy? policy, CancellationToken token = default) => - SetCoreAsync(cacheKey, value, PolicyDuration(policy), token); + SetCoreAsync(cacheKey, value, PolicyDuration(policy), policy, token); public ValueTask SetAsync(CacheKey cacheKey, T? value, TimeSpan expiration, CachePolicy? policy, CancellationToken token = default) => - SetCoreAsync(cacheKey, value, CallerDuration(expiration), token); + SetCoreAsync(cacheKey, value, CallerDuration(expiration), policy, token); public ValueTask SetAsync(CacheKey cacheKey, T? value, DateTimeOffset expiration, CachePolicy? policy, CancellationToken token = default) => - SetCoreAsync(cacheKey, value, CallerDuration(expiration), token); + SetCoreAsync(cacheKey, value, CallerDuration(expiration), policy, token); public ValueTask SetAsync(KeyValuePair[] keyValues, CachePolicy? policy, CancellationToken token = default) => - SetCoreAsync(keyValues, PolicyDuration(policy), token); + SetCoreAsync(keyValues, PolicyDuration(policy), policy, token); public ValueTask SetAsync(KeyValuePair[] keyValues, TimeSpan expiration, CachePolicy? policy, CancellationToken token = default) => - SetCoreAsync(keyValues, CallerDuration(expiration), token); + SetCoreAsync(keyValues, CallerDuration(expiration), policy, token); public ValueTask SetAsync(KeyValuePair[] keyValues, DateTimeOffset expiration, CachePolicy? policy, CancellationToken token = default) => - SetCoreAsync(keyValues, CallerDuration(expiration), token); + SetCoreAsync(keyValues, CallerDuration(expiration), policy, token); public ValueTask TryAddAsync(CacheKey cacheKey, T? value, CachePolicy? policy, CancellationToken token = default) => - TryAddCoreAsync(cacheKey, value, PolicyDuration(policy), token); + TryAddCoreAsync(cacheKey, value, PolicyDuration(policy), policy, token); public ValueTask TryAddAsync(CacheKey cacheKey, T? value, TimeSpan expiration, CachePolicy? policy, CancellationToken token = default) => - TryAddCoreAsync(cacheKey, value, CallerDuration(expiration), token); + TryAddCoreAsync(cacheKey, value, CallerDuration(expiration), policy, token); public ValueTask TryAddAsync(CacheKey cacheKey, T? value, DateTimeOffset expiration, CachePolicy? policy, CancellationToken token = default) => - TryAddCoreAsync(cacheKey, value, CallerDuration(expiration), token); + TryAddCoreAsync(cacheKey, value, CallerDuration(expiration), policy, token); public ValueTask ContainsAsync(CacheKey cacheKey, CancellationToken token = default) { @@ -397,22 +419,33 @@ private async ValueTask RefreshCoreAsync(CacheKey cacheKey, DateTimeOff return ret; } - private ValueTask SetCoreAsync(CacheKey cacheKey, T? value, TimeSpan duration, CancellationToken token) + private ValueTask SetCoreAsync(CacheKey cacheKey, T? value, TimeSpan duration, CachePolicy? policy, CancellationToken token) { NotCacheableException.ThrowIfNotCacheable(); - return SetInternalAsync(cacheKey, ToRedisKey(cacheKey, token), value, duration, token); + var redisKey = ToRedisKey(cacheKey, token); + var format = FormatFor(policy, forWrite: true); + return format.Writer is null + ? SetInternalAsync(cacheKey, redisKey, value, duration, default(PlainFormat), token) + : SetInternalAsync(cacheKey, redisKey, value, duration, format, token); } - private ValueTask SetCoreAsync(KeyValuePair[] keyValues, TimeSpan duration, CancellationToken token) + private ValueTask SetCoreAsync(KeyValuePair[] keyValues, TimeSpan duration, CachePolicy? policy, CancellationToken token) { NotCacheableException.ThrowIfNotCacheable(); - return SetInternalAsync(keyValues, duration, token); + var format = FormatFor(policy, forWrite: true); + return format.Writer is null + ? SetInternalAsync(keyValues, duration, default(PlainFormat), token) + : SetInternalAsync(keyValues, duration, format, token); } - private ValueTask TryAddCoreAsync(CacheKey cacheKey, T? value, TimeSpan duration, CancellationToken token) + private ValueTask TryAddCoreAsync(CacheKey cacheKey, T? value, TimeSpan duration, CachePolicy? policy, CancellationToken token) { NotCacheableException.ThrowIfNotCacheable(); - return TryAddInternalAsync(cacheKey, ToRedisKey(cacheKey, token), value, duration, token); + var redisKey = ToRedisKey(cacheKey, token); + var format = FormatFor(policy, forWrite: true); + return format.Writer is null + ? TryAddInternalAsync(cacheKey, redisKey, value, duration, default(PlainFormat), token) + : TryAddInternalAsync(cacheKey, redisKey, value, duration, format, token); } private ValueTask GetOrAddCoreAsync(CacheKey cacheKey, TState state, Func> generator, TimeSpan duration, CachePolicy? policy, CancellationToken token) @@ -420,21 +453,26 @@ private ValueTask TryAddCoreAsync(CacheKey cacheKey, T? value, TimeSpan NotCacheableException.ThrowIfNotCacheable(); ArgumentNullException.ThrowIfNull(generator); var redisKey = ToRedisKey(cacheKey, token); - return GetOrAddInternalAsync(cacheKey, redisKey, state, generator, (policy ?? DefaultPolicy)?.FactoryTimeout, duration, token); + var format = FormatFor(policy, forWrite: true); + return format.IsDefault + ? GetOrAddInternalAsync(cacheKey, redisKey, state, generator, (policy ?? DefaultPolicy)?.FactoryTimeout, duration, default(PlainFormat), token) + : GetOrAddInternalAsync(cacheKey, redisKey, state, generator, (policy ?? DefaultPolicy)?.FactoryTimeout, duration, format, token); } - private ValueTask GetOrAddInternalAsync(CacheKey cacheKey, RedisKey redisKey, TState state, Func> generator, TimeSpan? factoryTimeout, TimeSpan expiration, CancellationToken token) + private ValueTask GetOrAddInternalAsync(CacheKey cacheKey, RedisKey redisKey, TState state, Func> generator, TimeSpan? factoryTimeout, TimeSpan expiration, TFormat format, CancellationToken token) + where TFormat : struct, IValueFormat { NotCacheableException.ThrowIfNotCacheable(); - return ShareGenerationAsync> Generator, TimeSpan? FactoryTimeout, TimeSpan Expiration)>( + return ShareGenerationAsync> Generator, TimeSpan? FactoryTimeout, TimeSpan Expiration, TFormat Format)>( redisKey, - (this, cacheKey, redisKey, state, generator, factoryTimeout, expiration), - static (args, run) => args.Cache.GenerateAndStoreAsync(args.Key, args.RedisKey, args.State, args.Generator, args.FactoryTimeout, args.Expiration, run), + (this, cacheKey, redisKey, state, generator, factoryTimeout, expiration, format), + static (args, run) => args.Cache.GenerateAndStoreAsync(args.Key, args.RedisKey, args.State, args.Generator, args.FactoryTimeout, args.Expiration, args.Format, run), token); } /// The work of a shared run, which ends the run itself (see ): out of the table with its result or failure before its task completes. - private async ValueTask GenerateAndStoreAsync(CacheKey cacheKey, RedisKey redisKey, TState state, Func> generator, TimeSpan? factoryTimeout, TimeSpan expiration, IInFlightEnd run) + private async ValueTask GenerateAndStoreAsync(CacheKey cacheKey, RedisKey redisKey, TState state, Func> generator, TimeSpan? factoryTimeout, TimeSpan expiration, TFormat format, IInFlightEnd run) + where TFormat : struct, IValueFormat { T? ret; try @@ -450,18 +488,16 @@ private ValueTask TryAddCoreAsync(CacheKey cacheKey, T? value, TimeSpan var operation = StartOperation(nameof(GetOrAddAsync)); try { - var value = await _read.ExecuteAsync(static (s, token) => + var value = await ReadStringAsync(redisKey, token).ConfigureAwait(false); + (found, cached, var unreadable) = InterpretReadResult(value, format); + if (unreadable != 0) { - token.ThrowIfCancellationRequested(); - return s.Self.Database.StringGetAsync(s.Key, s.Self.ReadFlagsFor(s.Key)).AsValueTask(); - }, - (Self: this, Key: redisKey), - RedisValue.Null, - token).ConfigureAwait(false); - (found, cached) = InterpretReadResult(value); + await RemoveUnreadableAsync(redisKey, value, unreadable, token).ConfigureAwait(false); + } + operation.Stop(); } - catch (Exception ex) + catch (Exception ex) when (format.Handles(ex)) { operation.Stop(); LogRedisCacheException(ex); @@ -483,7 +519,7 @@ private ValueTask TryAddCoreAsync(CacheKey cacheKey, T? value, TimeSpan LogCacheMissed(Logged(cacheKey, redisKey, typeof(T))); ret = await RunGeneratorAsync(cacheKey, state, generator, factoryTimeout, token).ConfigureAwait(false); - if ((!IsDefault(ret) || _cacheNullValues) && await SetInternalAsync(cacheKey, redisKey, ret, expiration, token).ConfigureAwait(false)) + if ((!IsDefault(ret) || _cacheNullValues) && await SetInternalAsync(cacheKey, redisKey, ret, expiration, format, token).ConfigureAwait(false)) { run.Expiration = StoredDeadline(expiration); } @@ -511,21 +547,23 @@ private ValueTask TryAddCoreAsync(CacheKey cacheKey, T? value, TimeSpan private InFlight GenerationsOf() => (InFlight)_generations.GetOrAdd(typeof(T), static _ => new InFlight()); - private ValueTask GetOrAddGeneratedAsync(CacheKey cacheKey, RedisKey redisKey, TState state, Func>> generator, CachePolicy? policy, CancellationToken token) => - ShareGenerationAsync>> Generator, CachePolicy? Policy)>( + private ValueTask GetOrAddGeneratedAsync(CacheKey cacheKey, RedisKey redisKey, TState state, Func>> generator, CachePolicy? policy, TFormat format, CancellationToken token) + where TFormat : struct, IValueFormat => + ShareGenerationAsync>> Generator, CachePolicy? Policy, TFormat Format)>( redisKey, - (this, cacheKey, redisKey, state, generator, policy), - static (args, run) => args.Cache.GenerateExpiringAndStoreAsync(args.Key, args.RedisKey, args.State, args.Generator, args.Policy, run), + (this, cacheKey, redisKey, state, generator, policy, format), + static (args, run) => args.Cache.GenerateExpiringAndStoreAsync(args.Key, args.RedisKey, args.State, args.Generator, args.Policy, args.Format, run), token); - /// As , for a generator that returns the value's deadline; ends the run itself. - private async ValueTask GenerateExpiringAndStoreAsync(CacheKey cacheKey, RedisKey redisKey, TState state, Func>> generator, CachePolicy? policy, IInFlightEnd run) + /// As , for a generator that returns the value's deadline; ends the run itself. + private async ValueTask GenerateExpiringAndStoreAsync(CacheKey cacheKey, RedisKey redisKey, TState state, Func>> generator, CachePolicy? policy, TFormat format, IInFlightEnd run) + where TFormat : struct, IValueFormat { T? ret; try { var token = run.Token; - var (found, cached) = await ReadGetOrAddProbeAsync(redisKey, token).ConfigureAwait(false); + var (found, cached) = await ReadGetOrAddProbeAsync(redisKey, format, token).ConfigureAwait(false); if (found) { // The probe reads no TTL; a result call that joined this run reads it itself. @@ -539,7 +577,7 @@ private ValueTask TryAddCoreAsync(CacheKey cacheKey, T? value, TimeSpan var duration = generated.Expiration is { } expiration ? Remaining(expiration) : PolicyDuration(policy); if (duration is { } lifetime && (!IsDefault(generated.Value) || _cacheNullValues) - && await SetInternalAsync(cacheKey, redisKey, generated.Value, lifetime, token).ConfigureAwait(false)) + && await SetInternalAsync(cacheKey, redisKey, generated.Value, lifetime, format, token).ConfigureAwait(false)) { run.Expiration = StoredDeadline(lifetime); } @@ -574,8 +612,15 @@ private Task RunGeneratorAsync(CacheKey cacheKey, TSta : FactoryTimeout.RunAsync(ct => generator(state, ct), factoryTimeout, cacheKey, Name, Telemetry, token); /// Borrowed memory is safe here because every write awaits its command and the connection copies the value while writing it; pooled memory goes back only then. - private (RedisValue Value, SerializedPayload Payload) SerializeValue(T? value) + private (RedisValue Value, SerializedPayload Payload) SerializeValue(T? value, TFormat format) + where TFormat : struct, IValueFormat { + if (format.Writer is { } writer) + { + var headered = SerializedPayload.Serialize(writer, _valueSerializers.FormatIdsOf(writer).Span, value!); + return (headered.Memory, headered); + } + if (_memorySerializer is { } memory) { var payload = SerializedPayload.Serialize(memory, value); @@ -589,22 +634,72 @@ private Task RunGeneratorAsync(CacheKey cacheKey, TSta /// CacheNullValues is on); non-empty -> deserialize. Legacy round-trip to default(T) /// is also treated as a miss when the option is off. /// - private (bool Found, T? Value) InterpretReadResult(RedisValue value) + private (bool Found, T? Value, int Unreadable) InterpretReadResult(RedisValue value, TFormat format) + where TFormat : struct, IValueFormat { if (value.IsNull) { - return (false, default); + return (false, default, 0); + } + + T? deserialized; + if (value.Length() == 0) + { + deserialized = default; } - var deserialized = value.Length() == 0 ? default : _serializer.Deserialize(value); + else if (format.Sniff) + { + var outcome = _formats.Reader!.Read(((byte[]?)value)!); + if (outcome.IsMiss) + { + if (outcome.Failure is { } failure) + { + LogRedisCacheException(failure); + } + + return (false, default, outcome.UnreadableFormat); + } + + deserialized = outcome.Value; + } + else + { + deserialized = _serializer.Deserialize(value); + } + if (!_cacheNullValues && IsDefault(deserialized)) { - return (false, default); + return (false, default, 0); } - return (true, deserialized); + return (true, deserialized, 0); + } + + /// As , for one value of a batch: a format no serializer owns joins , which the batch drops after it has reported. + private (bool Found, T? Value) InterpretBatchRead(RedisKey redisKey, RedisValue value, TFormat format, ref List<(RedisKey Key, RedisValue Read, int FormatId)>? unreadableKeys) + where TFormat : struct, IValueFormat + { + var (found, deserialized, unreadable) = InterpretReadResult(value, format); + if (unreadable != 0) + { + (unreadableKeys ??= []).Add((redisKey, value, unreadable)); + } + + return (found, deserialized); } + private ValueTask ReadStringAsync(RedisKey redisKey, CancellationToken token) => + _read.ExecuteAsync(static (s, token) => + { + token.ThrowIfCancellationRequested(); + return s.Self.Database.StringGetAsync(s.Key, s.Self.ReadFlagsFor(s.Key)).AsValueTask(); + }, + (Self: this, Key: redisKey), + RedisValue.Null, + token); + // GenerateAndStoreAsync carries an inlined copy of this read; a change to one goes in both. - private async ValueTask<(bool Found, T? Value)> ReadGetOrAddProbeAsync(RedisKey redisKey, CancellationToken token) + private async ValueTask<(bool Found, T? Value)> ReadGetOrAddProbeAsync(RedisKey redisKey, TFormat format, CancellationToken token) + where TFormat : struct, IValueFormat { token.ThrowIfCancellationRequested(); @@ -618,18 +713,16 @@ private Task RunGeneratorAsync(CacheKey cacheKey, TSta T? deserialized = default; try { - var value = await _read.ExecuteAsync(static (s, token) => + var value = await ReadStringAsync(redisKey, token).ConfigureAwait(false); + (found, deserialized, var unreadable) = InterpretReadResult(value, format); + if (unreadable != 0) { - token.ThrowIfCancellationRequested(); - return s.Self.Database.StringGetAsync(s.Key, s.Self.ReadFlagsFor(s.Key)).AsValueTask(); - }, - (Self: this, Key: redisKey), - RedisValue.Null, - token).ConfigureAwait(false); - (found, deserialized) = InterpretReadResult(value); + await RemoveUnreadableAsync(redisKey, value, unreadable, token).ConfigureAwait(false); + } + operation.Stop(); } - catch (Exception ex) + catch (Exception ex) when (format.Handles(ex)) { operation.Stop(); LogRedisCacheException(ex); @@ -642,7 +735,8 @@ private Task RunGeneratorAsync(CacheKey cacheKey, TSta return (found, deserialized); } - private async ValueTask SetInternalAsync(CacheKey cacheKey, RedisKey redisKey, T? value, TimeSpan expiration, CancellationToken token) + private async ValueTask SetInternalAsync(CacheKey cacheKey, RedisKey redisKey, T? value, TimeSpan expiration, TFormat format, CancellationToken token) + where TFormat : struct, IValueFormat { bool ret = default; token.ThrowIfCancellationRequested(); @@ -677,7 +771,7 @@ private async ValueTask SetInternalAsync(CacheKey cacheKey, RedisKey re } else { - var (serialized, payload) = SerializeValue(value); + var (serialized, payload) = SerializeValue(value, format); _auditKeySize?.Invoke(Logged(cacheKey, redisKey, typeof(T)), serialized); using (payload) { @@ -707,7 +801,8 @@ private async ValueTask SetInternalAsync(CacheKey cacheKey, RedisKey re return ret; } - private async ValueTask SetInternalAsync(KeyValuePair[] keyValues, TimeSpan expiration, CancellationToken token) + private async ValueTask SetInternalAsync(KeyValuePair[] keyValues, TimeSpan expiration, TFormat format, CancellationToken token) + where TFormat : struct, IValueFormat { bool ret = default; token.ThrowIfCancellationRequested(); @@ -726,8 +821,8 @@ private async ValueTask SetInternalAsync(KeyValuePair[] k await EnsureConnectedAsync(_write, token).ConfigureAwait(false); var committed = await PerSlotAsync( SlotGroups(redisKeys, _logger), - (Self: this, KeyValues: keyValues, Keys: redisKeys, Payloads: payloads, Expiration: expiration, Token: token), - static (s, group) => s.Self.SetSlotAsync(s.KeyValues, s.Keys, s.Payloads, group, s.Expiration, s.Token), + (Self: this, KeyValues: keyValues, Keys: redisKeys, Payloads: payloads, Expiration: expiration, Format: format, Token: token), + static (s, group) => s.Self.SetSlotAsync(s.KeyValues, s.Keys, s.Payloads, group, s.Expiration, s.Format, s.Token), false, LogRedisCacheException).ConfigureAwait(false); ret = Array.TrueForAll(committed, static c => c); @@ -752,7 +847,8 @@ private async ValueTask SetInternalAsync(KeyValuePair[] k } /// One transaction for the keys of one slot; it serializes into , which the caller disposes. - private ValueTask SetSlotAsync(KeyValuePair[] keyValues, RedisKey[] redisKeys, SerializedPayload[] payloads, int[] group, TimeSpan expiration, CancellationToken token) + private ValueTask SetSlotAsync(KeyValuePair[] keyValues, RedisKey[] redisKeys, SerializedPayload[] payloads, int[] group, TimeSpan expiration, TFormat format, CancellationToken token) + where TFormat : struct, IValueFormat { var transaction = Database.CreateTransaction(asyncState: null); foreach (var i in group) @@ -772,7 +868,7 @@ private ValueTask SetSlotAsync(KeyValuePair[] keyValues, } else { - (var serialized, payloads[i]) = SerializeValue(value); + (var serialized, payloads[i]) = SerializeValue(value, format); _auditKeySize?.Invoke(Logged(keyValues[i].Key, redisKey, typeof(T)), serialized); transaction.StringSetAsync(redisKey, serialized, expiration, When.Always, CommandFlags.DemandMaster).Forget(); } @@ -793,7 +889,8 @@ private ValueTask SetSlotAsync(KeyValuePair[] keyValues, /// Safe to retry — an attempt whose reply was lost is refused by the key it just wrote, and /// reports the same false the exception would have. /// - private async ValueTask TryAddInternalAsync(CacheKey cacheKey, RedisKey redisKey, T? value, TimeSpan expiration, CancellationToken token) + private async ValueTask TryAddInternalAsync(CacheKey cacheKey, RedisKey redisKey, T? value, TimeSpan expiration, TFormat format, CancellationToken token) + where TFormat : struct, IValueFormat { bool ret = default; token.ThrowIfCancellationRequested(); @@ -818,7 +915,7 @@ private async ValueTask TryAddInternalAsync(CacheKey cacheKey, RedisKey else { NoteWritten(redisKey); - var (serialized, payload) = isNull ? (RedisValue.EmptyString, default(SerializedPayload)) : SerializeValue(value); + var (serialized, payload) = isNull ? (RedisValue.EmptyString, default(SerializedPayload)) : SerializeValue(value, format); _auditKeySize?.Invoke(Logged(cacheKey, redisKey, typeof(T)), serialized); using (payload) { @@ -926,7 +1023,8 @@ private async ValueTask RemoveAsync(RedisKey[] redisKey, CancellationTo return ret; } - private async ValueTask GetAsync(RedisKey redisKey, CancellationToken token) + private async ValueTask GetAsync(RedisKey redisKey, TFormat format, CancellationToken token) + where TFormat : struct, IValueFormat { T? ret = default; bool found = false; @@ -937,22 +1035,20 @@ private async ValueTask RemoveAsync(RedisKey[] redisKey, CancellationTo return default; } - var operation = StartOperation(); + var operation = StartOperation(nameof(GetAsync)); try { - var value = await _read.ExecuteAsync(static (s, token) => + var value = await ReadStringAsync(redisKey, token).ConfigureAwait(false); + (found, ret, var unreadable) = InterpretReadResult(value, format); + if (unreadable != 0) { - token.ThrowIfCancellationRequested(); - return s.Self.Database.StringGetAsync(s.Key, s.Self.ReadFlagsFor(s.Key)).AsValueTask(); - }, - (Self: this, Key: redisKey), - RedisValue.Null, - token).ConfigureAwait(false); - (found, ret) = InterpretReadResult(value); + await RemoveUnreadableAsync(redisKey, value, unreadable, token).ConfigureAwait(false); + } + operation.Stop(); } - catch (Exception ex) + catch (Exception ex) when (format.Handles(ex)) { operation.Stop(); LogRedisCacheException(ex); @@ -965,7 +1061,8 @@ private async ValueTask RemoveAsync(RedisKey[] redisKey, CancellationTo return ret; } - private async ValueTask[]> GetAsyncInternal(CacheKey[] keys, CancellationToken token) + private async ValueTask[]> GetAsyncInternal(CacheKey[] keys, TFormat format, CancellationToken token) + where TFormat : struct, IValueFormat { KeyValuePair[] retValues = new KeyValuePair[keys.Length]; token.ThrowIfCancellationRequested(); @@ -978,8 +1075,9 @@ private async ValueTask RemoveAsync(RedisKey[] redisKey, CancellationTo { return GetDefaultValues(keys); } - var operation = StartOperation(); + var operation = StartOperation(nameof(GetAsyncInternal)); var reads = InitReads(redisKeys); + List<(RedisKey Key, RedisValue Read, int FormatId)>? unreadableKeys = null; try { await EnsureConnectedAsync(_read, token).ConfigureAwait(false); @@ -1010,14 +1108,14 @@ private async ValueTask RemoveAsync(RedisKey[] redisKey, CancellationTo for (var j = 0; j < group.Length; j++) { var i = group[j]; - var (found, obj) = InterpretReadResult(values[j]); + var (found, obj) = InterpretBatchRead(redisKeys[i], values[j], format, ref unreadableKeys); reads[i].Hit = found; retValues[i] = new KeyValuePair(keys[i], obj); } } operation.Stop(); } - catch (Exception ex) + catch (Exception ex) when (format.Handles(ex)) { operation.Stop(); LogRedisCacheException(ex); @@ -1029,10 +1127,16 @@ private async ValueTask RemoveAsync(RedisKey[] redisKey, CancellationTo TrackPerKey(operation, reads); } + if (unreadableKeys is not null) + { + await RemoveUnreadableAsync(unreadableKeys, format, token).ConfigureAwait(false); + } + return retValues; } - private async ValueTask> GetCacheEntryInternalAsync(CacheKey cacheKey, CancellationToken token, string methodName = nameof(GetCacheEntryInternalAsync)) + private async ValueTask> GetCacheEntryInternalAsync(CacheKey cacheKey, TFormat format, CancellationToken token, string methodName = nameof(GetCacheEntryInternalAsync)) + where TFormat : struct, IValueFormat { token.ThrowIfCancellationRequested(); var redisKey = ToRedisKey(cacheKey, token); @@ -1086,17 +1190,20 @@ private async ValueTask RemoveAsync(RedisKey[] redisKey, CancellationTo } var (value, expiration) = result.Value; - var (found, deserialized) = InterpretReadResult(value); - if (!found) + var (found, deserialized, unreadable) = InterpretReadResult(value, format); + if (found) { - operation.Stop(); - return ret; + ret = _cacheEntryFactory.Create(deserialized, Clock.ToDateTimeOffset(expiration)); + } + + if (unreadable != 0) + { + await RemoveUnreadableAsync(redisKey, value, unreadable, token).ConfigureAwait(false); } - ret = _cacheEntryFactory.Create(deserialized, Clock.ToDateTimeOffset(expiration)); operation.Stop(); } - catch (Exception ex) + catch (Exception ex) when (format.Handles(ex)) { operation.Stop(); LogRedisCacheException(ex); @@ -1109,7 +1216,8 @@ private async ValueTask RemoveAsync(RedisKey[] redisKey, CancellationTo return ret; } - private async ValueTask>[]> GetCacheEntriesInternalAsync(CacheKey[] keys, CancellationToken token) + private async ValueTask>[]> GetCacheEntriesInternalAsync(CacheKey[] keys, TFormat format, CancellationToken token) + where TFormat : struct, IValueFormat { token.ThrowIfCancellationRequested(); if (keys.Length == 0) @@ -1122,9 +1230,10 @@ private async ValueTask RemoveAsync(RedisKey[] redisKey, CancellationTo return GetDefaultEntries(keys); } - var operation = StartOperation(); + var operation = StartOperation(nameof(GetCacheEntriesInternalAsync)); var retValues = GetDefaultEntries(keys); var reads = InitReads(redisKeys); + List<(RedisKey Key, RedisValue Read, int FormatId)>? unreadableKeys = null; try { await EnsureConnectedAsync(_read, token).ConfigureAwait(false); @@ -1158,7 +1267,7 @@ static async (s, group) => for (var j = 0; j < group.Length; j++) { var i = group[j]; - var (found, deserialized) = InterpretReadResult(values[j]); + var (found, deserialized) = InterpretBatchRead(redisKeys[i], values[j], format, ref unreadableKeys); if (!found) { continue; @@ -1171,7 +1280,7 @@ static async (s, group) => } operation.Stop(); } - catch (Exception ex) + catch (Exception ex) when (format.Handles(ex)) { operation.Stop(); LogRedisCacheException(ex); @@ -1183,6 +1292,11 @@ static async (s, group) => TrackPerKey(operation, reads); } + if (unreadableKeys is not null) + { + await RemoveUnreadableAsync(unreadableKeys, format, token).ConfigureAwait(false); + } + return retValues; } diff --git a/src/UiPath.Caching/Redis/RedisCacheBase.cs b/src/UiPath.Caching/Redis/RedisCacheBase.cs index 45db61b3..a96d183c 100644 --- a/src/UiPath.Caching/Redis/RedisCacheBase.cs +++ b/src/UiPath.Caching/Redis/RedisCacheBase.cs @@ -2,6 +2,7 @@ using System.Text; using UiPath.Caching.Config; using UiPath.Caching.Policies; +using UiPath.Caching.Serialization; using UiPath.Caching.Telemetry; namespace UiPath.Caching.Redis; @@ -127,6 +128,18 @@ IDisposable IMasterReadScope.DemandMaster() internal void NoteAllInvalidated() => _recentTouches?.TouchAll(); + internal void TrackUnreadable(int formatId, string cacheName) + { + try + { + Telemetry.TrackMetric(ValueReader.UnreadableMetric, 1, [new("format", $"0x{(formatId < 0 ? 0x1F : formatId):X2}"), new("cache", cacheName)]); + } + catch (Exception ex) + { + Telemetry.TryTrackException(ex); + } + } + /// The flag a read of carries: the master when any one of them is guarded. internal CommandFlags ReadFlagsFor(RedisKey[] keys) { diff --git a/src/UiPath.Caching/Redis/RedisHashCache.cs b/src/UiPath.Caching/Redis/RedisHashCache.cs index b8cd6851..b96cbe78 100644 --- a/src/UiPath.Caching/Redis/RedisHashCache.cs +++ b/src/UiPath.Caching/Redis/RedisHashCache.cs @@ -13,6 +13,7 @@ internal sealed partial class RedisHashCache : RedisCacheBase, IHashCache, ISpan /// Times HashFieldBatchSize: the field count above which a hash is read by pages. private const int PagedReadFactor = 10; + private const int SerializerFailed = int.MaxValue; private static readonly TimeSpan ChunkedWriteOrphanLifetime = TimeSpan.FromMinutes(10); @@ -29,6 +30,8 @@ internal sealed partial class RedisHashCache : RedisCacheBase, IHashCache, ISpan private readonly bool _auditLargeValues; private readonly bool _cacheNullValues; private readonly int _fieldBatchSize; + private ValueSerializerRegistry _valueSerializers = ValueSerializerRegistry.Empty; + private ValueFormatResolver _formats; public RedisHashCache( IRedisConnector redis, @@ -45,6 +48,7 @@ public RedisHashCache( { _serializer = serializer; _memorySerializer = serializer as IMemorySerializerProxy; + _formats = new ValueFormatResolver(serializer, ValueSerializerRegistry.Empty, DefaultPolicy); _logger = logger; _logFailure = LogRedisHashCacheException; _logReadScriptUnavailable = LogReadScriptUnavailable; @@ -60,36 +64,43 @@ public RedisHashCache( public string Name => KnownCacheProviderNames.Redis; - internal ValueSerializerRegistry ValueSerializers { get; init; } = ValueSerializerRegistry.Empty; + internal ValueSerializerRegistry ValueSerializers + { + get => _valueSerializers; + init + { + _valueSerializers = value; + _formats = new ValueFormatResolver(_serializer, value, DefaultPolicy); + } + } // For tests: runs after a chunked replacement filled its staging key, before the rename. internal Func? OnStaged { get; set; } [OverloadResolutionPriority(1)] - public async ValueTask GetItemAsync(CacheKey cacheKey, string field, CachePolicy? policy, CancellationToken token = default) + public ValueTask GetItemAsync(CacheKey cacheKey, string field, CachePolicy? policy, CancellationToken token = default) { - NotCacheableException.ThrowIfNotCacheable(); - HashFields.ValidateFieldForRead(field); - return await GetInnerAsync(cacheKey, field, token); + var format = FormatFor(policy, forWrite: false); + return format.Sniff ? GetItemCoreAsync(cacheKey, field, format, token) : GetItemCoreAsync(cacheKey, field, default, token); } [OverloadResolutionPriority(1)] - public async ValueTask> GetAsync(CacheKey cacheKey, CachePolicy? policy, CancellationToken token = default) + public ValueTask> GetAsync(CacheKey cacheKey, CachePolicy? policy, CancellationToken token = default) { - NotCacheableException.ThrowIfNotCacheable(); - return await GetInnerAsync(cacheKey, token); + var format = FormatFor(policy, forWrite: false); + return format.Sniff ? GetCoreAsync(cacheKey, format, token) : GetCoreAsync(cacheKey, default, token); } - public async ValueTask> GetAsync(CacheKey cacheKey, string[] fields, CachePolicy? policy, CancellationToken token = default) + public ValueTask> GetAsync(CacheKey cacheKey, string[] fields, CachePolicy? policy, CancellationToken token = default) { - NotCacheableException.ThrowIfNotCacheable(); - return await GetInnerAsync(cacheKey, fields, token); + var format = FormatFor(policy, forWrite: false); + return format.Sniff ? GetFieldsCoreAsync(cacheKey, fields, format, token) : GetFieldsCoreAsync(cacheKey, fields, default, token); } - public async ValueTask>> GetCacheEntryAsync(CacheKey cacheKey, CachePolicy? policy, CancellationToken token = default) + public ValueTask>> GetCacheEntryAsync(CacheKey cacheKey, CachePolicy? policy, CancellationToken token = default) { - NotCacheableException.ThrowIfNotCacheable(); - return await GetInnerCacheEntryAsync(cacheKey, token); + var format = FormatFor(policy, forWrite: false); + return format.Sniff ? GetCacheEntryCoreAsync(cacheKey, format, token) : GetCacheEntryCoreAsync(cacheKey, default, token); } public ValueTask> GetOrAddAsync(CacheKey cacheKey, Func>> generator, CachePolicy? policy, CancellationToken token = default) @@ -156,66 +167,8 @@ public ValueTask RefreshAsync(CacheKey cacheKey, TimeSpan expiration, C public ValueTask RefreshAsync(CacheKey cacheKey, DateTimeOffset expiration, CachePolicy? policy, CancellationToken token = default) => RefreshCoreAsync(cacheKey, GetExpiration(expiration), token); - public async ValueTask RefreshAsync(CacheKey cacheKey, HashCacheEntryOptions options, CachePolicy? policy, CancellationToken token = default) - { - NotCacheableException.ThrowIfNotCacheable(); - var redisKey = ToRedisKey(cacheKey, token); - var expiration = GetExpiration(options, policy); - var now = Clock.GetUtcNow(); - var ret = false; - NoteWritten(redisKey); - var operation = StartOperation(); - try - { - if (expiration < now) - { - ret = await _write.ExecuteAsync(static (s, token) => - { - token.ThrowIfCancellationRequested(); - return s.Self.Database.KeyDeleteAsync(s.Key, CommandFlags.DemandMaster).AsValueTask(); - }, - (Self: this, Key: redisKey), - default, - token).ConfigureAwait(false); - } - else - { - await EnsureConnectedAsync(_write, token).ConfigureAwait(false); - var transaction = Database.CreateTransaction(); - using (QueueMetadataWrite(transaction, Logged(cacheKey, redisKey, typeof(T)), redisKey, options.Metadata)) - { - QueueExpirationUpdate(transaction, redisKey, expiration); - - ret = await _write.ExecuteAsync(static (s, token) => - { - token.ThrowIfCancellationRequested(); - return s.ExecuteAsync(CommandFlags.DemandMaster).AsValueTask(); - }, - transaction, - default, - token).ConfigureAwait(false); - } - - if (!ret) - { - LogRedisTransactionFailed(); - } - } - operation.Stop(); - } - catch (Exception ex) - { - operation.Stop(); - LogRedisHashCacheException(ex); - } - finally - { - NoteWritten(redisKey); - operation.Track(ret); - } - - return ret; - } + public ValueTask RefreshAsync(CacheKey cacheKey, HashCacheEntryOptions options, CachePolicy? policy, CancellationToken token = default) => + RefreshWithOptionsAsync(cacheKey, options, policy, FormatFor(policy, forWrite: true).Writer, token); public ValueTask RemoveAsync(CacheKey cacheKey, CancellationToken token = default) { @@ -229,13 +182,13 @@ public ValueTask RemoveFieldsAsync(CacheKey cacheKey, string[] fields, RemoveFieldsCoreAsync(cacheKey, fields, versioned: false, token); public ValueTask SetAsync(CacheKey cacheKey, IDictionary values, CachePolicy? policy, CancellationToken token = default) => - SetCoreAsync(cacheKey, values, GetExpiration(policy), token); + SetCoreAsync(cacheKey, values, GetExpiration(policy), policy, token); public ValueTask SetAsync(CacheKey cacheKey, IDictionary values, TimeSpan expiration, CachePolicy? policy, CancellationToken token = default) => - SetCoreAsync(cacheKey, values, GetExpiration(expiration), token); + SetCoreAsync(cacheKey, values, GetExpiration(expiration), policy, token); public ValueTask SetAsync(CacheKey cacheKey, IDictionary values, DateTimeOffset expiration, CachePolicy? policy, CancellationToken token = default) => - SetCoreAsync(cacheKey, values, GetExpiration(expiration), token); + SetCoreAsync(cacheKey, values, GetExpiration(expiration), policy, token); public ValueTask SetAsync(CacheKey cacheKey, IDictionary values, HashCacheEntryOptions options, CachePolicy? policy, CancellationToken token = default) => SetCountingAsync(cacheKey, values, options, policy, report: null, token); @@ -250,10 +203,14 @@ public async ValueTask SetVersionedAsync(CacheKey cacheKey, I public ValueTask RemoveFieldsVersionedAsync(CacheKey cacheKey, string[] fields, CancellationToken token = default) => RemoveFieldsCoreAsync(cacheKey, fields, versioned: true, token); - public bool TryDecodeFields(IReadOnlyDictionary encoded, [MaybeNullWhen(false)] out Dictionary values) + public bool TryDecodeFields(IReadOnlyDictionary encoded, CachePolicy? policy, [MaybeNullWhen(false)] out Dictionary values) { try { + // The held key's own policy decides the gate, as it does for a read. Where serializers are registered and that policy is not known, nothing is decoded. + var sniff = FormatFor(policy, forWrite: false).Sniff; + var ambiguous = _formats.Reader is not null && !sniff; + var unreadable = 0; var decoded = new Dictionary(encoded.Count); foreach (var (field, bytes) in encoded) { @@ -263,13 +220,24 @@ public bool TryDecodeFields(IReadOnlyDictionary encoded, [Ma return false; } - decoded[field] = DeserializeField(bytes is null ? RedisValue.Null : (byte[])bytes.Clone()); + if (ambiguous && bytes is { Length: > 0 }) + { + values = null; + return false; + } + + decoded[field] = DeserializeField(bytes is null ? RedisValue.Null : (byte[])bytes.Clone(), sniff, ref unreadable); + if (unreadable != 0) + { + values = null; + return false; + } } values = decoded; return true; } - catch (Exception ex) when (ex is FormatException or InvalidOperationException or System.Text.Json.JsonException or NotSupportedException) + catch (Exception ex) when (ex is not OutOfMemoryException) { values = null; return false; @@ -291,16 +259,186 @@ public bool TryDecodeFields(IReadOnlyDictionary encoded, [Ma public ValueTask?> GetMetadataAsync(CacheKey cacheKey, CancellationToken token = default) { NotCacheableException.ThrowIfNotCacheable(); - return GetInnerAsync>(cacheKey, KnownFieldNames.MetadataKey, token); + var format = FormatFor(policy: null, forWrite: false); + return format.Sniff + ? GetInnerAsync, ValueFormat>(cacheKey, KnownFieldNames.MetadataKey, format, token) + : GetInnerAsync, PlainFormat>(cacheKey, KnownFieldNames.MetadataKey, default, token); } - public async ValueTask SetMetadataAsync(CacheKey cacheKey, IDictionary metadata, CancellationToken token = default) + public ValueTask SetMetadataAsync(CacheKey cacheKey, IDictionary metadata, CancellationToken token = default) => + SetMetadataCoreAsync(cacheKey, metadata, FormatFor(policy: null, forWrite: true).Writer, token); + + void IInvalidationSink.Invalidated(CacheKey key) => NoteInvalidated(_redisKeyStrategy, key); + + void IInvalidationSink.InvalidatedAll() => NoteAllInvalidated(); + + /// Throws a transaction's own failure, not the cancellation of what it queued. + private static async Task CommitAsync(ITransaction transaction, string what, RedisKey key, Task first, Task second) + { + bool committed; + try + { + committed = await transaction.ExecuteAsync(CommandFlags.DemandMaster).ConfigureAwait(false); + } + catch + { + first.Forget(); + second.Forget(); + throw; + } + + if (!committed) + { + first.Forget(); + second.Forget(); + throw new RedisException($"{what} of {key} did not commit."); + } + + await ObserveAllAsync([first, second]).ConfigureAwait(false); + } + + private static async Task SettleAsync(List pending) + { + foreach (var task in pending) + { + try + { + await task.ConfigureAwait(false); + } + catch + { + // Observed; the caller rethrows its own. + } + } + } + + /// Awaits every task and rethrows the first failure. + private static async Task ObserveAllAsync(List pending) + { + Exception? first = null; + foreach (var task in pending) + { + try + { + await task.ConfigureAwait(false); + } + catch (Exception ex) + { + first ??= ex; + } + } + + if (first is not null) + { + System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw(first); + } + } + + private static void QueueExpirationUpdate(ITransaction transaction, RedisKey redisKey, DateTimeOffset expiration) + { + if (expiration != DateTimeOffset.MaxValue) + { + transaction.KeyExpireAsync(redisKey, expiration.UtcDateTime, CommandFlags.DemandMaster | CommandFlags.FireAndForget).Forget(); + return; + } + transaction.KeyPersistAsync(redisKey, CommandFlags.DemandMaster | CommandFlags.FireAndForget).Forget(); + } + + private static ImmutableDictionary Empty() => ImmutableDictionary.Empty; + + private static DateTimeOffset? ToExpiration(long expireAt) => + // PEXPIRETIME answers -1 for a key without a TTL and -2 for a missing one. + expireAt < 0 ? null : DateTimeOffset.FromUnixTimeMilliseconds(expireAt); + + private static (HashEntry[] Entries, DateTimeOffset? Expiration) ReadReply(RedisResult reply) + { + var parts = ((RedisResult[]?)reply)!; + var fields = ((RedisValue[]?)parts[0])!; + var entries = new HashEntry[fields.Length / 2]; + for (var i = 0; i < entries.Length; i++) + { + entries[i] = new HashEntry(fields[2 * i], fields[(2 * i) + 1]); + } + + return (entries, ToExpiration((long)parts[1])); + } + + private static void Release(SerializedPayload[] payloads) + { + foreach (var payload in payloads) + { + payload.Dispose(); + } + } + + private async ValueTask RefreshWithOptionsAsync(CacheKey cacheKey, HashCacheEntryOptions options, CachePolicy? policy, ICacheValueSerializer? writer, CancellationToken token) + { + NotCacheableException.ThrowIfNotCacheable(); + var redisKey = ToRedisKey(cacheKey, token); + var expiration = GetExpiration(options, policy); + var now = Clock.GetUtcNow(); + var ret = false; + NoteWritten(redisKey); + var operation = StartOperation(nameof(RefreshAsync)); + try + { + if (expiration < now) + { + ret = await _write.ExecuteAsync(static (s, token) => + { + token.ThrowIfCancellationRequested(); + return s.Self.Database.KeyDeleteAsync(s.Key, CommandFlags.DemandMaster).AsValueTask(); + }, + (Self: this, Key: redisKey), + default, + token).ConfigureAwait(false); + } + else + { + await EnsureConnectedAsync(_write, token).ConfigureAwait(false); + var transaction = Database.CreateTransaction(); + using (QueueMetadataWrite(transaction, Logged(cacheKey, redisKey, typeof(T)), redisKey, options.Metadata, writer)) + { + QueueExpirationUpdate(transaction, redisKey, expiration); + + ret = await _write.ExecuteAsync(static (s, token) => + { + token.ThrowIfCancellationRequested(); + return s.ExecuteAsync(CommandFlags.DemandMaster).AsValueTask(); + }, + transaction, + default, + token).ConfigureAwait(false); + } + + if (!ret) + { + LogRedisTransactionFailed(); + } + } + operation.Stop(); + } + catch (Exception ex) + { + operation.Stop(); + LogRedisHashCacheException(ex); + } + finally + { + NoteWritten(redisKey); + operation.Track(ret); + } + + return ret; + } + + private async ValueTask SetMetadataCoreAsync(CacheKey cacheKey, IDictionary metadata, ICacheValueSerializer? writer, CancellationToken token) { NotCacheableException.ThrowIfNotCacheable(); var redisKey = ToRedisKey(cacheKey, token); NoteWritten(redisKey); var ret = false; - var operation = StartOperation(); + var operation = StartOperation(nameof(SetMetadataAsync)); try { var keyExists = await _read.ExecuteAsync(static (s, token) => @@ -315,7 +453,7 @@ public async ValueTask SetMetadataAsync(CacheKey cacheKey, IDictionary< { if (metadata.Count > 0) { - var (metadataValue, payload) = SerializeFieldValue(metadata); + var (metadataValue, payload) = SerializeFieldValue(metadata, writer); if (_auditLargeValues) { AuditKeySize(Logged(cacheKey, redisKey, typeof(T)), KnownFieldNames.MetadataKey, metadataValue); @@ -394,107 +532,33 @@ public async ValueTask SetMetadataAsync(CacheKey cacheKey, IDictionary< return ret; } - void IInvalidationSink.Invalidated(CacheKey key) => NoteInvalidated(_redisKeyStrategy, key); - - void IInvalidationSink.InvalidatedAll() => NoteAllInvalidated(); - - /// Throws a transaction's own failure, not the cancellation of what it queued. - private static async Task CommitAsync(ITransaction transaction, string what, RedisKey key, Task first, Task second) - { - bool committed; - try - { - committed = await transaction.ExecuteAsync(CommandFlags.DemandMaster).ConfigureAwait(false); - } - catch - { - first.Forget(); - second.Forget(); - throw; - } - - if (!committed) - { - first.Forget(); - second.Forget(); - throw new RedisException($"{what} of {key} did not commit."); - } - - await ObserveAllAsync([first, second]).ConfigureAwait(false); - } - - private static async Task SettleAsync(List pending) - { - foreach (var task in pending) - { - try - { - await task.ConfigureAwait(false); - } - catch - { - // Observed; the caller rethrows its own. - } - } - } - - /// Awaits every task and rethrows the first failure. - private static async Task ObserveAllAsync(List pending) + private async ValueTask GetItemCoreAsync(CacheKey cacheKey, string field, TFormat format, CancellationToken token) + where TFormat : struct, IValueFormat { - Exception? first = null; - foreach (var task in pending) - { - try - { - await task.ConfigureAwait(false); - } - catch (Exception ex) - { - first ??= ex; - } - } - - if (first is not null) - { - System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw(first); - } + NotCacheableException.ThrowIfNotCacheable(); + HashFields.ValidateFieldForRead(field); + return await GetInnerAsync(cacheKey, field, format, token); } - private static void QueueExpirationUpdate(ITransaction transaction, RedisKey redisKey, DateTimeOffset expiration) + private async ValueTask> GetCoreAsync(CacheKey cacheKey, TFormat format, CancellationToken token) + where TFormat : struct, IValueFormat { - if (expiration != DateTimeOffset.MaxValue) - { - transaction.KeyExpireAsync(redisKey, expiration.UtcDateTime, CommandFlags.DemandMaster | CommandFlags.FireAndForget).Forget(); - return; - } - transaction.KeyPersistAsync(redisKey, CommandFlags.DemandMaster | CommandFlags.FireAndForget).Forget(); + NotCacheableException.ThrowIfNotCacheable(); + return await GetInnerAsync(cacheKey, format, token); } - private static ImmutableDictionary Empty() => ImmutableDictionary.Empty; - - private static DateTimeOffset? ToExpiration(long expireAt) => - // PEXPIRETIME answers -1 for a key without a TTL and -2 for a missing one. - expireAt < 0 ? null : DateTimeOffset.FromUnixTimeMilliseconds(expireAt); - - private static (HashEntry[] Entries, DateTimeOffset? Expiration) ReadReply(RedisResult reply) + private async ValueTask> GetFieldsCoreAsync(CacheKey cacheKey, string[] fields, TFormat format, CancellationToken token) + where TFormat : struct, IValueFormat { - var parts = ((RedisResult[]?)reply)!; - var fields = ((RedisValue[]?)parts[0])!; - var entries = new HashEntry[fields.Length / 2]; - for (var i = 0; i < entries.Length; i++) - { - entries[i] = new HashEntry(fields[2 * i], fields[(2 * i) + 1]); - } - - return (entries, ToExpiration((long)parts[1])); + NotCacheableException.ThrowIfNotCacheable(); + return await GetInnerAsync(cacheKey, fields, format, token); } - private static void Release(SerializedPayload[] payloads) + private async ValueTask>> GetCacheEntryCoreAsync(CacheKey cacheKey, TFormat format, CancellationToken token) + where TFormat : struct, IValueFormat { - foreach (var payload in payloads) - { - payload.Dispose(); - } + NotCacheableException.ThrowIfNotCacheable(); + return await GetInnerCacheEntryAsync(cacheKey, format, token); } private ValueTask RemoveFieldsCoreAsync(CacheKey cacheKey, string[] fields, bool versioned, CancellationToken token) @@ -515,15 +579,58 @@ private ValueTask SetCountingAsync(CacheKey cacheKey, IDictionary(); HashFields.ValidateForWrite(values); var redisKey = ToRedisKey(cacheKey, token); - var metadata = values.Count > 0 || _cacheNullValues ? options.Metadata : null; + var writer = FormatFor(policy, forWrite: true).Writer; + var requested = values.Count > 0 || _cacheNullValues ? options.Metadata : null; + var metadata = MetadataToWrite(requested, writer); var expiration = GetExpiration(options, policy); var setOption = values.Count == 0 && _cacheNullValues ? HashCacheSetOption.KeyReplace : options.SetOption; // Everything that can throw runs before the fields and the metadata rent buffers, which SetInnerAsync then owns. - var (entries, payloads) = SerializeFields(values, metadata); + var (entries, payloads) = SerializeFields(values, metadata, writer); + if (requested is not null && metadata is null && setOption == HashCacheSetOption.HashReplace) + { + // A replacement too large for one transaction drops the stale metadata field after its chunks, since it is not atomic anyway. + return entries.Length > _fieldBatchSize + ? DropMetadataAfterAsync(redisKey, SetInnerAsync(cacheKey, redisKey, entries, payloads, setOption, expiration, token, report), token) + : SetInnerAsync(cacheKey, redisKey, entries, payloads, setOption, expiration, token, report, dropMetadata: true); + } + return SetInnerAsync(cacheKey, redisKey, entries, payloads, setOption, expiration, token, report); } + /// Deletes the metadata field an earlier write left, after a chunked write: a replacement too large for one command is not atomic anyway. + private async ValueTask DropMetadataAfterAsync(RedisKey redisKey, ValueTask write, CancellationToken token) + { + if (!await write.ConfigureAwait(false)) + { + return false; + } + + var dropped = false; + try + { + dropped = await _write.ExecuteAsync(static async (s, token) => + { + token.ThrowIfCancellationRequested(); + await s.Self.Database.HashDeleteAsync(s.Key, KnownFieldNames.MetadataKey, CommandFlags.DemandMaster).ConfigureAwait(false); + return true; + }, + (Self: this, Key: redisKey), + default, + token).ConfigureAwait(false); + } + catch (Exception ex) + { + LogRedisHashCacheException(ex); + } + finally + { + NoteWritten(redisKey); + } + + return dropped; + } + /// The hash, or its length when it is too large for one command. private async ValueTask<(HashEntry[] Entries, DateTimeOffset? Expiration)> ParseReplyAsync(RedisKey redisKey, RedisResult reply) { @@ -538,7 +645,7 @@ private ValueTask SetCountingAsync(CacheKey cacheKey, IDictionary HashGetFieldsAsync(RedisKey redisKey, stri private async ValueTask> GetOrAddCoreAsync(CacheKey cacheKey, TState state, Func>> generator, DateTimeOffset effectiveExpiration, HashCacheSetOption setOption, CachePolicy? policy, CancellationToken token) { NotCacheableException.ThrowIfNotCacheable(); - var (found, cached) = await GetInnerWithFoundAsync(cacheKey, token).ConfigureAwait(false); + var (found, cached) = await GetInnerWithFoundAsync(cacheKey, policy, token).ConfigureAwait(false); if (found) { return cached; @@ -615,59 +722,18 @@ private async ValueTask HashGetFieldsAsync(RedisKey redisKey, stri ? generator(state, token) : FactoryTimeout.RunAsync(ct => generator(state, ct), factoryTimeout, cacheKey, Name, Telemetry, token); - private async ValueTask<(bool Found, IDictionary Values)> GetInnerWithFoundAsync(CacheKey cacheKey, CancellationToken token) + private ValueTask SetEmptyMarkerAsync(CacheKey cacheKey, HashCacheEntryOptions options, CachePolicy? policy, CancellationToken token) { var redisKey = ToRedisKey(cacheKey, token); - if (!IsConnected) - { - return (false, Empty()); - } - - var operation = StartOperation(nameof(GetOrAddAsync)); - bool found = false; - IDictionary ret = Empty(); - try - { - var hashEntries = await _read.ExecuteAsync(static (s, token) => - { - token.ThrowIfCancellationRequested(); - return s.Self.ReadAllEntriesAsync(s.Key); - }, - (Self: this, Key: redisKey), - [], - token).ConfigureAwait(false); - - if (hashEntries.Length == 0) - { - operation.Stop(); - return (false, ret); - } - - (ret, found) = ReadFields(hashEntries); - operation.Stop(); - } - catch (Exception ex) - { - operation.Stop(); - LogRedisHashCacheException(ex); - } - finally + var writer = FormatFor(policy, forWrite: true).Writer; + var expiration = GetExpiration(options, policy); + if (options.Metadata is not { Count: > 0 } metadataValues) { - TrackRead(operation, found, redisKey); + return SetInnerAsync(cacheKey, redisKey, [new HashEntry(KnownFieldNames.MetadataKey, RedisValue.EmptyString)], [], HashCacheSetOption.KeyReplace, expiration, token); } - return (found, ret); - } - - private ValueTask SetEmptyMarkerAsync(CacheKey cacheKey, HashCacheEntryOptions options, CachePolicy? policy, CancellationToken token) - { - var redisKey = ToRedisKey(cacheKey, token); - RedisValue metadata = options.Metadata != null && options.Metadata.Count > 0 - ? _serializer.Serialize(options.Metadata) - : RedisValue.EmptyString; - var entries = new[] { new HashEntry(KnownFieldNames.MetadataKey, metadata) }; - var expiration = GetExpiration(options, policy); - return SetInnerAsync(cacheKey, redisKey, entries, [], HashCacheSetOption.KeyReplace, expiration, token); + var (metadata, payload) = SerializeFieldValue(metadataValues, writer); + return SetInnerAsync(cacheKey, redisKey, [new HashEntry(KnownFieldNames.MetadataKey, metadata)], [payload], HashCacheSetOption.KeyReplace, expiration, token); } private async ValueTask RefreshCoreAsync(CacheKey cacheKey, DateTimeOffset localExpiration, CancellationToken token) @@ -714,15 +780,16 @@ private async ValueTask RefreshCoreAsync(CacheKey cacheKey, DateTimeOff } /// Returns the metadata's payload, which the caller disposes once the transaction has executed. - private SerializedPayload QueueMetadataWrite(ITransaction transaction, LoggedKey logged, RedisKey redisKey, IDictionary? metadata) + private SerializedPayload QueueMetadataWrite(ITransaction transaction, LoggedKey logged, RedisKey redisKey, IDictionary? metadata, ICacheValueSerializer? writer) { + metadata = MetadataToWrite(metadata, writer); if (metadata != null) { if (_cacheNullValues) { transaction.AddCondition(Condition.KeyExists(redisKey)); } - var (value, payload) = SerializeFieldValue(metadata); + var (value, payload) = SerializeFieldValue(metadata, writer); if (_auditLargeValues) { AuditKeySize(logged, KnownFieldNames.MetadataKey, value); @@ -751,17 +818,21 @@ private SerializedPayload QueueMetadataWrite(ITransaction transaction, LoggedKey return default; } - private ValueTask SetCoreAsync(CacheKey cacheKey, IDictionary values, DateTimeOffset effective, CancellationToken token) + private ValueTask SetCoreAsync(CacheKey cacheKey, IDictionary values, DateTimeOffset effective, CachePolicy? policy, CancellationToken token) { NotCacheableException.ThrowIfNotCacheable(); HashFields.ValidateForWrite(values); var redisKey = ToRedisKey(cacheKey, token); - var (hashEntries, payloads) = SerializeFields(values); + var (hashEntries, payloads) = SerializeFields(values, metadata: null, FormatFor(policy, forWrite: true).Writer); return SetInnerAsync(cacheKey, redisKey, hashEntries, payloads, HashCacheSetOption.KeyReplace, effective, token); } + /// With a non-JSON serializer the metadata field is left out when it is empty and nulls are not cached, so the hash reads back with none; the JSON default keeps writing it as it always has. + private IDictionary? MetadataToWrite(IDictionary? metadata, ICacheValueSerializer? writer) => + writer is not null && !_cacheNullValues && metadata is { Count: 0 } ? null : metadata; + /// One entry per field, then the metadata entry when there is metadata; the payloads go back once the write has completed. - private (HashEntry[] Entries, SerializedPayload[] Payloads) SerializeFields(IDictionary values, IDictionary? metadata = null) + private (HashEntry[] Entries, SerializedPayload[] Payloads) SerializeFields(IDictionary values, IDictionary? metadata, ICacheValueSerializer? writer) { var count = metadata is null ? values.Count : values.Count + 1; var entries = new HashEntry[count]; @@ -771,12 +842,12 @@ private ValueTask SetCoreAsync(CacheKey cacheKey, IDictionary SetCoreAsync(CacheKey cacheKey, IDictionaryBorrowed memory is safe here because every write awaits its command and the connection copies the value while writing it; pooled memory goes back only then. - private (RedisValue Value, SerializedPayload Payload) SerializeFieldValue(T? value) + private (RedisValue Value, SerializedPayload Payload) SerializeFieldValue(T? value, ICacheValueSerializer? writer) { if (_cacheNullValues && IsDefault(value)) { return (RedisValue.EmptyString, default); } + if (writer is not null) + { + var headered = SerializedPayload.Serialize(writer, _valueSerializers.FormatIdsOf(writer).Span, value!); + return (headered.Memory, headered); + } if (_memorySerializer is { } memory) { var payload = SerializedPayload.Serialize(memory, value); @@ -803,52 +879,8 @@ private ValueTask SetCoreAsync(CacheKey cacheKey, IDictionary>> GetCacheEntryForKeyAsync(CacheKey cacheKey, RedisKey redisKey, CancellationToken token) - { - token.ThrowIfCancellationRequested(); - // Per attempt, since a retry cannot re-run a drained transaction; boxed so the pipeline stays bool. - var read = new StrongBox?>(); - var committed = await _read.ExecuteAsync(static async (s, token) => - { - token.ThrowIfCancellationRequested(); - if (await s.Self.TryReadScriptAsync(ReadWithExpirationScript, [s.Key], s.Self._logReadScriptUnavailable, [(long)s.Self._fieldBatchSize * PagedReadFactor]).ConfigureAwait(false) is { } reply) - { - Interlocked.CompareExchange(ref s.Read.Value, new(await s.Self.ParseReplyAsync(s.Key, reply).ConfigureAwait(false)), null); - return true; - } - - // An HGETALL with a PTTL in one round trip. Not atomic: a key that expires between them reads as fields without a TTL. - var flags = s.Self.ReadFlagsFor(s.Key); - var hashEntriesTask = s.Self.Database.HashGetAllAsync(s.Key, flags); - var expireTimeToLiveTask = s.Self.Database.KeyTimeToLiveAsync(s.Key, flags); - try - { - var entries = await hashEntriesTask.ConfigureAwait(false); - var expiration = s.Self.Clock.ToDateTimeOffset(await expireTimeToLiveTask.ConfigureAwait(false)); - Interlocked.CompareExchange(ref s.Read.Value, new((entries, expiration)), null); - } - catch - { - hashEntriesTask.Forget(); - expireTimeToLiveTask.Forget(); - throw; - } - - return true; - }, - (Self: this, Key: redisKey, Read: read), - default, - token).ConfigureAwait(false); - if (!committed || read.Value is not { } result) - { - throw new InvalidOperationException("Unable to read from redis"); - } - - return ParseCacheEntry(cacheKey, redisKey, result.Value.Entries, result.Value.Expiration); - } - [SuppressMessage("SonarLint.Rule", "S3776")] - private ICacheEntry> ParseCacheEntry(CacheKey cacheKey, RedisKey redisKey, HashEntry[] hashEntries, DateTimeOffset? expireTime) + private ICacheEntry> ParseCacheEntry(CacheKey cacheKey, RedisKey redisKey, HashEntry[] hashEntries, DateTimeOffset? expireTime, bool sniff, ref int unreadable) { if (hashEntries.Length == 0) { @@ -863,16 +895,16 @@ private ValueTask SetCoreAsync(CacheKey cacheKey, IDictionary SetCoreAsync(CacheKey cacheKey, IDictionary>(v); + extendedProps = ReadValue>(v, sniff, ref unreadable); } continue; } - if (KnownFieldNames.IsSystemField(key)) - { - continue; - } - values.Add(key, DeserializeField(v)); + values.Add(name.ToString(), DeserializeField(v, sniff, ref unreadable)); + } + + if (unreadable != 0) + { + return Default(); } if (values.Count == 0 && (!_cacheNullValues || !hasEmptyMarker)) @@ -906,7 +939,7 @@ private ValueTask SetCoreAsync(CacheKey cacheKey, IDictionary(RedisValue value) + private T? DeserializeField(RedisValue value, bool sniff, ref int unreadable) { if (value.IsNull) { @@ -916,7 +949,7 @@ private ValueTask SetCoreAsync(CacheKey cacheKey, IDictionary(value); + var deserialized = ReadValue(value, sniff, ref unreadable); if (!_cacheNullValues && IsDefault(deserialized)) { return default; @@ -924,11 +957,50 @@ private ValueTask SetCoreAsync(CacheKey cacheKey, IDictionaryThe value by its format header when the call sniffs, else through the proxy; a header no serializer owns sets to its id, and a serializer that throws to ; both read as nothing. + private T? ReadValue(RedisValue value, bool sniff, ref int unreadable) + { + if (!sniff) + { + return _serializer.Deserialize(value); + } + + var outcome = _formats.Reader!.Read(((byte[]?)value)!); + if (outcome.IsMiss) + { + if (outcome.Failure is { } failure) + { + LogRedisHashCacheException(failure); + unreadable = unreadable == 0 ? SerializerFailed : unreadable; + } + else + { + unreadable = outcome.UnreadableFormat; + } + + return default; + } + + return outcome.Value; + } + + private ValueFormat FormatFor(CachePolicy? policy, bool forWrite) => _formats.For(policy, forWrite); + + // The hash is left alone: a node that cannot read a format may be a stale one, and removing one field would leave the rest looking like a whole hash. + private void TrackUnreadableRead(int unreadable) + { + if (unreadable is not 0 and not SerializerFailed) + { + TrackUnreadable(unreadable, Name); + } + } + private bool IsCacheHit(RedisValue value) => !value.IsNull && (value.Length() != 0 || _cacheNullValues); - private async ValueTask GetInnerAsync(CacheKey cacheKey, string field, CancellationToken token) + private async ValueTask GetInnerAsync(CacheKey cacheKey, string field, TFormat format, CancellationToken token) + where TFormat : struct, IValueFormat { var redisKey = ToRedisKey(cacheKey, token); if (!IsConnected) @@ -948,11 +1020,13 @@ private bool IsCacheHit(RedisValue value) => (Self: this, Key: redisKey, Field: field), RedisValue.Null, token).ConfigureAwait(false); - ret = DeserializeField(value); - found = IsCacheHit(value); + var unreadable = 0; + ret = DeserializeField(value, format.Sniff, ref unreadable); + found = unreadable == 0 && IsCacheHit(value); + TrackUnreadableRead(unreadable); operation.Stop(); } - catch (Exception ex) + catch (Exception ex) when (format.Handles(ex)) { operation.Stop(); LogRedisHashCacheException(ex); @@ -965,11 +1039,12 @@ private bool IsCacheHit(RedisValue value) => return ret; } - private async ValueTask> GetInnerAsync(CacheKey cacheKey, string[] fields, CancellationToken token) + private async ValueTask> GetInnerAsync(CacheKey cacheKey, string[] fields, TFormat format, CancellationToken token) + where TFormat : struct, IValueFormat { if (fields == null || fields.Length == 0) { - return await GetInnerAsync(cacheKey, token); + return await GetInnerAsync(cacheKey, format, token); } HashFields.ValidateFieldsForRead(fields); @@ -980,7 +1055,7 @@ private bool IsCacheHit(RedisValue value) => { return ret; } - var operation = StartOperation(); + var operation = StartOperation(nameof(GetInnerAsync)); bool found = false; try { @@ -994,20 +1069,27 @@ private bool IsCacheHit(RedisValue value) => token).ConfigureAwait(false); if (values.Length == fields.Length) { + var unreadable = 0; var dict = new Dictionary(fields.Length); var anyPresent = false; for (var i = 0; i < fields.Length; i++) { var v = values[i]; - dict.Add(fields[i], DeserializeField(v)); + dict.Add(fields[i], DeserializeField(v, format.Sniff, ref unreadable)); anyPresent |= IsCacheHit(v); } - ret = dict; - found = anyPresent; + + if (unreadable == 0) + { + ret = dict; + found = anyPresent; + } + + TrackUnreadableRead(unreadable); } operation.Stop(); } - catch (Exception ex) + catch (Exception ex) when (format.Handles(ex)) { operation.Stop(); LogRedisHashCacheException(ex); @@ -1020,7 +1102,8 @@ private bool IsCacheHit(RedisValue value) => return ret; } - private async ValueTask> GetInnerAsync(CacheKey cacheKey, CancellationToken token) + private async ValueTask> GetInnerAsync(CacheKey cacheKey, TFormat format, CancellationToken token) + where TFormat : struct, IValueFormat { var redisKey = ToRedisKey(cacheKey, token); IDictionary ret = Empty(); @@ -1030,7 +1113,7 @@ private bool IsCacheHit(RedisValue value) => return ret; } - var operation = StartOperation(); + var operation = StartOperation(nameof(GetInnerAsync)); bool found = false; try { @@ -1044,11 +1127,13 @@ private bool IsCacheHit(RedisValue value) => token).ConfigureAwait(false); if (hashEntries.Length > 0) { - (ret, found) = ReadFields(hashEntries); + var unreadable = 0; + (ret, found) = ReadFields(hashEntries, format.Sniff, ref unreadable); + TrackUnreadableRead(unreadable); } operation.Stop(); } - catch (Exception ex) + catch (Exception ex) when (format.Handles(ex)) { operation.Stop(); LogRedisHashCacheException(ex); @@ -1061,7 +1146,61 @@ private bool IsCacheHit(RedisValue value) => return ret; } - private async ValueTask>> GetInnerCacheEntryAsync(CacheKey cacheKey, CancellationToken token) + private ValueTask<(bool Found, IDictionary Values)> GetInnerWithFoundAsync(CacheKey cacheKey, CachePolicy? policy, CancellationToken token) + { + var format = FormatFor(policy, forWrite: false); + return format.Sniff ? GetInnerWithFoundAsync(cacheKey, format, token) : GetInnerWithFoundAsync(cacheKey, default, token); + } + + private async ValueTask<(bool Found, IDictionary Values)> GetInnerWithFoundAsync(CacheKey cacheKey, TFormat format, CancellationToken token) + where TFormat : struct, IValueFormat + { + var redisKey = ToRedisKey(cacheKey, token); + if (!IsConnected) + { + return (false, Empty()); + } + + var operation = StartOperation(nameof(GetOrAddAsync)); + bool found = false; + IDictionary ret = Empty(); + try + { + var hashEntries = await _read.ExecuteAsync(static (s, token) => + { + token.ThrowIfCancellationRequested(); + return s.Self.ReadAllEntriesAsync(s.Key); + }, + (Self: this, Key: redisKey), + [], + token).ConfigureAwait(false); + + if (hashEntries.Length == 0) + { + operation.Stop(); + return (false, ret); + } + + var unreadable = 0; + (ret, found) = ReadFields(hashEntries, format.Sniff, ref unreadable); + TrackUnreadableRead(unreadable); + operation.Stop(); + } + catch (Exception ex) when (format.Handles(ex)) + { + operation.Stop(); + LogRedisHashCacheException(ex); + } + finally + { + TrackRead(operation, found, redisKey); + } + + return (found, ret); + } + + private async ValueTask>> GetInnerCacheEntryAsync(CacheKey cacheKey, TFormat format, CancellationToken token) + where TFormat : struct, IValueFormat { var ret = Default(); if(!IsConnected) @@ -1070,13 +1209,13 @@ private bool IsCacheHit(RedisValue value) => } var redisKey = ToRedisKey(cacheKey, token); - var operation = StartOperation(); + var operation = StartOperation(nameof(GetInnerCacheEntryAsync)); try { - ret = await GetCacheEntryForKeyAsync(cacheKey, redisKey, token).ConfigureAwait(false); + ret = await GetCacheEntryForKeyAsync(cacheKey, redisKey, format, token).ConfigureAwait(false); operation.Stop(); } - catch (Exception ex) + catch (Exception ex) when (format.Handles(ex)) { operation.Stop(); LogRedisHashCacheException(ex); @@ -1089,8 +1228,56 @@ private bool IsCacheHit(RedisValue value) => return ret; } + private async ValueTask>> GetCacheEntryForKeyAsync(CacheKey cacheKey, RedisKey redisKey, TFormat format, CancellationToken token) + where TFormat : struct, IValueFormat + { + token.ThrowIfCancellationRequested(); + // Per attempt, since a retry cannot re-run a drained transaction; boxed so the pipeline stays bool. + var read = new StrongBox?>(); + var committed = await _read.ExecuteAsync(static async (s, token) => + { + token.ThrowIfCancellationRequested(); + if (await s.Self.TryReadScriptAsync(ReadWithExpirationScript, [s.Key], s.Self._logReadScriptUnavailable, [(long)s.Self._fieldBatchSize * PagedReadFactor]).ConfigureAwait(false) is { } reply) + { + Interlocked.CompareExchange(ref s.Read.Value, new(await s.Self.ParseReplyAsync(s.Key, reply).ConfigureAwait(false)), null); + return true; + } + + // An HGETALL with a PTTL in one round trip. Not atomic: a key that expires between them reads as fields without a TTL. + var flags = s.Self.ReadFlagsFor(s.Key); + var hashEntriesTask = s.Self.Database.HashGetAllAsync(s.Key, flags); + var expireTimeToLiveTask = s.Self.Database.KeyTimeToLiveAsync(s.Key, flags); + try + { + var entries = await hashEntriesTask.ConfigureAwait(false); + var expiration = s.Self.Clock.ToDateTimeOffset(await expireTimeToLiveTask.ConfigureAwait(false)); + Interlocked.CompareExchange(ref s.Read.Value, new((entries, expiration)), null); + } + catch + { + hashEntriesTask.Forget(); + expireTimeToLiveTask.Forget(); + throw; + } + + return true; + }, + (Self: this, Key: redisKey, Read: read), + default, + token).ConfigureAwait(false); + if (!committed || read.Value is not { } result) + { + throw new InvalidOperationException("Unable to read from redis"); + } + + var unreadable = 0; + var entry = ParseCacheEntry(cacheKey, redisKey, result.Value.Entries, result.Value.Expiration, format.Sniff, ref unreadable); + TrackUnreadableRead(unreadable); + return entry; + } + /// Owns : they go back to the pool on every exit, including one before the command starts. - private async ValueTask SetInnerAsync(CacheKey cacheKey, RedisKey redisKey, HashEntry[] hashEntries, SerializedPayload[] payloads, HashCacheSetOption setOption, DateTimeOffset expiration, CancellationToken token, WriteReport? report = null) + private async ValueTask SetInnerAsync(CacheKey cacheKey, RedisKey redisKey, HashEntry[] hashEntries, SerializedPayload[] payloads, HashCacheSetOption setOption, DateTimeOffset expiration, CancellationToken token, WriteReport? report = null, bool dropMetadata = false) { try { @@ -1123,7 +1310,7 @@ private async ValueTask SetInnerAsync(CacheKey cacheKey, RedisKey redis var operation = StartOperation(nameof(SetAsync)); try { - ret = await WriteAsync(redisKey, hashEntries, setOption, expiration, now, report, token).ConfigureAwait(false); + ret = await WriteAsync(redisKey, hashEntries, setOption, expiration, now, report, dropMetadata, token).ConfigureAwait(false); operation.Stop(); } catch (Exception ex) @@ -1172,7 +1359,7 @@ private bool TryChunkedWriteKeys(RedisKey redisKey, HashCacheSetOption setOption return SameSlot(redisKey, staging, _logger); } - private async ValueTask WriteAsync(RedisKey redisKey, HashEntry[] hashEntries, HashCacheSetOption setOption, DateTimeOffset expiration, DateTimeOffset now, WriteReport? report, CancellationToken token) + private async ValueTask WriteAsync(RedisKey redisKey, HashEntry[] hashEntries, HashCacheSetOption setOption, DateTimeOffset expiration, DateTimeOffset now, WriteReport? report, bool dropMetadata, CancellationToken token) { if (expiration < now || hashEntries.Length == 0) { @@ -1185,7 +1372,7 @@ private async ValueTask WriteAsync(RedisKey redisKey, HashEntry[] hashEntr return await _write.ExecuteAsync(static (s, token) => s.Self.WriteInChunksAsync(s.Key, s.Staging, s.Entries, s.Option, s.Expiration, s.Report, token), (Self: this, Key: redisKey, Staging: staging, Entries: hashEntries, Option: setOption, Expiration: expiration, Report: report), default, token).ConfigureAwait(false); } - return await WriteInTransactionAsync(redisKey, hashEntries, setOption, expiration, report, token).ConfigureAwait(false); + return await WriteInTransactionAsync(redisKey, hashEntries, setOption, expiration, report, dropMetadata, token).ConfigureAwait(false); } private ValueTask DeleteKeyAsync(RedisKey redisKey, CancellationToken token) => @@ -1199,7 +1386,7 @@ private ValueTask DeleteKeyAsync(RedisKey redisKey, CancellationToken toke default, token); - private async ValueTask WriteInTransactionAsync(RedisKey redisKey, HashEntry[] hashEntries, HashCacheSetOption setOption, DateTimeOffset expiration, WriteReport? report, CancellationToken token) + private async ValueTask WriteInTransactionAsync(RedisKey redisKey, HashEntry[] hashEntries, HashCacheSetOption setOption, DateTimeOffset expiration, WriteReport? report, bool dropMetadata, CancellationToken token) { await EnsureConnectedAsync(_write, token).ConfigureAwait(false); var transaction = Database.CreateTransaction(); @@ -1209,6 +1396,12 @@ private async ValueTask WriteInTransactionAsync(RedisKey redisKey, HashEnt } transaction.HashSetAsync(redisKey, hashEntries, CommandFlags.DemandMaster).Forget(); + if (dropMetadata && setOption == HashCacheSetOption.HashReplace) + { + // In the write's own transaction, where a concurrent writer's newer metadata cannot be hit. + transaction.HashDeleteAsync(redisKey, KnownFieldNames.MetadataKey, CommandFlags.DemandMaster).Forget(); + } + Task? counted = null; if (report is not null) { @@ -1471,26 +1664,30 @@ private RedisKey ToRedisKey(CacheKey cacheKey, CancellationToken token = default } /// The hash's user fields, and whether they count as a hit: a value other than the null marker, or the empty marker while nulls are cached. - private (Dictionary Values, bool Found) ReadFields(HashEntry[] hashEntries) + private (Dictionary Values, bool Found) ReadFields(HashEntry[] hashEntries, bool sniff, ref int unreadable) { var values = new Dictionary(); var hasEmptyMarker = false; var anyValue = false; foreach (var hashEntry in hashEntries) { - var name = hashEntry.Name.ToString(); - if (name == KnownFieldNames.MetadataKey) + if (KnownFieldNames.IsMetadata(hashEntry.Name)) { hasEmptyMarker = hashEntry.Value.Length() == 0; continue; } - if (KnownFieldNames.IsSystemField(name)) + if (KnownFieldNames.IsSystemField(hashEntry.Name)) { continue; } var v = hashEntry.Value; anyValue |= IsCacheHit(v); - values.Add(name, DeserializeField(v)); + values.Add(hashEntry.Name.ToString(), DeserializeField(v, sniff, ref unreadable)); + } + + if (unreadable != 0) + { + return (new Dictionary(), false); } return (values, anyValue || (hasEmptyMarker && _cacheNullValues)); diff --git a/src/UiPath.Caching/Serialization/IValueFormat.cs b/src/UiPath.Caching/Serialization/IValueFormat.cs new file mode 100644 index 00000000..ad70300f --- /dev/null +++ b/src/UiPath.Caching/Serialization/IValueFormat.cs @@ -0,0 +1,12 @@ +namespace UiPath.Caching.Serialization; + +/// What a Redis path asks of a call's format; implemented by structs, so a path compiled for carries none of the header steps. +internal interface IValueFormat +{ + ICacheValueSerializer? Writer { get; } + + bool Sniff { get; } + + /// Whether a read that failed with logs it and reads as a miss. + bool Handles(Exception exception); +} diff --git a/src/UiPath.Caching/Serialization/PlainFormat.cs b/src/UiPath.Caching/Serialization/PlainFormat.cs new file mode 100644 index 00000000..d36aa100 --- /dev/null +++ b/src/UiPath.Caching/Serialization/PlainFormat.cs @@ -0,0 +1,11 @@ +namespace UiPath.Caching.Serialization; + +/// The JSON default: no header written, none looked for. +internal readonly struct PlainFormat : IValueFormat +{ + public ICacheValueSerializer? Writer => null; + + public bool Sniff => false; + + public bool Handles(Exception exception) => true; +} diff --git a/src/UiPath.Caching/Serialization/ReadOutcome.cs b/src/UiPath.Caching/Serialization/ReadOutcome.cs new file mode 100644 index 00000000..29737541 --- /dev/null +++ b/src/UiPath.Caching/Serialization/ReadOutcome.cs @@ -0,0 +1,29 @@ +namespace UiPath.Caching.Serialization; + +internal readonly struct ReadOutcome +{ + private ReadOutcome(T? value, int unreadableFormat, Exception? failure) + { + Value = value; + UnreadableFormat = unreadableFormat; + Failure = failure; + } + + public T? Value { get; } + + /// The format id no registered serializer reads, or -1 for a header cut short; 0 otherwise. Only such data is removed. + public int UnreadableFormat { get; } + + /// What a registered serializer threw; null otherwise. A miss that keeps the key, since the data may be fine for another version of the type. + public Exception? Failure { get; } + + public bool IsUnreadable => UnreadableFormat != 0; + + public bool IsMiss => IsUnreadable || Failure is not null; + + public static ReadOutcome Read(T? value) => new(value, 0, null); + + public static ReadOutcome Unreadable(int formatId) => new(default, formatId, null); + + public static ReadOutcome Failed(Exception failure) => new(default, 0, failure); +} diff --git a/src/UiPath.Caching/Serialization/ValueFormat.cs b/src/UiPath.Caching/Serialization/ValueFormat.cs new file mode 100644 index 00000000..88f5d1b1 --- /dev/null +++ b/src/UiPath.Caching/Serialization/ValueFormat.cs @@ -0,0 +1,9 @@ +namespace UiPath.Caching.Serialization; + +/// What one call writes with and whether its reads look for a format header; the default is JSON with no sniffing. +internal readonly record struct ValueFormat(ICacheValueSerializer? Writer, bool Sniff) : IValueFormat +{ + public bool IsDefault => Writer is null && !Sniff; + + public bool Handles(Exception exception) => exception is not OutOfMemoryException; +} diff --git a/src/UiPath.Caching/Serialization/ValueFormatResolver.cs b/src/UiPath.Caching/Serialization/ValueFormatResolver.cs new file mode 100644 index 00000000..beb1b406 --- /dev/null +++ b/src/UiPath.Caching/Serialization/ValueFormatResolver.cs @@ -0,0 +1,36 @@ +namespace UiPath.Caching.Serialization; + +/// Decides per call which serializer a write uses and whether a read looks for a format header. +internal sealed class ValueFormatResolver +{ + private readonly ValueSerializerRegistry _registry; + private readonly CachePolicy _defaultPolicy; + + public ValueFormatResolver(ISerializerProxy proxy, ValueSerializerRegistry registry, CachePolicy defaultPolicy) + { + _registry = registry; + _defaultPolicy = defaultPolicy; + Reader = registry.IsEmpty ? null : new ValueReader(proxy, registry); + } + + /// Null while no serializer is registered: nothing then reads by header. + public ValueReader? Reader { get; } + + /// A call without a policy takes the default policy's, and a policy that sets no write serializer falls back to the default's. A write that names a serializer nobody registered throws. + public ValueFormat For(CachePolicy? policy, bool forWrite) + { + var effective = WriteSource(policy); + if (Reader is null && effective.Serializer is null) + { + return default; + } + + return new(forWrite ? WriterOf(effective) : null, Reader is { } reader && reader.SniffsFor(effective)); + } + + // Per field, as PolicyDuration does, without building a merged policy: the one that supplies Write supplies its resolved slot too. + private CachePolicy WriteSource(CachePolicy? policy) => + policy is null || (policy.Serializer?.Write is null && policy.WriteSerializer is null) ? _defaultPolicy : policy; + + private ICacheValueSerializer? WriterOf(CachePolicy effective) => _registry.WriteSerializerFor(effective); +} diff --git a/src/UiPath.Caching/Serialization/ValueReader.cs b/src/UiPath.Caching/Serialization/ValueReader.cs new file mode 100644 index 00000000..54763a55 --- /dev/null +++ b/src/UiPath.Caching/Serialization/ValueReader.cs @@ -0,0 +1,52 @@ +using System.Buffers; + +namespace UiPath.Caching.Serialization; + +/// Reads a stored value by its format header: headerless bytes go to the JSON proxy, headered ones to the registered serializer of that id. +internal sealed class ValueReader(ISerializerProxy json, ValueSerializerRegistry registry) +{ + public const string UnreadableMetric = "cache.serializer.unreadable"; + + // The one proxy whose output is known to be JSON; a subclass or another proxy may store bytes that start with a header byte. + private readonly bool _jsonProxyIsDefault = json.GetType() == typeof(SystemJsonByteSerializerProxy); + + // Only a policy that writes a non-JSON format puts headers where a custom proxy could not tell them from its own bytes; Read lists never open the gate. + public bool SniffsFor(CachePolicy? policy) => + !registry.IsEmpty && (_jsonProxyIsDefault || WritesHeaders(policy)); + + public ReadOutcome Read(byte[] value) + { + if (!ValueHeader.TryRead(value, out var formatId, out var headerLength)) + { + try + { + return ReadOutcome.Read(json.Deserialize(value)); + } + catch (OutOfMemoryException ex) + { + // Headerless bytes read as they do with no registry, where OutOfMemoryException is a logged miss. + return ReadOutcome.Failed(ex); + } + } + + if (formatId < 0 || !registry.TryGetByFormat(formatId, out var serializer)) + { + return ReadOutcome.Unreadable(formatId); + } + + try + { + return ReadOutcome.Read(serializer.Deserialize(formatId, new ReadOnlySequence(value, headerLength, value.Length - headerLength))); + } + catch (Exception ex) when (ex is not OutOfMemoryException) + { + // A miss that keeps the key: a version of T another node writes, or a disposed serializer at shutdown, says nothing about the data. + return ReadOutcome.Failed(ex); + } + } + + private static bool WritesHeaders(CachePolicy? policy) => + policy is not null + && (policy.WriteSerializer is not null + || (policy.Serializer?.Write is { } name && !string.Equals(name, CacheValueFormats.JsonName, StringComparison.OrdinalIgnoreCase))); +} diff --git a/tests/UiPath.Caching.Tests/Fakes/PassthroughValueSerializer.cs b/tests/UiPath.Caching.Tests/Fakes/PassthroughValueSerializer.cs new file mode 100644 index 00000000..542ab8b6 --- /dev/null +++ b/tests/UiPath.Caching.Tests/Fakes/PassthroughValueSerializer.cs @@ -0,0 +1,54 @@ +using System.Buffers; +using System.Text; +using System.Text.Json; + +namespace UiPath.Caching.Tests.Fakes; + +/// Writes a array or string as its raw bytes under its first id, anything else as JSON text, and reads them back. +internal sealed class PassthroughValueSerializer(params int[] ids) : ICacheValueSerializer +{ + private int _serialized; + private int _deserialized; + + public IReadOnlyList FormatIds { get; } = ids.Length == 0 ? [0x10] : ids; + + public int Serialized => Volatile.Read(ref _serialized); + + public int Deserialized => Volatile.Read(ref _deserialized); + + public int LastFormatId { get; private set; } + + public byte[] LastBody { get; private set; } = []; + + public int Serialize(T value, IBufferWriter target) + { + Interlocked.Increment(ref _serialized); + var bytes = value switch + { + byte[] raw => raw, + string text => Encoding.UTF8.GetBytes(text), + _ => JsonSerializer.SerializeToUtf8Bytes(value), + }; + target.Write(bytes); + return FormatIds[0]; + } + + public T? Deserialize(int formatId, ReadOnlySequence source) + { + Interlocked.Increment(ref _deserialized); + LastFormatId = formatId; + var body = source.ToArray(); + LastBody = body; + if (typeof(T) == typeof(byte[])) + { + return (T)(object)body; + } + + if (typeof(T) == typeof(string)) + { + return (T)(object)Encoding.UTF8.GetString(body); + } + + return JsonSerializer.Deserialize(body); + } +} diff --git a/tests/UiPath.Caching.Tests/Fakes/PeerCluster.cs b/tests/UiPath.Caching.Tests/Fakes/PeerCluster.cs index f14214dc..beb37b08 100644 --- a/tests/UiPath.Caching.Tests/Fakes/PeerCluster.cs +++ b/tests/UiPath.Caching.Tests/Fakes/PeerCluster.cs @@ -468,7 +468,7 @@ public ValueTask RemoveFieldsVersionedAsync(CacheKey cacheKey, string[] ValueTask ISpanKeyHashCache.RemoveFieldsAsync(CacheKey cacheKey, string[] fields, CancellationToken token) => RemoveFieldsAfter(cacheKey.Name, fields, counted: false); - public bool TryDecodeFields(IReadOnlyDictionary encoded, [MaybeNullWhen(false)] out Dictionary values) + public bool TryDecodeFields(IReadOnlyDictionary encoded, CachePolicy? policy, [MaybeNullWhen(false)] out Dictionary values) { if (FailsDecode) { diff --git a/tests/UiPath.Caching.Tests/Fakes/ThrowingValueSerializer.cs b/tests/UiPath.Caching.Tests/Fakes/ThrowingValueSerializer.cs new file mode 100644 index 00000000..100fbfe4 --- /dev/null +++ b/tests/UiPath.Caching.Tests/Fakes/ThrowingValueSerializer.cs @@ -0,0 +1,15 @@ +using System.Buffers; + +namespace UiPath.Caching.Tests.Fakes; + +/// Owns its ids and throws on every read, as a serializer does on input it cannot parse; picks what it throws. +internal sealed class ThrowingValueSerializer(params int[] ids) : ICacheValueSerializer +{ + public Exception? Failure { get; init; } + + public IReadOnlyList FormatIds { get; } = ids.Length == 0 ? [0x10] : ids; + + public int Serialize(T value, IBufferWriter target) => throw new NotSupportedException(); + + public T? Deserialize(int formatId, ReadOnlySequence source) => throw Failure ?? new InvalidDataException("corrupt"); +} diff --git a/tests/UiPath.Caching.Tests/InnerTierTests.cs b/tests/UiPath.Caching.Tests/InnerTierTests.cs index add59048..af679ff4 100644 --- a/tests/UiPath.Caching.Tests/InnerTierTests.cs +++ b/tests/UiPath.Caching.Tests/InnerTierTests.cs @@ -95,7 +95,7 @@ public ValueTask SetVersionedAsync(CacheKey cacheKey, IDictio public ValueTask RemoveFieldsVersionedAsync(CacheKey cacheKey, string[] fields, CancellationToken token = default) => throw new NotSupportedException(); - public bool TryDecodeFields(IReadOnlyDictionary encoded, [System.Diagnostics.CodeAnalysis.MaybeNullWhen(false)] out Dictionary values) => + public bool TryDecodeFields(IReadOnlyDictionary encoded, CachePolicy? policy, [System.Diagnostics.CodeAnalysis.MaybeNullWhen(false)] out Dictionary values) => throw new NotSupportedException(); } } diff --git a/tests/UiPath.Caching.Tests/Redis/KnownFieldNamesTests.cs b/tests/UiPath.Caching.Tests/Redis/KnownFieldNamesTests.cs new file mode 100644 index 00000000..d8556708 --- /dev/null +++ b/tests/UiPath.Caching.Tests/Redis/KnownFieldNamesTests.cs @@ -0,0 +1,76 @@ +using System.Text; +using StackExchange.Redis; + +namespace UiPath.Caching.Tests.Redis; + +public class KnownFieldNamesTests +{ + public static TheoryData UserFields() => + [ + "_metadatb_", + "_vXrsion_", + "_Version_", + "_METADATA_", + "_version", + "version_", + "_version_ ", + "_metadata_1", + "metadata", + "", + "x", + ]; + + [Fact] + public void Version_and_metadata_are_system_fields_as_bytes() + { + KnownFieldNames.IsSystemField("_version_"u8).Should().BeTrue(); + KnownFieldNames.IsSystemField("_metadata_"u8).Should().BeTrue(); + KnownFieldNames.IsVersion("_version_"u8).Should().BeTrue(); + KnownFieldNames.IsMetadata("_metadata_"u8).Should().BeTrue(); + KnownFieldNames.IsVersion("_metadata_"u8).Should().BeFalse(); + KnownFieldNames.IsMetadata("_version_"u8).Should().BeFalse(); + } + + [Theory] + [MemberData(nameof(UserFields))] + public void Other_fields_are_not_system_fields_as_bytes(string field) + { + var bytes = Encoding.UTF8.GetBytes(field).AsSpan(); + + KnownFieldNames.IsSystemField(bytes).Should().BeFalse(field); + KnownFieldNames.IsVersion(bytes).Should().BeFalse(field); + KnownFieldNames.IsMetadata(bytes).Should().BeFalse(field); + } + + [Theory] + [MemberData(nameof(UserFields))] + public void Byte_and_string_checks_agree(string field) + { + KnownFieldNames.IsSystemField(Encoding.UTF8.GetBytes(field).AsSpan()).Should().Be(KnownFieldNames.IsSystemField(field)); + } + + [Fact] + public void A_name_Redis_hands_back_is_matched_whatever_its_storage() + { + RedisValue raw = Encoding.UTF8.GetBytes("_version_"); + RedisValue text = "_version_"; + + KnownFieldNames.IsVersion(raw).Should().BeTrue(); + KnownFieldNames.IsVersion(text).Should().BeTrue(); + KnownFieldNames.IsMetadata(raw).Should().BeFalse(); + KnownFieldNames.IsSystemField(raw).Should().BeTrue(); + KnownFieldNames.IsSystemField((RedisValue)Encoding.UTF8.GetBytes("_metadata_")).Should().BeTrue(); + KnownFieldNames.IsSystemField((RedisValue)"_metadata_").Should().BeTrue(); + KnownFieldNames.IsSystemField((RedisValue)Encoding.UTF8.GetBytes("_metadatb_")).Should().BeFalse(); + KnownFieldNames.IsSystemField((RedisValue)"_vXrsion_").Should().BeFalse(); + KnownFieldNames.IsVersion((RedisValue)Encoding.UTF8.GetBytes("_metadata_")).Should().BeFalse(); + } + + [Fact] + public void A_numeric_or_empty_name_is_not_a_system_field() + { + KnownFieldNames.IsSystemField((RedisValue)12).Should().BeFalse(); + KnownFieldNames.IsSystemField(RedisValue.EmptyString).Should().BeFalse(); + KnownFieldNames.IsSystemField(RedisValue.Null).Should().BeFalse(); + } +} diff --git a/tests/UiPath.Caching.Tests/Redis/PeerFieldUpdateIntegrationTests.cs b/tests/UiPath.Caching.Tests/Redis/PeerFieldUpdateIntegrationTests.cs index bfb39f46..fa87ed97 100644 --- a/tests/UiPath.Caching.Tests/Redis/PeerFieldUpdateIntegrationTests.cs +++ b/tests/UiPath.Caching.Tests/Redis/PeerFieldUpdateIntegrationTests.cs @@ -2,6 +2,7 @@ using StackExchange.Redis; using StackExchange.Redis.Profiling; using UiPath.Caching.Config; +using UiPath.Caching.Tests.Fakes; namespace UiPath.Caching.Tests.Redis; @@ -43,6 +44,30 @@ public async Task A_peer_serves_a_written_field_from_memory_without_reading_the_ (await Counted(key)).Should().Be(2); } + [Fact] + public async Task A_peer_applies_fields_written_in_the_policy_serializers_format_from_memory() + { + Assert.SkipUnless(fixture.Enabled, "Set RUN_REDIS_INTEGRATION_TESTS=1 (Docker required) to run."); + var writer = Node("a", peerFieldUpdates: true, headered: true); + var peer = Node("b", peerFieldUpdates: true, headered: true); + await WarmAsync(writer, peer); + var key = Unique(); + (await writer.Hashes.SetAsync(key, Fields(("a", "1"), ("b", "2")), TimeSpan.FromMinutes(10), Ct)).Should().BeTrue(); + _ = peer.Profiler.Session.FinishProfiling().ToList(); + (await peer.Hashes.GetAsync(key, Ct)).Should().HaveCount(2); + await EventsConsumedAsync(peer); + (await peer.Hashes.GetAsync(key, Ct)).Should().HaveCount(2); + _ = peer.Profiler.Session.FinishProfiling().ToList(); + + (await writer.Hashes.SetAsync(key, Fields(("c", "3")), new HashCacheEntryOptions(TimeToLive: TimeSpan.FromMinutes(10), SetOption: HashCacheSetOption.HashReplace), Ct)).Should().BeTrue(); + var seen = await UntilAsync(async () => (await peer.Hashes.GetAsync(key, Ct)).ContainsKey("c")); + + seen.Should().BeTrue("the peer's event is applied to its copy"); + (await peer.Hashes.GetAsync(key, Ct)).Should().BeEquivalentTo(Fields(("a", "1"), ("b", "2"), ("c", "3"))); + peer.Profiler.Session.FinishProfiling().Select(c => c.Command).Should().NotContain(InnerReadCommands); + (await RawField(key, "c")).Should().Equal(0x10, (byte)'3'); + } + [Fact] public async Task Two_nodes_merging_and_applying_on_the_same_hashes_under_load_never_stall() { @@ -431,6 +456,18 @@ private async Task HashLength(string key) return 0; } + [System.Diagnostics.CodeAnalysis.SuppressMessage("Major Code Smell", "S1751:Loops with at most one iteration should be refactored", Justification = "The first key matching the pattern is the one wanted.")] + private async Task RawField(string key, string field) + { + await using var redis = await ConnectionMultiplexer.ConnectAsync(fixture.ConnectionString); + await foreach (var found in redis.GetServers()[0].KeysAsync(pattern: $"*{key}")) + { + return await redis.GetDatabase().HashGetAsync(found, field); + } + + return null; + } + private async Task SetRaw(string key, string field, long value) { await using var redis = await ConnectionMultiplexer.ConnectAsync(fixture.ConnectionString); @@ -454,7 +491,7 @@ private async Task SetRaw(string key, string field, long value) return null; } - private Peer Node(string source, bool peerFieldUpdates, int fieldBatch = 1000, bool readScripts = false) + private Peer Node(string source, bool peerFieldUpdates, int fieldBatch = 1000, bool readScripts = false, bool headered = false) { var profiler = new CommandCountingProfiler(); var provider = new ServiceCollection() @@ -475,11 +512,19 @@ private Peer Node(string source, bool peerFieldUpdates, int fieldBatch = 1000, b }); b.AddBroadcast(); b.AddInMemoryRedis(o => o.PeerFieldUpdates = peerFieldUpdates); + if (headered) + { + b.AddValueSerializer("fast", _ => new PassthroughValueSerializer(0x10)); + } }, o => { o.AppShortName = _app; o.SourceUri = new Uri($"urn:{source}-{_app}"); + if (headered) + { + o.DefaultCachePolicy = new CachePolicy { Serializer = new SerializerPolicy { Write = "fast" } }; + } }) .BuildServiceProvider(); _nodes.Add(provider); diff --git a/tests/UiPath.Caching.Tests/Redis/RedisCacheSerializerTests.cs b/tests/UiPath.Caching.Tests/Redis/RedisCacheSerializerTests.cs new file mode 100644 index 00000000..4715f452 --- /dev/null +++ b/tests/UiPath.Caching.Tests/Redis/RedisCacheSerializerTests.cs @@ -0,0 +1,724 @@ +using System.Text; +using Microsoft.Extensions.Logging.Abstractions; +using Microsoft.Extensions.Options; +using StackExchange.Redis; +using UiPath.Caching.Config; +using UiPath.Caching.Policies; +using UiPath.Caching.Serialization; +using UiPath.Caching.Tests.Fakes; +using UiPath.Caching.Tests.Telemetry; + +namespace UiPath.Caching.Tests.Redis; + +[Collection(WriterPoolCollection.Name)] +public class RedisCacheSerializerTests(ITestContextAccessor testContextAccessor) +{ + private const string UnreadableMetric = "cache.serializer.unreadable"; + + private readonly IDatabase _database = Substitute.For(); + private readonly RecordingTelemetryProvider _telemetry = new(); + private readonly RecordingResiliencePipeline _write = new(); + private readonly PassthroughValueSerializer _fast = new(0x10); + private readonly ITransaction _transaction = Substitute.For(); + + private CancellationToken Token => testContextAccessor.Current.CancellationToken; + + public static TheoryData WritePaths() => + ["set", "set many", "try add", "get or add", "get or add with expiration", "get or add result", "get or add batch", "get or add batch with state"]; + + public static TheoryData Scenarios() => ["no registry", "registry, json write", "registry, fast write"]; + + [Fact] + public async Task Policy_serializer_writes_a_headered_value() + { + var sut = Create(out var registry); + var captured = CaptureWrites(); + + await sut.SetAsync("k", "hello", TimeSpan.FromMinutes(1), ResolvedPolicy(registry, "fast"), Token); + + captured.Should().ContainSingle().Which.Should().Equal(Header(0x10, "hello")); + _fast.Serialized.Should().Be(1); + } + + [Theory] + [MemberData(nameof(WritePaths))] + public async Task Every_write_path_writes_the_policy_serializer(string path) + { + var sut = Create(out var registry); + var captured = CaptureWrites(); + + await Write(sut, path, ResolvedPolicy(registry, "fast")); + + captured.Should().NotBeEmpty(); + captured.Should().OnlyContain(value => value[0] == 0x10); + } + + [Fact] + public async Task Default_policy_writes_byte_identical_json() + { + var sut = Create(out _); + var captured = CaptureWrites(); + + await sut.SetAsync("k", "hello", TimeSpan.FromMinutes(1), policy: null, Token); + await sut.SetAsync("k", "hello", TimeSpan.FromMinutes(1), new CachePolicy(), Token); + await sut.SetAsync("k", "hello", TimeSpan.FromMinutes(1), new CachePolicy { Serializer = new SerializerPolicy { Write = "json" } }, Token); + + var json = new SystemJsonByteSerializerProxy().Serialize("hello"); + captured.Should().HaveCount(3); + captured.Should().OnlyContain(value => value.SequenceEqual(json!)); + _fast.Serialized.Should().Be(0); + } + + [Fact] + public async Task A_default_policy_that_names_a_serializer_applies_when_the_call_has_no_policy() + { + var sut = Create(FastRegistry(), defaultPolicy: new CachePolicy { Serializer = new SerializerPolicy { Write = "fast" } }); + var captured = CaptureWrites(); + + await sut.SetAsync("k", "hello", TimeSpan.FromMinutes(1), policy: null, Token); + + captured.Should().ContainSingle().Which.Should().Equal(Header(0x10, "hello")); + } + + [Fact] + public async Task Headered_value_reads_under_a_default_policy() + { + var sut = Create(out _); + _database.StringGetAsync(Arg.Any(), Arg.Any()).Returns((RedisValue)Header(0x10, "stored")); + + var value = await sut.GetAsync("k", policy: null, Token); + + value.Should().Be("stored"); + _fast.LastFormatId.Should().Be(0x10); + } + + [Fact] + public async Task Json_still_reads_under_a_policy_that_names_a_serializer() + { + var sut = Create(out var registry); + var proxy = new SystemJsonByteSerializerProxy(); + _database.StringGetAsync(Arg.Any(), Arg.Any()).Returns((RedisValue)proxy.Serialize("json value")); + + var value = await sut.GetAsync("k", ResolvedPolicy(registry, "fast"), Token); + + value.Should().Be("json value"); + _fast.Deserialized.Should().Be(0); + } + + [Fact] + public async Task Unknown_format_is_a_miss_and_the_key_is_removed_if_it_still_holds_the_bytes_read() + { + var sut = Create(out _); + var conditions = CaptureConditions(); + byte[] stored = [0x1E, (byte)'a', (byte)'b']; + _database.StringGetAsync(Arg.Any(), Arg.Any()).Returns((RedisValue)stored); + + var value = await sut.GetAsync("k", policy: null, Token); + + value.Should().BeNull(); + _ = _transaction.Received(1).KeyDeleteAsync((RedisKey)"test:str:k", CommandFlags.DemandMaster); + _ = _database.DidNotReceive().KeyDeleteAsync(Arg.Any(), Arg.Any()); + conditions.Should().ContainSingle().Which.Should().Be(Condition.StringEqual("test:str:k", stored).ToString()); + } + + [Fact] + public async Task A_key_that_changed_after_the_read_is_not_deleted_and_the_call_is_still_a_miss() + { + var sut = Create(out _); + _database.StringGetAsync(Arg.Any(), Arg.Any()).Returns((RedisValue)new byte[] { 0x1E, 1, 2 }); + _transaction.ExecuteAsync(Arg.Any()).Returns(false); + + var value = await sut.GetAsync("k", policy: null, Token); + + value.Should().BeNull(); + _ = _database.DidNotReceive().KeyDeleteAsync(Arg.Any(), Arg.Any()); + _telemetry.Metrics.Should().ContainSingle(m => m.Name == UnreadableMetric); + } + + [Fact] + public async Task The_conditional_removal_is_reported_as_a_remove() + { + var sut = Create(out _); + _database.StringGetAsync(Arg.Any(), Arg.Any()).Returns((RedisValue)new byte[] { 0x1E, 1, 2 }); + + await sut.GetAsync("k", policy: null, Token); + + _telemetry.Metrics.Should().Contain(m => m.Name == "Caching.Stats.Hits.Redis.RemoveAsync.String"); + var metric = _telemetry.Metrics.Should().ContainSingle(m => m.Name == UnreadableMetric).Subject; + metric.Value.Should().Be(1); + metric.Properties.Should().Contain("format", "0x1E").And.Contain("cache", "Redis"); + } + + [Fact] + public async Task An_unreadable_key_that_cannot_be_removed_is_still_a_miss() + { + var sut = Create(out _); + _database.StringGetAsync(Arg.Any(), Arg.Any()).Returns((RedisValue)new byte[] { 0x1E, 1, 2 }); + _transaction.ExecuteAsync(Arg.Any()).Returns(Task.FromException(new RedisException("down"))); + + var value = await sut.GetAsync("k", policy: null, Token); + + value.Should().BeNull(); + } + + [Fact] + public async Task An_unreadable_key_is_removed_before_the_generator_runs() + { + var sut = Create(out _); + var order = new List(); + _database.StringGetAsync(Arg.Any(), Arg.Any()).Returns((RedisValue)new byte[] { 0x1E, 1, 2 }); + _transaction.ExecuteAsync(Arg.Any()).Returns(_ => + { + order.Add("delete"); + return true; + }); + _database.StringSetAsync(Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any()).Returns(true); + + Task Generate(CancellationToken token) + { + order.Add("generate"); + return Task.FromResult("fresh"); + } + + var value = await sut.GetOrAddAsync("k", Generate, TimeSpan.FromMinutes(1), policy: null, Token); + + value.Should().Be("fresh"); + order.Should().Equal("delete", "generate"); + } + + [Fact] + public async Task A_headered_value_whose_serializer_throws_is_a_miss_and_the_key_stays() + { + var sut = Create(new ValueSerializerRegistry([("bad", new ThrowingValueSerializer(), false)])); + _database.StringGetAsync(Arg.Any(), Arg.Any()).Returns((RedisValue)new byte[] { 0x10, 1, 2 }); + + var value = await sut.GetAsync("k", policy: null, Token); + + value.Should().BeNull(); + AssertNothingDeleted(); + _telemetry.Metrics.Should().NotContain(m => m.Name == UnreadableMetric); + } + + [Fact] + public async Task An_out_of_memory_from_a_serializer_propagates_from_every_read_path_and_runs_no_generator() + { + var sut = Create(new ValueSerializerRegistry([("bad", new ThrowingValueSerializer { Failure = new OutOfMemoryException() }, false)])); + _database.StringGetAsync(Arg.Any(), Arg.Any()).Returns((RedisValue)new byte[] { 0x10, 1, 2 }); + _database.StringGetAsync(Arg.Any(), Arg.Any()).Returns([(RedisValue)new byte[] { 0x10, 1, 2 }, RedisValue.Null]); + var generated = 0; + Task Generate(CancellationToken token) + { + generated++; + return Task.FromResult("fresh"); + } + + await FluentActions.Awaiting(() => sut.GetAsync("k", policy: null, Token).AsTask()).Should().ThrowAsync(); + await FluentActions.Awaiting(() => sut.GetAsync(["a", "b"], policy: null, Token).AsTask()).Should().ThrowAsync(); + await FluentActions.Awaiting(() => sut.GetCacheEntryAsync("k", policy: null, Token).AsTask()).Should().ThrowAsync(); + await FluentActions.Awaiting(() => sut.GetCacheEntriesAsync(["a", "b"], policy: null, Token).AsTask()).Should().ThrowAsync(); + await FluentActions.Awaiting(() => sut.GetOrAddAsync("k", Generate, TimeSpan.FromMinutes(1), policy: null, Token).AsTask()).Should().ThrowAsync(); + await FluentActions.Awaiting(() => sut.GetOrAddResultAsync("k", Generate, TimeSpan.FromMinutes(1), policy: null, Token).AsTask()).Should().ThrowAsync(); + + generated.Should().Be(0); + } + + [Fact] + public async Task A_headered_value_whose_serializer_throws_is_a_miss_that_keeps_the_key_on_every_read_path() + { + var sut = Create(new ValueSerializerRegistry([("bad", new ThrowingValueSerializer(), false)])); + _database.StringGetAsync(Arg.Any(), Arg.Any()).Returns((RedisValue)new byte[] { 0x10, 1, 2 }); + _database.StringGetAsync(Arg.Any(), Arg.Any()).Returns([(RedisValue)new byte[] { 0x10, 1, 2 }, RedisValue.Null]); + _database.StringSetAsync(Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any()).Returns(true); + + (await sut.GetAsync(["a", "b"], policy: null, Token)).Select(v => v.Value).Should().Equal(null, null); + (await sut.GetCacheEntryAsync("k", policy: null, Token)).Found.Should().BeFalse(); + (await sut.GetCacheEntriesAsync(["a", "b"], policy: null, Token)).Select(v => v.Value.Found).Should().Equal(false, false); + Task Generate(CancellationToken token) => Task.FromResult("fresh"); + (await sut.GetOrAddAsync("k", Generate, TimeSpan.FromMinutes(1), policy: null, Token)).Should().Be("fresh"); + + AssertNothingDeleted(); + _telemetry.Metrics.Should().NotContain(m => m.Name == UnreadableMetric); + } + + [Fact] + public async Task An_unknown_format_on_the_entry_reads_is_a_miss_and_removed_if_unchanged() + { + var sut = Create(out _); + var conditions = CaptureConditions(); + byte[] stored = [0x1E, (byte)'a']; + _database.StringGetAsync(Arg.Any(), Arg.Any()).Returns((RedisValue)stored); + _database.StringGetAsync(Arg.Any(), Arg.Any()).Returns([(RedisValue)stored, (RedisValue)new SystemJsonByteSerializerProxy().Serialize("ok")!]); + _database.KeyTimeToLiveAsync(Arg.Any(), Arg.Any()).Returns(TimeSpan.FromMinutes(1)); + + (await sut.GetCacheEntryAsync("k", policy: null, Token)).Found.Should().BeFalse(); + var entries = await sut.GetCacheEntriesAsync(["a", "b"], policy: null, Token); + + entries.Select(e => e.Value.Found).Should().Equal(false, true); + conditions.Should().HaveCount(2).And.OnlyContain(c => c == Condition.StringEqual("test:str:k", stored).ToString() || c == Condition.StringEqual("test:str:a", stored).ToString()); + _ = _transaction.Received(1).KeyDeleteAsync((RedisKey)"test:str:k", CommandFlags.DemandMaster); + _ = _transaction.Received(1).KeyDeleteAsync((RedisKey)"test:str:a", CommandFlags.DemandMaster); + _ = _database.DidNotReceive().KeyDeleteAsync(Arg.Any(), Arg.Any()); + _telemetry.Metrics.Count(m => m.Name == UnreadableMetric).Should().Be(2); + } + + [Fact] + public async Task Json_that_does_not_parse_is_a_miss_and_the_key_stays() + { + var sut = Create(out _); + _database.StringGetAsync(Arg.Any(), Arg.Any()).Returns((RedisValue)Encoding.UTF8.GetBytes("{not json")); + + var value = await sut.GetAsync("k", policy: null, Token); + + value.Should().BeNull(); + await _database.DidNotReceive().KeyDeleteAsync(Arg.Any(), Arg.Any()); + _telemetry.Metrics.Should().NotContain(m => m.Name == UnreadableMetric); + } + + [Fact] + public async Task A_value_round_trips_through_the_serializer_the_policy_names() + { + var sut = Create(out var registry); + var captured = CaptureWrites(); + var policy = ResolvedPolicy(registry, "fast"); + + await sut.SetAsync("k", "round trip", TimeSpan.FromMinutes(1), policy, Token); + _database.StringGetAsync(Arg.Any(), Arg.Any()).Returns((RedisValue)captured.Single()); + var read = await sut.GetAsync("k", policy, Token); + + read.Should().Be("round trip"); + _fast.Serialized.Should().Be(1); + _fast.Deserialized.Should().Be(1); + _fast.LastFormatId.Should().Be(0x10); + } + + [Fact] + public async Task A_value_round_trips_through_a_two_byte_format_id() + { + var extended = new PassthroughValueSerializer(0x105); + var registry = new ValueSerializerRegistry([("extended", extended, false)]); + var sut = Create(registry); + var captured = CaptureWrites(); + var policy = ResolvedPolicy(registry, "extended"); + + await sut.SetAsync("k", "round trip", TimeSpan.FromMinutes(1), policy, Token); + _database.StringGetAsync(Arg.Any(), Arg.Any()).Returns((RedisValue)captured.Single()); + var read = await sut.GetAsync("k", policy: null, Token); + + captured.Single().Take(2).Should().Equal(0x1F, 0x05); + read.Should().Be("round trip"); + extended.LastFormatId.Should().Be(0x105); + } + + [Fact] + public async Task A_binary_payload_that_is_not_utf8_round_trips() + { + var sut = Create(out var registry); + var captured = CaptureWrites(); + var policy = ResolvedPolicy(registry, "fast"); + byte[] payload = [0xFF, 0xFE, 0x00, 0x80, 0xC3, 0x28]; + + await sut.SetAsync("k", payload, TimeSpan.FromMinutes(1), policy, Token); + _database.StringGetAsync(Arg.Any(), Arg.Any()).Returns((RedisValue)captured.Single()); + var read = await sut.GetAsync("k", policy: null, Token); + + captured.Single().Should().Equal([0x10, .. payload]); + read.Should().Equal(payload); + } + + [Fact] + public async Task Raw_proxy_never_sniffs() + { + var messagePack = new PassthroughValueSerializer(0x02); + var sut = Create(new ValueSerializerRegistry([("messagepack", messagePack, true)]), new RawByteSerializerProxy()); + _database.StringGetAsync(Arg.Any(), Arg.Any()).Returns((RedisValue)new byte[] { 0x02, 1, 2 }); + + var value = await sut.GetAsync("k", policy: null, Token); + + value.Should().Equal(0x02, 1, 2); + messagePack.Deserialized.Should().Be(0); + await _database.DidNotReceive().KeyDeleteAsync(Arg.Any(), Arg.Any()); + } + + [Fact] + public async Task Raw_proxy_bytes_with_an_unregistered_header_byte_are_kept() + { + var sut = Create(new ValueSerializerRegistry([("messagepack", new PassthroughValueSerializer(0x02), true)]), new RawByteSerializerProxy()); + _database.StringGetAsync(Arg.Any(), Arg.Any()).Returns((RedisValue)new byte[] { 0x1B, 1, 2 }); + + var value = await sut.GetAsync("k", policy: null, Token); + + value.Should().Equal(0x1B, 1, 2); + await _database.DidNotReceive().KeyDeleteAsync(Arg.Any(), Arg.Any()); + } + + [Fact] + public async Task Custom_proxy_never_sniffs_without_a_policy_serializer() + { + var sut = Create(FastRegistry(), new ByteProxy()); + _database.StringGetAsync(Arg.Any(), Arg.Any()).Returns((RedisValue)Header(0x10, "x")); + + var value = await sut.GetAsync("k", policy: null, Token); + + value.Should().Equal(Header(0x10, "x")); + _fast.Deserialized.Should().Be(0); + } + + [Fact] + public async Task Custom_proxy_sniffs_when_the_policy_names_a_serializer() + { + var registry = FastRegistry(); + var sut = Create(registry, new ByteProxy()); + _database.StringGetAsync(Arg.Any(), Arg.Any()).Returns((RedisValue)Header(0x10, "x")); + + var value = await sut.GetAsync("k", ResolvedPolicy(registry, "fast"), Token); + + value.Should().Be("x"); + } + + [Fact] + public async Task Batch_reads_mixed_formats_per_key() + { + var sut = Create(out _); + var proxy = new SystemJsonByteSerializerProxy(); + RedisValue[] stored = [proxy.Serialize("json")!, Header(0x10, "headered"), RedisValue.Null]; + _database.StringGetAsync(Arg.Any(), Arg.Any()).Returns(stored); + + var values = await sut.GetAsync(["a", "b", "c"], policy: null, Token); + + values.Select(v => v.Value).Should().Equal("json", "headered", null); + } + + [Fact] + public async Task Batch_read_removes_only_the_unreadable_keys() + { + var sut = Create(out _); + var proxy = new SystemJsonByteSerializerProxy(); + RedisValue[] stored = [proxy.Serialize("json")!, new byte[] { 0x1E, 1 }, Header(0x10, "headered")]; + _database.StringGetAsync(Arg.Any(), Arg.Any()).Returns(stored); + + var values = await sut.GetAsync(["a", "b", "c"], policy: null, Token); + + values.Select(v => v.Value).Should().Equal("json", null, "headered"); + _ = _transaction.Received(1).KeyDeleteAsync(Arg.Any(), CommandFlags.DemandMaster); + _ = _transaction.Received(1).KeyDeleteAsync((RedisKey)"test:str:b", CommandFlags.DemandMaster); + } + + [Fact] + public async Task An_out_of_memory_from_headerless_json_is_a_miss_whatever_is_registered() + { + var sut = Create(new ValueSerializerRegistry([("fast", _fast, false)]), new OutOfMemoryProxy(), new CachePolicy { Serializer = new SerializerPolicy { Write = "fast" } }); + var json = new SystemJsonByteSerializerProxy().Serialize("a")!; + _database.StringGetAsync(Arg.Any(), Arg.Any()).Returns((RedisValue)json); + _database.StringGetAsync(Arg.Any(), Arg.Any()).Returns([(RedisValue)json, RedisValue.Null]); + + (await sut.GetAsync("k", policy: null, Token)).Should().BeNull(); + (await sut.GetAsync(["a", "b"], policy: null, Token)).Select(v => v.Value).Should().Equal(null, null); + (await sut.GetCacheEntryAsync("k", policy: null, Token)).Found.Should().BeFalse(); + (await sut.GetCacheEntriesAsync(["a", "b"], policy: null, Token)).Select(e => e.Value.Found).Should().Equal(false, false); + } + + [Fact] + public async Task A_batch_read_cancelled_after_the_reply_returns_misses_for_the_unreadable_keys() + { + var sut = Create(out _); + using var cts = CancellationTokenSource.CreateLinkedTokenSource(Token); + _database.StringGetAsync(Arg.Any(), Arg.Any()).Returns(_ => + { + cts.Cancel(); + return new RedisValue[] { new byte[] { 0x1E, 1 }, Header(0x10, "headered") }; + }); + + var values = await sut.GetAsync(["a", "b"], policy: null, cts.Token); + + values.Select(v => v.Value).Should().Equal(null, "headered"); + } + + [Fact] + public async Task A_batch_entries_read_cancelled_after_the_reply_returns_misses_for_the_unreadable_keys() + { + var sut = Create(out _); + using var cts = CancellationTokenSource.CreateLinkedTokenSource(Token); + _database.StringGetAsync(Arg.Any(), Arg.Any()).Returns(_ => + { + cts.Cancel(); + return new RedisValue[] { new byte[] { 0x1E, 1 }, Header(0x10, "headered") }; + }); + + var entries = await sut.GetCacheEntriesAsync(["a", "b"], policy: null, cts.Token); + + entries.Select(e => e.Value.Found).Should().Equal(false, true); + } + + [Fact] + public async Task Cached_null_stays_a_cached_null() + { + var sut = Create(out _, cacheNullValues: true); + _database.StringGetAsync(Arg.Any(), Arg.Any()).Returns(RedisValue.EmptyString); + var generated = false; + + Task Generate(CancellationToken token) + { + generated = true; + return Task.FromResult("x"); + } + + var value = await sut.GetOrAddAsync("k", Generate, TimeSpan.FromMinutes(1), policy: null, Token); + + value.Should().BeNull(); + generated.Should().BeFalse(); + _telemetry.Metrics.Should().NotContain(m => m.Name == UnreadableMetric); + } + + [Fact] + public async Task Unknown_name_on_an_ad_hoc_policy_throws_at_call_time() + { + var sut = Create(out _); + var policy = new CachePolicy { Serializer = new SerializerPolicy { Write = "nope" } }; + + var act = async () => await sut.SetAsync("k", "hello", TimeSpan.FromMinutes(1), policy, Token); + + (await act.Should().ThrowAsync()).Which.Message.Should().Contain("nope"); + await _database.DidNotReceiveWithAnyArgs().StringSetAsync(default, default, default, default, default); + } + + [Theory] + [MemberData(nameof(WritePaths))] + public async Task Unknown_name_on_an_ad_hoc_policy_throws_on_every_write_path(string path) + { + var sut = Create(out _); + var policy = new CachePolicy { Serializer = new SerializerPolicy { Write = "nope" } }; + + var act = async () => await Write(sut, path, policy); + + await act.Should().ThrowAsync(); + await _database.DidNotReceiveWithAnyArgs().StringSetAsync(default, default, default, default, default); + _database.DidNotReceiveWithAnyArgs().CreateTransaction(default); + } + + [Theory] + [MemberData(nameof(WritePaths))] + public async Task A_policy_that_names_a_registered_serializer_without_a_resolved_slot_still_writes_it(string path) + { + var sut = Create(out _); + var captured = CaptureWrites(); + var adHoc = new CachePolicy { Serializer = new SerializerPolicy { Write = "fast" } }; + CachePolicyMerger.WriteSerializerOf(adHoc).Should().BeNull(); + + await Write(sut, path, adHoc); + + captured.Should().NotBeEmpty(); + captured.Should().OnlyContain(value => value[0] == 0x10); + } + + [Fact] + public async Task A_merged_policy_with_an_empty_slot_still_writes_the_named_serializer() + { + var sut = Create(out _); + var captured = CaptureWrites(); + var merged = CachePolicyMerger.Merge( + new CachePolicy { DistributedExpiration = TimeSpan.FromMinutes(1) }, + new CachePolicy { Serializer = new SerializerPolicy { Write = "fast" } }); + + await sut.SetAsync("k", "hello", merged, Token); + + captured.Should().ContainSingle().Which.Should().Equal(Header(0x10, "hello")); + } + + [Fact] + public async Task Large_value_round_trips() + { + var sut = Create(out var registry); + var captured = CaptureWrites(); + var large = new string('x', 200 * 1024); + + await sut.SetAsync("k", large, TimeSpan.FromMinutes(1), ResolvedPolicy(registry, "fast"), Token); + _database.StringGetAsync(Arg.Any(), Arg.Any()).Returns((RedisValue)captured.Single()); + var read = await sut.GetAsync("k", policy: null, Token); + + read.Should().Be(large); + } + + [Fact] + public async Task A_headered_write_goes_through_the_write_pipeline() + { + var sut = Create(out var registry); + CaptureWrites(); + + await sut.SetAsync("k", "hello", TimeSpan.FromMinutes(1), ResolvedPolicy(registry, "fast"), Token); + + _write.ResultTypes.Should().Contain(typeof(bool)); + } + + [Theory] + [MemberData(nameof(Scenarios))] + public async Task Every_operation_reports_the_method_name_it_reported_before_serializers(string scenario) + { + var registry = scenario == "no registry" ? ValueSerializerRegistry.Empty : FastRegistry(); + var sut = Create(registry); + var policy = scenario.EndsWith("fast write", StringComparison.Ordinal) ? ResolvedPolicy(registry, "fast") : null; + CaptureWrites(); + _database.StringGetAsync(Arg.Any(), Arg.Any()).Returns([RedisValue.Null, RedisValue.Null]); + var minute = TimeSpan.FromMinutes(1); + Task Generate(CancellationToken token) => Task.FromResult("hello"); + Task> GenerateExpiring(CancellationToken token) => Task.FromResult(new GeneratedValue("hello")); + Task[]> GenerateMany(string[] states, CancellationToken token) => + Task.FromResult(states.Select(s => new KeyValuePair(s, "hello")).ToArray()); + + (await MethodsOf(async () => await sut.GetAsync("k", policy, Token))).Should().Equal("GetAsync"); + (await MethodsOf(async () => await sut.GetAsync(["a", "b"], policy, Token))).Should().Equal("GetAsyncInternal"); + (await MethodsOf(async () => await sut.GetCacheEntryAsync("k", policy, Token))).Should().Equal("GetCacheEntryInternalAsync"); + (await MethodsOf(async () => await sut.GetCacheEntriesAsync(["a", "b"], policy, Token))).Should().Equal("GetCacheEntriesInternalAsync"); + (await MethodsOf(async () => await sut.SetAsync("k", "hello", minute, policy, Token))).Should().Equal("SetAsync"); + (await MethodsOf(async () => await sut.SetAsync([new KeyValuePair("a", "x")], minute, policy, Token))).Should().Equal("SetAsync"); + (await MethodsOf(async () => await sut.TryAddAsync("k", "hello", minute, policy, Token))).Should().Equal("TryAddAsync"); + (await MethodsOf(async () => await sut.GetOrAddAsync("k", Generate, minute, policy, Token))).Should().Equal("GetOrAddAsync", "SetAsync"); + (await MethodsOf(async () => await sut.GetOrAddWithExpirationAsync("k", GenerateExpiring, policy, Token))).Should().Equal("GetOrAddAsync", "SetAsync"); + (await MethodsOf(async () => await sut.GetOrAddResultAsync("k", Generate, minute, policy, Token))).Should().Equal("GetOrAddResultAsync", "SetAsync"); + (await MethodsOf(async () => await sut.GetOrAddAsync([new KeyValuePair("a", "a")], GenerateMany, minute, policy, Token))) + .Should().Equal("GetCacheEntriesInternalAsync", "SetAsync"); + (await MethodsOf(async () => await sut.RemoveAsync("k", Token))).Should().Equal("RemoveAsync"); + } + + [Fact] + public async Task A_pooled_buffer_is_not_returned_while_its_write_is_in_flight() + { + using var pool = new RecordingPool(); + var sut = Create(out var registry); + var inFlight = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + _database.StringSetAsync(Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any()).Returns(_ => inFlight.Task); + + var write = sut.SetAsync("k", new string('x', 100 * 1024), TimeSpan.FromMinutes(1), ResolvedPolicy(registry, "fast"), Token).AsTask(); + var payload = pool.RentedByThisThread.MaxBy(array => array.Length)!; + + pool.Returned.Should().NotContain(array => ReferenceEquals(array, payload)); + + inFlight.SetResult(true); + await write; + + pool.Returned.Should().Contain(array => ReferenceEquals(array, payload)); + } + + private static byte[] Header(byte id, string body) => [id, .. Encoding.UTF8.GetBytes(body)]; + + private static CachePolicy ResolvedPolicy(ValueSerializerRegistry registry, string write) => + new DefaultCachePolicyFactory(null, new CachePolicy { Serializer = new SerializerPolicy { Write = write } }, valueSerializers: registry).Default!; + + private static ValueTask Write(RedisCache sut, string path, CachePolicy policy) => path switch + { + "set" => sut.SetAsync("k", "hello", TimeSpan.FromMinutes(1), policy), + "set many" => sut.SetAsync([new KeyValuePair("a", "hello"), new KeyValuePair("b", "world")], TimeSpan.FromMinutes(1), policy), + "try add" => sut.TryAddAsync("k", "hello", TimeSpan.FromMinutes(1), policy), + "get or add with expiration" => Written(sut.GetOrAddWithExpirationAsync("k", _ => Task.FromResult(new GeneratedValue("hello")), policy)), + "get or add result" => Written(sut.GetOrAddResultAsync("k", _ => Task.FromResult("hello"), TimeSpan.FromMinutes(1), policy), static r => r.Value), + "get or add batch" => Written(sut.GetOrAddAsync([new KeyValuePair("a", "a")], static (states, _) => Task.FromResult(states.Select(s => new KeyValuePair(s, "hello")).ToArray()), TimeSpan.FromMinutes(1), policy), static r => r[0].Value), + "get or add batch with state" => Written(sut.GetOrAddAsync([new KeyValuePair("a", "a")], "hello", static (value, states, _) => Task.FromResult(states.Select(s => new KeyValuePair(s, value)).ToArray()), TimeSpan.FromMinutes(1), policy), static r => r[0].Value), + _ => GetOrAdd(sut, policy), + }; + + private static async ValueTask Written(ValueTask generated) => await generated is not null; + + private static async ValueTask Written(ValueTask generated, Func value) => value(await generated) is not null; + + private static async ValueTask GetOrAdd(RedisCache sut, CachePolicy policy) => + await sut.GetOrAddAsync("k", _ => Task.FromResult("hello"), TimeSpan.FromMinutes(1), policy) is not null; + + private List CaptureConditions() + { + var conditions = new List(); + _transaction.AddCondition(Arg.Do(c => conditions.Add(c.ToString()!))); + return conditions; + } + + private void AssertNothingDeleted() + { + _ = _database.DidNotReceive().KeyDeleteAsync(Arg.Any(), Arg.Any()); + _ = _database.DidNotReceive().KeyDeleteAsync(Arg.Any(), Arg.Any()); + _ = _transaction.DidNotReceive().KeyDeleteAsync(Arg.Any(), Arg.Any()); + } + + private ValueSerializerRegistry FastRegistry() => new([("fast", _fast, false)]); + + private async Task MethodsOf(Func operation) + { + var (metrics, dependencies) = (_telemetry.Metrics.Count, _telemetry.Dependencies.Count); + await operation(); + return + [ + .. _telemetry.Metrics.Skip(metrics).Select(m => m.Name).Where(n => n.StartsWith("Caching.Stats.", StringComparison.Ordinal)).Select(n => n.Split('.')[4]) + .Concat(_telemetry.Dependencies.Skip(dependencies).Select(d => d.Name)) + .Distinct() + .Order(StringComparer.Ordinal), + ]; + } + + private RedisCache Create(out ValueSerializerRegistry registry, bool cacheNullValues = false) + { + registry = FastRegistry(); + return Create(registry, cacheNullValues: cacheNullValues); + } + + private RedisCache Create(ValueSerializerRegistry registry, ISerializerProxy? proxy = null, CachePolicy? defaultPolicy = null, bool cacheNullValues = false) + { + var connector = Substitute.For(); + connector.Database.Returns(_database); + connector.Version.Returns(new Version(6, 0)); + connector.IsConnected.Returns(true); + _database.Multiplexer.GetHashSlot(Arg.Any()).Returns(0); + _transaction.ExecuteAsync(Arg.Any()).Returns(true); + _database.CreateTransaction(Arg.Any()).Returns(_transaction); + var pipelines = Substitute.For(); + pipelines.Get(ResiliencePipelineNames.Read).Returns(EmptyResiliencePipeline.Instance); + pipelines.Get(ResiliencePipelineNames.Write).Returns(_write); + var strategy = Substitute.For(); + strategy.GetRedisKey(Arg.Any()).Returns(call => (RedisKey)$"test:str:{call.Arg()}"); + var strategies = Substitute.For(); + strategies.Create(Arg.Any(), Arg.Any()).Returns(strategy); + var options = new RedisCacheOptions { RedisKeyStrategyFactory = strategies, ReadScriptsEnabled = false, CacheNullValues = cacheNullValues }; + var cacheOptions = new CacheOptions { AppShortName = "test", ConnectionMonitorEnabled = true }; + + return new RedisCache( + connector, + proxy ?? new SystemJsonByteSerializerProxy(), + pipelines, + _telemetry, + options, + cacheOptions, + new DefaultCachePolicyFactory(null, defaultPolicy, valueSerializers: registry), + TimeProvider.System, + NullLogger.Instance) + { + ValueSerializers = registry, + }; + } + + private List CaptureWrites() + { + var captured = new List(); + _database.StringSetAsync(Arg.Any(), Arg.Do(v => captured.Add(((byte[]?)v)!)), Arg.Any(), Arg.Any(), Arg.Any()).Returns(true); + _database.StringGetAsync(Arg.Any(), Arg.Any()).Returns(RedisValue.Null); + _transaction.StringSetAsync(Arg.Any(), Arg.Do(v => captured.Add(((byte[]?)v)!)), Arg.Any(), Arg.Any(), Arg.Any()).Returns(true); + return captured; + } + + private sealed class OutOfMemoryProxy : ISerializerProxy + { + public byte[]? Serialize(object? value) => throw new NotSupportedException(); + + public T? Deserialize(byte[]? value) => throw new OutOfMemoryException(); + + public bool TryDeserialize(string? value, out T? result) => throw new NotSupportedException(); + + public bool TryDeserialize(object? value, out T? result) => throw new NotSupportedException(); + } + + private sealed class ByteProxy : ISerializerProxy + { + public byte[]? Serialize(object? value) => (byte[]?)value; + + public T? Deserialize(byte[]? value) => (T?)(object?)value; + + public bool TryDeserialize(string? value, out T? result) => throw new NotSupportedException(); + + public bool TryDeserialize(object? value, out T? result) => throw new NotSupportedException(); + } +} diff --git a/tests/UiPath.Caching.Tests/Redis/RedisHashCacheSerializerTests.cs b/tests/UiPath.Caching.Tests/Redis/RedisHashCacheSerializerTests.cs new file mode 100644 index 00000000..73b56803 --- /dev/null +++ b/tests/UiPath.Caching.Tests/Redis/RedisHashCacheSerializerTests.cs @@ -0,0 +1,664 @@ +using System.Text; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; +using StackExchange.Redis; +using UiPath.Caching.Config; +using UiPath.Caching.Policies; +using UiPath.Caching.Serialization; +using UiPath.Caching.Tests.Fakes; +using UiPath.Caching.Tests.Telemetry; + +namespace UiPath.Caching.Tests.Redis; + +[Collection(WriterPoolCollection.Name)] +public class RedisHashCacheSerializerTests(ITestContextAccessor testContextAccessor) +{ + private const string UnreadableMetric = "cache.serializer.unreadable"; + + private readonly IDatabase _database = Substitute.For(); + private readonly ITransaction _transaction = Substitute.For(); + private readonly RecordingTelemetryProvider _telemetry = new(); + private readonly RecordingResiliencePipeline _write = new(); + private readonly PassthroughValueSerializer _fast = new(0x10); + + private CancellationToken Token => testContextAccessor.Current.CancellationToken; + + public static TheoryData Scenarios() => ["no registry", "registry, json write", "registry, fast write"]; + + [Fact] + public async Task Fields_and_metadata_use_the_policy_serializer() + { + var sut = Create(out var registry); + var written = CaptureWrites(); + var options = new HashCacheEntryOptions(DateTimeOffset.UtcNow.AddMinutes(5), Metadata: new Dictionary { ["m"] = "v" }); + + await sut.SetAsync("k", new Dictionary { ["a"] = "x" }, options, ResolvedPolicy(registry), Token); + + var entries = written.Should().ContainSingle().Subject; + entries.Should().HaveCount(2); + Bytes(entries, "a").Should().Equal(Header(0x10, "x")); + Bytes(entries, "_metadata_").Should().Equal(Header(0x10, "{\"m\":\"v\"}")); + } + + [Fact] + public async Task The_json_default_writes_the_same_bytes_as_before() + { + var sut = Create(out _); + var written = CaptureWrites(); + var options = new HashCacheEntryOptions(DateTimeOffset.UtcNow.AddMinutes(5), Metadata: new Dictionary { ["m"] = "v" }); + + await sut.SetAsync("k", new Dictionary { ["a"] = "x" }, options, policy: null, Token); + + var entries = written.Should().ContainSingle().Subject; + Bytes(entries, "a").Should().Equal(new SystemJsonByteSerializerProxy().Serialize("x")); + Bytes(entries, "_metadata_").Should().Equal(Encoding.UTF8.GetBytes("{\"m\":\"v\"}")); + _fast.Serialized.Should().Be(0); + } + + [Fact] + public async Task Empty_metadata_is_not_written_when_nulls_are_not_cached() + { + var sut = Create(out var registry); + var written = CaptureWrites(); + var options = new HashCacheEntryOptions(DateTimeOffset.UtcNow.AddMinutes(5), Metadata: new Dictionary()); + + await sut.SetAsync("k", new Dictionary { ["a"] = "x" }, options, ResolvedPolicy(registry), Token); + + var entries = written.Should().ContainSingle().Subject; + entries.Select(e => e.Name.ToString()).Should().Equal("a"); + } + + [Fact] + public async Task A_hash_replace_with_empty_metadata_deletes_the_old_metadata_field_for_a_non_json_write() + { + var sut = Create(out var registry); + var written = CaptureWrites(); + var options = new HashCacheEntryOptions(DateTimeOffset.UtcNow.AddMinutes(5), Metadata: new Dictionary(), SetOption: HashCacheSetOption.HashReplace); + + await sut.SetAsync("k", new Dictionary { ["a"] = "x" }, options, ResolvedPolicy(registry), Token); + + written.Should().ContainSingle().Which.Select(e => e.Name.ToString()).Should().Equal("a"); + _ = _transaction.Received(1).HashDeleteAsync((RedisKey)"test:hash:k", Arg.Is(f => f == "_metadata_"), CommandFlags.DemandMaster); + _ = _database.DidNotReceive().HashDeleteAsync(Arg.Any(), Arg.Any(), Arg.Any()); + } + + [Fact] + public async Task A_key_replace_with_empty_metadata_needs_no_field_delete_for_a_non_json_write() + { + var sut = Create(out var registry); + CaptureWrites(); + var options = new HashCacheEntryOptions(DateTimeOffset.UtcNow.AddMinutes(5), Metadata: new Dictionary()); + + await sut.SetAsync("k", new Dictionary { ["a"] = "x" }, options, ResolvedPolicy(registry), Token); + + _ = _database.DidNotReceive().HashDeleteAsync(Arg.Any(), Arg.Any(), Arg.Any()); + _ = _transaction.DidNotReceive().HashDeleteAsync(Arg.Any(), Arg.Any(), Arg.Any()); + } + + [Fact] + public async Task A_hash_replace_with_metadata_writes_it_and_deletes_nothing() + { + var sut = Create(out var registry); + var written = CaptureWrites(); + var options = new HashCacheEntryOptions(DateTimeOffset.UtcNow.AddMinutes(5), Metadata: new Dictionary { ["m"] = "v" }, SetOption: HashCacheSetOption.HashReplace); + + await sut.SetAsync("k", new Dictionary { ["a"] = "x" }, options, ResolvedPolicy(registry), Token); + + written.Should().ContainSingle().Which.Select(e => e.Name.ToString()).Should().Equal("a", "_metadata_"); + _ = _database.DidNotReceive().HashDeleteAsync(Arg.Any(), Arg.Any(), Arg.Any()); + _ = _transaction.DidNotReceive().HashDeleteAsync(Arg.Any(), Arg.Any(), Arg.Any()); + } + + [Fact] + public async Task The_json_default_hash_replace_with_empty_metadata_writes_the_same_bytes_and_deletes_nothing() + { + var sut = Create(out _); + var written = CaptureWrites(); + var options = new HashCacheEntryOptions(DateTimeOffset.UtcNow.AddMinutes(5), Metadata: new Dictionary(), SetOption: HashCacheSetOption.HashReplace); + + await sut.SetAsync("k", new Dictionary { ["a"] = "x" }, options, policy: null, Token); + + Bytes(written.Single(), "_metadata_").Should().Equal(Encoding.UTF8.GetBytes("{}")); + _ = _database.DidNotReceive().HashDeleteAsync(Arg.Any(), Arg.Any(), Arg.Any()); + _ = _transaction.DidNotReceive().HashDeleteAsync(Arg.Any(), Arg.Any(), Arg.Any()); + } + + [Fact] + public async Task Empty_metadata_reads_as_none() + { + var sut = Create(out _); + _database.HashGetAllAsync(Arg.Any(), Arg.Any()).Returns([Entry("a", Header(0x10, "x"))]); + _database.KeyTimeToLiveAsync(Arg.Any(), Arg.Any()).Returns(TimeSpan.FromMinutes(1)); + + var entry = await sut.GetCacheEntryAsync("k", policy: null, Token); + + entry.Found.Should().BeTrue(); + entry.Metadata.Should().BeNull(); + } + + [Fact] + public async Task The_json_default_still_writes_empty_metadata() + { + var sut = Create(out _); + var written = CaptureWrites(); + var options = new HashCacheEntryOptions(DateTimeOffset.UtcNow.AddMinutes(5), Metadata: new Dictionary()); + + await sut.SetAsync("k", new Dictionary { ["a"] = "x" }, options, policy: null, Token); + + Bytes(written.Single(), "_metadata_").Should().Equal(Encoding.UTF8.GetBytes("{}")); + } + + [Fact] + public async Task Cached_empty_marker_is_kept() + { + var sut = Create(out var registry, cacheNullValues: true); + var written = CaptureWrites(); + _database.HashGetAllAsync(Arg.Any(), Arg.Any()).Returns([]); + + var generated = await sut.GetOrAddAsync("k", _ => Task.FromResult>(new Dictionary()), TimeSpan.FromMinutes(1), ResolvedPolicy(registry), Token); + + generated.Should().BeEmpty(); + var entries = written.Should().ContainSingle().Subject; + var marker = entries.Should().ContainSingle().Subject; + marker.Name.ToString().Should().Be("_metadata_"); + marker.Value.Length().Should().Be(0); + } + + [Fact] + public async Task Headered_fields_read_under_any_policy() + { + var sut = Create(out _); + _database.HashGetAllAsync(Arg.Any(), Arg.Any()).Returns([Entry("a", Header(0x10, "x")), Entry("b", Json("y"))]); + + var values = await sut.GetAsync("k", policy: null, Token); + + values.Should().BeEquivalentTo(new Dictionary { ["a"] = "x", ["b"] = "y" }); + } + + [Fact] + public async Task Version_field_is_never_sniffed() + { + var sut = Create(out _); + _database.HashGetAllAsync(Arg.Any(), Arg.Any()).Returns( + [ + Entry("a", Header(0x10, "x")), + Entry("_version_", Encoding.UTF8.GetBytes("12")), + ]); + _database.KeyTimeToLiveAsync(Arg.Any(), Arg.Any()).Returns(TimeSpan.FromMinutes(1)); + + var entry = await sut.GetCacheEntryAsync("k", policy: null, Token); + + HashVersions.NumberOf(entry).Should().Be(12); + entry.Value.Should().ContainKey("a"); + _telemetry.Metrics.Should().NotContain(m => m.Name == UnreadableMetric); + } + + [Fact] + public async Task A_version_field_that_looks_like_a_header_is_not_an_unreadable_format() + { + var sut = Create(out _); + _database.HashGetAllAsync(Arg.Any(), Arg.Any()).Returns( + [ + Entry("a", Header(0x10, "x")), + Entry("_version_", [0x1E, (byte)'1']), + ]); + _database.KeyTimeToLiveAsync(Arg.Any(), Arg.Any()).Returns(TimeSpan.FromMinutes(1)); + + var entry = await sut.GetCacheEntryAsync("k", policy: null, Token); + + entry.Found.Should().BeTrue(); + await _database.DidNotReceive().KeyDeleteAsync(Arg.Any(), Arg.Any()); + } + + [Theory] + [MemberData(nameof(Scenarios))] + public async Task Every_operation_reports_the_method_name_it_reported_before_serializers(string scenario) + { + var registry = scenario == "no registry" ? ValueSerializerRegistry.Empty : new ValueSerializerRegistry([("fast", _fast, false)]); + var fastWrite = scenario.EndsWith("fast write", StringComparison.Ordinal); + var sut = Create(registry, defaultPolicy: fastWrite ? new CachePolicy { Serializer = new SerializerPolicy { Write = "fast" } } : null); + var policy = fastWrite ? ResolvedPolicy(registry) : null; + CaptureWrites(); + _database.KeyExistsAsync(Arg.Any(), Arg.Any()).Returns(true); + var values = new Dictionary { ["a"] = "x" }; + var options = new HashCacheEntryOptions(DateTimeOffset.UtcNow.AddMinutes(5), Metadata: new Dictionary { ["m"] = "v" }); + Task> Generate(CancellationToken token) => Task.FromResult>(values); + + (await MethodsOf(async () => await sut.GetItemAsync("k", "a", policy, Token))).Should().Equal("GetAsync"); + (await MethodsOf(async () => await sut.GetAsync("k", policy, Token))).Should().Equal("GetInnerAsync"); + (await MethodsOf(async () => await sut.GetAsync("k", ["a", "b"], policy, Token))).Should().Equal("GetInnerAsync"); + (await MethodsOf(async () => await sut.GetCacheEntryAsync("k", policy, Token))).Should().Equal("GetInnerCacheEntryAsync"); + (await MethodsOf(async () => await sut.GetMetadataAsync("k", Token))).Should().Equal("GetAsync"); + (await MethodsOf(async () => await sut.SetAsync("k", values, options, policy, Token))).Should().Equal("SetAsync"); + (await MethodsOf(async () => await sut.GetOrAddAsync("k", Generate, TimeSpan.FromMinutes(1), policy, Token))).Should().Equal("GetOrAddAsync", "SetAsync"); + (await MethodsOf(async () => await sut.RefreshAsync("k", options, policy, Token))).Should().Equal("RefreshAsync"); + (await MethodsOf(async () => await sut.SetMetadataAsync("k", new Dictionary { ["m"] = "v" }, Token))).Should().Equal("SetMetadataAsync"); + } + + [Fact] + public async Task One_unreadable_field_is_a_whole_hash_miss_and_the_hash_stays() + { + var sut = Create(out _); + _database.HashGetAllAsync(Arg.Any(), Arg.Any()).Returns([Entry("a", Json("x")), Entry("b", [0x1E, 1])]); + + var values = await sut.GetAsync("k", policy: null, Token); + + values.Should().BeEmpty(); + AssertNothingDeleted(); + _telemetry.Metrics.Should().ContainSingle(m => m.Name == UnreadableMetric).Which.Properties.Should().Contain("format", "0x1E"); + } + + [Fact] + public async Task A_field_whose_serializer_throws_is_a_whole_hash_miss_and_the_hash_stays() + { + var sut = Create(new ValueSerializerRegistry([("bad", new ThrowingValueSerializer(), false)])); + _database.HashGetAllAsync(Arg.Any(), Arg.Any()).Returns([Entry("a", Json("x")), Entry("b", [0x10, 1])]); + + var values = await sut.GetAsync("k", policy: null, Token); + + values.Should().BeEmpty(); + AssertNothingDeleted(); + _telemetry.Metrics.Should().NotContain(m => m.Name == UnreadableMetric); + } + + [Fact] + public async Task An_out_of_memory_from_a_serializer_propagates_from_every_hash_read_and_runs_no_generator() + { + var sut = Create(new ValueSerializerRegistry([("bad", new ThrowingValueSerializer { Failure = new OutOfMemoryException() }, false)])); + _database.HashGetAllAsync(Arg.Any(), Arg.Any()).Returns([Entry("a", [0x10, 1])]); + _database.HashGetAsync(Arg.Any(), Arg.Any(), Arg.Any()).Returns((RedisValue)new byte[] { 0x10, 1 }); + _database.HashGetAsync(Arg.Any(), Arg.Any(), Arg.Any()).Returns([(RedisValue)new byte[] { 0x10, 1 }]); + var generated = 0; + Task> Generate(CancellationToken token) + { + generated++; + return Task.FromResult>(new Dictionary { ["a"] = "x" }); + } + + await FluentActions.Awaiting(() => sut.GetAsync("k", policy: null, Token).AsTask()).Should().ThrowAsync(); + await FluentActions.Awaiting(() => sut.GetAsync("k", ["a"], policy: null, Token).AsTask()).Should().ThrowAsync(); + await FluentActions.Awaiting(() => sut.GetItemAsync("k", "a", policy: null, Token).AsTask()).Should().ThrowAsync(); + await FluentActions.Awaiting(() => sut.GetCacheEntryAsync("k", policy: null, Token).AsTask()).Should().ThrowAsync(); + await FluentActions.Awaiting(() => sut.GetOrAddAsync("k", Generate, policy: null, Token).AsTask()).Should().ThrowAsync(); + + generated.Should().Be(0); + } + + [Fact] + public async Task Fields_round_trip_through_the_serializer_the_policy_names() + { + var sut = Create(out var registry); + var written = CaptureWrites(); + var options = new HashCacheEntryOptions(DateTimeOffset.UtcNow.AddMinutes(5), Metadata: new Dictionary { ["m"] = "v" }); + var policy = ResolvedPolicy(registry); + + await sut.SetAsync("k", new Dictionary { ["a"] = "x", ["b"] = "y" }, options, policy, Token); + _database.HashGetAllAsync(Arg.Any(), Arg.Any()).Returns(written.Single()); + _database.KeyTimeToLiveAsync(Arg.Any(), Arg.Any()).Returns(TimeSpan.FromMinutes(1)); + var entry = await sut.GetCacheEntryAsync("k", policy, Token); + + entry.Value.Should().BeEquivalentTo(new Dictionary { ["a"] = "x", ["b"] = "y" }); + entry.Metadata.Should().BeEquivalentTo(new Dictionary { ["m"] = "v" }); + _fast.Serialized.Should().Be(3); + _fast.Deserialized.Should().Be(3); + } + + [Fact] + public async Task A_field_round_trips_through_a_two_byte_format_id() + { + var extended = new PassthroughValueSerializer(0x105); + var registry = new ValueSerializerRegistry([("extended", extended, false)]); + var sut = Create(registry); + var written = CaptureWrites(); + var policy = new DefaultCachePolicyFactory(null, new CachePolicy { Serializer = new SerializerPolicy { Write = "extended" } }, valueSerializers: registry).Default!; + + await sut.SetAsync("k", new Dictionary { ["a"] = "x" }, new HashCacheEntryOptions(DateTimeOffset.UtcNow.AddMinutes(5)), policy, Token); + _database.HashGetAllAsync(Arg.Any(), Arg.Any()).Returns(written.Single()); + _database.KeyTimeToLiveAsync(Arg.Any(), Arg.Any()).Returns(TimeSpan.FromMinutes(1)); + var entry = await sut.GetCacheEntryAsync("k", policy: null, Token); + + Bytes(written.Single(), "a").Take(2).Should().Equal(0x1F, 0x05); + entry.Value.Should().BeEquivalentTo(new Dictionary { ["a"] = "x" }); + extended.LastFormatId.Should().Be(0x105); + } + + [Fact] + public async Task A_binary_field_that_is_not_utf8_round_trips() + { + var sut = Create(out var registry); + var written = CaptureWrites(); + var policy = ResolvedPolicy(registry); + byte[] payload = [0xFF, 0xFE, 0x00, 0x80, 0xC3, 0x28]; + + await sut.SetAsync("k", new Dictionary { ["a"] = payload }, new HashCacheEntryOptions(DateTimeOffset.UtcNow.AddMinutes(5)), policy, Token); + _database.HashGetAllAsync(Arg.Any(), Arg.Any()).Returns(written.Single()); + _database.KeyTimeToLiveAsync(Arg.Any(), Arg.Any()).Returns(TimeSpan.FromMinutes(1)); + var entry = await sut.GetCacheEntryAsync("k", policy: null, Token); + + Bytes(written.Single(), "a").Should().Equal([0x10, .. payload]); + entry.Value!["a"].Should().Equal(payload); + } + + [Fact] + public async Task An_unreadable_metadata_field_is_a_whole_hash_miss_and_the_hash_stays() + { + var sut = Create(out _); + _database.HashGetAllAsync(Arg.Any(), Arg.Any()).Returns([Entry("a", Json("x")), Entry("_metadata_", [0x1E, 1])]); + _database.KeyTimeToLiveAsync(Arg.Any(), Arg.Any()).Returns(TimeSpan.FromMinutes(1)); + + var entry = await sut.GetCacheEntryAsync("k", policy: null, Token); + + entry.Found.Should().BeFalse(); + AssertNothingDeleted(); + } + + [Fact] + public async Task An_unreadable_field_in_a_field_read_is_a_whole_hash_miss_and_the_hash_stays() + { + var sut = Create(out _); + _database.HashGetAsync(Arg.Any(), Arg.Any(), Arg.Any()).Returns([(RedisValue)Json("x"), (RedisValue)new byte[] { 0x1E, 1 }]); + + var values = await sut.GetAsync("k", ["a", "b"], policy: null, Token); + + values.Should().BeEmpty(); + AssertNothingDeleted(); + } + + [Fact] + public async Task An_unreadable_single_field_is_a_miss_and_the_hash_stays() + { + var sut = Create(out _); + _database.HashGetAsync(Arg.Any(), Arg.Any(), Arg.Any()).Returns((RedisValue)new byte[] { 0x1E, 1 }); + + var value = await sut.GetItemAsync("k", "a", policy: null, Token); + + value.Should().BeNull(); + AssertNothingDeleted(); + } + + [Fact] + public async Task An_unreadable_field_in_a_get_or_add_read_regenerates_and_writes_over_the_hash() + { + var sut = Create(out _); + var order = new List(); + CaptureWrites(); + _database.HashGetAllAsync(Arg.Any(), Arg.Any()).Returns([Entry("a", [0x1E, 1])]); + _transaction.HashSetAsync(Arg.Any(), Arg.Do(_ => order.Add("write")), Arg.Any()).Returns(Task.CompletedTask); + + Task> Generate(CancellationToken token) + { + order.Add("generate"); + return Task.FromResult>(new Dictionary { ["a"] = "fresh" }); + } + + var values = await sut.GetOrAddAsync("k", Generate, TimeSpan.FromMinutes(1), policy: null, Token); + + values["a"].Should().Be("fresh"); + order.Should().Equal("generate", "write"); + _telemetry.Metrics.Should().ContainSingle(m => m.Name == UnreadableMetric); + } + + [Fact] + public async Task GetMetadata_sniffs_with_the_exact_json_proxy() + { + var sut = Create(out _); + _database.HashGetAsync(Arg.Any(), Arg.Any(), Arg.Any()).Returns((RedisValue)Header(0x10, "{\"m\":\"v\"}")); + + var metadata = await sut.GetMetadataAsync("k", Token); + + metadata.Should().BeEquivalentTo(new Dictionary { ["m"] = "v" }); + } + + [Fact] + public async Task GetMetadata_does_not_sniff_for_a_custom_proxy() + { + var sut = Create(new ValueSerializerRegistry([("fast", _fast, false)]), new ByteProxy()); + _database.HashGetAsync(Arg.Any(), Arg.Any(), Arg.Any()).Returns((RedisValue)Header(0x10, "{\"m\":\"v\"}")); + + var metadata = await sut.GetMetadataAsync("k", Token); + + metadata.Should().BeNull(); + _fast.Deserialized.Should().Be(0); + } + + [Fact] + public async Task SetMetadata_writes_the_default_policy_serializer() + { + var registry = new ValueSerializerRegistry([("fast", _fast, false)]); + var sut = Create(registry, defaultPolicy: new CachePolicy { Serializer = new SerializerPolicy { Write = "fast" } }); + _database.KeyExistsAsync(Arg.Any(), Arg.Any()).Returns(true); + _database.HashSetAsync(Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any()).Returns(true); + _transaction.ExecuteAsync(Arg.Any()).Returns(true); + var written = new List(); + _transaction.HashSetAsync(Arg.Any(), Arg.Do(e => written.Add(((byte[]?)e[0].Value)!)), Arg.Any()).Returns(Task.CompletedTask); + _database.HashSetAsync(Arg.Any(), Arg.Any(), Arg.Do(v => written.Add(((byte[]?)v)!)), Arg.Any(), Arg.Any()).Returns(true); + + await sut.SetMetadataAsync("k", new Dictionary { ["m"] = "v" }, Token); + + written.Should().ContainSingle().Which.Should().Equal(Header(0x10, "{\"m\":\"v\"}")); + } + + [Fact] + public void Peer_fields_with_headered_bytes_decode_through_the_reader() + { + var sut = Create(out _); + var encoded = new Dictionary { ["a"] = Header(0x10, "x"), ["b"] = Json("y"), ["c"] = null }; + + sut.TryDecodeFields(encoded, new CachePolicy(), out var values).Should().BeTrue(); + + values.Should().BeEquivalentTo(new Dictionary { ["a"] = "x", ["b"] = "y", ["c"] = null }); + } + + [Fact] + public void Peer_fields_in_a_format_this_node_cannot_read_are_not_applied() + { + var sut = Create(out _); + + sut.TryDecodeFields(new Dictionary { ["a"] = [0x1E, 1] }, new CachePolicy(), out var values).Should().BeFalse(); + + values.Should().BeNull(); + } + + [Fact] + public void Peer_fields_are_not_decoded_where_a_custom_proxy_could_mistake_headered_bytes_for_its_own() + { + var sut = Create(new ValueSerializerRegistry([("fast", _fast, false)]), new ByteProxy()); + + sut.TryDecodeFields(new Dictionary { ["a"] = Header(0x10, "x") }, new CachePolicy(), out var values).Should().BeFalse(); + + values.Should().BeNull(); + _fast.Deserialized.Should().Be(0); + } + + [Fact] + public void Peer_fields_decode_through_the_reader_for_a_custom_proxy_when_the_default_policy_writes_a_serializer() + { + var sut = Create(new ValueSerializerRegistry([("fast", _fast, false)]), new ByteProxy(), new CachePolicy { Serializer = new SerializerPolicy { Write = "fast" } }); + + sut.TryDecodeFields(new Dictionary { ["a"] = Header(0x10, "x") }, new CachePolicy(), out var values).Should().BeTrue(); + + values.Should().BeEquivalentTo(new Dictionary { ["a"] = "x" }); + } + + [Fact] + public void Peer_fields_decode_through_a_custom_proxy_when_no_serializer_is_registered() + { + var sut = Create(ValueSerializerRegistry.Empty, new ByteProxy()); + + sut.TryDecodeFields(new Dictionary { ["a"] = [0x10, 1] }, new CachePolicy(), out var values).Should().BeTrue(); + + values!["a"].Should().Equal(0x10, 1); + } + + [Fact] + public void Peer_fields_of_a_key_whose_policy_writes_json_are_not_decoded_by_the_default_policys_gate() + { + var sut = Create(new ValueSerializerRegistry([("fast", _fast, false)]), new ByteProxy(), new CachePolicy { Serializer = new SerializerPolicy { Write = "fast" } }); + var keyPolicy = new CachePolicy { Serializer = new SerializerPolicy { Write = "json" } }; + + sut.TryDecodeFields(new Dictionary { ["a"] = [0x10, 1, 2] }, keyPolicy, out var values).Should().BeFalse(); + + values.Should().BeNull(); + _fast.Deserialized.Should().Be(0); + } + + [Fact] + public void Peer_fields_decode_for_an_unknown_key_policy_with_the_default_proxy() + { + var sut = Create(out _); + + sut.TryDecodeFields(new Dictionary { ["a"] = Json("y"), ["b"] = Header(0x10, "x") }, policy: null, out var values).Should().BeTrue(); + + values.Should().BeEquivalentTo(new Dictionary { ["a"] = "y", ["b"] = "x" }); + } + + [Fact] + public void Peer_fields_are_not_decoded_for_an_unknown_key_policy_with_a_custom_proxy() + { + var sut = Create(new ValueSerializerRegistry([("fast", _fast, false)]), new ByteProxy()); + + sut.TryDecodeFields(new Dictionary { ["a"] = [1, 2] }, policy: null, out var values).Should().BeFalse(); + + values.Should().BeNull(); + } + + [Fact] + public void Peer_fields_whose_serializer_throws_are_not_applied() + { + var sut = Create(new ValueSerializerRegistry([("bad", new ThrowingValueSerializer(), false)])); + + sut.TryDecodeFields(new Dictionary { ["a"] = [0x10, 1] }, new CachePolicy(), out var values).Should().BeFalse(); + + values.Should().BeNull(); + } + + [Fact] + public void Peer_fields_that_fail_to_decode_for_any_reason_are_not_applied() + { + var sut = Create(ValueSerializerRegistry.Empty, new FailingProxy()); + + sut.TryDecodeFields(new Dictionary { ["a"] = [1] }, new CachePolicy(), out var values).Should().BeFalse(); + + values.Should().BeNull(); + } + + [Fact] + public async Task Stored_size_audit_reports_the_stored_length_including_the_header() + { + var logger = Substitute.For>(); + logger.IsEnabled(Arg.Any()).Returns(true); + var sut = Create(out var registry, logger: logger, auditThreshold: 1000); + CaptureWrites(); + var body = new string('x', 1000); + + await sut.SetAsync("k", new Dictionary { ["a"] = body }, new HashCacheEntryOptions(DateTimeOffset.UtcNow.AddMinutes(5)), ResolvedPolicy(registry), Token); + + var warnings = logger.ReceivedCalls() + .Where(c => c.GetMethodInfo().Name == nameof(ILogger.Log) && (LogLevel)c.GetArguments()[0]! == LogLevel.Warning) + .Select(c => c.GetArguments()[2]?.ToString() ?? string.Empty) + .Where(m => m.Contains("large value", StringComparison.Ordinal)) + .ToArray(); + warnings.Should().ContainSingle().Which.Should().Contain("length 1001"); + } + + private static byte[] Header(byte id, string body) => [id, .. Encoding.UTF8.GetBytes(body)]; + + private static byte[] Json(string value) => new SystemJsonByteSerializerProxy().Serialize(value)!; + + private static HashEntry Entry(string name, byte[] value) => new(Encoding.UTF8.GetBytes(name), value); + + private static byte[] Bytes(HashEntry[] entries, string name) => ((byte[]?)entries.Single(e => e.Name.ToString() == name).Value)!; + + private static CachePolicy ResolvedPolicy(ValueSerializerRegistry registry) => + new DefaultCachePolicyFactory(null, new CachePolicy { Serializer = new SerializerPolicy { Write = "fast" } }, valueSerializers: registry).Default!; + + private void AssertNothingDeleted() + { + _database.DidNotReceive().KeyDeleteAsync(Arg.Any(), Arg.Any()); + _transaction.DidNotReceive().KeyDeleteAsync(Arg.Any(), Arg.Any()); + } + + private RedisHashCache Create(out ValueSerializerRegistry registry, bool cacheNullValues = false, ILogger? logger = null, int auditThreshold = 0) + { + registry = new ValueSerializerRegistry([("fast", _fast, false)]); + return Create(registry, cacheNullValues: cacheNullValues, logger: logger, auditThreshold: auditThreshold); + } + + private async Task MethodsOf(Func operation) + { + var (metrics, dependencies) = (_telemetry.Metrics.Count, _telemetry.Dependencies.Count); + await operation(); + return + [ + .. _telemetry.Metrics.Skip(metrics).Select(m => m.Name).Where(n => n.StartsWith("Caching.Stats.", StringComparison.Ordinal)).Select(n => n.Split('.')[4]) + .Concat(_telemetry.Dependencies.Skip(dependencies).Select(d => d.Name)) + .Distinct() + .Order(StringComparer.Ordinal), + ]; + } + + private RedisHashCache Create(ValueSerializerRegistry registry, ISerializerProxy? proxy = null, CachePolicy? defaultPolicy = null, bool cacheNullValues = false, ILogger? logger = null, int auditThreshold = 0) + { + var connector = Substitute.For(); + connector.Database.Returns(_database); + connector.Version.Returns(new Version(6, 0)); + connector.IsConnected.Returns(true); + _database.Multiplexer.GetHashSlot(Arg.Any()).Returns(0); + _database.CreateTransaction(Arg.Any()).Returns(_transaction); + _transaction.ExecuteAsync(Arg.Any()).Returns(true); + var pipelines = Substitute.For(); + pipelines.Get(ResiliencePipelineNames.Read).Returns(EmptyResiliencePipeline.Instance); + pipelines.Get(ResiliencePipelineNames.Write).Returns(_write); + var strategy = Substitute.For(); + strategy.GetRedisKey(Arg.Any()).Returns(call => (RedisKey)$"test:hash:{call.Arg()}"); + var strategies = Substitute.For(); + strategies.Create(Arg.Any(), Arg.Any()).Returns(strategy); + var options = new RedisCacheOptions { RedisKeyStrategyFactory = strategies, ReadScriptsEnabled = false, CacheNullValues = cacheNullValues }; + var cacheOptions = new CacheOptions { AppShortName = "test", ConnectionMonitorEnabled = true, AuditEnabled = auditThreshold > 0, LargeValueThreshold = auditThreshold }; + + return new RedisHashCache( + connector, + proxy ?? new SystemJsonByteSerializerProxy(), + pipelines, + _telemetry, + options, + cacheOptions, + new DefaultCachePolicyFactory(null, defaultPolicy, valueSerializers: registry), + TimeProvider.System, + logger ?? NullLogger.Instance) + { + ValueSerializers = registry, + }; + } + + private List CaptureWrites() + { + var written = new List(); + _transaction.HashSetAsync(Arg.Any(), Arg.Do(written.Add), Arg.Any()).Returns(Task.CompletedTask); + _transaction.KeyExpireAsync(Arg.Any(), Arg.Any(), Arg.Any()).Returns(true); + _database.KeyDeleteAsync(Arg.Any(), Arg.Any()).Returns(true); + return written; + } + + private sealed class FailingProxy : ISerializerProxy + { + public byte[]? Serialize(object? value) => throw new NotSupportedException(); + + public T? Deserialize(byte[]? value) => throw new ArgumentException("not decodable"); + + public bool TryDeserialize(string? value, out T? result) => throw new NotSupportedException(); + + public bool TryDeserialize(object? value, out T? result) => throw new NotSupportedException(); + } + + private sealed class ByteProxy : ISerializerProxy + { + public byte[]? Serialize(object? value) => (byte[]?)value; + + public T? Deserialize(byte[]? value) => (T?)(object?)value; + + public bool TryDeserialize(string? value, out T? result) => throw new NotSupportedException(); + + public bool TryDeserialize(object? value, out T? result) => throw new NotSupportedException(); + } +} diff --git a/tests/UiPath.Caching.Tests/Redis/RedisHashCacheTests.cs b/tests/UiPath.Caching.Tests/Redis/RedisHashCacheTests.cs index c1d7cb4d..934ca175 100644 --- a/tests/UiPath.Caching.Tests/Redis/RedisHashCacheTests.cs +++ b/tests/UiPath.Caching.Tests/Redis/RedisHashCacheTests.cs @@ -1384,8 +1384,8 @@ public void Decoding_peer_fields_hands_each_cache_its_own_copy_of_the_event_byte byte[] shared = [1, 2, 3]; var encoded = new Dictionary { ["a"] = shared }; - Sut.TryDecodeFields(encoded, out var first).Should().BeTrue(); - Sut.TryDecodeFields(encoded, out var second).Should().BeTrue(); + Sut.TryDecodeFields(encoded, policy: null, out var first).Should().BeTrue(); + Sut.TryDecodeFields(encoded, policy: null, out var second).Should().BeTrue(); first!["a"].Should().Equal(shared).And.NotBeSameAs(shared); second!["a"].Should().NotBeSameAs(first["a"]).And.NotBeSameAs(shared); diff --git a/tests/UiPath.Caching.Tests/Redis/SerializerIntegrationTests.cs b/tests/UiPath.Caching.Tests/Redis/SerializerIntegrationTests.cs new file mode 100644 index 00000000..628d88ac --- /dev/null +++ b/tests/UiPath.Caching.Tests/Redis/SerializerIntegrationTests.cs @@ -0,0 +1,369 @@ +using Microsoft.Extensions.Caching.Distributed; +using Microsoft.Extensions.DependencyInjection; +using StackExchange.Redis; +using UiPath.Caching.Config; +using UiPath.Caching.Redis; +using UiPath.Caching.Telemetry; +using UiPath.Caching.Tests.Fakes; +using UiPath.Caching.Tests.Telemetry; + +namespace UiPath.Caching.Tests.Redis; + +[Collection("RedisIntegration")] +[Trait("Category", "Integration")] +public class SerializerIntegrationTests(RedisContainerFixture fixture) +{ + private const string FastPolicy = "fast"; + + private readonly PassthroughValueSerializer _serializer = new(0x10); + + private static CancellationToken Ct => TestContext.Current.CancellationToken; + + [Fact] + public async Task A_policy_serializer_writes_a_headered_value_that_any_policy_reads() + { + Assert.SkipUnless(fixture.Enabled, "Set RUN_REDIS_INTEGRATION_TESTS=1 (Docker required) to run."); + using var provider = Build(); + var (cache, policy) = CacheAndPolicy(provider); + await using var raw = await ConnectionMultiplexer.ConnectAsync(fixture.ConnectionString); + var key = Unique(); + + (await cache.SetAsync(key, "hello", policy, Ct)).Should().BeTrue(); + + var stored = await RawValueAsync(raw, key); + stored![0].Should().Be(0x10); + System.Text.Encoding.UTF8.GetString(stored, 1, stored.Length - 1).Should().Be("hello"); + (await cache.GetAsync(key, policy: null, Ct)).Should().Be("hello"); + (await cache.GetAsync(key, policy, Ct)).Should().Be("hello"); + (await cache.GetCacheEntryAsync(key, policy: null, Ct)).Value.Should().Be("hello"); + } + + [Fact] + public async Task A_value_round_trips_through_the_registered_serializer_the_policy_picks() + { + Assert.SkipUnless(fixture.Enabled, "Set RUN_REDIS_INTEGRATION_TESTS=1 (Docker required) to run."); + using var provider = Build(); + var (cache, policy) = CacheAndPolicy(provider); + var key = Unique(); + + await cache.SetAsync(key, "round trip", policy, Ct); + var read = await cache.GetAsync(key, policy, Ct); + + read.Should().Be("round trip"); + _serializer.Serialized.Should().Be(1); + _serializer.Deserialized.Should().Be(1); + _serializer.LastFormatId.Should().Be(0x10); + } + + [Fact] + public async Task Hash_fields_and_metadata_round_trip_through_the_registered_serializer_the_policy_picks() + { + Assert.SkipUnless(fixture.Enabled, "Set RUN_REDIS_INTEGRATION_TESTS=1 (Docker required) to run."); + using var provider = Build(); + var hashes = provider.GetRequiredService().CreateHashCache(KnownCacheProviderNames.Redis); + var policy = provider.GetRequiredService().Resolve(FastPolicy)!; + var key = Unique(); + var values = new Dictionary { ["a"] = "x", ["b"] = "y" }; + var options = new HashCacheEntryOptions(TimeToLive: TimeSpan.FromMinutes(5), Metadata: new Dictionary { ["m"] = "v" }); + + (await hashes.SetAsync(key, values, options, policy, Ct)).Should().BeTrue(); + var entry = await hashes.GetCacheEntryAsync(key, policy, Ct); + + entry.Value.Should().BeEquivalentTo(values); + entry.Metadata.Should().BeEquivalentTo(options.Metadata); + _serializer.Serialized.Should().Be(3); + _serializer.Deserialized.Should().Be(3); + } + + [Fact] + public async Task A_value_written_without_a_policy_serializer_is_plain_json() + { + Assert.SkipUnless(fixture.Enabled, "Set RUN_REDIS_INTEGRATION_TESTS=1 (Docker required) to run."); + using var provider = Build(); + var (cache, policy) = CacheAndPolicy(provider); + await using var raw = await ConnectionMultiplexer.ConnectAsync(fixture.ConnectionString); + var key = Unique(); + + await cache.SetAsync(key, "hello", policy: null, Ct); + + System.Text.Encoding.UTF8.GetString((await RawValueAsync(raw, key))!).Should().Be("\"hello\""); + (await cache.GetAsync(key, policy, Ct)).Should().Be("hello"); + } + + [Fact] + public async Task A_key_written_as_json_then_headered_then_json_reads_each_time() + { + Assert.SkipUnless(fixture.Enabled, "Set RUN_REDIS_INTEGRATION_TESTS=1 (Docker required) to run."); + using var provider = Build(); + var (cache, policy) = CacheAndPolicy(provider); + var key = Unique(); + + await cache.SetAsync(key, "one", policy: null, Ct); + (await cache.GetAsync(key, policy, Ct)).Should().Be("one"); + await cache.SetAsync(key, "two", policy, Ct); + (await cache.GetAsync(key, policy: null, Ct)).Should().Be("two"); + await cache.SetAsync(key, "three", policy: null, Ct); + (await cache.GetAsync(key, policy, Ct)).Should().Be("three"); + } + + [Fact] + public async Task GetOrAdd_and_TryAdd_and_batch_writes_use_the_policy_serializer() + { + Assert.SkipUnless(fixture.Enabled, "Set RUN_REDIS_INTEGRATION_TESTS=1 (Docker required) to run."); + using var provider = Build(); + var (cache, policy) = CacheAndPolicy(provider); + await using var raw = await ConnectionMultiplexer.ConnectAsync(fixture.ConnectionString); + var (added, generated, many) = (Unique(), Unique(), Unique()); + + (await cache.TryAddAsync(added, "a", policy, Ct)).Should().BeTrue(); + (await cache.GetOrAddAsync(generated, _ => Task.FromResult("g"), policy, Ct)).Should().Be("g"); + await cache.SetAsync([new KeyValuePair(many, "m")], policy, Ct); + + foreach (var key in new[] { added, generated, many }) + { + (await RawValueAsync(raw, key))![0].Should().Be(0x10, key); + } + + (await cache.GetAsync([added, generated, many], policy: null, Ct)).Select(v => v.Value).Should().Equal("a", "g", "m"); + } + + [Fact] + public async Task A_value_in_an_unknown_format_is_a_miss_and_the_key_is_removed() + { + Assert.SkipUnless(fixture.Enabled, "Set RUN_REDIS_INTEGRATION_TESTS=1 (Docker required) to run."); + using var provider = Build(); + var (cache, _) = CacheAndPolicy(provider); + await using var raw = await ConnectionMultiplexer.ConnectAsync(fixture.ConnectionString); + var key = Unique(); + await cache.SetAsync(key, "seed", policy: null, Ct); + var redisKey = RawKey(raw, key); + await raw.GetDatabase().StringSetAsync(redisKey, new byte[] { 0x1E, 1, 2 }); + + (await cache.GetAsync(key, policy: null, Ct)).Should().BeNull(); + + (await raw.GetDatabase().KeyExistsAsync(redisKey)).Should().BeFalse(); + } + + [Fact] + public async Task A_key_that_a_peer_rewrote_after_the_read_survives_the_removal_of_the_unreadable_value() + { + Assert.SkipUnless(fixture.Enabled, "Set RUN_REDIS_INTEGRATION_TESTS=1 (Docker required) to run."); + var telemetry = new RecordingTelemetryProvider(); + using var provider = Build(telemetry: telemetry); + var (cache, _) = CacheAndPolicy(provider); + await using var raw = await ConnectionMultiplexer.ConnectAsync(fixture.ConnectionString); + var key = Unique(); + await cache.SetAsync(key, "seed", policy: null, Ct); + var redisKey = RawKey(raw, key); + var newer = new SystemJsonByteSerializerProxy().Serialize("newer")!; + await raw.GetDatabase().StringSetAsync(redisKey, new byte[] { 0x1E, 1, 2 }); + telemetry.MetricRecorded = metric => + { + if (metric.Name == "cache.serializer.unreadable") + { + raw.GetDatabase().StringSet(redisKey, newer); + } + }; + + (await cache.GetAsync(key, policy: null, Ct)).Should().BeNull(); + + ((byte[]?)await raw.GetDatabase().StringGetAsync(redisKey)).Should().Equal(newer); + (await cache.GetAsync(key, policy: null, Ct)).Should().Be("newer"); + } + + [Fact] + public async Task A_key_that_a_peer_rewrote_after_a_batch_read_survives_the_removal_of_the_unreadable_value() + { + Assert.SkipUnless(fixture.Enabled, "Set RUN_REDIS_INTEGRATION_TESTS=1 (Docker required) to run."); + var telemetry = new RecordingTelemetryProvider(); + using var provider = Build(telemetry: telemetry); + var (cache, _) = CacheAndPolicy(provider); + await using var raw = await ConnectionMultiplexer.ConnectAsync(fixture.ConnectionString); + var (changed, stale) = (Unique(), Unique()); + await cache.SetAsync(changed, "seed", policy: null, Ct); + await cache.SetAsync(stale, "seed", policy: null, Ct); + var (changedKey, staleKey) = (RawKey(raw, changed), RawKey(raw, stale)); + var newer = new SystemJsonByteSerializerProxy().Serialize("newer")!; + await raw.GetDatabase().StringSetAsync(changedKey, new byte[] { 0x1E, 1, 2 }); + await raw.GetDatabase().StringSetAsync(staleKey, new byte[] { 0x1E, 1, 2 }); + var rewritten = false; + telemetry.MetricRecorded = metric => + { + if (metric.Name == "cache.serializer.unreadable" && !rewritten) + { + rewritten = true; + raw.GetDatabase().StringSet(changedKey, newer); + } + }; + + await cache.GetAsync([changed, stale], policy: null, Ct); + + ((byte[]?)await raw.GetDatabase().StringGetAsync(changedKey)).Should().Equal(newer); + (await raw.GetDatabase().KeyExistsAsync(staleKey)).Should().BeFalse(); + } + + [Fact] + public async Task A_node_with_no_registry_keeps_a_headered_value_and_one_with_another_registry_removes_it() + { + Assert.SkipUnless(fixture.Enabled, "Set RUN_REDIS_INTEGRATION_TESTS=1 (Docker required) to run."); + using var writer = Build(); + using var noRegistry = Build(registry: "none"); + using var otherRegistry = Build(registry: "other"); + var (cache, policy) = CacheAndPolicy(writer); + await using var raw = await ConnectionMultiplexer.ConnectAsync(fixture.ConnectionString); + var key = Unique(); + await cache.SetAsync(key, "headered", policy, Ct); + var redisKey = RawKey(raw, key); + var stored = (byte[]?)await raw.GetDatabase().StringGetAsync(redisKey); + + (await noRegistry.GetRequiredService().CreateCache(KnownCacheProviderNames.Redis).GetAsync(key, policy: null, Ct)).Should().BeNull(); + ((byte[]?)await raw.GetDatabase().StringGetAsync(redisKey)).Should().Equal(stored); + + (await otherRegistry.GetRequiredService().CreateCache(KnownCacheProviderNames.Redis).GetAsync(key, policy: null, Ct)).Should().BeNull(); + (await raw.GetDatabase().KeyExistsAsync(redisKey)).Should().BeFalse(); + } + + [Fact] + public async Task A_headered_value_whose_serializer_throws_is_a_miss_and_the_key_stays() + { + Assert.SkipUnless(fixture.Enabled, "Set RUN_REDIS_INTEGRATION_TESTS=1 (Docker required) to run."); + using var provider = Build(registry: "throwing"); + var cache = provider.GetRequiredService().CreateCache(KnownCacheProviderNames.Redis); + await using var raw = await ConnectionMultiplexer.ConnectAsync(fixture.ConnectionString); + var key = Unique(); + await cache.SetAsync(key, "seed", policy: null, Ct); + var redisKey = RawKey(raw, key); + byte[] stored = [0x10, 1, 2]; + await raw.GetDatabase().StringSetAsync(redisKey, stored); + + (await cache.GetAsync(key, policy: null, Ct)).Should().BeNull(); + + ((byte[]?)await raw.GetDatabase().StringGetAsync(redisKey)).Should().Equal(stored); + } + + [Fact] + public async Task A_hash_with_a_field_in_an_unknown_format_is_a_miss_and_the_hash_stays() + { + Assert.SkipUnless(fixture.Enabled, "Set RUN_REDIS_INTEGRATION_TESTS=1 (Docker required) to run."); + using var provider = Build(); + var hashes = provider.GetRequiredService().CreateHashCache(KnownCacheProviderNames.Redis); + await using var raw = await ConnectionMultiplexer.ConnectAsync(fixture.ConnectionString); + var key = Unique(); + await hashes.SetAsync(key, new Dictionary { ["a"] = "x" }, new HashCacheEntryOptions(TimeToLive: TimeSpan.FromMinutes(5)), policy: null, Ct); + var redisKey = RawKey(raw, key); + await raw.GetDatabase().HashSetAsync(redisKey, "b", new byte[] { 0x1E, 1 }); + + (await hashes.GetAsync(key, policy: null, Ct)).Should().BeEmpty(); + + (await raw.GetDatabase().HashGetAsync(redisKey, "b")).HasValue.Should().BeTrue(); + (await raw.GetDatabase().HashLengthAsync(redisKey)).Should().BeGreaterThanOrEqualTo(2); + } + + [Theory] + [InlineData(1000)] + [InlineData(1)] + public async Task A_hash_replace_with_empty_metadata_drops_the_old_metadata_for_a_non_json_write(int fieldBatchSize) + { + Assert.SkipUnless(fixture.Enabled, "Set RUN_REDIS_INTEGRATION_TESTS=1 (Docker required) to run."); + using var provider = Build(fieldBatchSize: fieldBatchSize); + var hashes = provider.GetRequiredService().CreateHashCache(KnownCacheProviderNames.Redis); + var policy = provider.GetRequiredService().Resolve(FastPolicy)!; + var key = Unique(); + var values = new Dictionary { ["a"] = "x", ["b"] = "y" }; + var withMetadata = new HashCacheEntryOptions(TimeToLive: TimeSpan.FromMinutes(5), Metadata: new Dictionary { ["m"] = "v" }); + var replacing = new HashCacheEntryOptions(TimeToLive: TimeSpan.FromMinutes(5), Metadata: new Dictionary(), SetOption: HashCacheSetOption.HashReplace); + await hashes.SetAsync(key, values, withMetadata, policy, Ct); + (await hashes.GetCacheEntryAsync(key, policy, Ct)).Metadata.Should().NotBeNullOrEmpty(); + + (await hashes.SetAsync(key, values, replacing, policy, Ct)).Should().BeTrue(); + + var entry = await hashes.GetCacheEntryAsync(key, policy, Ct); + entry.Value.Should().BeEquivalentTo(values); + entry.Metadata.Should().BeNull(); + } + + [Fact] + public async Task A_large_value_round_trips() + { + Assert.SkipUnless(fixture.Enabled, "Set RUN_REDIS_INTEGRATION_TESTS=1 (Docker required) to run."); + using var provider = Build(); + var (cache, policy) = CacheAndPolicy(provider); + var key = Unique(); + var large = new string('x', 200 * 1024); + + await cache.SetAsync(key, large, policy, Ct); + + (await cache.GetAsync(key, policy: null, Ct)).Should().Be(large); + } + + [Fact] + public async Task Distributed_cache_bytes_that_start_with_a_header_byte_read_back_unchanged() + { + Assert.SkipUnless(fixture.Enabled, "Set RUN_REDIS_INTEGRATION_TESTS=1 (Docker required) to run."); + using var provider = Build(distributed: true); + var cache = provider.GetRequiredService(); + var key = Unique(); + + await cache.SetAsync(key, [0x10, 1, 2], new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(5) }, Ct); + + (await cache.GetAsync(key, Ct)).Should().Equal(0x10, 1, 2); + } + + private static string Unique() => $"it-{Guid.NewGuid():N}"; + + private static (ICache Cache, CachePolicy Policy) CacheAndPolicy(ServiceProvider provider) => + (provider.GetRequiredService().CreateCache(KnownCacheProviderNames.Redis), provider.GetRequiredService().Resolve(FastPolicy)!); + + private static RedisKey RawKey(ConnectionMultiplexer raw, string key) => + raw.GetServer(raw.GetEndPoints()[0]).Keys(pattern: $"*{key}*").Single(); + + private static async Task RawValueAsync(ConnectionMultiplexer raw, string key) => + await raw.GetDatabase().StringGetAsync(RawKey(raw, key)); + + private ServiceProvider Build(bool distributed = false, string registry = "fast", RecordingTelemetryProvider? telemetry = null, int fieldBatchSize = 1000) + { + var services = new ServiceCollection(); + if (telemetry is not null) + { + services.AddSingleton(telemetry); + } + + return services + .AddCaching( + b => + { + b.AddRedisConnection(o => o.ConnectionString = fixture.ConnectionString); + b.AddRedis(o => o.HashFieldBatchSize = fieldBatchSize); + switch (registry) + { + case "fast": + b.AddValueSerializer("fast", _ => _serializer); + break; + case "other": + b.AddValueSerializer("other", _ => new PassthroughValueSerializer(0x12)); + break; + case "throwing": + b.AddValueSerializer("bad", _ => new ThrowingValueSerializer()); + break; + } + + if (distributed) + { + b.AddDistributedCache(KnownCacheProviderNames.Redis); + } + }, + o => + { + o.AppShortName = "serializerit"; + if (registry == "fast") + { + o.Policies[FastPolicy] = new CachePolicy + { + DistributedExpiration = TimeSpan.FromMinutes(5), + Serializer = new SerializerPolicy { Write = "fast" }, + }; + } + }) + .BuildServiceProvider(); + } +} diff --git a/tests/UiPath.Caching.Tests/Serialization/ValueFormatResolverTests.cs b/tests/UiPath.Caching.Tests/Serialization/ValueFormatResolverTests.cs new file mode 100644 index 00000000..aabccc6a --- /dev/null +++ b/tests/UiPath.Caching.Tests/Serialization/ValueFormatResolverTests.cs @@ -0,0 +1,105 @@ +using UiPath.Caching.Config; +using UiPath.Caching.Serialization; +using UiPath.Caching.Tests.Fakes; + +namespace UiPath.Caching.Tests.Serialization; + +public class ValueFormatResolverTests +{ + private static readonly CachePolicy WritesFast = new() { Serializer = new SerializerPolicy { Write = "fast" } }; + + private readonly PassthroughValueSerializer _fast = new(0x10); + + [Fact] + public void A_policy_without_a_serializer_writes_the_default_policys() + { + var sut = Resolver(defaultPolicy: WritesFast); + + var format = sut.For(new CachePolicy { DistributedExpiration = TimeSpan.FromMinutes(1) }, forWrite: true); + + format.Writer.Should().BeSameAs(_fast); + } + + [Fact] + public void A_policy_that_sets_only_read_still_writes_the_default_policys_serializer() + { + var sut = Resolver(defaultPolicy: WritesFast); + + var format = sut.For(new CachePolicy { Serializer = new SerializerPolicy { Read = ["json", "fast"] } }, forWrite: true); + + format.Writer.Should().BeSameAs(_fast); + } + + [Fact] + public void A_policy_that_writes_json_wins_over_the_default_policys_serializer() + { + var sut = Resolver(defaultPolicy: WritesFast); + + sut.For(new CachePolicy { Serializer = new SerializerPolicy { Write = "json" } }, forWrite: true).Writer.Should().BeNull(); + } + + [Fact] + public void A_policy_that_names_its_own_serializer_wins_over_the_default_policys() + { + var other = new PassthroughValueSerializer(0x12); + var sut = Resolver(defaultPolicy: WritesFast, other: other); + + sut.For(new CachePolicy { Serializer = new SerializerPolicy { Write = "other" } }, forWrite: true).Writer.Should().BeSameAs(other); + } + + [Fact] + public void A_policy_with_a_resolved_slot_keeps_it_over_the_default_policys() + { + var other = new PassthroughValueSerializer(0x12); + var resolved = CachePolicyMerger.WithWriteSerializer(new CachePolicy { Serializer = new SerializerPolicy { Write = "other" } }, other); + var sut = Resolver(defaultPolicy: WritesFast, other: other); + + sut.For(resolved, forWrite: true).Writer.Should().BeSameAs(other); + } + + [Fact] + public void A_read_takes_the_default_policys_write_for_the_gate_of_a_policy_without_one() + { + var sut = Resolver(proxy: new RawByteSerializerProxy(), defaultPolicy: WritesFast); + + sut.For(new CachePolicy(), forWrite: false).Sniff.Should().BeTrue(); + sut.For(new CachePolicy { Serializer = new SerializerPolicy { Write = "json" } }, forWrite: false).Sniff.Should().BeFalse(); + } + + [Fact] + public void The_json_default_resolves_without_allocating() + { + var sut = Resolver(); + var adHoc = new CachePolicy { DistributedExpiration = TimeSpan.FromMinutes(1) }; + var json = new CachePolicy { Serializer = new SerializerPolicy { Write = "json" } }; + Measure(sut, adHoc, json); + + var allocated = Measure(sut, adHoc, json); + + allocated.Should().Be(0); + } + + private static long Measure(ValueFormatResolver sut, CachePolicy adHoc, CachePolicy json) + { + var before = GC.GetAllocatedBytesForCurrentThread(); + for (var i = 0; i < 100; i++) + { + _ = sut.For(null, forWrite: true); + _ = sut.For(adHoc, forWrite: true); + _ = sut.For(json, forWrite: false); + } + + return GC.GetAllocatedBytesForCurrentThread() - before; + } + + private ValueFormatResolver Resolver(ISerializerProxy? proxy = null, CachePolicy? defaultPolicy = null, ICacheValueSerializer? other = null) + { + List<(string, ICacheValueSerializer, bool)> entries = [("fast", _fast, false)]; + if (other is not null) + { + entries.Add(("other", other, false)); + } + + return new(proxy ?? new SystemJsonByteSerializerProxy(), new ValueSerializerRegistry(entries), defaultPolicy ?? new CachePolicy()); + } +} diff --git a/tests/UiPath.Caching.Tests/Serialization/ValueReaderTests.cs b/tests/UiPath.Caching.Tests/Serialization/ValueReaderTests.cs new file mode 100644 index 00000000..cad378e1 --- /dev/null +++ b/tests/UiPath.Caching.Tests/Serialization/ValueReaderTests.cs @@ -0,0 +1,316 @@ +using System.Text; +using System.Text.Json; +using UiPath.Caching.Config; +using UiPath.Caching.Serialization; +using UiPath.Caching.Tests.Fakes; + +namespace UiPath.Caching.Tests.Serialization; + +public class ValueReaderTests +{ + private static readonly CachePolicy NamesASerializer = new() { Serializer = new SerializerPolicy { Write = "fast" } }; + + public static TheoryData JsonValues() => new() + { + { "\"text\"", "string" }, + { "42", "number" }, + { "null", "null literal" }, + { " {\"a\":1}", "space-led object" }, + { "\n[1]", "newline-led array" }, + { "\r\n\"x\"", "return-led string" }, + { "\t7", "tab-led number" }, + }; + + public static TheoryData Sniffing() => new() + { + { "exact json proxy, no policy", false, true }, + { "exact json proxy, policy names a serializer", true, true }, + { "subclass, no policy", false, false }, + { "subclass, policy names a serializer", true, true }, + { "raw proxy, no policy", false, false }, + { "raw proxy, policy names a serializer", true, true }, + { "custom proxy, no policy", false, false }, + { "custom proxy, policy names a serializer", true, true }, + }; + + [Fact] + public void A_header_picks_the_serializer_and_is_sliced_off() + { + var fast = new PassthroughValueSerializer(0x10, 0x11); + var sut = Reader(registry: Registry(("fast", fast))); + + var outcome = sut.Read([0x11, 1, 2, 3]); + + outcome.IsUnreadable.Should().BeFalse(); + outcome.Value.Should().Equal(1, 2, 3); + fast.LastFormatId.Should().Be(0x11); + } + + [Fact] + public void A_headered_value_reads_through_the_serializer_that_owns_its_id() + { + var first = new PassthroughValueSerializer(0x10); + var second = new PassthroughValueSerializer(0x12); + var sut = Reader(registry: Registry(("first", first), ("second", second))); + + sut.Read([0x12, (byte)'o', (byte)'k']).Value.Should().Be("ok"); + + first.Deserialized.Should().Be(0); + second.Deserialized.Should().Be(1); + } + + [Fact] + public void An_extended_id_is_the_marker_plus_one_byte() + { + var fast = new PassthroughValueSerializer(0x105); + var sut = Reader(registry: Registry(("fast", fast))); + + var outcome = sut.Read([0x1F, 0x05, 9, 8]); + + outcome.Value.Should().Equal(9, 8); + fast.LastFormatId.Should().Be(0x105); + } + + [Fact] + public void An_unregistered_id_is_unreadable_and_names_the_id() + { + var fast = new PassthroughValueSerializer(0x10); + var sut = Reader(registry: Registry(("fast", fast))); + + var outcome = sut.Read([0x11, 1]); + + outcome.IsUnreadable.Should().BeTrue(); + outcome.UnreadableFormat.Should().Be(0x11); + outcome.Value.Should().BeNull(); + fast.Deserialized.Should().Be(0); + } + + [Fact] + public void A_serializer_that_throws_is_a_miss_with_its_failure_and_not_an_unreadable_format() + { + var sut = Reader(registry: Registry(("bad", new ThrowingValueSerializer()))); + + var outcome = sut.Read([0x10, 1, 2]); + + outcome.IsUnreadable.Should().BeFalse(); + outcome.IsMiss.Should().BeTrue(); + outcome.UnreadableFormat.Should().Be(0); + outcome.Failure.Should().BeOfType(); + outcome.Value.Should().BeNull(); + } + + [Theory] + [InlineData(typeof(ObjectDisposedException))] + [InlineData(typeof(InvalidOperationException))] + [InlineData(typeof(NotSupportedException))] + public void Whatever_a_known_serializer_throws_is_a_miss_that_keeps_the_key(Type failure) + { + var sut = Reader(registry: Registry(("bad", new ThrowingValueSerializer { Failure = (Exception)Activator.CreateInstance(failure, "x")! }))); + + var outcome = sut.Read([0x10, 1, 2]); + + outcome.IsUnreadable.Should().BeFalse(); + outcome.Failure.Should().BeOfType(failure); + } + + [Fact] + public void An_out_of_memory_failure_is_never_swallowed() + { + var sut = Reader(registry: Registry(("bad", new ThrowingValueSerializer { Failure = new OutOfMemoryException() }))); + + var act = () => sut.Read([0x10, 1, 2]); + + act.Should().Throw(); + } + + [Fact] + public void An_out_of_memory_from_headerless_json_is_a_miss_as_with_no_registry() + { + var sut = Reader(new OutOfMemoryProxy()); + + var outcome = sut.Read([0x22, 0x61, 0x22]); + + outcome.Failure.Should().BeOfType(); + outcome.IsMiss.Should().BeTrue(); + outcome.IsUnreadable.Should().BeFalse(); + } + + [Fact] + public void A_header_with_no_body_goes_to_the_serializer_and_fails_with_it() + { + var sut = Reader(registry: Registry(("bad", new ThrowingValueSerializer()))); + + var outcome = sut.Read([0x10]); + + outcome.IsUnreadable.Should().BeFalse(); + outcome.Failure.Should().NotBeNull(); + } + + [Fact] + public void Json_that_does_not_parse_still_throws() + { + var sut = Reader(); + + var act = () => sut.Read(Encoding.UTF8.GetBytes("{not json")); + + act.Should().Throw(); + } + + [Fact] + public void An_unknown_id_has_no_failure() + { + Reader().Read([0x1E, 1]).Failure.Should().BeNull(); + } + + [Fact] + public void An_unregistered_extended_id_is_unreadable() + { + var sut = Reader(registry: Registry(("fast", new PassthroughValueSerializer(0x105)))); + + sut.Read([0x1F, 0x06, 1]).UnreadableFormat.Should().Be(0x106); + } + + [Fact] + public void A_lone_extended_marker_is_unreadable() + { + var sut = Reader(); + + var outcome = sut.Read([0x1F]); + + outcome.IsUnreadable.Should().BeTrue(); + outcome.UnreadableFormat.Should().Be(-1); + } + + [Theory] + [MemberData(nameof(JsonValues))] + public void Headerless_bytes_read_as_json_even_when_serializers_are_registered(string json, string because) + { + var fast = new PassthroughValueSerializer(); + var sut = Reader(registry: Registry(("fast", fast))); + + var outcome = sut.Read(Encoding.UTF8.GetBytes(json)); + + outcome.IsUnreadable.Should().BeFalse(because); + outcome.Value.GetRawText().Should().Be(JsonDocument.Parse(json).RootElement.GetRawText(), because); + fast.Deserialized.Should().Be(0, because); + } + + [Fact] + public void A_base64_byte_array_written_by_the_default_proxy_reads_back() + { + var proxy = new SystemJsonByteSerializerProxy(); + var original = new byte[] { 2, 3, 0xFF, 0x10 }; + var sut = Reader(proxy); + + sut.Read(proxy.Serialize(original)!).Value.Should().Equal(original); + } + + [Fact] + public void A_cached_null_literal_reads_as_null() + { + var proxy = new SystemJsonByteSerializerProxy(); + + Reader(proxy).Read(proxy.Serialize(null)!).Value.Should().BeNull(); + } + + [Theory] + [MemberData(nameof(Sniffing))] + public void Sniffing_is_gated_by_the_proxy_and_the_policy(string scenario, bool withPolicy, bool expected) + { + ISerializerProxy proxy = scenario.Split(',')[0] switch + { + "exact json proxy" => new SystemJsonByteSerializerProxy(), + "subclass" => new JsonSubclass(), + "raw proxy" => new RawByteSerializerProxy(), + _ => new CustomProxy(), + }; + + Reader(proxy).SniffsFor(withPolicy ? NamesASerializer : null).Should().Be(expected, scenario); + } + + [Theory] + [InlineData("json")] + [InlineData("JSON")] + [InlineData("Json")] + public void A_json_policy_does_not_open_the_gate_for_a_raw_proxy(string write) + { + var jsonPolicy = new CachePolicy { Serializer = new SerializerPolicy { Write = write } }; + + Reader(new RawByteSerializerProxy()).SniffsFor(jsonPolicy).Should().BeFalse(); + Reader(new CustomProxy()).SniffsFor(jsonPolicy).Should().BeFalse(); + Reader(new JsonSubclass()).SniffsFor(jsonPolicy).Should().BeFalse(); + } + + [Fact] + public void A_json_policy_that_lists_other_formats_to_read_does_not_open_the_gate_for_a_raw_proxy() + { + var migratingBack = new CachePolicy { Serializer = new SerializerPolicy { Write = "json", Read = ["json", "fast"] } }; + + Reader(new RawByteSerializerProxy()).SniffsFor(migratingBack).Should().BeFalse(); + } + + [Theory] + [InlineData("json")] + [InlineData("fast")] + public void The_default_json_proxy_sniffs_whatever_the_policy_writes(string write) + { + var policy = new CachePolicy { Serializer = new SerializerPolicy { Write = write, Read = ["json", "fast"] } }; + + Reader(new SystemJsonByteSerializerProxy()).SniffsFor(policy).Should().BeTrue(); + } + + [Fact] + public void A_resolved_write_serializer_opens_the_gate_for_a_raw_proxy() + { + var resolved = CachePolicyMerger.WithWriteSerializer(new CachePolicy(), new PassthroughValueSerializer()); + + Reader(new RawByteSerializerProxy()).SniffsFor(resolved).Should().BeTrue(); + } + + [Fact] + public void A_policy_without_a_write_serializer_does_not_open_the_gate() + { + var readsOnly = new CachePolicy { Serializer = new SerializerPolicy { Read = ["fast"] } }; + + Reader(new RawByteSerializerProxy()).SniffsFor(readsOnly).Should().BeFalse(); + } + + [Fact] + public void An_empty_registry_never_sniffs() + { + var sut = Reader(registry: ValueSerializerRegistry.Empty); + + sut.SniffsFor(null).Should().BeFalse(); + sut.SniffsFor(NamesASerializer).Should().BeFalse(); + } + + private static ValueSerializerRegistry Registry(params (string Name, ICacheValueSerializer Serializer)[] entries) => + new(entries.Select(e => (e.Name, e.Serializer, false))); + + private static ValueReader Reader(ISerializerProxy? json = null, ValueSerializerRegistry? registry = null) => + new(json ?? new SystemJsonByteSerializerProxy(), registry ?? Registry(("fast", new PassthroughValueSerializer()))); + + private sealed class CustomProxy : ISerializerProxy + { + public byte[]? Serialize(object? value) => throw new NotSupportedException(); + + public T? Deserialize(byte[]? value) => throw new NotSupportedException(); + + public bool TryDeserialize(string? value, out T? result) => throw new NotSupportedException(); + + public bool TryDeserialize(object? value, out T? result) => throw new NotSupportedException(); + } + + private sealed class OutOfMemoryProxy : ISerializerProxy + { + public byte[]? Serialize(object? value) => throw new NotSupportedException(); + + public T? Deserialize(byte[]? value) => throw new OutOfMemoryException(); + + public bool TryDeserialize(string? value, out T? result) => throw new NotSupportedException(); + + public bool TryDeserialize(object? value, out T? result) => throw new NotSupportedException(); + } + + private sealed class JsonSubclass : SystemJsonByteSerializerProxy; +}