diff options
author | vnugent <public@vaughnnugent.com> | 2024-03-10 16:46:50 -0400 |
---|---|---|
committer | vnugent <public@vaughnnugent.com> | 2024-03-10 16:46:50 -0400 |
commit | e5bb0ee302e789cb96e7ecfe839cbbcc8e3fd5d7 (patch) | |
tree | 6f4d824eaea0f4c433f98c0685bf66c06b30e16a /plugins/ObjectCacheServer/src/ServerClusterConfig.cs | |
parent | 6b87785026ca57d6f41cff87ddbd066362f3cacc (diff) |
Squashed commit of the following:
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 'plugins/ObjectCacheServer/src/ServerClusterConfig.cs')
-rw-r--r-- | plugins/ObjectCacheServer/src/ServerClusterConfig.cs | 152 |
1 files changed, 152 insertions, 0 deletions
diff --git a/plugins/ObjectCacheServer/src/ServerClusterConfig.cs b/plugins/ObjectCacheServer/src/ServerClusterConfig.cs new file mode 100644 index 0000000..8e098cd --- /dev/null +++ b/plugins/ObjectCacheServer/src/ServerClusterConfig.cs @@ -0,0 +1,152 @@ +/* +* Copyright (c) 2024 Vaughn Nugent +* +* Library: VNLib +* Package: ObjectCacheServer +* File: ServerClusterConfig.cs +* +* ServerClusterConfig.cs is part of ObjectCacheServer which is part of the larger +* VNLib collection of libraries and utilities. +* +* ObjectCacheServer 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. +* +* ObjectCacheServer 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 System; +using System.Net; +using System.Linq; +using System.Text.Json; +using System.Collections.Generic; + +using VNLib.Plugins; +using VNLib.Utils.Logging; +using VNLib.Utils.Extensions; +using VNLib.Plugins.Extensions.Loading; +using VNLib.Data.Caching.Extensions.Clustering; + +namespace VNLib.Data.Caching.ObjectCache.Server +{ + [ConfigurationName("cluster")] + internal sealed class ServerClusterConfig(PluginBase plugin, IConfigScope config) + { + public TimeSpan DiscoveryInterval { get; } = config.GetRequiredProperty("discovery_interval_sec", p => p.GetTimeSpan(TimeParseType.Seconds)); + + public TimeSpan EventQueuePurgeInterval { get; } = config.GetRequiredProperty("queue_purge_interval_sec", p => p.GetTimeSpan(TimeParseType.Seconds)); + + public int MaxQueueDepth { get; } = (int)config.GetRequiredProperty("max_queue_depth", p => p.GetUInt32()); + + public string? DiscoveryPath { get; } = config.GetValueOrDefault("discovery_path", p => p.GetString(), null); + + public string ConnectPath { get; } = config.GetRequiredProperty("connect_path", p => p.GetString()!); + + public string WellKnownPath { get; } = config.GetValueOrDefault("well_known_path", p => p.GetString()!, CacheConstants.DefaultWellKnownPath) + ?? CacheConstants.DefaultWellKnownPath; + + public bool VerifyIp { get; } = config.GetRequiredProperty("verify_ip", p => p.GetBoolean()); + + /// <summary> + /// The maximum number of peer connections to allow + /// </summary> + public uint MaxPeerConnections { get; } = config.GetValueOrDefault("max_peers", p => p.GetUInt32(), 10u); + + /// <summary> + /// The maxium number of concurrent client connections to allow + /// before rejecting new connections + /// </summary> + public uint MaxConcurrentConnections { get; } = config.GetValueOrDefault("max_concurrent_connections", p => p.GetUInt32(), 100u); + + const string CacheConfigTemplate = +@" +Cluster Configuration: + Node Id: {id} + TlsEndabled: {tls} + Verify Ip: {vi} + Well-Known: {wk} + Cache Endpoint: {ep} + Discovery Endpoint: {dep} + Discovery Interval: {di} + Max Peer Connections: {mpc} + Max Queue Depth: {mqd} + Event Queue Purge Interval: {eqpi} +"; + + internal CacheNodeConfiguration BuildNodeConfig() + { + CacheNodeConfiguration conf = new(); + + //Get the port of the primary webserver + int port; + bool usingTls; + { + //Get the port number of the first virtual host + JsonElement firstHost = plugin.HostConfig.GetProperty("virtual_hosts") + .EnumerateArray() + .First(); + + port = firstHost.GetProperty("interface") + .GetProperty("port") + .GetInt32(); + + //If the ssl element is present, ssl is enabled for the server + usingTls = firstHost.TryGetProperty("ssl", out _); + } + string hostname = Dns.GetHostName(); + + //Server id is just dns name for now + string nodeId = $"{hostname}:{port}"; + + Uri connectEp = BuildUri(usingTls, hostname, port, ConnectPath); + Uri? discoveryEp = null; + + + conf.WithCacheEndpoint(connectEp) + .WithNodeId(nodeId) + .WithTls(usingTls); + + //Get the discovery path (optional) + if (!string.IsNullOrWhiteSpace(DiscoveryPath)) + { + //Build the discovery endpoint, it must be an absolute uri + discoveryEp = BuildUri(usingTls, hostname, port, DiscoveryPath); + conf.EnableAdvertisment(discoveryEp); + } + + //print the cluster configuration to the log + plugin.Log.Information(CacheConfigTemplate, + nodeId, + usingTls, + VerifyIp, + WellKnownPath, + connectEp, + discoveryEp, + DiscoveryInterval, + MaxPeerConnections, + MaxQueueDepth, + EventQueuePurgeInterval + ); + + return conf; + } + + private static Uri BuildUri(bool tls, string host, int port, string path) + { + return new UriBuilder + { + Scheme = tls ? "https" : "http", + Host = host, + Port = port, + Path = path + }.Uri; + } + } +} |