aboutsummaryrefslogtreecommitdiff
path: root/Libs/VNLib.Plugins.Essentials.Sessions.Runtime/VnCacheClient.cs
blob: bb980697217917a076ca2529805f6f4bc8791ad8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* Copyright (c) 2022 Vaughn Nugent
* 
* Library: VNLib
* Package: VNLib.Plugins.Essentials.Sessions.Runtime
* File: VnCacheClient.cs 
*
* VnCacheClient.cs is part of VNLib.Plugins.Essentials.Sessions.Runtime which is part of the larger 
* VNLib collection of libraries and utilities.
*
* VNLib.Plugins.Essentials.Sessions.Runtime is free software: you can redistribute it and/or modify 
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 2 of the License,
* or (at your option) any later version.
*
* VNLib.Plugins.Essentials.Sessions.Runtime 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 
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License 
* along with VNLib.Plugins.Essentials.Sessions.Runtime. If not, see http://www.gnu.org/licenses/.
*/

using System;
using System.Text.Json;
using System.Net.Sockets;
using System.Net.WebSockets;
using System.Security.Cryptography;

using VNLib.Utils;
using VNLib.Utils.Memory;
using VNLib.Utils.Logging;
using VNLib.Utils.Extensions;
using VNLib.Data.Caching.Extensions;
using VNLib.Net.Messaging.FBM.Client;
using VNLib.Plugins.Extensions.Loading;

namespace VNLib.Plugins.Essentials.Sessions.Runtime
{
    /// <summary>
    /// A wrapper to simplify a cache client object
    /// </summary>
    public sealed class VnCacheClient : OpenResourceHandle<FBMClient?>
    {
        FBMClient? _client;

        /// <summary>
        /// The wrapped client
        /// </summary>
        public override FBMClient? Resource => _client;


        private TimeSpan RetryInterval;

        private readonly ILogProvider? DebugLog;
        private readonly IUnmangedHeap? ClientHeap;

        /// <summary>
        /// Initializes an emtpy client wrapper that still requires 
        /// configuration loading
        /// </summary>
        /// <param name="debugLog">An optional debugging log</param>
        /// <param name="heap">An optional <see cref="IUnmangedHeap"/> for <see cref="FBMClient"/> buffers</param>
        public VnCacheClient(ILogProvider? debugLog, IUnmangedHeap? heap = null)
        {
            DebugLog = debugLog;
            //Default to 10 seconds
            RetryInterval = TimeSpan.FromSeconds(10);

            ClientHeap = heap;
        }

        protected override void Free()
        {
            _client?.Dispose();
            _client = null;
        }

        /// <summary>
        /// Loads required configuration variables from the config store and 
        /// intializes the interal client
        /// </summary>
        /// <param name="config">A dictionary of configuration varables</param>
        /// <exception cref="KeyNotFoundException"></exception>
        public async Task LoadConfigAsync(PluginBase pbase, IReadOnlyDictionary<string, JsonElement> config)
        {
            int maxMessageSize = config["max_message_size"].GetInt32();
            string? brokerAddress = config["broker_address"].GetString() ?? throw new KeyNotFoundException("Missing required configuration variable broker_address");

            //Get keys async
            Task<string?> clientPrivTask = pbase.TryGetSecretAsync("client_private_key");
            Task<string?> brokerPubTask = pbase.TryGetSecretAsync("broker_public_key");

            //Wait for all tasks to complete
            string?[] keys = await Task.WhenAll(clientPrivTask, brokerPubTask);

            byte[] privKey = Convert.FromBase64String(keys[0] ?? throw new KeyNotFoundException("Missing required secret client_private_key"));
            byte[] brokerPub = Convert.FromBase64String(keys[1] ?? throw new KeyNotFoundException("Missing required secret broker_public_key"));

            RetryInterval = config["retry_interval_sec"].GetTimeSpan(TimeParseType.Seconds);

            Uri brokerUri = new(brokerAddress);

            //Init the client with default settings
            FBMClientConfig conf = FBMDataCacheExtensions.GetDefaultConfig(ClientHeap ?? Memory.Shared, maxMessageSize, DebugLog);

            _client = new(conf);
            //Add the configuration
            _client.UseBroker(brokerUri)
                .ImportBrokerPublicKey(brokerPub)
                .ImportClientPrivateKey(privKey)
                .UseTls(brokerUri.Scheme == Uri.UriSchemeHttps);

            //Zero the key memory
            Memory.InitializeBlock(privKey.AsSpan());
            Memory.InitializeBlock(brokerPub.AsSpan());
        }

        /// <summary>
        /// Discovers nodes in the configured cluster and connects to a random node
        /// </summary>
        /// <param name="Log">A <see cref="ILogProvider"/> to write log events to</param>
        /// <param name="cancellationToken">A token to cancel the operation</param>
        /// <returns>A task that completes when the operation has been cancelled or an unrecoverable error occured</returns>
        /// <exception cref="InvalidOperationException"></exception>
        /// <exception cref="OperationCanceledException"></exception>
        public async Task RunAsync(ILogProvider Log, CancellationToken cancellationToken)
        {
            _ = Resource ?? throw new InvalidOperationException("Client configuration not loaded, cannot connect to cache servers");

            while (true)
            {
                //Load the server list
                ActiveServer[]? servers;
                while (true)
                {
                    try
                    {
                        Log.Debug("Discovering cluster nodes in broker");
                        //Get server list
                        servers = await Resource.DiscoverNodesAsync(cancellationToken);
                        break;
                    }
                    catch (HttpRequestException re) when (re.InnerException is SocketException)
                    {
                        Log.Warn("Broker server is unreachable");
                    }
                    catch (Exception ex)
                    {
                        Log.Warn("Failed to get server list from broker, reason {r}", ex.Message);
                    }
                    //Gen random ms delay
                    int randomMsDelay = RandomNumberGenerator.GetInt32(1000, 2000);
                    await Task.Delay(randomMsDelay, cancellationToken);
                }
                if (servers?.Length == 0)
                {
                    Log.Warn("No cluster nodes found, retrying");
                    await Task.Delay(RetryInterval, cancellationToken);
                    continue;
                }
                //select random server from the list of servers
                ActiveServer selected = servers!.SelectRandom();
                try
                {
                    Log.Debug("Connecting to server {server}", selected.ServerId);
                    //Try to connect to server
                    await Resource.ConnectAndWaitForExitAsync(selected, cancellationToken);
                    Log.Debug("Cache server disconnected");
                }
                catch (WebSocketException wse)
                {
                    Log.Warn("Failed to connect to cache server {reason}", wse.Message);
                    continue;
                }
                catch (HttpRequestException he) when (he.InnerException is SocketException)
                {
                    Log.Debug("Failed to connect to recommended server {server}", selected.ServerId);
                    //Continue next loop
                    continue;
                }
            }
        }
    }
}