-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStartReplicatorHandler.cs
More file actions
380 lines (319 loc) · 15.2 KB
/
StartReplicatorHandler.cs
File metadata and controls
380 lines (319 loc) · 15.2 KB
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
using Couchbase.Lite;
using Couchbase.Lite.Sync;
using System.Collections;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using TestServer.Utilities;
namespace TestServer.Handlers;
using FilterGenerator = Func<IReadOnlyDictionary<string, JsonElement>?, HandlerList.IReplicatorFilter>;
using FilterFunction = Func<Document, DocumentFlags, bool>;
internal static partial class HandlerList
{
internal interface IReplicatorFilter
{
bool Execute(Document doc, DocumentFlags flags);
}
internal static class ReplicatorFilters
{
public static readonly IReadOnlyDictionary<string, FilterGenerator> FilterMap =
new Dictionary<string, FilterGenerator>
{
["deletedDocumentsOnly"] = (_) => new ReplicatorDeletedOnlyFilter(),
["documentIDs"] = (args) => new ReplicatorDocumentIDsFilter(args)
};
}
internal sealed class ReplicatorDeletedOnlyFilter : IReplicatorFilter
{
public bool Execute(Document doc, DocumentFlags flags)
{
return flags.HasFlag(DocumentFlags.Deleted);
}
}
internal sealed class ReplicatorDocumentIDsFilter : IReplicatorFilter
{
private const string DocumentIDsKey = "documentIDs";
private readonly Dictionary<string, IReadOnlySet<string>> _allowedDocumentIDs;
public ReplicatorDocumentIDsFilter(IReadOnlyDictionary<string, JsonElement>? args)
{
if(args?.ContainsKey(DocumentIDsKey) == false) {
throw new ApplicationStatusException("documentIDs filter is missing documentIDs argument", HttpStatusCode.BadRequest);
}
if (args![DocumentIDsKey].ValueKind != JsonValueKind.Object) {
throw new ApplicationStatusException("documentIDs filter documentIDs argument wrong type (expecting dictionary of arrays)",
HttpStatusCode.BadRequest);
}
_allowedDocumentIDs = args[DocumentIDsKey].EnumerateObject().ToDictionary(x => x.Name, IReadOnlySet<string> (x) =>
{
if (x.Value.ValueKind != JsonValueKind.Array) {
throw new ApplicationStatusException($"documentIDs filter documentIDs argument contained invalid list for '{x.Name}'",
HttpStatusCode.BadRequest);
}
return x.Value.EnumerateArray().Select(y => y.ToString()).ToHashSet();
});
}
public bool Execute(Document doc, DocumentFlags flags)
{
if(doc.Collection == null) {
throw new ApplicationStatusException("Document had null collection in filter", HttpStatusCode.InternalServerError);
}
return _allowedDocumentIDs.TryGetValue(doc.Collection.FullName, out var d) && d.Contains(doc.Id);
}
}
[SuppressMessage("ReSharper", "InconsistentNaming")]
internal readonly record struct DocumentReplicationEvent
{
public required bool isPush { get; init; }
public required string collection { get; init; }
public required string documentID { get; init; }
public required IReadOnlyList<string> flags { get; init; }
public ErrorReturnBody? error { get; init; }
}
internal sealed class ReplicatorDocumentListener : IDisposable, IEnumerable<DocumentReplicationEvent>
{
private readonly ListenerToken _listenerToken;
private List<DocumentReplicationEventArgs> _events = [];
public ReplicatorDocumentListener(Replicator replicator)
{
_listenerToken = replicator.AddDocumentReplicationListener(HandleChange);
}
private void HandleChange(object? sender, DocumentReplicationEventArgs e)
{
_events.Add(e);
}
public void Dispose()
{
try {
_listenerToken.Remove();
} catch(ObjectDisposedException) {
// Replicator already disposed, no need to do anything else
}
}
public IEnumerator<DocumentReplicationEvent> GetEnumerator()
{
var events = Interlocked.Exchange(ref _events, new List<DocumentReplicationEventArgs>());
foreach (var entry in events) {
foreach (var doc in entry.Documents) {
ErrorReturnBody? error = null;
if(doc.Error != null) {
var details = Router.MapError(doc.Error);
error = new ErrorReturnBody
{
code = details.code,
domain = details.domain,
message = doc.Error.Message
};
}
yield return new()
{
isPush = entry.IsPush,
collection = $"{doc.ScopeName}.{doc.CollectionName}",
documentID = doc.Id,
flags = [doc.Flags == 0 ? "None" : doc.Flags.ToString()],
error = error
};
}
}
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
[SuppressMessage("ReSharper", "InconsistentNaming")]
internal readonly record struct StartReplicatorAuthenticator
{
private const string BasicType = "BASIC";
// Note that System.Text.Json does not support private fields or properties
public readonly string username = "";
public readonly string password = "";
public required string type { get; init; }
[JsonConstructor]
public StartReplicatorAuthenticator(string type, string? username = null, string? password = null)
{
this.type = type;
if (type != BasicType) {
return;
}
this.username = username ?? throw new JsonException("Missing username property in auth");
this.password = password ?? throw new JsonException("Missing password property in auth");
}
public Authenticator CreateAuthenticator()
{
return type == BasicType
? new BasicAuthenticator(username, password)
: throw new NotImplementedException("Non-BASIC auth not implemented");
}
}
[SuppressMessage("ReSharper", "InconsistentNaming")]
[method: JsonConstructor]
internal record StartReplicatorFilter(
string name,
[property: JsonPropertyName("params")] IReadOnlyDictionary<string, JsonElement>? parameters = null)
{
public required string name { get; init; } = name;
}
[SuppressMessage("ReSharper", "InconsistentNaming")]
[method: JsonConstructor]
internal record StartReplicatorConflictResolver(
string name,
[property: JsonPropertyName("params")] IReadOnlyDictionary<string, JsonElement>? parameters = null)
{
public required string name { get; init; } = name;
}
[SuppressMessage("ReSharper", "InconsistentNaming")]
internal readonly record struct StartReplicatorCollection
{
public required IReadOnlyList<string> names { get; init; }
public IReadOnlyList<string> channels { get; init; }
public IReadOnlyList<string> documentIDs { get; init; }
public StartReplicatorFilter? pushFilter { get; init; }
public StartReplicatorFilter? pullFilter { get; init; }
public StartReplicatorConflictResolver? conflictResolver { get; init; }
public IConflictResolver? ConflictResolver { get; }
[JsonConstructor]
public StartReplicatorCollection(IReadOnlyList<string> names, IReadOnlyList<string?>? channels = null,
IReadOnlyList<string?>? documentIDs = null, StartReplicatorFilter? pushFilter = null, StartReplicatorFilter? pullFilter = null,
StartReplicatorConflictResolver? conflictResolver = null)
{
this.names = names;
this.channels = channels.NotNull().ToList();
this.documentIDs = documentIDs.NotNull().ToList();
this.pushFilter = pushFilter;
this.pullFilter = pullFilter;
this.conflictResolver = conflictResolver;
switch(conflictResolver?.name) {
case null:
break;
case "local-wins":
ConflictResolver = new LocalWinsConflictResolver();
break;
case "remote-wins":
ConflictResolver = new RemoteWinsConflictResolver();
break;
case "delete":
ConflictResolver = new DeleteConflictResolver();
break;
case "merge":
ConflictResolver = new MergeConflictResolver(conflictResolver.parameters);
break;
default:
throw new JsonException($"Bad conflict resolver choice {conflictResolver}");
}
}
}
[SuppressMessage("ReSharper", "InconsistentNaming")]
internal readonly record struct StartReplicatorConfig
{
public required string database { get; init; }
public required string endpoint { get; init; }
public required bool continuous { get; init; }
public required string replicatorType { get; init; }
public required IReadOnlyList<StartReplicatorCollection> collections { get; init; }
public StartReplicatorAuthenticator? authenticator { get; init; }
public ReplicatorType ReplicatorType { get; }
public bool enableDocumentListener { get; init; }
public bool enableAutoPurge { get; init; }
public string? pinnedServerCert { get; init; }
public IReadOnlyDictionary<string, string?>? headers { get; init; }
[JsonConstructor]
public StartReplicatorConfig(string database, string endpoint,
string replicatorType, bool continuous, IReadOnlyList<StartReplicatorCollection> collections,
StartReplicatorAuthenticator? authenticator = null, bool enableDocumentListener = false,
bool enableAutoPurge = true, string? pinnedServerCert = null, IReadOnlyDictionary<string, string?>? headers = null)
{
this.database = database;
this.endpoint = endpoint;
this.replicatorType = replicatorType;
this.continuous = continuous;
this.collections = collections;
this.authenticator = authenticator;
this.enableDocumentListener = enableDocumentListener;
this.enableAutoPurge = enableAutoPurge;
this.pinnedServerCert = pinnedServerCert;
this.headers = headers;
if (replicatorType.ToLowerInvariant() == "pull") {
ReplicatorType = ReplicatorType.Pull;
} else if (replicatorType.ToLowerInvariant() == "push") {
ReplicatorType = ReplicatorType.Push;
} else if (replicatorType.ToLowerInvariant() == "pushandpull") {
ReplicatorType = ReplicatorType.PushAndPull;
} else {
throw new JsonException($"Invalid replicatorType '{replicatorType}' (expecting push, pull, or pushAndPull)");
}
}
}
[SuppressMessage("ReSharper", "InconsistentNaming")]
[method: JsonConstructor]
internal readonly record struct StartReplicatorBody(StartReplicatorConfig config, bool reset = false)
{
public required StartReplicatorConfig config { get; init; } = config;
}
private static FilterFunction? GetFilter(Session session, StartReplicatorFilter? input)
{
if (input is null) {
return null;
}
if(!ReplicatorFilters.FilterMap.TryGetValue(input.name, out var filterGen)) {
throw new JsonException($"Unknown push filter {input.name}");
}
var filter = filterGen(input.parameters);
session.ObjectManager.KeepAlive(filter);
return filter.Execute;
}
[HttpHandler("startReplicator")]
public static Task StartReplicatorHandler(int version, Session session, JsonDocument body, HttpListenerResponse response)
{
if(!body.RootElement.TryDeserialize<StartReplicatorBody>(response, version, out var deserializedBody)) {
return Task.CompletedTask;
}
var db = session.ObjectManager.GetDatabase(deserializedBody.config.database);
if (db == null) {
response.WriteBody(Router.CreateErrorResponse($"Unable to find db named '{deserializedBody.config.database}'!"), version, HttpStatusCode.BadRequest);
return Task.CompletedTask;
}
var endpoint = new URLEndpoint(new Uri(deserializedBody.config.endpoint));
var collectionConfigs = new List<CollectionConfiguration>();
if (deserializedBody.config.collections.Any()) {
foreach (var c in deserializedBody.config.collections) {
foreach(var name in c.names) {
var spec = CollectionSpec(name);
var coll = db.GetCollection(spec.name, spec.scope);
if (coll == null) {
response.WriteBody(Router.CreateErrorResponse($"Unable to find collection '{name}'"), version, HttpStatusCode.BadRequest);
return Task.CompletedTask;
}
var collectionConfig = new CollectionConfiguration(coll)
{
Channels = c.channels.Any() ? c.channels.ToImmutableList() : null,
DocumentIDs = c.documentIDs.Any() ? c.documentIDs.ToImmutableList() : null,
PushFilter = GetFilter(session, c.pushFilter),
PullFilter = GetFilter(session, c.pullFilter),
ConflictResolver = c.ConflictResolver
};
collectionConfigs.Add(collectionConfig);
}
}
} else {
collectionConfigs = CollectionConfiguration.FromCollections(db.GetDefaultCollection());
}
var replConfig = new ReplicatorConfiguration(collectionConfigs, endpoint)
{
Authenticator = deserializedBody.config.authenticator?.CreateAuthenticator(),
Continuous = deserializedBody.config.continuous,
ReplicatorType = deserializedBody.config.ReplicatorType,
EnableAutoPurge = deserializedBody.config.enableAutoPurge,
PinnedServerCertificate = deserializedBody.config.pinnedServerCert != null
? new(Encoding.ASCII.GetBytes(deserializedBody.config.pinnedServerCert))
: null,
Headers = deserializedBody.config.headers?.ToImmutableDictionary() ?? ImmutableDictionary<string, string?>.Empty,
};
var (repl, id) = session.ObjectManager.RegisterObject(() => new Replicator(replConfig));
if(deserializedBody.config.enableDocumentListener) {
session.ObjectManager.RegisterObject(() => new ReplicatorDocumentListener(repl), $"{id}_listener");
}
repl.Start(deserializedBody.reset);
response.WriteBody(new { id }, version);
return Task.CompletedTask;
}
}