/
EmbeddedVNodeBuilder.cs
698 lines (622 loc) · 28.7 KB
/
EmbeddedVNodeBuilder.cs
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using EventStore.Common.Options;
using EventStore.Common.Utils;
using EventStore.Core;
using EventStore.Core.Authentication;
using EventStore.Core.Cluster.Settings;
using EventStore.Core.Services.Gossip;
using EventStore.Core.Services.Monitoring;
using EventStore.Core.TransactionLog.Checkpoint;
using EventStore.Core.TransactionLog.Chunks;
using EventStore.Core.TransactionLog.FileNamingStrategy;
using EventStore.Core.Util;
using EventStore.Projections.Core;
namespace EventStore.ClientAPI.Embedded
{
/// <summary>
/// Allows a client to build a <see cref="ClusterVNode" /> for use with the Embedded client API by specifying
/// high level options rather than using the constructor of <see cref="ClusterVNode"/> directly.
/// </summary>
public class EmbeddedVNodeBuilder
{
// ReSharper disable FieldCanBeMadeReadOnly.Local - as more options are added
private int _chunkSize;
private string _dbPath;
private long _chunksCacheSize;
private bool _inMemoryDb;
private IPEndPoint _internalTcp;
private IPEndPoint _internalSecureTcp;
private IPEndPoint _externalTcp;
private IPEndPoint _externalSecureTcp;
private IPEndPoint _internalHttp;
private IPEndPoint _externalHttp;
private List<string> _httpPrefixes;
private bool _enableTrustedAuth;
private X509Certificate2 _certificate;
private int _workerThreads;
private bool _discoverViaDns;
private string _clusterDns;
private List<IPEndPoint> _gossipSeeds;
private TimeSpan _minFlushDelay;
private int _clusterNodeCount;
private int _prepareAckCount;
private int _commitAckCount;
private TimeSpan _prepareTimeout;
private TimeSpan _commitTimeout;
private int _nodePriority;
private bool _useSsl;
private string _sslTargetHost;
private bool _sslValidateServer;
private TimeSpan _statsPeriod;
private StatsStorage _statsStorage;
private IAuthenticationProviderFactory _authenticationProviderFactory;
private bool _disableScavengeMerging;
private bool _adminOnPublic;
private bool _statsOnPublic;
private bool _gossipOnPublic;
private TimeSpan _gossipInterval;
private TimeSpan _gossipAllowedTimeDifference;
private TimeSpan _gossipTimeout;
private TimeSpan _intTcpHeartbeatTimeout;
private TimeSpan _intTcpHeartbeatInterval;
private TimeSpan _extTcpHeartbeatTimeout;
private TimeSpan _extTcpHeartbeatInterval;
private bool _skipVerifyDbHashes;
private int _maxMemtableSize;
private List<ISubsystem> _subsystems;
private int _clusterGossipPort;
private ProjectionsMode _projectionsMode;
private int _projectionsThreads;
// ReSharper restore FieldCanBeMadeReadOnly.Local
private EmbeddedVNodeBuilder()
{
_chunkSize = TFConsts.ChunkSize;
_dbPath = Path.Combine(Path.GetTempPath(), "EmbeddedEventStore", string.Format("{0:yyyy-MM-dd_HH.mm.ss.ffffff}-EmbeddedNode", DateTime.UtcNow));
_chunksCacheSize = TFConsts.ChunksCacheSize;
_inMemoryDb = true;
_externalTcp = new IPEndPoint(Opts.ExternalIpDefault, Opts.ExternalHttpPortDefault);
_externalSecureTcp = null;
_externalTcp = new IPEndPoint(Opts.ExternalIpDefault, Opts.ExternalHttpPortDefault);
_externalSecureTcp = null;
_externalHttp = new IPEndPoint(Opts.ExternalIpDefault, Opts.ExternalHttpPortDefault);
_externalHttp = new IPEndPoint(Opts.ExternalIpDefault, Opts.ExternalHttpPortDefault);
_httpPrefixes = new List<string>();
_enableTrustedAuth = Opts.EnableTrustedAuthDefault;
_certificate = null;
_workerThreads = Opts.WorkerThreadsDefault;
_discoverViaDns = false;
_clusterDns = Opts.ClusterDnsDefault;
_gossipSeeds = new List<IPEndPoint>();
_minFlushDelay = TimeSpan.FromMilliseconds(Opts.MinFlushDelayMsDefault);
_clusterNodeCount = 1;
_prepareAckCount = 1;
_commitAckCount = 1;
_prepareTimeout = TimeSpan.FromMilliseconds(Opts.PrepareTimeoutMsDefault);
_commitTimeout = TimeSpan.FromMilliseconds(Opts.CommitTimeoutMsDefault);
_nodePriority = Opts.NodePriorityDefault;
_useSsl = Opts.UseInternalSslDefault;
_sslTargetHost = Opts.SslTargetHostDefault;
_sslValidateServer = Opts.SslValidateServerDefault;
_statsPeriod = TimeSpan.FromSeconds(Opts.StatsPeriodDefault);
_statsStorage = StatsStorage.Stream;
_authenticationProviderFactory = new InternalAuthenticationProviderFactory();
_disableScavengeMerging = Opts.DisableScavengeMergeDefault;
_adminOnPublic = Opts.AdminOnExtDefault;
_statsOnPublic = Opts.StatsOnExtDefault;
_gossipOnPublic = Opts.GossipOnExtDefault;
_gossipInterval = TimeSpan.FromMilliseconds(Opts.GossipIntervalMsDefault);
_gossipAllowedTimeDifference = TimeSpan.FromMilliseconds(Opts.GossipAllowedDifferenceMsDefault);
_gossipTimeout = TimeSpan.FromMilliseconds(Opts.GossipTimeoutMsDefault);
_intTcpHeartbeatInterval = TimeSpan.FromMilliseconds(Opts.IntTcpHeartbeatInvervalDefault);
_intTcpHeartbeatTimeout = TimeSpan.FromMilliseconds(Opts.IntTcpHeartbeatTimeoutDefault);
_extTcpHeartbeatInterval = TimeSpan.FromMilliseconds(Opts.ExtTcpHeartbeatIntervalDefault);
_extTcpHeartbeatTimeout = TimeSpan.FromMilliseconds(Opts.ExtTcpHeartbeatTimeoutDefault);
_skipVerifyDbHashes = Opts.SkipDbVerifyDefault;
_maxMemtableSize = Opts.MaxMemtableSizeDefault;
_subsystems = new List<ISubsystem>();
_clusterGossipPort = Opts.ClusterGossipPortDefault;
}
/// <summary>
/// Returns a builder set to construct options for a single node instance
/// </summary>
/// <returns>A <see cref="EmbeddedVNodeBuilder"/> with the options set</returns>
public static EmbeddedVNodeBuilder AsSingleNode()
{
var ret = new EmbeddedVNodeBuilder
{
_clusterNodeCount = 1,
_prepareAckCount = 1,
_commitAckCount = 1
};
return ret;
}
/// <summary>
/// Returns a builder set to construct options for a cluster node instance with a cluster size
/// </summary>
/// <returns>A <see cref="EmbeddedVNodeBuilder"/> with the options set</returns>
public static EmbeddedVNodeBuilder AsClusterMember(int clusterSize)
{
int quorumSize = clusterSize/2;
var ret = new EmbeddedVNodeBuilder
{
_clusterNodeCount = clusterSize,
_prepareAckCount = quorumSize,
_commitAckCount = quorumSize
};
return ret;
}
/// <summary>
/// Sets the mode and the number of threads on which to run projections.
/// </summary>
/// <param name="projectionsMode">The mode in which to run the projections system</param>
/// <param name="numberOfThreads">The number of threads to use for projections. Defaults to 3.</param>
/// <returns>A <see cref="EmbeddedVNodeBuilder"/> with the options set</returns>
public EmbeddedVNodeBuilder RunProjections(ProjectionsMode projectionsMode, int numberOfThreads = Opts.ProjectionThreadsDefault)
{
_projectionsMode = projectionsMode;
_projectionsThreads = numberOfThreads;
return this;
}
/// <summary>
/// Adds a custom subsystem to the builder. NOTE: This is an advanced use case that most people will never need!
/// </summary>
/// <param name="subsystem">The subsystem to add</param>
/// <returns>A <see cref="EmbeddedVNodeBuilder"/> with the options set</returns>
public EmbeddedVNodeBuilder AddCustomSubsystem(ISubsystem subsystem)
{
_subsystems.Add(subsystem);
return this;
}
/// <summary>
/// Returns a builder set to run in memory only
/// </summary>
/// <returns>A <see cref="EmbeddedVNodeBuilder"/> with the options set</returns>
public EmbeddedVNodeBuilder RunInMemory()
{
_inMemoryDb = true;
_dbPath = Path.Combine(Path.GetTempPath(), "EmbeddedEventStore", string.Format("{0:yyyy-MM-dd_HH.mm.ss.ffffff}-EmbeddedNode", DateTime.UtcNow));
return this;
}
/// <summary>
/// Returns a builder set to write database files to the specified path
/// </summary>
/// <param name="path">The path on disk in which to write the database files</param>
/// <returns>A <see cref="EmbeddedVNodeBuilder"/> with the options set</returns>
public EmbeddedVNodeBuilder RunOnDisk(string path)
{
_inMemoryDb = false;
_dbPath = path;
return this;
}
/// <summary>
/// Sets the default endpoints on localhost (1113 tcp, 2113 http)
/// </summary>
/// <returns>A <see cref="EmbeddedVNodeBuilder"/> with the options set</returns>
public EmbeddedVNodeBuilder OnDefaultEndpoints()
{
_internalHttp = new IPEndPoint(Opts.InternalIpDefault, 2112);
_internalTcp = new IPEndPoint(Opts.InternalIpDefault, 1112);
_externalHttp = new IPEndPoint(Opts.ExternalIpDefault, 2113);
_externalTcp = new IPEndPoint(Opts.InternalIpDefault, 1113);
return this;
}
/// <summary>
/// Sets the internal http endpoint to the specified value
/// </summary>
/// <returns>A <see cref="EmbeddedVNodeBuilder"/> with the options set</returns>
public EmbeddedVNodeBuilder WithInternalHttpOn(IPEndPoint endpoint)
{
_internalHttp = endpoint;
return this;
}
/// <summary>
/// Sets the internal gossip port (used when using cluster dns, this should point to a known port gossip will be running on)
/// </summary>
/// <returns>A <see cref="EmbeddedVNodeBuilder"/> with the options set</returns>
public EmbeddedVNodeBuilder WithClusterGossipPort(int port)
{
_clusterGossipPort = port;
return this;
}
/// <summary>
/// Sets the external http endpoint to the specified value
/// </summary>
/// <returns>A <see cref="EmbeddedVNodeBuilder"/> with the options set</returns>
public EmbeddedVNodeBuilder WithExternalHttpOn(IPEndPoint endpoint)
{
_externalHttp = endpoint;
return this;
}
/// <summary>
/// Sets the internal tcp endpoint to the specified value
/// </summary>
/// <returns>A <see cref="EmbeddedVNodeBuilder"/> with the options set</returns>
public EmbeddedVNodeBuilder WithInternalTcpOn(IPEndPoint endpoint)
{
_internalTcp = endpoint;
return this;
}
/// <summary>
/// Sets the internal tcp endpoint to the specified value
/// </summary>
/// <returns>A <see cref="EmbeddedVNodeBuilder"/> with the options set</returns>
public EmbeddedVNodeBuilder WithInternalSecureTcpOn(IPEndPoint endpoint)
{
_internalSecureTcp = endpoint;
return this;
}
/// <summary>
/// Sets the external tcp endpoint to the specified value
/// </summary>
/// <returns>A <see cref="EmbeddedVNodeBuilder"/> with the options set</returns>
public EmbeddedVNodeBuilder WithExternalTcpOn(IPEndPoint endpoint)
{
_externalTcp = endpoint;
return this;
}
/// <summary>
/// Sets the external tcp endpoint to the specified value
/// </summary>
/// <returns>A <see cref="EmbeddedVNodeBuilder"/> with the options set</returns>
public EmbeddedVNodeBuilder WithExternalSecureTcpOn(IPEndPoint endpoint)
{
_externalSecureTcp = endpoint;
return this;
}
/// <summary>
/// Sets that SSL should be used on connections
/// </summary>
/// <returns>A <see cref="EmbeddedVNodeBuilder"/> with the options set</returns>
public EmbeddedVNodeBuilder EnableSsl()
{
_useSsl = true;
return this;
}
/// <summary>
/// Sets the gossip seeds this node should talk to
/// </summary>
/// <param name="endpoints">The gossip seeds this node should try to talk to</param>
/// <returns>A <see cref="EmbeddedVNodeBuilder"/> with the options set</returns>
public EmbeddedVNodeBuilder WithGossipSeeds(params IPEndPoint[] endpoints)
{
_gossipSeeds.Clear();
_gossipSeeds.AddRange(endpoints);
_discoverViaDns = false;
return this;
}
/// <summary>
/// Sets the maximum size a memtable is allowed to reach (in count) before being moved to be a ptable
/// </summary>
/// <param name="size">The maximum count</param>
/// <returns>A <see cref="EmbeddedVNodeBuilder"/> with the options set</returns>
public EmbeddedVNodeBuilder MaximumMemoryTableSizeOf(int size)
{
_maxMemtableSize = size;
return this;
}
/// <summary>
/// Marks that the existing database files should not be checked for checksums on startup.
/// </summary>
/// <returns>A <see cref="EmbeddedVNodeBuilder"/> with the options set</returns>
public EmbeddedVNodeBuilder DoNotVerifyDbHashes()
{
_skipVerifyDbHashes = false;
return this;
}
/// <summary>
/// Disables gossip on the public (client) interface
/// </summary>
/// <returns>A <see cref="EmbeddedVNodeBuilder"/> with the options set</returns>
public EmbeddedVNodeBuilder NoGossipOnPublicInterface()
{
_gossipOnPublic = false;
return this;
}
/// <summary>
/// Disables the admin interface on the public (client) interface
/// </summary>
/// <returns>A <see cref="EmbeddedVNodeBuilder"/> with the options set</returns>
public EmbeddedVNodeBuilder NoAdminOnPublicInterface()
{
_adminOnPublic = false;
return this;
}
/// <summary>
/// Disables statistics screens on the public (client) interface
/// </summary>
/// <returns>A <see cref="EmbeddedVNodeBuilder"/> with the options set</returns>
public EmbeddedVNodeBuilder NoStatsOnPublicInterface()
{
_adminOnPublic = false;
return this;
}
/// <summary>
/// Marks that the existing database files should be checked for checksums on startup.
/// </summary>
/// <returns>A <see cref="EmbeddedVNodeBuilder"/> with the options set</returns>
public EmbeddedVNodeBuilder VerifyDbHashes()
{
_skipVerifyDbHashes = true;
return this;
}
/// <summary>
/// Sets the dns name used for the discovery of other cluster nodes
/// </summary>
/// <param name="name">The dns name the node should use to discover gossip partners</param>
/// <returns>A <see cref="EmbeddedVNodeBuilder"/> with the options set</returns>
public EmbeddedVNodeBuilder WithClusterDnsName(string name)
{
_clusterDns = name;
_discoverViaDns = true;
return this;
}
/// <summary>
/// Sets the number of worker threads to use in shared threadpool
/// </summary>
/// <param name="count">The number of worker threads</param>
/// <returns>A <see cref="EmbeddedVNodeBuilder"/> with the options set</returns>
public EmbeddedVNodeBuilder WithWorkerThreads(int count)
{
_workerThreads = count;
return this;
}
/// <summary>
/// Adds a http prefix for the external http endpoint
/// </summary>
/// <param name="prefix">The prefix to add</param>
/// <returns>A <see cref="EmbeddedVNodeBuilder"/> with the options set</returns>
public EmbeddedVNodeBuilder AddHttpPrefix(string prefix)
{
_httpPrefixes.Add(prefix);
return this;
}
/// <summary>
/// Sets the Server SSL Certificate to be loaded from a file
/// </summary>
/// <param name="path">The path to the certificate file</param>
/// <param name="password">The password for the certificate</param>
/// <returns>A <see cref="EmbeddedVNodeBuilder"/> with the options set</returns>
public EmbeddedVNodeBuilder WithServerCertificateFromFile(string path, string password)
{
var cert = new X509Certificate2(path, password);
_certificate = cert;
return this;
}
/// <summary>
/// Sets the heartbeat interval for the internal network interface.
/// </summary>
/// <param name="heartbeatInterval">The heartbeat interval</param>
/// <returns>A <see cref="EmbeddedVNodeBuilder"/> with the options set</returns>
public EmbeddedVNodeBuilder WithInternalHeartbeatInterval(TimeSpan heartbeatInterval)
{
_intTcpHeartbeatInterval = heartbeatInterval;
return this;
}
/// <summary>
/// Sets the heartbeat interval for the external network interface.
/// </summary>
/// <param name="heartbeatInterval">The heartbeat interval</param>
/// <returns>A <see cref="EmbeddedVNodeBuilder"/> with the options set</returns>
public EmbeddedVNodeBuilder WithExternalHeartbeatInterval(TimeSpan heartbeatInterval)
{
_extTcpHeartbeatInterval = heartbeatInterval;
return this;
}
/// <summary>
/// Sets the heartbeat timeout for the internal network interface.
/// </summary>
/// <param name="heartbeatTimeout">The heartbeat timeout</param>
/// <returns>A <see cref="EmbeddedVNodeBuilder"/> with the options set</returns>
public EmbeddedVNodeBuilder WithInternalHeartbeatTimeout(TimeSpan heartbeatTimeout)
{
_intTcpHeartbeatTimeout = heartbeatTimeout;
return this;
}
/// <summary>
/// Sets the heartbeat timeout for the external network interface.
/// </summary>
/// <param name="heartbeatTimeout">The heartbeat timeout</param>
/// <returns>A <see cref="EmbeddedVNodeBuilder"/> with the options set</returns>
public EmbeddedVNodeBuilder WithExternalHeartbeatTimeout(TimeSpan heartbeatTimeout)
{
_extTcpHeartbeatTimeout = heartbeatTimeout;
return this;
}
/// <summary>
/// Sets the Server SSL Certificate to be loaded from a certificate store
/// </summary>
/// <param name="storeLocation">The location of the certificate store</param>
/// <param name="storeName">The name of the certificate store</param>
/// <param name="certificateSubjectName">The subject name of the certificate</param>
/// <param name="certificateThumbprint">The thumbpreint of the certificate</param>
/// <returns>A <see cref="EmbeddedVNodeBuilder"/> with the options set</returns>
public EmbeddedVNodeBuilder WithServerCertificateFromStore(StoreLocation storeLocation, StoreName storeName, string certificateSubjectName, string certificateThumbprint)
{
var store = new X509Store(storeName, storeLocation);
try
{
store.Open(OpenFlags.OpenExistingOnly);
}
catch (Exception exc)
{
throw new Exception(string.Format("Couldn't open certificate store '{0}' in location {1}'.", storeName, storeLocation), exc);
}
if (!string.IsNullOrWhiteSpace(certificateThumbprint))
{
var certificates = store.Certificates.Find(X509FindType.FindByThumbprint, certificateThumbprint, true);
if (certificates.Count == 0)
throw new Exception(string.Format("Could not find valid certificate with thumbprint '{0}'.", certificateThumbprint));
//Can this even happen?
if (certificates.Count > 1)
throw new Exception(string.Format("Cannot determine a unique certificate from thumbprint '{0}'.", certificateThumbprint));
_certificate = certificates[0];
return this;
}
if (!string.IsNullOrWhiteSpace(certificateSubjectName))
{
var certificates = store.Certificates.Find(X509FindType.FindBySubjectName, certificateSubjectName, true);
if (certificates.Count == 0)
throw new Exception(string.Format("Could not find valid certificate with thumbprint '{0}'.", certificateThumbprint));
//Can this even happen?
if (certificates.Count > 1)
throw new Exception(string.Format("Cannot determine a unique certificate from thumbprint '{0}'.", certificateThumbprint));
_certificate = certificates[0];
return this;
}
throw new ArgumentException("No thumbprint or subject name was specified for a certificate, but a certificate store was specified.");
}
private void EnsureHttpPrefixes()
{
if (_httpPrefixes == null || _httpPrefixes.IsEmpty())
_httpPrefixes = new List<string>(new[] {_externalHttp.ToHttpUrl()});
if (!Runtime.IsMono)
return;
if (!_httpPrefixes.Contains(x => x.Contains("localhost")) && Equals(_externalHttp.Address, IPAddress.Loopback))
{
_httpPrefixes.Add(string.Format("http://localhost:{0}/", _externalHttp.Port));
}
}
private void SetUpProjectionsIfNeeded()
{
if (_projectionsMode == ProjectionsMode.None)
return;
ProjectionType internalProjectionType;
switch (_projectionsMode)
{
case ProjectionsMode.System:
internalProjectionType = ProjectionType.System;
break;
case ProjectionsMode.All:
internalProjectionType = ProjectionType.All;
break;
default:
throw new ArgumentOutOfRangeException();
}
_subsystems.Add(new ProjectionsSubsystem(_projectionsThreads, internalProjectionType));
}
/// <summary>
/// Converts an <see cref="EmbeddedVNodeBuilder"/> to a <see cref="ClusterVNode"/>.
/// </summary>
/// <param name="builder"></param>
/// <returns></returns>
public static implicit operator ClusterVNode(EmbeddedVNodeBuilder builder)
{
builder.EnsureHttpPrefixes();
builder.SetUpProjectionsIfNeeded();
var dbConfig = CreateDbConfig(builder._chunkSize, builder._dbPath, builder._chunksCacheSize,
builder._inMemoryDb);
var db = new TFChunkDb(dbConfig);
var vNodeSettings = new ClusterVNodeSettings(Guid.NewGuid(),
0,
builder._internalTcp,
builder._internalSecureTcp,
builder._externalTcp,
builder._externalSecureTcp,
builder._internalHttp,
builder._externalHttp,
builder._httpPrefixes.ToArray(),
builder._enableTrustedAuth,
builder._certificate,
builder._workerThreads,
builder._discoverViaDns,
builder._clusterDns,
builder._gossipSeeds.ToArray(),
builder._minFlushDelay,
builder._clusterNodeCount,
builder._prepareAckCount,
builder._commitAckCount,
builder._prepareTimeout,
builder._commitTimeout,
builder._useSsl,
builder._sslTargetHost,
builder._sslValidateServer,
builder._statsPeriod,
builder._statsStorage,
builder._nodePriority,
builder._authenticationProviderFactory,
builder._disableScavengeMerging,
builder._adminOnPublic,
builder._statsOnPublic,
builder._gossipOnPublic,
builder._gossipInterval,
builder._gossipAllowedTimeDifference,
builder._gossipTimeout,
builder._intTcpHeartbeatTimeout,
builder._intTcpHeartbeatInterval,
builder._extTcpHeartbeatTimeout,
builder._extTcpHeartbeatInterval,
!builder._skipVerifyDbHashes,
builder._maxMemtableSize);
return new ClusterVNode(db, vNodeSettings, builder.GetGossipSource(), builder._subsystems.ToArray());
}
private IGossipSeedSource GetGossipSource()
{
IGossipSeedSource gossipSeedSource;
if (_discoverViaDns)
{
gossipSeedSource = new DnsGossipSeedSource(_clusterDns, _clusterGossipPort);
}
else
{
if ((_gossipSeeds == null || _gossipSeeds.Count == 0) && _clusterNodeCount > 1)
{
throw new Exception("DNS discovery is disabled, but no gossip seed endpoints have been specified. "
+ "Specify gossip seeds");
}
if (_gossipSeeds == null)
throw new ApplicationException("Gossip seeds cannot be null");
gossipSeedSource = new KnownEndpointGossipSeedSource(_gossipSeeds.ToArray());
}
return gossipSeedSource;
}
private static TFChunkDbConfig CreateDbConfig(int chunkSize, string dbPath, long chunksCacheSize, bool inMemDb)
{
ICheckpoint writerChk;
ICheckpoint chaserChk;
ICheckpoint epochChk;
ICheckpoint truncateChk;
if (inMemDb)
{
writerChk = new InMemoryCheckpoint(Checkpoint.Writer);
chaserChk = new InMemoryCheckpoint(Checkpoint.Chaser);
epochChk = new InMemoryCheckpoint(Checkpoint.Epoch, initValue: -1);
truncateChk = new InMemoryCheckpoint(Checkpoint.Truncate, initValue: -1);
}
else
{
var writerCheckFilename = Path.Combine(dbPath, Checkpoint.Writer + ".chk");
var chaserCheckFilename = Path.Combine(dbPath, Checkpoint.Chaser + ".chk");
var epochCheckFilename = Path.Combine(dbPath, Checkpoint.Epoch + ".chk");
var truncateCheckFilename = Path.Combine(dbPath, Checkpoint.Truncate + ".chk");
if (Runtime.IsMono)
{
writerChk = new FileCheckpoint(writerCheckFilename, Checkpoint.Writer, cached: true);
chaserChk = new FileCheckpoint(chaserCheckFilename, Checkpoint.Chaser, cached: true);
epochChk = new FileCheckpoint(epochCheckFilename, Checkpoint.Epoch, cached: true, initValue: -1);
truncateChk = new FileCheckpoint(truncateCheckFilename, Checkpoint.Truncate, cached: true, initValue: -1);
}
else
{
writerChk = new MemoryMappedFileCheckpoint(writerCheckFilename, Checkpoint.Writer, cached: true);
chaserChk = new MemoryMappedFileCheckpoint(chaserCheckFilename, Checkpoint.Chaser, cached: true);
epochChk = new MemoryMappedFileCheckpoint(epochCheckFilename, Checkpoint.Epoch, cached: true, initValue: -1);
truncateChk = new MemoryMappedFileCheckpoint(truncateCheckFilename, Checkpoint.Truncate, cached: true, initValue: -1);
}
}
var nodeConfig = new TFChunkDbConfig(dbPath,
new VersionedPatternFileNamingStrategy(dbPath, "chunk-"),
chunkSize,
chunksCacheSize,
writerChk,
chaserChk,
epochChk,
truncateChk,
inMemDb);
return nodeConfig;
}
}
}