| | | 1 | | using Elsa.KeyValues.Contracts; |
| | | 2 | | using Elsa.KeyValues.Entities; |
| | | 3 | | using Elsa.KeyValues.Models; |
| | | 4 | | using Elsa.Workflows.Runtime.Models; |
| | | 5 | | |
| | | 6 | | namespace Elsa.Workflows.Runtime; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Stores workflow dispatch outbox items in the existing key-value store. |
| | | 10 | | /// </summary> |
| | 17 | 11 | | public class KeyValueWorkflowDispatchOutboxStore(IKeyValueStore keyValueStore, IPayloadSerializer payloadSerializer) : I |
| | | 12 | | { |
| | | 13 | | private const string LegacyKeyPrefix = "Elsa:WorkflowDispatchOutbox:"; |
| | | 14 | | private const string ItemKeyPrefix = "Elsa:WorkflowDispatchOutbox:Items:"; |
| | | 15 | | private const string IndexKeyPrefix = "Elsa:WorkflowDispatchOutbox:Index:"; |
| | | 16 | | private const string IndexByIdKeyPrefix = "Elsa:WorkflowDispatchOutbox:IndexById:"; |
| | | 17 | | private const string RecoveryKeyPrefix = "Elsa:WorkflowDispatchOutbox:Recovery:"; |
| | | 18 | | private const string StateKeyPrefix = "Elsa:WorkflowDispatchOutbox:State:"; |
| | | 19 | | private const string LegacyScanCompletedKey = $"{StateKeyPrefix}LegacyScanCompleted"; |
| | | 20 | | |
| | | 21 | | /// <inheritdoc /> |
| | | 22 | | public async Task SaveAsync(WorkflowDispatchOutboxItem item, CancellationToken cancellationToken = default) |
| | | 23 | | { |
| | 9 | 24 | | await keyValueStore.SaveAsync(new SerializedKeyValuePair |
| | 9 | 25 | | { |
| | 9 | 26 | | Key = GetRecoveryKey(item.Id), |
| | 9 | 27 | | SerializedValue = item.Id, |
| | 9 | 28 | | TenantId = item.TenantId |
| | 9 | 29 | | }, cancellationToken); |
| | | 30 | | |
| | 9 | 31 | | await keyValueStore.SaveAsync(new SerializedKeyValuePair |
| | 9 | 32 | | { |
| | 9 | 33 | | Key = GetItemKey(item.Id), |
| | 9 | 34 | | SerializedValue = payloadSerializer.Serialize(item), |
| | 9 | 35 | | TenantId = item.TenantId |
| | 9 | 36 | | }, cancellationToken); |
| | | 37 | | |
| | 9 | 38 | | await SaveIndexAsync(item, cancellationToken); |
| | 9 | 39 | | await keyValueStore.DeleteAsync(GetRecoveryKey(item.Id), cancellationToken); |
| | 9 | 40 | | } |
| | | 41 | | |
| | | 42 | | private async Task SaveIndexAsync(WorkflowDispatchOutboxItem item, CancellationToken cancellationToken) |
| | | 43 | | { |
| | 12 | 44 | | var indexKey = GetIndexKey(item); |
| | | 45 | | |
| | 12 | 46 | | await keyValueStore.SaveAsync(new SerializedKeyValuePair |
| | 12 | 47 | | { |
| | 12 | 48 | | Key = indexKey, |
| | 12 | 49 | | SerializedValue = item.Id, |
| | 12 | 50 | | TenantId = item.TenantId |
| | 12 | 51 | | }, cancellationToken); |
| | | 52 | | |
| | 12 | 53 | | await keyValueStore.SaveAsync(new SerializedKeyValuePair |
| | 12 | 54 | | { |
| | 12 | 55 | | Key = GetIndexByIdKey(item.Id), |
| | 12 | 56 | | SerializedValue = indexKey, |
| | 12 | 57 | | TenantId = item.TenantId |
| | 12 | 58 | | }, cancellationToken); |
| | 12 | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <inheritdoc /> |
| | | 62 | | public async Task<IEnumerable<WorkflowDispatchOutboxItem>> FindManyAsync(CancellationToken cancellationToken = defau |
| | | 63 | | { |
| | 3 | 64 | | return await FindManyAsync(maxCount: 0, cancellationToken); |
| | 3 | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <inheritdoc /> |
| | | 68 | | public async Task<IEnumerable<WorkflowDispatchOutboxItem>> FindManyAsync(int maxCount, CancellationToken cancellatio |
| | | 69 | | { |
| | 10 | 70 | | var indexedItems = await FindIndexedItemsAsync(maxCount, cancellationToken); |
| | 10 | 71 | | var recoverableItems = await FindRecoveryItemsAsync(maxCount, cancellationToken); |
| | 10 | 72 | | var legacyItems = await FindLegacyItemsAsync(maxCount, cancellationToken); |
| | 10 | 73 | | var items = indexedItems |
| | 10 | 74 | | .Concat(recoverableItems) |
| | 10 | 75 | | .Concat(legacyItems) |
| | 17 | 76 | | .GroupBy(x => x.Id) |
| | 16 | 77 | | .Select(x => x.First()) |
| | 25 | 78 | | .OrderBy(x => x.CreatedAt); |
| | | 79 | | |
| | 10 | 80 | | if (maxCount > 0) |
| | 7 | 81 | | return items.Take(maxCount).ToList(); |
| | | 82 | | |
| | 3 | 83 | | return items.ToList(); |
| | 10 | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <inheritdoc /> |
| | | 87 | | public async Task DeleteAsync(string id, CancellationToken cancellationToken = default) |
| | | 88 | | { |
| | 6 | 89 | | var itemKey = GetItemKey(id); |
| | 6 | 90 | | var record = await keyValueStore.FindAsync(new KeyValueFilter { Key = itemKey }, cancellationToken); |
| | | 91 | | |
| | 6 | 92 | | if (record == null) |
| | | 93 | | { |
| | 4 | 94 | | await DeleteIndexesForMissingItemAsync(id, cancellationToken); |
| | 4 | 95 | | await keyValueStore.DeleteAsync(GetRecoveryKey(id), cancellationToken); |
| | 4 | 96 | | await keyValueStore.DeleteAsync(GetLegacyKey(id), cancellationToken); |
| | 4 | 97 | | await keyValueStore.DeleteAsync(itemKey, cancellationToken); |
| | 4 | 98 | | return; |
| | | 99 | | } |
| | | 100 | | |
| | 2 | 101 | | var item = payloadSerializer.Deserialize<WorkflowDispatchOutboxItem>(record.SerializedValue); |
| | | 102 | | |
| | 2 | 103 | | if (item != null) |
| | | 104 | | { |
| | 1 | 105 | | await keyValueStore.DeleteAsync(GetIndexKey(item), cancellationToken); |
| | 1 | 106 | | await keyValueStore.DeleteAsync(GetIndexByIdKey(id), cancellationToken); |
| | | 107 | | } |
| | | 108 | | else |
| | | 109 | | { |
| | 1 | 110 | | await DeleteIndexesForMissingItemAsync(id, cancellationToken); |
| | | 111 | | } |
| | | 112 | | |
| | 2 | 113 | | await keyValueStore.DeleteAsync(GetRecoveryKey(id), cancellationToken); |
| | 2 | 114 | | await keyValueStore.DeleteAsync(GetLegacyKey(id), cancellationToken); |
| | 2 | 115 | | await keyValueStore.DeleteAsync(itemKey, cancellationToken); |
| | 6 | 116 | | } |
| | | 117 | | |
| | | 118 | | private async Task<IEnumerable<WorkflowDispatchOutboxItem>> FindIndexedItemsAsync(int maxCount, CancellationToken ca |
| | | 119 | | { |
| | 10 | 120 | | var indexRecords = (await keyValueStore.FindManyAsync(new KeyValueFilter |
| | 10 | 121 | | { |
| | 10 | 122 | | Key = IndexKeyPrefix, |
| | 10 | 123 | | StartsWith = true, |
| | 10 | 124 | | OrderByKey = true, |
| | 10 | 125 | | Take = maxCount > 0 ? maxCount : null |
| | 10 | 126 | | }, cancellationToken)).ToList(); |
| | 17 | 127 | | var itemKeys = indexRecords.Select(x => GetItemKey(x.SerializedValue)).ToList(); |
| | | 128 | | |
| | 10 | 129 | | var itemRecords = itemKeys.Count == 0 |
| | 10 | 130 | | ? Array.Empty<SerializedKeyValuePair>() |
| | 10 | 131 | | : await keyValueStore.FindManyAsync(new KeyValueFilter |
| | 10 | 132 | | { |
| | 10 | 133 | | Keys = itemKeys |
| | 10 | 134 | | }, cancellationToken); |
| | 17 | 135 | | var itemRecordLookup = itemRecords.ToDictionary(x => x.Key); |
| | 10 | 136 | | var items = new List<WorkflowDispatchOutboxItem>(); |
| | | 137 | | |
| | 34 | 138 | | foreach (var indexRecord in indexRecords) |
| | | 139 | | { |
| | 7 | 140 | | var id = indexRecord.SerializedValue; |
| | 7 | 141 | | var itemKey = GetItemKey(id); |
| | | 142 | | |
| | 7 | 143 | | if (!itemRecordLookup.TryGetValue(itemKey, out var itemRecord)) |
| | | 144 | | { |
| | 0 | 145 | | await keyValueStore.DeleteAsync(indexRecord.Key, cancellationToken); |
| | 0 | 146 | | await keyValueStore.DeleteAsync(GetIndexByIdKey(id), cancellationToken); |
| | 0 | 147 | | continue; |
| | | 148 | | } |
| | | 149 | | |
| | 7 | 150 | | var item = payloadSerializer.Deserialize<WorkflowDispatchOutboxItem>(itemRecord.SerializedValue); |
| | | 151 | | |
| | 7 | 152 | | if (item == null) |
| | | 153 | | { |
| | 1 | 154 | | await DeleteUnrecoverableItemAsync(id, itemKey, indexRecord.Key, recoveryKey: null, cancellationToken: c |
| | 1 | 155 | | continue; |
| | | 156 | | } |
| | | 157 | | |
| | 6 | 158 | | items.Add(item); |
| | 6 | 159 | | } |
| | | 160 | | |
| | 10 | 161 | | return items; |
| | 10 | 162 | | } |
| | | 163 | | |
| | | 164 | | private async Task<IEnumerable<WorkflowDispatchOutboxItem>> FindRecoveryItemsAsync(int maxCount, CancellationToken c |
| | | 165 | | { |
| | 10 | 166 | | var recoveryRecords = (await keyValueStore.FindManyAsync(new KeyValueFilter |
| | 10 | 167 | | { |
| | 10 | 168 | | Key = RecoveryKeyPrefix, |
| | 10 | 169 | | StartsWith = true, |
| | 10 | 170 | | OrderByKey = true, |
| | 10 | 171 | | Take = maxCount > 0 ? maxCount : null |
| | 10 | 172 | | }, cancellationToken)).ToList(); |
| | | 173 | | |
| | 10 | 174 | | if (recoveryRecords.Count == 0) |
| | 7 | 175 | | return []; |
| | | 176 | | |
| | 7 | 177 | | var itemKeys = recoveryRecords.Select(x => GetItemKey(x.SerializedValue)).ToList(); |
| | 7 | 178 | | var itemRecords = (await keyValueStore.FindManyAsync(new KeyValueFilter { Keys = itemKeys }, cancellationToken)) |
| | 3 | 179 | | var items = new List<WorkflowDispatchOutboxItem>(); |
| | | 180 | | |
| | 14 | 181 | | foreach (var recoveryRecord in recoveryRecords) |
| | | 182 | | { |
| | 4 | 183 | | var id = recoveryRecord.SerializedValue; |
| | | 184 | | |
| | 4 | 185 | | if (!itemRecords.TryGetValue(GetItemKey(id), out var itemRecord)) |
| | | 186 | | { |
| | 0 | 187 | | await DeleteIndexesForMissingItemAsync(id, cancellationToken); |
| | 0 | 188 | | await keyValueStore.DeleteAsync(recoveryRecord.Key, cancellationToken); |
| | 0 | 189 | | continue; |
| | | 190 | | } |
| | | 191 | | |
| | 4 | 192 | | var item = payloadSerializer.Deserialize<WorkflowDispatchOutboxItem>(itemRecord.SerializedValue); |
| | | 193 | | |
| | 4 | 194 | | if (item == null) |
| | | 195 | | { |
| | 1 | 196 | | await DeleteUnrecoverableItemAsync(id, itemRecord.Key, indexKey: null, recoveryKey: recoveryRecord.Key, |
| | 1 | 197 | | continue; |
| | | 198 | | } |
| | | 199 | | |
| | 3 | 200 | | await SaveIndexAsync(item, cancellationToken); |
| | 3 | 201 | | await keyValueStore.DeleteAsync(recoveryRecord.Key, cancellationToken); |
| | 3 | 202 | | items.Add(item); |
| | 3 | 203 | | } |
| | | 204 | | |
| | 3 | 205 | | return items; |
| | 10 | 206 | | } |
| | | 207 | | |
| | | 208 | | private async Task<IEnumerable<WorkflowDispatchOutboxItem>> FindLegacyItemsAsync(int maxCount, CancellationToken can |
| | | 209 | | { |
| | 10 | 210 | | var legacyScanCompleted = await keyValueStore.FindAsync(new KeyValueFilter { Key = LegacyScanCompletedKey }, can |
| | | 211 | | |
| | 10 | 212 | | if (legacyScanCompleted != null) |
| | 4 | 213 | | return []; |
| | | 214 | | |
| | 6 | 215 | | var records = await keyValueStore.FindManyAsync(new KeyValueFilter |
| | 6 | 216 | | { |
| | 6 | 217 | | Key = LegacyKeyPrefix, |
| | 6 | 218 | | StartsWith = true, |
| | 6 | 219 | | OrderByKey = true, |
| | 6 | 220 | | Take = GetLegacyScanTake(maxCount) |
| | 6 | 221 | | }, cancellationToken); |
| | 6 | 222 | | var recordList = records.ToList(); |
| | | 223 | | |
| | 6 | 224 | | var recoverableItems = recordList |
| | 6 | 225 | | .Where(IsRecoverableItemRecord) |
| | 9 | 226 | | .Select(x => new |
| | 9 | 227 | | { |
| | 9 | 228 | | Record = x, |
| | 9 | 229 | | Item = payloadSerializer.Deserialize<WorkflowDispatchOutboxItem>(x.SerializedValue) |
| | 9 | 230 | | }) |
| | 9 | 231 | | .Where(x => x.Item != null) |
| | 6 | 232 | | .ToList(); |
| | 6 | 233 | | var recoverableItemsToMigrate = maxCount > 0 ? recoverableItems.Take(maxCount).ToList() : recoverableItems; |
| | 6 | 234 | | var items = new List<WorkflowDispatchOutboxItem>(); |
| | | 235 | | |
| | 28 | 236 | | foreach (var recoverableItem in recoverableItemsToMigrate) |
| | | 237 | | { |
| | 8 | 238 | | var record = recoverableItem.Record; |
| | 8 | 239 | | var item = recoverableItem.Item!; |
| | 8 | 240 | | await SaveAsync(item, cancellationToken); |
| | | 241 | | |
| | 8 | 242 | | if (!record.Key.StartsWith(ItemKeyPrefix, StringComparison.Ordinal)) |
| | 5 | 243 | | await keyValueStore.DeleteAsync(record.Key, cancellationToken); |
| | | 244 | | |
| | 8 | 245 | | items.Add(item); |
| | 8 | 246 | | } |
| | | 247 | | |
| | 6 | 248 | | if (IsLegacyScanComplete(recordList.Count, maxCount)) |
| | | 249 | | { |
| | 5 | 250 | | await keyValueStore.SaveAsync(new SerializedKeyValuePair |
| | 5 | 251 | | { |
| | 5 | 252 | | Key = LegacyScanCompletedKey, |
| | 5 | 253 | | SerializedValue = "true" |
| | 5 | 254 | | }, cancellationToken); |
| | | 255 | | } |
| | | 256 | | |
| | 6 | 257 | | return items; |
| | 10 | 258 | | } |
| | | 259 | | |
| | | 260 | | private async Task DeleteIndexesForMissingItemAsync(string id, CancellationToken cancellationToken) |
| | | 261 | | { |
| | 6 | 262 | | var lookupRecord = await keyValueStore.FindAsync(new KeyValueFilter { Key = GetIndexByIdKey(id) }, cancellationT |
| | | 263 | | |
| | 6 | 264 | | if (lookupRecord != null) |
| | | 265 | | { |
| | 5 | 266 | | if (IsIndexKeyForId(lookupRecord.SerializedValue, id)) |
| | | 267 | | { |
| | 3 | 268 | | await keyValueStore.DeleteAsync(lookupRecord.SerializedValue, cancellationToken); |
| | 3 | 269 | | await keyValueStore.DeleteAsync(lookupRecord.Key, cancellationToken); |
| | 3 | 270 | | return; |
| | | 271 | | } |
| | | 272 | | |
| | 2 | 273 | | await keyValueStore.DeleteAsync(lookupRecord.Key, cancellationToken); |
| | | 274 | | } |
| | | 275 | | |
| | 3 | 276 | | var matchingIndexRecords = await keyValueStore.FindManyAsync(new KeyValueFilter |
| | 3 | 277 | | { |
| | 3 | 278 | | Key = IndexKeyPrefix, |
| | 3 | 279 | | StartsWith = true |
| | 3 | 280 | | }, cancellationToken); |
| | | 281 | | |
| | 16 | 282 | | foreach (var indexRecord in matchingIndexRecords.Where(x => x.SerializedValue == id || x.Key.EndsWith($":{id}", |
| | | 283 | | { |
| | 3 | 284 | | await keyValueStore.DeleteAsync(indexRecord.Key, cancellationToken); |
| | | 285 | | } |
| | 6 | 286 | | } |
| | | 287 | | |
| | | 288 | | private async Task DeleteUnrecoverableItemAsync(string id, string itemKey, string? indexKey, string? recoveryKey, Ca |
| | | 289 | | { |
| | 2 | 290 | | if (indexKey != null) |
| | | 291 | | { |
| | 1 | 292 | | await keyValueStore.DeleteAsync(indexKey, cancellationToken); |
| | 1 | 293 | | await keyValueStore.DeleteAsync(GetIndexByIdKey(id), cancellationToken); |
| | | 294 | | } |
| | | 295 | | else |
| | | 296 | | { |
| | 1 | 297 | | await DeleteIndexesForMissingItemAsync(id, cancellationToken); |
| | | 298 | | } |
| | | 299 | | |
| | 2 | 300 | | await keyValueStore.DeleteAsync(recoveryKey ?? GetRecoveryKey(id), cancellationToken); |
| | 2 | 301 | | await keyValueStore.DeleteAsync(GetLegacyKey(id), cancellationToken); |
| | 2 | 302 | | await keyValueStore.DeleteAsync(itemKey, cancellationToken); |
| | 2 | 303 | | } |
| | | 304 | | |
| | 37 | 305 | | private static string GetItemKey(string id) => $"{ItemKeyPrefix}{id}"; |
| | | 306 | | |
| | 8 | 307 | | private static string GetLegacyKey(string id) => $"{LegacyKeyPrefix}{id}"; |
| | | 308 | | |
| | 13 | 309 | | private static string GetIndexKey(WorkflowDispatchOutboxItem item) => $"{IndexKeyPrefix}{item.CreatedAt.UtcTicks:D20 |
| | | 310 | | |
| | 20 | 311 | | private static string GetIndexByIdKey(string id) => $"{IndexByIdKeyPrefix}{id}"; |
| | | 312 | | |
| | 25 | 313 | | private static string GetRecoveryKey(string id) => $"{RecoveryKeyPrefix}{id}"; |
| | | 314 | | |
| | | 315 | | private static int? GetLegacyScanTake(int maxCount) |
| | | 316 | | { |
| | 12 | 317 | | if (maxCount <= 0) |
| | 4 | 318 | | return null; |
| | | 319 | | |
| | 8 | 320 | | return maxCount == int.MaxValue ? int.MaxValue : maxCount + 1; |
| | | 321 | | } |
| | | 322 | | |
| | | 323 | | private static bool IsLegacyScanComplete(int recordCount, int maxCount) |
| | | 324 | | { |
| | 6 | 325 | | var scanTake = GetLegacyScanTake(maxCount); |
| | 6 | 326 | | return scanTake == null || recordCount < scanTake.Value; |
| | | 327 | | } |
| | | 328 | | |
| | | 329 | | private static bool IsIndexKeyForId(string indexKey, string id) |
| | | 330 | | { |
| | 5 | 331 | | var expectedSuffix = $":{id}"; |
| | | 332 | | |
| | 5 | 333 | | if (!indexKey.StartsWith(IndexKeyPrefix, StringComparison.Ordinal) || !indexKey.EndsWith(expectedSuffix, StringC |
| | 2 | 334 | | return false; |
| | | 335 | | |
| | 3 | 336 | | var ticksStart = IndexKeyPrefix.Length; |
| | 3 | 337 | | var ticksLength = indexKey.Length - ticksStart - expectedSuffix.Length; |
| | | 338 | | |
| | 3 | 339 | | if (ticksLength != 20) |
| | 0 | 340 | | return false; |
| | | 341 | | |
| | 126 | 342 | | foreach (var character in indexKey.AsSpan(ticksStart, ticksLength)) |
| | | 343 | | { |
| | 60 | 344 | | if (character is < '0' or > '9') |
| | 0 | 345 | | return false; |
| | | 346 | | } |
| | | 347 | | |
| | 3 | 348 | | return true; |
| | | 349 | | } |
| | | 350 | | |
| | | 351 | | private static bool IsRecoverableItemRecord(SerializedKeyValuePair record) |
| | | 352 | | { |
| | 10 | 353 | | return !record.Key.StartsWith(IndexKeyPrefix, StringComparison.Ordinal) |
| | 10 | 354 | | && !record.Key.StartsWith(IndexByIdKeyPrefix, StringComparison.Ordinal) |
| | 10 | 355 | | && !record.Key.StartsWith(RecoveryKeyPrefix, StringComparison.Ordinal) |
| | 10 | 356 | | && !record.Key.StartsWith(StateKeyPrefix, StringComparison.Ordinal); |
| | | 357 | | } |
| | | 358 | | } |