aboutsummaryrefslogtreecommitdiff
path: root/lib/VNLib.Data.Caching.ObjectCache/src
diff options
context:
space:
mode:
authorLibravatar vnugent <public@vaughnnugent.com>2024-03-24 21:26:44 -0400
committerLibravatar vnugent <public@vaughnnugent.com>2024-03-24 21:26:44 -0400
commit49c3641def5ae1b7557ed61ed7bb28bbf425ccc9 (patch)
tree4eb1bd863074e64922e35ff730f354ac40528698 /lib/VNLib.Data.Caching.ObjectCache/src
parente5bb0ee302e789cb96e7ecfe839cbbcc8e3fd5d7 (diff)
Squashed commit of the following:
commit a504435151efbe1d19404fa44859b15c629f6d5d Author: vnugent <public@vaughnnugent.com> Date: Sun Mar 24 20:55:01 2024 -0400 chore: Updated compose and added some more logging commit c74440ff12daa03cc4b7792d0c3baad46a11a465 Author: vnugent <public@vaughnnugent.com> Date: Mon Mar 18 21:57:57 2024 -0400 feat: message checksum support & dynamic serializers commit 9983582db08d3e6c456295ea96e482cbb4f31f42 Author: vnugent <public@vaughnnugent.com> Date: Sun Mar 10 21:58:28 2024 -0400 source tree project location updated commit 60f09bde87b5c59ef937c62ef64b7745bc3711b5 Merge: 2f75659 e5bb0ee Author: vnugent <public@vaughnnugent.com> Date: Sun Mar 10 16:50:09 2024 -0400 Merge remote-tracking branch 'origin/master' into develop commit 2f7565976472f0f056db60520bf253a776112c10 Merge: 323ff67 6b87785 Author: vnugent <public@vaughnnugent.com> Date: Sun Mar 10 16:45:23 2024 -0400 merge master commit 323ff67badfc46ad638d75f059d60d9425ccb2fa Author: vnugent <public@vaughnnugent.com> Date: Sun Mar 10 15:50:07 2024 -0400 ci(server): Conainerize and add vncache server packages commit 5d4192880654fd6e00e587814169415b42621327 Author: vnugent <public@vaughnnugent.com> Date: Sat Mar 9 19:13:21 2024 -0500 chore: #2 Minor fixes and polish before release commit a4b3504bb891829074d1efde0433eae010862181 Author: vnugent <public@vaughnnugent.com> Date: Sat Mar 9 16:30:44 2024 -0500 package updates commit 4d8cfc10382105b0acbd94df93ad3d05ff91db54 Author: vnugent <public@vaughnnugent.com> Date: Wed Mar 6 21:30:58 2024 -0500 refactor: #2 Centralize server state, default discovery endpoints & more commit 016a96a80cce025a86c6cf26707738f6a2eb2658 Author: vnugent <public@vaughnnugent.com> Date: Thu Feb 29 21:22:38 2024 -0500 feat: add future support for memory diagnostics, and some docs commit 456ead9bc8b0f61357bae93152ad0403c4940101 Author: vnugent <public@vaughnnugent.com> Date: Tue Feb 13 14:46:35 2024 -0500 fix: #1 shared cluster index on linux & latested core updates commit a481d63f964a5d5204cac2e95141f37f9a28d573 Author: vnugent <public@vaughnnugent.com> Date: Tue Jan 23 15:43:50 2024 -0500 cache extension api tweaks
Diffstat (limited to 'lib/VNLib.Data.Caching.ObjectCache/src')
-rw-r--r--lib/VNLib.Data.Caching.ObjectCache/src/BlobCacheLIstener.cs72
-rw-r--r--lib/VNLib.Data.Caching.ObjectCache/src/BlobCacheListenerConfig.cs56
-rw-r--r--lib/VNLib.Data.Caching.ObjectCache/src/CacheEntry.cs4
3 files changed, 123 insertions, 9 deletions
diff --git a/lib/VNLib.Data.Caching.ObjectCache/src/BlobCacheLIstener.cs b/lib/VNLib.Data.Caching.ObjectCache/src/BlobCacheLIstener.cs
index 972bf5e..7908313 100644
--- a/lib/VNLib.Data.Caching.ObjectCache/src/BlobCacheLIstener.cs
+++ b/lib/VNLib.Data.Caching.ObjectCache/src/BlobCacheLIstener.cs
@@ -43,7 +43,6 @@ using System.Threading;
using System.Threading.Tasks;
using VNLib.Utils.Logging;
-using VNLib.Net.Messaging.FBM;
using VNLib.Net.Messaging.FBM.Server;
using static VNLib.Data.Caching.Constants;
@@ -57,18 +56,22 @@ namespace VNLib.Data.Caching.ObjectCache
/// </remarks>
/// <param name="cache">The cache table to work from</param>
/// <param name="queue">The event queue to publish changes to</param>
- /// <param name="log">Writes error and debug logging information</param>
- /// <param name="memoryManager">The heap to alloc FBM buffers and <see cref="CacheEntry"/> cache buffers from</param>
+ /// <param name="config">The listener configuration object</param>
/// <exception cref="ArgumentNullException"></exception>
- public class BlobCacheListener<T>(IBlobCacheTable cache, ICacheListenerEventQueue<T> queue, ILogProvider log, IFBMMemoryManager memoryManager)
+ public class BlobCacheListener<T>(IBlobCacheTable cache, BlobCacheListenerConfig config, ICacheListenerEventQueue<T> queue)
: FBMListenerBase<T>, IDisposable
{
private bool disposedValue;
///<inheritdoc/>
- protected override ILogProvider Log { get; } = log;
+ protected override ILogProvider Log { get; } = config.Log;
///<inheritdoc/>
- protected override FBMListener Listener { get; } = new(memoryManager);
+ protected override FBMListener Listener { get; } = new(config.MemoryManager);
+
+ /// <summary>
+ /// The configuration instance for the listener
+ /// </summary>
+ public BlobCacheListenerConfig Config { get; } = config ?? throw new ArgumentNullException(nameof(config));
/// <summary>
/// A queue that stores update and delete events
@@ -80,6 +83,9 @@ namespace VNLib.Data.Caching.ObjectCache
/// </summary>
public IBlobCacheTable Cache { get; } = cache ?? throw new ArgumentNullException(nameof(cache));
+
+ private readonly ILogProvider _tLog = config.LogTransactions ? config.Log : new NullLogger();
+
///<inheritdoc/>
protected override async Task ProcessAsync(FBMContext context, T? userState, CancellationToken exitToken)
{
@@ -110,6 +116,25 @@ namespace VNLib.Data.Caching.ObjectCache
//Create change event for the object
ChangeEvent change = new(objectId, alternateId, false);
+ if (config.EnableMessageChecksums)
+ {
+ switch (context.Request.IsClientChecksumValid())
+ {
+ //0 is checksum sent, supported, but invalid
+ case 0:
+ context.CloseResponse(ResponseCodes.InvalidChecksum);
+ return;
+
+ case -2: //Method not supported, set an error header but allow the request
+ context.Response.WriteHeader(ChecksumWarning, "Checksum method not supported");
+ break;
+
+ case 1: //1 is checksum sent and valid
+ case -1: //No checksum sent
+ break;
+ }
+ }
+
await AddOrUpdateAsync(context, change, exitToken);
return;
}
@@ -199,6 +224,12 @@ namespace VNLib.Data.Caching.ObjectCache
if (handle.Cache.TryGetValue(objectId, out CacheEntry data))
{
+ //Compute an fnv message checksum and send it to the client
+ if (config.EnableMessageChecksums)
+ {
+ FbmMessageChecksum.WriteFnv1aChecksum(context.Response, data.GetDataSegment());
+ }
+
//Set the status code and write the buffered data to the response buffer
context.CloseResponse(ResponseCodes.Okay);
@@ -222,6 +253,8 @@ namespace VNLib.Data.Caching.ObjectCache
if (found)
{
EnqueEvent(change);
+
+ _tLog.Debug("Deleted cache entry {id}", change.CurrentId);
}
}
@@ -232,6 +265,8 @@ namespace VNLib.Data.Caching.ObjectCache
EnqueEvent(change);
+ _tLog.Debug("Cache entry {id} added or updated. New ID {nid}", change.CurrentId, change.AlternateId);
+
context.CloseResponse(ResponseCodes.Okay);
}
@@ -239,7 +274,7 @@ namespace VNLib.Data.Caching.ObjectCache
{
EventQueue.PublishEvent(change);
}
-
+
///<inheritdoc/>
protected virtual void Dispose(bool disposing)
@@ -258,5 +293,28 @@ namespace VNLib.Data.Caching.ObjectCache
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
+
+ sealed class NullLogger : ILogProvider
+ {
+ public void Flush()
+ { }
+
+ public object GetLogProvider() => null!;
+
+
+ public bool IsEnabled(LogLevel level) => false;
+
+ public void Write(LogLevel level, string value)
+ { }
+
+ public void Write(LogLevel level, Exception exception, string value = "")
+ { }
+
+ public void Write(LogLevel level, string value, params object?[] args)
+ { }
+
+ public void Write(LogLevel level, string value, params ValueType[] args)
+ { }
+ }
}
}
diff --git a/lib/VNLib.Data.Caching.ObjectCache/src/BlobCacheListenerConfig.cs b/lib/VNLib.Data.Caching.ObjectCache/src/BlobCacheListenerConfig.cs
new file mode 100644
index 0000000..492dfb8
--- /dev/null
+++ b/lib/VNLib.Data.Caching.ObjectCache/src/BlobCacheListenerConfig.cs
@@ -0,0 +1,56 @@
+/*
+* Copyright (c) 2024 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.Data.Caching.ObjectCache
+* File: BlobCacheListenerConfig.cs
+*
+* BlobCacheListenerConfig.cs is part of VNLib.Data.Caching.ObjectCache which is part of the larger
+* VNLib collection of libraries and utilities.
+*
+* VNLib.Data.Caching.ObjectCache 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.
+*
+* VNLib.Data.Caching.ObjectCache 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 this program. If not, see https://www.gnu.org/licenses/.
+*/
+
+using VNLib.Utils.Logging;
+using VNLib.Net.Messaging.FBM;
+
+namespace VNLib.Data.Caching.ObjectCache
+{
+ /// <summary>
+ /// A configuration object for <see cref="BlobCacheListener{T}"/>
+ /// </summary>
+ public sealed record class BlobCacheListenerConfig
+ {
+ /// <summary>
+ /// Writes error and debug logging information
+ /// </summary>
+ public ILogProvider Log { get; init; } = null!;
+
+ /// <summary>
+ /// The memory manager used for the internal FBM server listener
+ /// </summary>
+ public IFBMMemoryManager MemoryManager { get; init; } = null!;
+
+ /// <summary>
+ /// A flag that enables verifying and sending checksums with message
+ /// data in FBM header fields
+ /// </summary>
+ public bool EnableMessageChecksums { get; init; } = true;
+
+ /// <summary>
+ /// A flag that enables logging of transactions (events) to the log
+ /// </summary>
+ public bool LogTransactions { get; init; }
+ }
+}
diff --git a/lib/VNLib.Data.Caching.ObjectCache/src/CacheEntry.cs b/lib/VNLib.Data.Caching.ObjectCache/src/CacheEntry.cs
index 9370901..eddfc42 100644
--- a/lib/VNLib.Data.Caching.ObjectCache/src/CacheEntry.cs
+++ b/lib/VNLib.Data.Caching.ObjectCache/src/CacheEntry.cs
@@ -89,8 +89,8 @@ namespace VNLib.Data.Caching
/// <exception cref="ArgumentException"></exception>
public static CacheEntry FromExistingHandle(object handle, ICacheEntryMemoryManager manager)
{
- _ = handle ?? throw new ArgumentNullException(nameof(handle));
- _ = manager ?? throw new ArgumentNullException(nameof(manager));
+ ArgumentNullException.ThrowIfNull(handle);
+ ArgumentNullException.ThrowIfNull(manager);
//validate handle size it at least the minimum size
if (manager.GetHandleSize(handle) < DATA_SEGMENT_START)