-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
HazelcastClusterTopology.java
236 lines (197 loc) · 9.88 KB
/
HazelcastClusterTopology.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
/*
* Copyright (c) 2002-2017 "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.causalclustering.discovery;
import com.hazelcast.config.MemberAttributeConfig;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IAtomicReference;
import com.hazelcast.core.IMap;
import com.hazelcast.core.Member;
import com.hazelcast.core.MultiMap;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import org.neo4j.causalclustering.core.CausalClusteringSettings;
import org.neo4j.causalclustering.identity.ClusterId;
import org.neo4j.causalclustering.identity.MemberId;
import org.neo4j.helpers.AdvertisedSocketAddress;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.logging.Log;
import static java.util.Collections.emptyMap;
import static org.neo4j.helpers.SocketAddressFormat.socketAddress;
import static org.neo4j.helpers.collection.Iterables.asSet;
class HazelcastClusterTopology
{
// per server attributes
private static final String DISCOVERY_SERVER = "discovery_server"; // not currently used
static final String MEMBER_UUID = "member_uuid";
static final String TRANSACTION_SERVER = "transaction_server";
static final String RAFT_SERVER = "raft_server";
static final String CLIENT_CONNECTOR_ADDRESSES = "client_connector_addresses";
private static final String REFUSE_TO_BE_LEADER_KEY = "refuseToBeLeader";
// cluster-wide attributes
private static final String CLUSTER_UUID = "cluster_uuid";
static final String SERVER_TAGS_MULTIMAP_NAME = "tags";
static final String READ_REPLICA_TRANSACTION_SERVER_ADDRESS_MAP_NAME = "read-replica-transaction-servers";
static final String READ_REPLICA_BOLT_ADDRESS_MAP_NAME = "read_replicas"; // hz client uuid string -> boltAddress string
static final String READ_REPLICA_MEMBER_ID_MAP_NAME = "read-replica-member-ids";
static ReadReplicaTopology getReadReplicaTopology( HazelcastInstance hazelcastInstance, Log log )
{
Map<MemberId,ReadReplicaInfo> readReplicas = emptyMap();
if ( hazelcastInstance != null )
{
readReplicas = readReplicas( hazelcastInstance );
}
else
{
log.info( "Cannot currently bind to distributed discovery service." );
}
return new ReadReplicaTopology( readReplicas );
}
static CoreTopology getCoreTopology( HazelcastInstance hazelcastInstance, Config config, Log log )
{
Map<MemberId,CoreServerInfo> coreMembers = emptyMap();
boolean canBeBootstrapped = false;
ClusterId clusterId = null;
if ( hazelcastInstance != null )
{
Set<Member> hzMembers = hazelcastInstance.getCluster().getMembers();
canBeBootstrapped = canBeBootstrapped( hazelcastInstance, config );
coreMembers = toCoreMemberMap( hzMembers, log, hazelcastInstance );
clusterId = getClusterId( hazelcastInstance );
}
else
{
log.info( "Cannot currently bind to distributed discovery service." );
}
return new CoreTopology( clusterId, canBeBootstrapped, coreMembers );
}
private static ClusterId getClusterId( HazelcastInstance hazelcastInstance )
{
IAtomicReference<UUID> uuidReference = hazelcastInstance.getAtomicReference( CLUSTER_UUID );
UUID uuid = uuidReference.get();
return uuid != null ? new ClusterId( uuid ) : null;
}
static boolean casClusterId( HazelcastInstance hazelcastInstance, ClusterId clusterId )
{
IAtomicReference<UUID> uuidReference = hazelcastInstance.getAtomicReference( CLUSTER_UUID );
return uuidReference.compareAndSet( null, clusterId.uuid() ) || uuidReference.get().equals( clusterId.uuid() );
}
private static Map<MemberId,ReadReplicaInfo> readReplicas( HazelcastInstance hazelcastInstance )
{
IMap<String/*uuid*/,String/*boltAddress*/> clientAddressMap =
hazelcastInstance.getMap( READ_REPLICA_BOLT_ADDRESS_MAP_NAME );
IMap<String,String> txServerMap = hazelcastInstance.getMap( READ_REPLICA_TRANSACTION_SERVER_ADDRESS_MAP_NAME );
IMap<String,String> memberIdMap = hazelcastInstance.getMap( READ_REPLICA_MEMBER_ID_MAP_NAME );
MultiMap<String,String> serverTags = hazelcastInstance.getMultiMap( SERVER_TAGS_MULTIMAP_NAME );
Map<MemberId,ReadReplicaInfo> result = new HashMap<>();
for ( String hzUUID : clientAddressMap.keySet() )
{
ClientConnectorAddresses clientConnectorAddresses =
ClientConnectorAddresses.fromString( clientAddressMap.get( hzUUID ) );
AdvertisedSocketAddress catchupAddress =
socketAddress( txServerMap.get( hzUUID ), AdvertisedSocketAddress::new );
result.put( new MemberId( UUID.fromString( memberIdMap.get( hzUUID ) ) ),
new ReadReplicaInfo( clientConnectorAddresses, catchupAddress,
asSet( serverTags.get( hzUUID ) ) ) );
}
return result;
}
private static boolean canBeBootstrapped( HazelcastInstance hazelcastInstance, Config config )
{
Set<Member> members = hazelcastInstance.getCluster().getMembers();
Boolean refuseToBeLeader = config.get( CausalClusteringSettings.refuse_to_be_leader );
if ( refuseToBeLeader )
{
return false;
}
else
{
for ( Member member : members )
{
if ( !member.getBooleanAttribute( REFUSE_TO_BE_LEADER_KEY ) )
{
if ( member.localMember() )
{
return true;
}
else
{
return false;
}
}
}
return false;
}
}
static Map<MemberId,CoreServerInfo> toCoreMemberMap( Set<Member> members, Log log,
HazelcastInstance hazelcastInstance )
{
Map<MemberId,CoreServerInfo> coreMembers = new HashMap<>();
MultiMap<String,String> serverTagsMMap = hazelcastInstance.getMultiMap( SERVER_TAGS_MULTIMAP_NAME );
for ( Member member : members )
{
try
{
MemberId memberId = new MemberId( UUID.fromString( member.getStringAttribute( MEMBER_UUID ) ) );
CoreServerInfo coreServerInfo = new CoreServerInfo(
socketAddress( member.getStringAttribute( RAFT_SERVER ), AdvertisedSocketAddress::new ),
socketAddress( member.getStringAttribute( TRANSACTION_SERVER ), AdvertisedSocketAddress::new ),
ClientConnectorAddresses.fromString( member.getStringAttribute( CLIENT_CONNECTOR_ADDRESSES ) ),
asSet( serverTagsMMap.get( memberId.getUuid().toString() ) ) );
coreMembers.put( memberId, coreServerInfo );
}
catch ( IllegalArgumentException e )
{
log.warn( "Incomplete member attributes supplied from Hazelcast", e );
}
}
return coreMembers;
}
static void refreshTags( HazelcastInstance hazelcastInstance, String memberId, List<String> tags )
{
MultiMap<String,String> tagsMap = hazelcastInstance.getMultiMap( SERVER_TAGS_MULTIMAP_NAME );
Collection<String> existing = tagsMap.get( memberId );
Set<String> superfluous = existing.stream().filter( t -> !tags.contains( t ) ).collect( Collectors.toSet() );
Set<String> missing = tags.stream().filter( t -> !existing.contains( t ) ).collect( Collectors.toSet() );
missing.forEach( tag -> tagsMap.put( memberId, tag ) );
superfluous.forEach( tag -> tagsMap.remove( memberId, tag ) );
}
static MemberAttributeConfig buildMemberAttributesForCore( MemberId myself, Config config )
{
MemberAttributeConfig memberAttributeConfig = new MemberAttributeConfig();
memberAttributeConfig.setStringAttribute( MEMBER_UUID, myself.getUuid().toString() );
AdvertisedSocketAddress discoveryAddress = config.get( CausalClusteringSettings.discovery_advertised_address );
memberAttributeConfig.setStringAttribute( DISCOVERY_SERVER, discoveryAddress.toString() );
AdvertisedSocketAddress transactionSource =
config.get( CausalClusteringSettings.transaction_advertised_address );
memberAttributeConfig.setStringAttribute( TRANSACTION_SERVER, transactionSource.toString() );
AdvertisedSocketAddress raftAddress = config.get( CausalClusteringSettings.raft_advertised_address );
memberAttributeConfig.setStringAttribute( RAFT_SERVER, raftAddress.toString() );
ClientConnectorAddresses clientConnectorAddresses = ClientConnectorAddresses.extractFromConfig( config );
memberAttributeConfig.setStringAttribute( CLIENT_CONNECTOR_ADDRESSES, clientConnectorAddresses.toString() );
memberAttributeConfig.setBooleanAttribute( REFUSE_TO_BE_LEADER_KEY,
config.get( CausalClusteringSettings.refuse_to_be_leader ) );
return memberAttributeConfig;
}
}