-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
CausalClusterInProcessRunner.java
315 lines (262 loc) · 12.9 KB
/
CausalClusterInProcessRunner.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
/*
* Copyright (c) 2002-2018 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j 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.
*
* 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.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.harness;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ExecutionException;
import org.neo4j.causalclustering.core.CausalClusteringSettings;
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;
import static org.neo4j.logging.FormattedLogProvider.toOutputStream;
/**
* Simple main class for manual testing of the complete causal cluster stack, including server etc.
*/
public class CausalClusterInProcessRunner
{
private CausalClusterInProcessRunner()
{
}
public static void main( String[] args ) throws IOException, ExecutionException, InterruptedException
{
try
{
Path clusterPath = Files.createTempDirectory( "causal-cluster" );
System.out.println( "clusterPath = " + clusterPath );
CausalCluster cluster = new CausalCluster( 3, 3, clusterPath, toOutputStream( System.out ), PortPickingStrategy.DEFAULT );
System.out.println( "Waiting for cluster to boot up..." );
cluster.boot();
System.out.println( "Press ENTER to exit..." );
//noinspection ResultOfMethodCallIgnored
System.in.read();
System.out.println( "Shutting down..." );
cluster.shutdown();
}
catch ( Throwable e )
{
e.printStackTrace();
System.exit( -1 );
}
System.exit( 0 );
}
abstract static class PortPickingStrategy
{
public static final PortPickingStrategy DEFAULT = new PortPickingStrategy()
{
@Override
int port( int offset, int id )
{
return offset + id;
}
};
abstract int port( int offset, int id );
int hazelcastPort( int coreId )
{
return port( 55000, coreId );
}
int txCorePort( int coreId )
{
return port( 56000, coreId );
}
int raftCorePort( int coreId )
{
return port( 57000, coreId );
}
int boltCorePort( int coreId )
{
return port( 58000, coreId );
}
int httpCorePort( int coreId )
{
return port( 59000, coreId );
}
int httpsCorePort( int coreId )
{
return port( 60000, coreId );
}
int txReadReplicaPort( int replicaId )
{
return port( 56500, replicaId );
}
int boltReadReplicaPort( int replicaId )
{
return port( 58500, replicaId );
}
int httpReadReplicaPort( int replicaId )
{
return port( 59500, replicaId );
}
int httpsReadReplicaPort( int replicaId )
{
return port( 60500, replicaId );
}
}
static class CausalCluster
{
private final int nCores;
private final int nReplicas;
private final Path clusterPath;
private final Log log;
private final PortPickingStrategy portPickingStrategy;
private List<ServerControls> coreControls = synchronizedList( new ArrayList<>() );
private List<ServerControls> replicaControls = synchronizedList( new ArrayList<>() );
CausalCluster( int nCores, int nReplicas, Path clusterPath, LogProvider logProvider, PortPickingStrategy portPickingStrategy )
{
this.nCores = nCores;
this.nReplicas = nReplicas;
this.clusterPath = clusterPath;
this.log = logProvider.getLog( getClass() );
this.portPickingStrategy = portPickingStrategy;
}
void boot() throws IOException, InterruptedException
{
List<String> initialMembers = new ArrayList<>( nCores );
for ( int coreId = 0; coreId < nCores; coreId++ )
{
int hazelcastPort = portPickingStrategy.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 = portPickingStrategy.hazelcastPort( coreId );
int txPort = portPickingStrategy.txCorePort( coreId );
int raftPort = portPickingStrategy.raftCorePort( coreId );
int boltPort = portPickingStrategy.boltCorePort( coreId );
int httpPort = portPickingStrategy.httpCorePort( coreId );
int httpsPort = portPickingStrategy.httpsCorePort( coreId );
String homeDir = "core-" + coreId;
TestServerBuilder 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.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 );
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();
}
for ( int replicaId = 0; replicaId < nReplicas; replicaId++ )
{
int txPort = portPickingStrategy.txReadReplicaPort( replicaId );
int boltPort = portPickingStrategy.boltReadReplicaPort( replicaId );
int httpPort = portPickingStrategy.httpReadReplicaPort( replicaId );
int httpsPort = portPickingStrategy.httpsReadReplicaPort( replicaId );
String homeDir = "replica-" + replicaId;
TestServerBuilder 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.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 );
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 ) );
}
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();
}
}
}
}