-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
CoreClusterMember.java
179 lines (156 loc) · 6.85 KB
/
CoreClusterMember.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
/*
* Copyright (c) 2002-2016 "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.coreedge.discovery;
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.function.IntFunction;
import org.neo4j.coreedge.core.CoreEdgeClusterSettings;
import org.neo4j.coreedge.core.CoreGraphDatabase;
import org.neo4j.coreedge.core.consensus.RaftMachine;
import org.neo4j.coreedge.core.consensus.log.segmented.FileNames;
import org.neo4j.coreedge.core.state.CoreState;
import org.neo4j.coreedge.identity.MemberId;
import org.neo4j.helpers.AdvertisedSocketAddress;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.io.fs.DefaultFileSystemAbstraction;
import org.neo4j.kernel.GraphDatabaseDependencies;
import org.neo4j.logging.Level;
import static java.lang.String.format;
import static java.util.stream.Collectors.joining;
import static org.neo4j.coreedge.core.EnterpriseCoreEditionModule.CLUSTER_STATE_DIRECTORY_NAME;
import static org.neo4j.coreedge.core.consensus.log.RaftLog.PHYSICAL_LOG_DIRECTORY_NAME;
import static org.neo4j.helpers.collection.MapUtil.stringMap;
public class CoreClusterMember implements ClusterMember
{
private final File neo4jHome;
private final DiscoveryServiceFactory discoveryServiceFactory;
private final File storeDir;
private final Map<String, String> config = stringMap();
private final int serverId;
private final String boltAdvertisedAddress;
private CoreGraphDatabase database;
public CoreClusterMember( int serverId, int clusterSize,
List<AdvertisedSocketAddress> addresses,
DiscoveryServiceFactory discoveryServiceFactory,
String recordFormat,
File parentDir,
Map<String, String> extraParams,
Map<String, IntFunction<String>> instanceExtraParams )
{
this.serverId = serverId;
int hazelcastPort = 5000 + serverId;
int txPort = 6000 + serverId;
int raftPort = 7000 + serverId;
int boltPort = 8000 + serverId;
String initialMembers = addresses.stream().map( AdvertisedSocketAddress::toString ).collect( joining( "," ) );
config.put( "dbms.mode", "CORE" );
config.put( GraphDatabaseSettings.default_advertised_address.name(), "localhost" );
config.put( CoreEdgeClusterSettings.initial_discovery_members.name(), initialMembers );
config.put( CoreEdgeClusterSettings.discovery_listen_address.name(), "127.0.0.1:" + hazelcastPort );
config.put( CoreEdgeClusterSettings.transaction_listen_address.name(), "127.0.0.1:" + txPort );
config.put( CoreEdgeClusterSettings.raft_listen_address.name(), "127.0.0.1:" + raftPort );
config.put( CoreEdgeClusterSettings.expected_core_cluster_size.name(), String.valueOf( clusterSize ) );
config.put( GraphDatabaseSettings.store_internal_log_level.name(), Level.DEBUG.name() );
config.put( GraphDatabaseSettings.record_format.name(), recordFormat );
config.put( new GraphDatabaseSettings.BoltConnector( "bolt" ).type.name(), "BOLT" );
config.put( new GraphDatabaseSettings.BoltConnector( "bolt" ).enabled.name(), "true" );
config.put( new GraphDatabaseSettings.BoltConnector( "bolt" ).address.name(), "0.0.0.0:" + boltPort );
config.put( new GraphDatabaseSettings.BoltConnector( "bolt" ).listen_address.name(), "127.0.0.1:" + boltPort );
boltAdvertisedAddress = "127.0.0.1:" + boltPort;
config.put( new GraphDatabaseSettings.BoltConnector( "bolt" ).advertised_address.name(), boltAdvertisedAddress );
config.put( GraphDatabaseSettings.pagecache_memory.name(), "8m" );
config.put( GraphDatabaseSettings.auth_store.name(), new File( parentDir, "auth" ).getAbsolutePath() );
config.putAll( extraParams );
for ( Map.Entry<String, IntFunction<String>> entry : instanceExtraParams.entrySet() )
{
config.put( entry.getKey(), entry.getValue().apply( serverId ) );
}
this.neo4jHome = new File( parentDir, "server-core-" + serverId );
config.put( GraphDatabaseSettings.logs_directory.name(), new File( neo4jHome, "logs" ).getAbsolutePath() );
this.discoveryServiceFactory = discoveryServiceFactory;
storeDir = new File( new File( new File( neo4jHome, "data" ), "databases" ), "graph.db" );
storeDir.mkdirs();
}
public String boltAdvertisedAddress()
{
return boltAdvertisedAddress;
}
public String routingAddress()
{
return String.format( "bolt+routing://%s", boltAdvertisedAddress );
}
@Override
public void start()
{
database = new CoreGraphDatabase( storeDir, config,
GraphDatabaseDependencies.newDependencies(), discoveryServiceFactory );
}
@Override
public void shutdown()
{
if ( database != null )
{
database.shutdown();
database = null;
}
}
@Override
public CoreGraphDatabase database()
{
return database;
}
public File storeDir()
{
return storeDir;
}
public CoreState coreState()
{
return database.getDependencyResolver().resolveDependency( CoreState.class );
}
public RaftMachine raft()
{
return database.getDependencyResolver().resolveDependency( RaftMachine.class );
}
public MemberId id()
{
return database.getDependencyResolver().resolveDependency( RaftMachine.class ).identity();
}
public SortedMap<Long, File> getLogFileNames()
{
File clusterStateDir = new File( storeDir, CLUSTER_STATE_DIRECTORY_NAME );
File logFilesDir = new File( clusterStateDir, PHYSICAL_LOG_DIRECTORY_NAME );
return new FileNames( logFilesDir ).getAllFiles( new DefaultFileSystemAbstraction(), null );
}
public File homeDir()
{
return neo4jHome;
}
@Override
public String toString()
{
return format( "CoreClusterMember{serverId=%d}", serverId );
}
public int serverId()
{
return serverId;
}
}