-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
SharedDiscoveryService.java
179 lines (152 loc) · 6.29 KB
/
SharedDiscoveryService.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-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.causalclustering.discovery;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.CountDownLatch;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.neo4j.causalclustering.core.consensus.LeaderInfo;
import org.neo4j.causalclustering.identity.ClusterId;
import org.neo4j.causalclustering.identity.MemberId;
public final class SharedDiscoveryService
{
private static final int MIN_DISCOVERY_MEMBERS = 2;
private final ConcurrentMap<MemberId,CoreServerInfo> coreMembers;
private final ConcurrentMap<MemberId,ReadReplicaInfo> readReplicas;
private final Set<SharedDiscoveryCoreClient> listeningClients;
private final ConcurrentMap<String,ClusterId> clusterIdDbNames;
private final ConcurrentMap<String,LeaderInfo> leaderMap;
private final CountDownLatch enoughMembers;
SharedDiscoveryService()
{
coreMembers = new ConcurrentHashMap<>();
readReplicas = new ConcurrentHashMap<>();
listeningClients = new ConcurrentSkipListSet<>();
clusterIdDbNames = new ConcurrentHashMap<>();
leaderMap = new ConcurrentHashMap<>();
enoughMembers = new CountDownLatch( MIN_DISCOVERY_MEMBERS );
}
void waitForClusterFormation() throws InterruptedException
{
enoughMembers.await();
}
private boolean canBeBootstrapped( SharedDiscoveryCoreClient client )
{
Stream<SharedDiscoveryCoreClient> clientsWhoCanLeadForMyDb = listeningClients.stream()
.filter( c -> !c.refusesToBeLeader() && c.localDBName().equals( client.localDBName() ) );
Optional<SharedDiscoveryCoreClient> firstAppropriateClient = clientsWhoCanLeadForMyDb.findFirst();
return firstAppropriateClient.map( c -> c.equals( client ) ).orElse( false );
}
CoreTopology getCoreTopology( SharedDiscoveryCoreClient client )
{
//Extract config from client
String dbName = client.localDBName();
boolean canBeBootstrapped = canBeBootstrapped( client );
return getCoreTopology( dbName, canBeBootstrapped );
}
CoreTopology getCoreTopology( String dbName, boolean canBeBootstrapped )
{
return new CoreTopology( clusterIdDbNames.get( dbName ),
canBeBootstrapped, Collections.unmodifiableMap( coreMembers ) );
}
ReadReplicaTopology getReadReplicaTopology()
{
return new ReadReplicaTopology( Collections.unmodifiableMap( readReplicas ) );
}
void registerCoreMember( SharedDiscoveryCoreClient client )
{
CoreServerInfo previousMember = coreMembers.putIfAbsent( client.getMemberId(), client.getCoreServerInfo() );
if ( previousMember == null )
{
listeningClients.add( client );
enoughMembers.countDown();
notifyCoreClients();
}
}
void registerReadReplica( SharedDiscoveryReadReplicaClient client )
{
ReadReplicaInfo previousRR = readReplicas.putIfAbsent( client.getMemberId(), client.getReadReplicainfo() );
if ( previousRR == null )
{
notifyCoreClients();
}
}
void unRegisterCoreMember( SharedDiscoveryCoreClient client )
{
synchronized ( this )
{
listeningClients.remove( client );
coreMembers.remove( client.getMemberId() );
}
notifyCoreClients();
}
void unRegisterReadReplica( SharedDiscoveryReadReplicaClient client )
{
readReplicas.remove( client.getMemberId() );
notifyCoreClients();
}
synchronized void casLeaders( LeaderInfo leaderInfo, String dbName )
{
Optional<LeaderInfo> current = Optional.ofNullable( leaderMap.get( dbName ) );
boolean noUpdate = current.map( LeaderInfo::memberId ).equals( Optional.ofNullable( leaderInfo.memberId() ) );
boolean greaterOrEqualTermExists = current.map(l -> l.term() >= leaderInfo.term() ).orElse( false );
boolean success = !( greaterOrEqualTermExists || noUpdate );
if ( success )
{
leaderMap.put( dbName, leaderInfo );
}
}
boolean casClusterId( ClusterId clusterId, String dbName )
{
ClusterId previousId = clusterIdDbNames.putIfAbsent( dbName, clusterId );
boolean success = previousId == null || previousId.equals( clusterId );
if ( success )
{
notifyCoreClients();
}
return success;
}
Map<MemberId,RoleInfo> getCoreRoles()
{
Set<String> dbNames = clusterIdDbNames.keySet();
Set<MemberId> allLeaders = dbNames.stream()
.map( dbName -> Optional.ofNullable( leaderMap.get( dbName ) ) )
.filter( Optional::isPresent )
.map( Optional::get )
.map( LeaderInfo::memberId )
.collect( Collectors.toSet());
Function<MemberId,RoleInfo> roleMapper = m -> allLeaders.contains( m ) ? RoleInfo.LEADER : RoleInfo.FOLLOWER;
return coreMembers.keySet().stream().collect( Collectors.toMap( Function.identity(), roleMapper ) );
}
private synchronized void notifyCoreClients()
{
listeningClients.forEach( c -> {
c.onCoreTopologyChange( getCoreTopology( c ) );
c.onReadReplicaTopologyChange( getReadReplicaTopology() );
} );
}
}