-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
CausalClusterInProcessBuilder.java
471 lines (388 loc) · 17.7 KB
/
CausalClusterInProcessBuilder.java
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
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j Enterprise Edition. The included source
* code can be redistributed and/or modified under the terms of the
* GNU AFFERO GENERAL PUBLIC LICENSE Version 3
* (http://www.fsf.org/licensing/licenses/agpl-3.0.html) with the
* Commons Clause, as found in the associated LICENSE.txt file.
*
* This program 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.
*
* Neo4j object code can be licensed independently from the source
* under separate terms from the AGPL. Inquiries can be directed to:
* licensing@neo4j.com
*
* More information is also available at:
* https://neo4j.com/licensing/
*/
package org.neo4j.harness;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.neo4j.causalclustering.core.CausalClusteringSettings;
import org.neo4j.causalclustering.discovery.DiscoveryServiceFactorySelector;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.harness.internal.EnterpriseInProcessServerBuilder;
import org.neo4j.kernel.configuration.BoltConnector;
import org.neo4j.kernel.configuration.HttpConnector;
import org.neo4j.kernel.configuration.Settings;
import org.neo4j.kernel.impl.enterprise.configuration.EnterpriseEditionSettings;
import org.neo4j.kernel.impl.enterprise.configuration.OnlineBackupSettings;
import org.neo4j.logging.Log;
import org.neo4j.logging.LogProvider;
import org.neo4j.server.configuration.ServerSettings;
import static java.util.Collections.synchronizedList;
public class CausalClusterInProcessBuilder
{
public static WithCores init()
{
return new Builder();
}
/**
* Step Builder to ensure that Cluster has all the required pieces
* TODO: Add mapping methods to allow for core hosts and replicas to be unevenly distributed between databases
*/
public static class Builder implements WithCores, WithReplicas, WithLogger, WithPath, WithOptionalDatabasesAndPorts
{
private int numCoreHosts;
private int numReadReplicas;
private Log log;
private Path path;
private PortPickingFactory portFactory = PortPickingFactory.DEFAULT;
private final Map<String, String> config = new HashMap<>();
private List<String> databases = new ArrayList<>( Collections.singletonList( "default" ) );
private DiscoveryServiceFactorySelector.DiscoveryImplementation discoveryServiceFactory = DiscoveryServiceFactorySelector.DEFAULT;
@Override
public WithReplicas withCores( int n )
{
numCoreHosts = n;
return this;
}
@Override
public WithLogger withReplicas( int n )
{
numReadReplicas = n;
return this;
}
@Override
public WithPath withLogger( LogProvider l )
{
log = l.getLog( "org.neo4j.harness.CausalCluster" );
return this;
}
public Builder withConfig( String settingName, String value )
{
config.put( settingName, value );
return this;
}
@Override
public Builder atPath( Path p )
{
path = p;
return this;
}
@Override
public Builder withOptionalPortsStrategy( PortPickingStrategy s )
{
portFactory = new PortPickingFactory( s );
return this;
}
@Override
public Builder withOptionalDatabases( List<String> databaseNames )
{
if ( !databaseNames.isEmpty() )
{
databases = databaseNames;
}
return this;
}
@Override
public Builder withDiscoveryServiceFactory( DiscoveryServiceFactorySelector.DiscoveryImplementation discoveryServiceFactory )
{
this.discoveryServiceFactory = discoveryServiceFactory;
return this;
}
public CausalCluster build()
{
int nDatabases = databases.size();
if ( nDatabases > numCoreHosts )
{
throw new IllegalArgumentException(
"You cannot have more databases than core hosts. Each database in the cluster must have at least 1 core " + "host. You have provided " +
nDatabases + " databases and " + numCoreHosts + " core hosts." );
}
return new CausalCluster( this );
}
}
/**
* Builder step interfaces
*/
public interface WithCores
{
WithReplicas withCores( int n );
}
public interface WithReplicas
{
WithLogger withReplicas( int n );
}
public interface WithLogger
{
WithPath withLogger( LogProvider l );
}
public interface WithPath
{
Builder atPath( Path p );
}
interface WithOptionalDatabasesAndPorts
{
Builder withOptionalPortsStrategy( PortPickingStrategy s );
Builder withOptionalDatabases( List<String> databaseNames );
Builder withDiscoveryServiceFactory( DiscoveryServiceFactorySelector.DiscoveryImplementation discoveryServiceFactory );
}
/**
* Port picker functional interface
*/
public interface PortPickingStrategy
{
int port( int offset, int id );
}
/**
* Port picker factory
*/
public static final class PortPickingFactory
{
public static final PortPickingFactory DEFAULT = new PortPickingFactory( ( offset, id ) -> offset + id );
private final PortPickingStrategy st;
public PortPickingFactory( PortPickingStrategy st )
{
this.st = st;
}
int hazelcastPort( int coreId )
{
return st.port( 55000, coreId );
}
int txCorePort( int coreId )
{
return st.port( 56000, coreId );
}
int raftCorePort( int coreId )
{
return st.port( 57000, coreId );
}
int boltCorePort( int coreId )
{
return st.port( 58000, coreId );
}
int httpCorePort( int coreId )
{
return st.port( 59000, coreId );
}
int httpsCorePort( int coreId )
{
return st.port( 60000, coreId );
}
int txReadReplicaPort( int replicaId )
{
return st.port( 56500, replicaId );
}
int boltReadReplicaPort( int replicaId )
{
return st.port( 58500, replicaId );
}
int httpReadReplicaPort( int replicaId )
{
return st.port( 59500, replicaId );
}
int httpsReadReplicaPort( int replicaId )
{
return st.port( 60500, replicaId );
}
}
/**
* Implementation of in process Cluster
*/
public static class CausalCluster
{
private final int nCores;
private final int nReplicas;
private final List<String> databaseNames;
private final Path clusterPath;
private final Log log;
private final PortPickingFactory portFactory;
private final Map<String,String> config;
private final DiscoveryServiceFactorySelector.DiscoveryImplementation discoveryServiceFactory;
private List<ServerControls> coreControls = synchronizedList( new ArrayList<>() );
private List<ServerControls> replicaControls = synchronizedList( new ArrayList<>() );
private CausalCluster( CausalClusterInProcessBuilder.Builder builder )
{
this.nCores = builder.numCoreHosts;
this.nReplicas = builder.numReadReplicas;
this.clusterPath = builder.path;
this.log = builder.log;
this.portFactory = builder.portFactory;
this.databaseNames = builder.databases;
this.config = builder.config;
this.discoveryServiceFactory = builder.discoveryServiceFactory;
}
private Map<Integer,String> distributeHostsBetweenDatabases( int nHosts, List<String> databases )
{
//Max number of hosts per database is (nHosts / nDatabases) or (nHosts / nDatabases) + 1
int nDatabases = databases.size();
int maxCapacity = ( nHosts % nDatabases == 0 ) ? (nHosts / nDatabases) : (nHosts / nDatabases) + 1;
List<String> repeated =
databases.stream().flatMap( db -> IntStream.range( 0, maxCapacity ).mapToObj( ignored -> db ) ).collect( Collectors.toList() );
Map<Integer,String> mapping = new HashMap<>( nHosts );
for ( int hostId = 0; hostId < nHosts; hostId++ )
{
mapping.put( hostId, repeated.get( hostId ) );
}
return mapping;
}
public void boot() throws InterruptedException
{
List<String> initialMembers = new ArrayList<>( nCores );
Map<Integer,String> initialMembersToDatabase = distributeHostsBetweenDatabases( nCores, databaseNames );
for ( int coreId = 0; coreId < nCores; coreId++ )
{
int hazelcastPort = portFactory.hazelcastPort( coreId );
initialMembers.add( "localhost:" + hazelcastPort );
}
List<Thread> coreThreads = new ArrayList<>();
List<Thread> replicaThreads = new ArrayList<>();
for ( int coreId = 0; coreId < nCores; coreId++ )
{
int hazelcastPort = portFactory.hazelcastPort( coreId );
int txPort = portFactory.txCorePort( coreId );
int raftPort = portFactory.raftCorePort( coreId );
int boltPort = portFactory.boltCorePort( coreId );
int httpPort = portFactory.httpCorePort( coreId );
int httpsPort = portFactory.httpsCorePort( coreId );
String homeDir = "core-" + coreId;
EnterpriseInProcessServerBuilder builder = new EnterpriseInProcessServerBuilder( clusterPath.toFile(), homeDir );
String homePath = Paths.get( clusterPath.toString(), homeDir ).toAbsolutePath().toString();
builder.withConfig( GraphDatabaseSettings.neo4j_home.name(), homePath );
builder.withConfig( GraphDatabaseSettings.pagecache_memory.name(), "8m" );
builder.withConfig( EnterpriseEditionSettings.mode.name(), EnterpriseEditionSettings.Mode.CORE.name() );
builder.withConfig( CausalClusteringSettings.multi_dc_license.name(), "true" );
builder.withConfig( CausalClusteringSettings.initial_discovery_members.name(), String.join( ",", initialMembers ) );
builder.withConfig( CausalClusteringSettings.discovery_listen_address.name(), specifyPortOnly( hazelcastPort ) );
builder.withConfig( CausalClusteringSettings.transaction_listen_address.name(), specifyPortOnly( txPort ) );
builder.withConfig( CausalClusteringSettings.raft_listen_address.name(), specifyPortOnly( raftPort ) );
builder.withConfig( CausalClusteringSettings.database.name(), initialMembersToDatabase.get( coreId ) );
builder.withConfig( CausalClusteringSettings.minimum_core_cluster_size_at_formation.name(), String.valueOf( nCores ) );
builder.withConfig( CausalClusteringSettings.minimum_core_cluster_size_at_runtime.name(), String.valueOf( nCores ) );
builder.withConfig( CausalClusteringSettings.server_groups.name(), "core," + "core" + coreId );
configureConnectors( boltPort, httpPort, httpsPort, builder );
builder.withConfig( ServerSettings.jmx_module_enabled.name(), Settings.FALSE );
builder.withConfig( OnlineBackupSettings.online_backup_enabled, Settings.FALSE );
config.forEach( builder::withConfig );
builder.withDiscoveryServiceFactory( discoveryServiceFactory );
int finalCoreId = coreId;
Thread coreThread = new Thread( () ->
{
coreControls.add( builder.newServer() );
log.info( "Core " + finalCoreId + " started." );
} );
coreThreads.add( coreThread );
coreThread.start();
}
for ( Thread coreThread : coreThreads )
{
coreThread.join();
}
Map<Integer,String> replicasToDatabase = distributeHostsBetweenDatabases( nReplicas, databaseNames );
for ( int replicaId = 0; replicaId < nReplicas; replicaId++ )
{
int txPort = portFactory.txReadReplicaPort( replicaId );
int boltPort = portFactory.boltReadReplicaPort( replicaId );
int httpPort = portFactory.httpReadReplicaPort( replicaId );
int httpsPort = portFactory.httpsReadReplicaPort( replicaId );
String homeDir = "replica-" + replicaId;
EnterpriseInProcessServerBuilder builder = new EnterpriseInProcessServerBuilder( clusterPath.toFile(), homeDir );
String homePath = Paths.get( clusterPath.toString(), homeDir ).toAbsolutePath().toString();
builder.withConfig( GraphDatabaseSettings.neo4j_home.name(), homePath );
builder.withConfig( GraphDatabaseSettings.pagecache_memory.name(), "8m" );
builder.withConfig( EnterpriseEditionSettings.mode.name(), EnterpriseEditionSettings.Mode.READ_REPLICA.name() );
builder.withConfig( CausalClusteringSettings.initial_discovery_members.name(), String.join( ",", initialMembers ) );
builder.withConfig( CausalClusteringSettings.transaction_listen_address.name(), specifyPortOnly( txPort ) );
builder.withConfig( CausalClusteringSettings.database.name(), replicasToDatabase.get( replicaId ) );
builder.withConfig( CausalClusteringSettings.server_groups.name(), "replica," + "replica" + replicaId );
configureConnectors( boltPort, httpPort, httpsPort, builder );
builder.withConfig( ServerSettings.jmx_module_enabled.name(), Settings.FALSE );
builder.withConfig( OnlineBackupSettings.online_backup_enabled, Settings.FALSE );
builder.withDiscoveryServiceFactory( discoveryServiceFactory );
config.forEach( builder::withConfig );
int finalReplicaId = replicaId;
Thread replicaThread = new Thread( () ->
{
replicaControls.add( builder.newServer() );
log.info( "Read replica " + finalReplicaId + " started." );
} );
replicaThreads.add( replicaThread );
replicaThread.start();
}
for ( Thread replicaThread : replicaThreads )
{
replicaThread.join();
}
}
private static String specifyPortOnly( int port )
{
return ":" + port;
}
private static void configureConnectors( int boltPort, int httpPort, int httpsPort, TestServerBuilder builder )
{
builder.withConfig( new BoltConnector( "bolt" ).type.name(), "BOLT" );
builder.withConfig( new BoltConnector( "bolt" ).enabled.name(), "true" );
builder.withConfig( new BoltConnector( "bolt" ).listen_address.name(), specifyPortOnly( boltPort ) );
builder.withConfig( new BoltConnector( "bolt" ).advertised_address.name(), specifyPortOnly( boltPort ) );
builder.withConfig( new HttpConnector( "http", HttpConnector.Encryption.NONE ).type.name(), "HTTP" );
builder.withConfig( new HttpConnector( "http", HttpConnector.Encryption.NONE ).enabled.name(), "true" );
builder.withConfig( new HttpConnector( "http", HttpConnector.Encryption.NONE ).listen_address.name(), specifyPortOnly( httpPort ) );
builder.withConfig( new HttpConnector( "http", HttpConnector.Encryption.NONE ).advertised_address.name(), specifyPortOnly( httpPort ) );
builder.withConfig( new HttpConnector( "https", HttpConnector.Encryption.TLS ).type.name(), "HTTP" );
builder.withConfig( new HttpConnector( "https", HttpConnector.Encryption.TLS ).enabled.name(), "true" );
builder.withConfig( new HttpConnector( "https", HttpConnector.Encryption.TLS ).listen_address.name(), specifyPortOnly( httpsPort ) );
builder.withConfig( new HttpConnector( "https", HttpConnector.Encryption.TLS ).advertised_address.name(), specifyPortOnly( httpsPort ) );
}
public List<ServerControls> getCoreControls()
{
return coreControls;
}
public List<ServerControls> getReplicaControls()
{
return replicaControls;
}
public void shutdown() throws InterruptedException
{
shutdownControls( replicaControls );
shutdownControls( coreControls );
}
private void shutdownControls( Iterable<? extends ServerControls> controls ) throws InterruptedException
{
Collection<Thread> threads = new ArrayList<>();
for ( ServerControls control : controls )
{
Thread thread = new Thread( control::close );
threads.add( thread );
thread.start();
}
for ( Thread thread : threads )
{
thread.join();
}
}
}
}