Files
richard-churchman a6bbd81271 Features to exercise greater control over cache burden and improve cache warming:
* Added `Cache` flag to `EntityAnalysisRequestXPath`, updating POCOs, DTOs, and synchronisation mappings. Included migrations and updated pages and JS for new fields.
* Updated the Completions Endpoint to only return values if Cache is enabled in the EntityAnalysisModelRequestXPath for EntityAnalysisModelAbstractionRule configuration.
* Updated the parser to ignore fields if the parse processing type is Abstraction Rule, so to ensure that an Abstraction Rule does not compile anywhere, in the controller or syncronisation, if it trys to use a key that exists but is marked as Cache.
* Created migrations for the cache flag and added indexes to cache key columns.
* Implemented a new serialisation pattern for message pack payloads to reduce byte storage of names, replacing them with a `CacheIndexId` serialised as a single byte.
* Implemented indexing logic for `CacheIndexId`, which increments within a transaction per `EntityAnalysisModel`. Created `CacheIndexId` values for legacy data.
* Created migrations for legacy Redis cache payload data to adopt the new `CacheIndexId` format. Added a unique composite index to enforce sequencing and eliminate conflict risks.
* Implemented a new LRU cache warming strategy that populates the LRU cache based on journal usage. During warming, cache entries are populated in order of most recently used journals, exiting when byte limits are reached. This replaces a full cursor scan of payload data, reducing container startup time and mitigating Redis herd-stampede risks. Each journal recall writes to a sorted set (`LruJournals`) that maintains an ordered activity list for cache population. On payload entry removal, the corresponding journal is checked and the `LruJournal` key is removed if no further data exists. Added a `SortedSetLruJournalRemove` enum for task tracking. Implemented an `IsFull` method on the local LRU cache to exit warming once full.
* Updated TTL Counters to process `double` values instead of `int` to support summing of integer and float payload types, making Volume and Velocity rules available to TTL Counters and long-term state. Updated POCOs, DTOs, and synchronisation mappings, applied logic in invocation processes, and included migrations. Updated pages and JS for new fields.
* Updated TTL Counters to specify date resolution for TTL Counter entries, reducing memory footprint by allowing aggregation at broader date roundings (e.g. for long-term state, a daily entry expiry is sufficient). Updated VB.NET code wrappers for the new generic.
* Updated TTL Counters to support an `EnableSum` flag, which uses a `TtlCounterDataValue` field to specify the value to sum. Implemented a dropdown in the page and JS that returns only integer and float data types for selection.
* Updated the efficient no-boxing dictionary to instantiate on the basis of generic type, enabling use with both `int` (message pack) and `string` (core processing and legacy payload migrations). This touched a large number of files and is the primary reason for the size of this commit. Updated the dictionary to support addition of internal values via the interface for serialisation and mapping. Updated JSON serialisers to support the generic.
* Implemented invocation mapping logic for insert and recall between the indexed Redis payload and the string-keyed payload required for processing.
* Bumped the message pack envelope version flag to version 2 to reflect the new payload formats (noting that all data has been migrated and version 1 no longer exists in Redis).
* Updated documentation to cover `LruJournals` in the Redis section, the `Cache` field on the `EntityAnalysisModelsRequestXPath` page, and the `ResolutionInterval` and `TtlCounterDataValue` fields on the `EntityAnalysisTtlCounters` page.
2026-02-27 15:40:15 +02:00

343 lines
15 KiB
C#

/* Copyright (C) 2022-present Jube Holdings Limited.
*
* This file is part of Jube™ software.
*
* Jube™ is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License
* as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* Jube™ is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License along with Jube™. If not,
* see <https://www.gnu.org/licenses/>.
*/
namespace Jube.Migrations.Branches
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Cache;
using Cache.Redis.Serialization;
using Cache.Redis.Serialization.DictionaryNoBoxing.MessagePack;
using Dictionary;
using DynamicEnvironment;
using FluentMigrator;
using log4net;
using MessagePack;
using MessagePack.Resolvers;
using StackExchange.Redis;
[Migration(20251019102200)]
public class GitHubIssueBranch84(
CacheService cacheService,
DynamicEnvironment dynamicEnvironment,
ILog log) : Migration
{
public override void Up()
{
CreateLocalCacheInstance();
CreateLocalCacheInstanceKey();
CreateLocalCacheInstanceLru();
UpdateInlineScriptCodeForDictionaryNoBoxingAndDeprecatedAttributes();
ChangeKeyNamesIfNeededOrMigrateDataToEnvelopeNoBoxing();
}
private void CreateLocalCacheInstance()
{
Create.Table("LocalCacheInstance")
.WithColumn("Id").AsInt32().PrimaryKey().Identity()
.WithColumn("Instance").AsString().Nullable()
.WithColumn("Guid").AsGuid().Nullable()
.WithColumn("CreatedDate").AsDateTime().Nullable()
.WithColumn("Fill").AsByte().Nullable()
.WithColumn("FillStartedDate").AsDateTime().Nullable()
.WithColumn("FillCount").AsInt64().Nullable()
.WithColumn("FillBytes").AsInt64().Nullable()
.WithColumn("Filled").AsByte().Nullable()
.WithColumn("FillEndedDate").AsDateTime().Nullable()
.WithColumn("Bytes").AsInt64().Nullable()
.WithColumn("Count").AsInt64().Nullable()
.WithColumn("HeapSizeBytes").AsInt64().Nullable()
.WithColumn("TotalCommittedBytes").AsInt64().Nullable()
.WithColumn("UpdatedDate").AsDateTime().Nullable();
}
private void CreateLocalCacheInstanceKey()
{
Create.Table("LocalCacheInstanceKey")
.WithColumn("Id").AsInt32().PrimaryKey().Identity()
.WithColumn("LocalCacheInstanceId").AsInt32().Nullable()
.WithColumn("Key").AsString().Nullable()
.WithColumn("CreatedDate").AsDateTime().Nullable()
.WithColumn("Requests").AsInt64().Nullable()
.WithColumn("Misses").AsInt32().Nullable()
.WithColumn("MissRemoteResponseTime").AsInt64().Nullable()
.WithColumn("UnpackResponseTime").AsInt64().Nullable()
.WithColumn("HashSetSubscriptions").AsInt32().Nullable()
.WithColumn("HashRemove").AsInt32().Nullable()
.WithColumn("HashRemoveMiss").AsInt32().Nullable()
.WithColumn("HashRemoveSubscription").AsInt32().Nullable()
.WithColumn("HashRemoveSubscriptionMiss").AsInt32().Nullable()
.WithColumn("DualMiss").AsInt32().Nullable();
Create.Index().OnTable("LocalCacheInstanceKey")
.OnColumn("LocalCacheInstanceId").Ascending();
Create.ForeignKey().FromTable("LocalCacheInstanceKey").ForeignColumn("LocalCacheInstanceId")
.ToTable("LocalCacheInstance").PrimaryColumn("Id");
}
private void CreateLocalCacheInstanceLru()
{
Create.Table("LocalCacheInstanceLru")
.WithColumn("Id").AsInt32().PrimaryKey().Identity()
.WithColumn("LocalCacheInstanceId").AsInt32().Nullable()
.WithColumn("CreatedDate").AsDateTime().Nullable()
.WithColumn("Bytes").AsInt64().Nullable()
.WithColumn("Count").AsInt64().Nullable()
.WithColumn("RequestBytes").AsInt64().Nullable()
.WithColumn("RequestCount").AsInt64().Nullable()
.WithColumn("AddBytes").AsInt64().Nullable()
.WithColumn("AddCount").AsInt64().Nullable()
.WithColumn("RemoveBytes").AsInt64().Nullable()
.WithColumn("RemoveCount").AsInt64().Nullable()
.WithColumn("EvictionBytes").AsInt64().Nullable()
.WithColumn("EvictionCount").AsInt64().Nullable()
.WithColumn("UpdateBytes").AsInt64().Nullable()
.WithColumn("UpdateCount").AsInt64().Nullable();
Create.Index().OnTable("LocalCacheInstanceLru")
.OnColumn("LocalCacheInstanceId").Ascending();
Create.ForeignKey().FromTable("LocalCacheInstanceLru").ForeignColumn("LocalCacheInstanceId")
.ToTable("LocalCacheInstance").PrimaryColumn("Id");
}
private void UpdateInlineScriptCodeForDictionaryNoBoxingAndDeprecatedAttributes()
{
var code = "Imports log4net\nI" +
"mports System\n" +
"Imports System.Collections.Generic\n" +
"Imports Jube.Dictionary\n" +
"Imports Jube.Dictionary.Attributes\n" +
"Imports Microsoft.VisualBasic\n" +
"Public Class IssueOTP\n" +
" Inherits System.Attribute\n\n" +
" <ResponsePayload>\n" +
" Public Property OTP As String\n\n" +
" Private _log as ILog\n" +
" Public Sub New(Log As ILog)\n" +
" _log = Log\n End Sub\n\n" +
" Public Sub Execute(Data As DictionaryNoBoxing, Log As ILog)\n" +
" Data.Add(\"OTP\", RandomDigits(6))\n" +
" End Sub\n\n" +
" Private Function RandomDigits(ByVal length As Integer) As String\n" +
" Dim random = New Random()\n" +
" Dim s As String = String.Empty\n" +
" For i As Integer = 0 To length - 1\n" +
" s = String.Concat(s, random.[Next](10).ToString())\n" +
" Next\n" +
" Return s\n" +
" End Function\n" +
"End Class";
Update.Table("EntityAnalysisInlineScript").Set(new
{
Code = code
}).Where(new
{
Id = 1
});
}
private void ChangeKeyNamesIfNeededOrMigrateDataToEnvelopeNoBoxing()
{
var redisServers = cacheService.ConnectionMultiplexer?.GetEndPoints()
.Select(redisEndpoint => cacheService.ConnectionMultiplexer.GetServer(redisEndpoint)).ToList();
if (redisServers == null)
{
return;
}
const string journalPrefix = "Journal";
const string payloadLatestPrefix = "PayloadLatest";
var messagePackSerializerOptionsOld = MessagePackSerializerOptionsHelper
.ContractlessStandardResolverWithCompressionMessagePackSerializerOptions(true);
var messagePackSerializerOptionsNew = MessagePackSerializerOptions.Standard
.WithResolver(
CompositeResolver.Create(new EnvelopeDictionaryNoBoxingMessagePackFormatter<string>())
).WithCompression(dynamicEnvironment.AppSettings("RedisMessagePackCompression").Equals("True", StringComparison.OrdinalIgnoreCase)
? MessagePackCompression.Lz4BlockArray : MessagePackCompression.None);
foreach (var key in redisServers.SelectMany(redisServer => redisServer.Keys()))
{
try
{
var splits = key.ToString().Split(":");
if (splits[0] == "Payload")
{
if (CheckAndChangeKeyNameGivenNumberOfSplits(messagePackSerializerOptionsOld, messagePackSerializerOptionsNew,
splits, key, payloadLatestPrefix, journalPrefix))
{
continue;
}
cacheService.RedisDatabase?.KeyRename(key, String.Join(":", splits));
}
if (splits[0] != "PayloadCount")
{
continue;
}
CorrectWrongGuidFormatInThePayloadCountHashSet(splits);
}
catch (Exception ex)
{
log.Error($"Could not migrate key {key} for exception {ex}.");
}
}
}
private void CorrectWrongGuidFormatInThePayloadCountHashSet(string[] splits)
{
var redisKeyPayloadCount = String.Join(":", splits);
foreach (var hashEntry in cacheService.RedisDatabase.HashScan(redisKeyPayloadCount))
{
try
{
RenameHashKeyForCorrectGuidFormat(hashEntry, redisKeyPayloadCount);
}
catch (Exception ex)
{
log.Error($"PayloadCount key migration caused error for hashEntry {hashEntry.Name}: {ex.Message}");
}
}
}
private void RenameHashKeyForCorrectGuidFormat(HashEntry hashEntry, string redisKeyPayloadCount)
{
var guid = Guid.Parse(hashEntry.Name);
cacheService.RedisDatabase?.HashSet(redisKeyPayloadCount, guid.ToString("N"), hashEntry.Value);
cacheService.RedisDatabase?.HashDelete(redisKeyPayloadCount, hashEntry.Name);
}
private bool CheckAndChangeKeyNameGivenNumberOfSplits(MessagePackSerializerOptions messagePackSerializerOptionsOld,
MessagePackSerializerOptions messagePackSerializerOptionsNew, string[] splits, RedisKey key, string payloadLatestPrefix, string journalPrefix)
{
switch (splits.Length)
{
case 3:
MigrateAllHashKeyValuesFromDictionaryToEnvelopeOfDictionaryNoBoxing(messagePackSerializerOptionsOld, messagePackSerializerOptionsNew, key);
return true;
case 4:
splits[0] = payloadLatestPrefix;
break;
case 5:
splits[0] = journalPrefix;
break;
}
return false;
}
private void MigrateAllHashKeyValuesFromDictionaryToEnvelopeOfDictionaryNoBoxing(MessagePackSerializerOptions messagePackSerializerOptionsOld,
MessagePackSerializerOptions messagePackSerializerOptionsNew, RedisKey key)
{
foreach (var hashEntry in cacheService.RedisDatabase.HashScan(key))
{
try
{
if (!hashEntry.Value.HasValue)
{
continue;
}
MigrateHashKeyValueFromDictionaryToEnvelopeOfDictionaryNoBoxing(messagePackSerializerOptionsOld, messagePackSerializerOptionsNew, hashEntry, key);
}
catch (Exception ex)
{
log.Error($"Payload value migration caused for for hashEntry {hashEntry.Name}: {ex.Message}");
}
}
}
private void MigrateHashKeyValueFromDictionaryToEnvelopeOfDictionaryNoBoxing(MessagePackSerializerOptions messagePackSerializerOptionsOld,
MessagePackSerializerOptions messagePackSerializerOptionsNew, HashEntry hashEntry, RedisKey key)
{
var oldKeyValuePairs = MessagePackSerializer.Deserialize<Dictionary<string, object>>(hashEntry.Value,
messagePackSerializerOptionsOld);
var bytes = SerializeToMessagePackFormatForEnvelopeDictionaryNoBoxing(messagePackSerializerOptionsNew, oldKeyValuePairs);
cacheService.RedisDatabase.HashSet(key, hashEntry.Name, bytes);
}
private byte[] SerializeToMessagePackFormatForEnvelopeDictionaryNoBoxing(MessagePackSerializerOptions messagePackSerializerOptions, Dictionary<string, object> oldKeyValuePairs)
{
var ms = new MemoryStream();
MessagePackSerializer.Serialize(ms, MapToEnvelopeForDictionaryNoBoxing(oldKeyValuePairs),
messagePackSerializerOptions);
var bytes = ms.ToArray();
return bytes;
}
private static EnvelopeDictionaryNoBoxing<string> MapToEnvelopeForDictionaryNoBoxing(Dictionary<string, object> oldKeyValuePairs)
{
var dictionaryNoBoxingWrapper = new EnvelopeDictionaryNoBoxing<string>
{
Version = 1,
Data = MapToDictionaryNoBoxing(oldKeyValuePairs)
};
return dictionaryNoBoxingWrapper;
}
private static DictionaryNoBoxing<string> MapToDictionaryNoBoxing(Dictionary<string, object> oldKeyValuePairs)
{
var dictionaryNoBoxing = new DictionaryNoBoxing<string>(oldKeyValuePairs.Count);
foreach (var oldKeyValuePair in oldKeyValuePairs)
{
switch (oldKeyValuePair.Value)
{
case int i:
dictionaryNoBoxing.Add(oldKeyValuePair.Key, i);
break;
case string s:
dictionaryNoBoxing.Add(oldKeyValuePair.Key, s);
break;
case double d:
dictionaryNoBoxing.Add(oldKeyValuePair.Key, d);
break;
case DateTime d:
dictionaryNoBoxing.Add(oldKeyValuePair.Key, d);
break;
case bool d:
dictionaryNoBoxing.Add(oldKeyValuePair.Key, d);
break;
case null:
dictionaryNoBoxing.Add(oldKeyValuePair.Key, null);
break;
}
}
return dictionaryNoBoxing;
}
public override void Down()
{
}
}
}