aboutsummaryrefslogtreecommitdiff
path: root/plugins/ObjectCacheServer/src/Clustering/CacheNodeReplicationMaanger.cs
blob: a240dde2ec5073981908d0fbd06ddd80cb949987 (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
* Copyright (c) 2023 Vaughn Nugent
* 
* Library: VNLib
* Package: ObjectCacheServer
* File: CacheNodeReplicationMaanger.cs 
*
* CacheNodeReplicationMaanger.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.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;

using VNLib.Plugins;
using VNLib.Utils.Logging;
using VNLib.Data.Caching.Extensions;
using static VNLib.Data.Caching.Constants;
using VNLib.Net.Messaging.FBM;
using VNLib.Net.Messaging.FBM.Client;
using VNLib.Plugins.Extensions.Loading;
using VNLib.Data.Caching.Extensions.Clustering;
using VNLib.Data.Caching.ObjectCache.Server.Cache;

namespace VNLib.Data.Caching.ObjectCache.Server.Clustering
{

    /*
     * This class is responsible for replicating the cache with other nodes.
     * 
     * It does this by connecting to other nodes and listening for change events. 
     * When a change event occurs, it takes action against the local cache store, 
     * to keep it consistent with the other nodes.
     * 
     * Change events are only handled first-hand, meaning that events do not 
     * propagate to other nodes, they must be connected individually to each node
     * and listen for changes.
     */

    internal sealed class CacheNodeReplicationMaanger : IAsyncBackgroundWork
    {
        private const string LOG_SCOPE_NAME = "REPL";

        private static readonly TimeSpan GetItemTimeout = TimeSpan.FromSeconds(10);
        private const int MAX_MESSAGE_SIZE = 12 * 1024;

        private readonly PluginBase _plugin;
        private readonly ILogProvider _log;
        private readonly NodeConfig _nodeConfig;
        private readonly ICacheStore _cacheStore;
        private readonly ICachePeerAdapter _peerAdapter;
        private readonly FBMClientFactory _clientFactory;
       
        private readonly bool _isDebug;

        private int _openConnections;

        public CacheNodeReplicationMaanger(PluginBase plugin)
        {
            //Load the node config
            _nodeConfig = plugin.GetOrCreateSingleton<NodeConfig>();
            _cacheStore = plugin.GetOrCreateSingleton<CacheStore>();
            _peerAdapter = plugin.GetOrCreateSingleton<PeerDiscoveryManager>();       

            //Init fbm config with fixed message size
            FBMClientConfig clientConfig = FBMDataCacheExtensions.GetDefaultConfig(
                (plugin as ObjectCacheServerEntry)!.ListenerHeap,
                MAX_MESSAGE_SIZE,
                debugLog: plugin.IsDebug() ? plugin.Log : null
            );

            //Init ws fallback factory and client factory
            FBMFallbackClientWsFactory wsFactory = new();
            _clientFactory = new(in clientConfig, wsFactory);

            _plugin = plugin;
            _isDebug = plugin.IsDebug();
            _log = plugin.Log.CreateScope(LOG_SCOPE_NAME);
        }

        public async Task DoWorkAsync(ILogProvider pluginLog, CancellationToken exitToken)
        {
            _log.Information("Initializing node replication worker");

            try
            {
                while (true)
                {
                    //Get all new peers
                    CacheNodeAdvertisment[] peers = _peerAdapter.GetNewPeers();

                    if (peers.Length == 0 && _isDebug)
                    {
                        _log.Verbose("No new peers to connect to");
                    }

                    //Make sure we don't exceed the max connections
                    if(_openConnections >= _nodeConfig.MaxPeerConnections)
                    {
                        if (_isDebug)
                        {
                            _log.Verbose("Max peer connections reached, waiting for a connection to close");
                        }
                    }
                    else
                    {
                        //Connect to each peer as a background task
                        foreach (CacheNodeAdvertisment peer in peers)
                        {
                            _ = _plugin.ObserveWork(() => OnNewPeerDoWorkAsync(peer, _log, exitToken));
                        }
                    }

                    //Wait for a new peers
                    await Task.Delay(10000, exitToken);
                }
            }
            catch (OperationCanceledException)
            {
                //Normal exit
            }
            catch
            {
                _log.Error("Node replication worker exited with an error");
                throw;
            }
            finally
            {

            }

            _log.Information("Node replication worker exited");
        }

        private async Task OnNewPeerDoWorkAsync(CacheNodeAdvertisment newPeer, ILogProvider log, CancellationToken exitToken)
        {
            _ = newPeer ?? throw new ArgumentNullException(nameof(newPeer));

            //Setup client 
            FBMClient client = _clientFactory.CreateClient();

            //Add peer to monitor
            _peerAdapter.OnPeerListenerAttached(newPeer);

            Interlocked.Increment(ref _openConnections);

            try
            {
                log.Information("Establishing replication connection to peer {server}...", newPeer.NodeId);

                //Connect to the server
                await client.ConnectToCacheAsync(newPeer, _nodeConfig.Config, exitToken);

                log.Information("Connected to {server}, starting queue listeners", newPeer.NodeId);

                //Start worker tasks
                List<Task> workerTasks = new();

                for (int i = 0; i < Environment.ProcessorCount; i++)
                {
                    Task workerTask = Task.Run(() => ReplicationWorkerDoWorkAsync(client, log, exitToken), exitToken);

                    workerTasks.Add(workerTask);
                }

                //Wait for sync workers to exit
                await Task.WhenAll(workerTasks);

                log.Debug("All cache worker tasks exited successfully, disconnecting from {server}", newPeer.NodeId);

                //Disconnect client gracefully
                await client.DisconnectAsync(CancellationToken.None);
            }
            catch (InvalidResponseException ie)
            {
                //See if the plugin is unloading
                if (!exitToken.IsCancellationRequested)
                {
                    log.Debug("Peer {p} responded with invalid response packet, disconnected. reason\n {reason}", newPeer.NodeId, ie);
                }
                //Disconnect client gracefully
                try
                {
                    await client.DisconnectAsync(CancellationToken.None);
                }
                catch (Exception ex)
                {
                    log.Error(ex);
                }
            }
            catch (OperationCanceledException)
            {
                //Plugin unloading, Try to disconnect 
                try
                {
                    await client.DisconnectAsync(CancellationToken.None);
                }
                catch (Exception ex)
                {
                    log.Error(ex);
                }
            }
            catch (Exception ex)
            {
                log.Warn("Lost connection to peer {h}\n {m}", newPeer.NodeId, ex);
            }
            finally
            {
                Interlocked.Decrement(ref _openConnections);

                client.Dispose();

                //Notify monitor of disconnect
                _peerAdapter.OnPeerListenerDetatched(newPeer);
            }
        }

        //Wroker task callback method
        private async Task ReplicationWorkerDoWorkAsync(FBMClient client, ILogProvider log, CancellationToken exitToken)
        {
            //Reusable request message
            using FBMRequest request = new(in client.Config);

            WaitForChangeResult changedObject = new();

            //Listen for changes
            while (true)
            {
                //Wait for changes
                await client.WaitForChangeAsync(changedObject, exitToken);

                log.Debug("Object changed {typ} {obj}", changedObject.Status, changedObject.CurrentId);

                switch (changedObject.Status)
                {
                    /*
                     * During a WFC operation, if a NotFound response is received, it 
                     * means a wait queue was not found for the connection, usually meaning
                     * the server does not support replication.
                     */
                    case ResponseCodes.NotFound:
                        log.Error("Server cache not properly configured, worker exiting");
                        return;
                    case "deleted":
                        //Delete the object from the store
                        await _cacheStore.DeleteItemAsync(changedObject.CurrentId, CancellationToken.None);
                        break;
                    case "modified":
                        //Reload the record from the store
                        await UpdateRecordAsync(client, request, log, changedObject.CurrentId, changedObject.NewId, exitToken);
                        break;
                    default:
                        log.Error("Unknown status {status} received from server", changedObject.Status);
                        break;
                }

                changedObject.Status = null;
                changedObject.CurrentId = null;
                changedObject.NewId = null;

                //Reset request message
                request.Reset();
            }
        }

        private async Task UpdateRecordAsync(FBMClient client, FBMRequest modRequest, ILogProvider log, string objectId, string newId, CancellationToken cancellation)
        {
            //Set action as get/create
            modRequest.WriteHeader(HeaderCommand.Action, Actions.Get);
            //Set session-id header
            modRequest.WriteHeader(ObjectId, string.IsNullOrWhiteSpace(newId) ? objectId : newId);

            //Make request
            using FBMResponse response = await client.SendAsync(modRequest, GetItemTimeout, cancellation);

            response.ThrowIfNotSet();

            //Check response code
            string status = response.Headers.First(static s => s.Header == HeaderCommand.Status).Value.ToString();

            if (ResponseCodes.Okay.Equals(status, StringComparison.Ordinal))
            {
                //Update the record
                await _cacheStore.AddOrUpdateBlobAsync(objectId, newId, static (t) => t.ResponseBody, response, cancellation);
                log.Debug("Updated object {id}", objectId);
            }
            else
            {
                log.Warn("Object {id} was missing on the remote server", objectId);
            }
        }
    }
}