diff options
author | vnugent <public@vaughnnugent.com> | 2024-03-06 21:30:58 -0500 |
---|---|---|
committer | vnugent <public@vaughnnugent.com> | 2024-03-06 21:30:58 -0500 |
commit | 4d8cfc10382105b0acbd94df93ad3d05ff91db54 (patch) | |
tree | d9795c60b2e2a4871eddff43311866784c1c054b /lib/VNLib.Data.Caching.Extensions/src/Clustering | |
parent | 016a96a80cce025a86c6cf26707738f6a2eb2658 (diff) |
refactor: #2 Centralize server state, default discovery endpoints & more
Diffstat (limited to 'lib/VNLib.Data.Caching.Extensions/src/Clustering')
4 files changed, 33 insertions, 44 deletions
diff --git a/lib/VNLib.Data.Caching.Extensions/src/Clustering/CacheClientConfiguration.cs b/lib/VNLib.Data.Caching.Extensions/src/Clustering/CacheClientConfiguration.cs index 5a15d33..263f41a 100644 --- a/lib/VNLib.Data.Caching.Extensions/src/Clustering/CacheClientConfiguration.cs +++ b/lib/VNLib.Data.Caching.Extensions/src/Clustering/CacheClientConfiguration.cs @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023 Vaughn Nugent +* Copyright (c) 2024 Vaughn Nugent * * Library: VNLib * Package: VNLib.Data.Caching.Extensions @@ -37,14 +37,9 @@ namespace VNLib.Data.Caching.Extensions.Clustering public class CacheClientConfiguration { /// <summary> - /// Stores available cache servers to be used for discovery, and connections - /// </summary> - public INodeDiscoveryCollection NodeCollection { get; } = new NodeDiscoveryCollection(); - - /// <summary> /// The authentication manager to use for signing and verifying messages to and from the cache servers /// </summary> - public ICacheAuthManager AuthManager { get; private set; } + public ICacheAuthManager AuthManager { get; private set; } = null!; /// <summary> /// The error handler to use for handling errors that occur during the discovery process @@ -89,7 +84,7 @@ namespace VNLib.Data.Caching.Extensions.Clustering public CacheClientConfiguration WithInitialPeers(IEnumerable<Uri> peers) { //Check null - _ = peers ?? throw new ArgumentNullException(nameof(peers)); + ArgumentNullException.ThrowIfNull(peers); //Store peer array WellKnownNodes = peers.ToArray(); diff --git a/lib/VNLib.Data.Caching.Extensions/src/Clustering/CacheNodeConfiguration.cs b/lib/VNLib.Data.Caching.Extensions/src/Clustering/CacheNodeConfiguration.cs index 6b7ab48..c21ed05 100644 --- a/lib/VNLib.Data.Caching.Extensions/src/Clustering/CacheNodeConfiguration.cs +++ b/lib/VNLib.Data.Caching.Extensions/src/Clustering/CacheNodeConfiguration.cs @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023 Vaughn Nugent +* Copyright (c) 2024 Vaughn Nugent * * Library: VNLib * Package: VNLib.Data.Caching.Extensions @@ -23,6 +23,7 @@ */ using System; +using System.Runtime.CompilerServices; namespace VNLib.Data.Caching.Extensions.Clustering { @@ -85,8 +86,14 @@ namespace VNLib.Data.Caching.Extensions.Clustering return this; } + internal StrongBox<string> NodeIdRef { get; } = new(string.Empty); + ///<inheritdoc/> - public string NodeId { get; private set; } = null!; + public string NodeId + { + get => NodeIdRef.Value!; + private set => NodeIdRef.Value = value; + } /// <summary> /// Specifies the current server's cluster node id. If this @@ -99,10 +106,6 @@ namespace VNLib.Data.Caching.Extensions.Clustering public CacheNodeConfiguration WithNodeId(string nodeId) { NodeId = nodeId ?? throw new ArgumentNullException(nameof(nodeId)); - - //Update the node id in the node collection - (NodeCollection as NodeDiscoveryCollection)!.SetSelfId(nodeId); - return this; } diff --git a/lib/VNLib.Data.Caching.Extensions/src/Clustering/INodeDiscoveryEnumerator.cs b/lib/VNLib.Data.Caching.Extensions/src/Clustering/INodeDiscoveryEnumerator.cs index 677088a..e57e69b 100644 --- a/lib/VNLib.Data.Caching.Extensions/src/Clustering/INodeDiscoveryEnumerator.cs +++ b/lib/VNLib.Data.Caching.Extensions/src/Clustering/INodeDiscoveryEnumerator.cs @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023 Vaughn Nugent +* Copyright (c) 2024 Vaughn Nugent * * Library: VNLib * Package: VNLib.Data.Caching.Extensions @@ -28,7 +28,9 @@ using System.Collections.Generic; namespace VNLib.Data.Caching.Extensions.Clustering { /// <summary> - /// A custom enumerator for the node discovery process + /// A custom enumerator for the node discovery process. Simplifies the recursive processes + /// of discovering nodes in a cluster to a simple enumeration process. It allows for real-time + /// updates to the collection of discovered nodes as a union operation. /// </summary> public interface INodeDiscoveryEnumerator : IEnumerator<CacheNodeAdvertisment> { diff --git a/lib/VNLib.Data.Caching.Extensions/src/Clustering/NodeDiscoveryCollection.cs b/lib/VNLib.Data.Caching.Extensions/src/Clustering/NodeDiscoveryCollection.cs index b0e53e1..16b96a3 100644 --- a/lib/VNLib.Data.Caching.Extensions/src/Clustering/NodeDiscoveryCollection.cs +++ b/lib/VNLib.Data.Caching.Extensions/src/Clustering/NodeDiscoveryCollection.cs @@ -1,12 +1,12 @@ /* -* Copyright (c) 2023 Vaughn Nugent +* Copyright (c) 2024 Vaughn Nugent * * Library: VNLib * Package: VNLib.Data.Caching.Extensions * File: NodeDiscoveryCollection.cs * -* NodeDiscoveryCollection.cs is part of VNLib.Data.Caching.Extensions which is part of the larger -* VNLib collection of libraries and utilities. +* NodeDiscoveryCollection.cs is part of VNLib.Data.Caching.Extensions which is part +* of the larger VNLib collection of libraries and utilities. * * VNLib.Data.Caching.Extensions is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -26,24 +26,18 @@ using System; using System.Linq; using System.Collections; using System.Collections.Generic; +using System.Runtime.CompilerServices; + +using VNLib.Utils.Extensions; namespace VNLib.Data.Caching.Extensions.Clustering { /// <summary> /// Represents a collection of available cache nodes from a discovery process /// </summary> - public sealed class NodeDiscoveryCollection : INodeDiscoveryCollection + public sealed class NodeDiscoveryCollection(StrongBox<string?>? selfId) : INodeDiscoveryCollection { - private string? _selfId; - private LinkedList<CacheNodeAdvertisment> _peers; - - /// <summary> - /// Initializes a new empty <see cref="NodeDiscoveryCollection"/> - /// </summary> - public NodeDiscoveryCollection() - { - _peers = new(); - } + private LinkedList<CacheNodeAdvertisment> _peers = new(); /// <summary> /// Manually adds nodes to the collection that were not discovered through the discovery process @@ -62,39 +56,34 @@ namespace VNLib.Data.Caching.Extensions.Clustering } /// <summary> - /// Sets the id of the current node, so it can be excluded from discovery + /// Removes a vector of nodes from the internal collection /// </summary> - /// <param name="selfId">The id of the current node to exclude</param> - public void SetSelfId(string? selfId) => _selfId = selfId; + /// <param name="nodes">The vector containg nodes to remove from the collection</param> + public void RemoveManualNodes(IEnumerable<CacheNodeAdvertisment> nodes) => nodes.ForEach(n => _peers.Remove(n)); ///<inheritdoc/> - public INodeDiscoveryEnumerator BeginDiscovery() - { - return new NodeEnumerator(new(), _selfId); - } + public INodeDiscoveryEnumerator BeginDiscovery() => new NodeEnumerator(new(), selfId?.Value); ///<inheritdoc/> public INodeDiscoveryEnumerator BeginDiscovery(IEnumerable<CacheNodeAdvertisment> initialPeers) { + ArgumentNullException.ThrowIfNull(initialPeers); + //Init new enumerator with the initial peers - return new NodeEnumerator(new(initialPeers), _selfId); + return new NodeEnumerator(new(initialPeers), selfId?.Value); } ///<inheritdoc/> public void CompleteDiscovery(INodeDiscoveryEnumerator enumerator) { - _ = enumerator ?? throw new ArgumentNullException(nameof(enumerator)); + ArgumentNullException.ThrowIfNull(enumerator); //Capture all nodes from the enumerator and store them as our current peers _peers = (enumerator as NodeEnumerator)!.Peers; } ///<inheritdoc/> - public CacheNodeAdvertisment[] GetAllNodes() - { - //Capture all current peers - return _peers.ToArray(); - } + public CacheNodeAdvertisment[] GetAllNodes() => _peers.ToArray(); private sealed record class NodeEnumerator(LinkedList<CacheNodeAdvertisment> Peers, string? SelfNodeId) : INodeDiscoveryEnumerator { |