-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
LegacyGetServersProcedure.java
163 lines (145 loc) · 6.83 KB
/
LegacyGetServersProcedure.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
/*
* 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.load_balancing.procedure;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
import org.neo4j.causalclustering.core.CausalClusteringSettings;
import org.neo4j.causalclustering.core.consensus.LeaderLocator;
import org.neo4j.causalclustering.core.consensus.NoLeaderFoundException;
import org.neo4j.causalclustering.discovery.CoreServerInfo;
import org.neo4j.causalclustering.discovery.TopologyService;
import org.neo4j.causalclustering.identity.MemberId;
import org.neo4j.causalclustering.load_balancing.Endpoint;
import org.neo4j.causalclustering.load_balancing.LoadBalancingResult;
import org.neo4j.collection.RawIterator;
import org.neo4j.helpers.AdvertisedSocketAddress;
import org.neo4j.kernel.api.exceptions.ProcedureException;
import org.neo4j.kernel.api.proc.CallableProcedure;
import org.neo4j.kernel.api.proc.Context;
import org.neo4j.kernel.api.proc.Neo4jTypes;
import org.neo4j.kernel.api.proc.ProcedureSignature;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.logging.Log;
import org.neo4j.logging.LogProvider;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Stream.concat;
import static org.neo4j.causalclustering.core.CausalClusteringSettings.cluster_allow_reads_on_followers;
import static org.neo4j.causalclustering.load_balancing.Util.asList;
import static org.neo4j.causalclustering.load_balancing.Util.extractBoltAddress;
import static org.neo4j.causalclustering.load_balancing.procedure.ParameterNames.SERVERS;
import static org.neo4j.causalclustering.load_balancing.procedure.ParameterNames.TTL;
import static org.neo4j.causalclustering.load_balancing.procedure.ProcedureNames.GET_SERVERS_V1;
/**
* Returns endpoints and their capabilities.
*/
@Deprecated
public class LegacyGetServersProcedure implements CallableProcedure
{
private final String DESCRIPTION = "Returns cluster endpoints and their capabilities.";
private final ProcedureSignature procedureSignature =
ProcedureSignature.procedureSignature( GET_SERVERS_V1.fullyQualifiedProcedureName() )
.out( TTL.parameterName(), Neo4jTypes.NTInteger )
.out( SERVERS.parameterName(), Neo4jTypes.NTMap )
.description( DESCRIPTION )
.build();
private final TopologyService topologyService;
private final LeaderLocator leaderLocator;
private final Config config;
private final Log log;
public LegacyGetServersProcedure( TopologyService topologyService, LeaderLocator leaderLocator,
Config config, LogProvider logProvider )
{
this.topologyService = topologyService;
this.leaderLocator = leaderLocator;
this.config = config;
this.log = logProvider.getLog( getClass() );
}
@Override
public ProcedureSignature signature()
{
return procedureSignature;
}
@Override
public RawIterator<Object[],ProcedureException> apply( Context ctx, Object[] input ) throws ProcedureException
{
List<Endpoint> routeEndpoints = routeEndpoints();
List<Endpoint> writeEndpoints = writeEndpoints();
List<Endpoint> readEndpoints = readEndpoints();
return RawIterator.<Object[],ProcedureException>of( ResultFormatV1.build(
new LoadBalancingResult( routeEndpoints, writeEndpoints, readEndpoints,
config.get( CausalClusteringSettings.cluster_routing_ttl ) ) ) );
}
private Optional<AdvertisedSocketAddress> leaderBoltAddress()
{
MemberId leader;
try
{
leader = leaderLocator.getLeader();
}
catch ( NoLeaderFoundException e )
{
log.debug( "No leader server found. This can happen during a leader switch. No write end points available" );
return Optional.empty();
}
return topologyService.coreServers().find( leader ).map( extractBoltAddress() );
}
private List<Endpoint> routeEndpoints()
{
Stream<AdvertisedSocketAddress> routers = topologyService.coreServers()
.members().values().stream().map( extractBoltAddress() );
List<Endpoint> routeEndpoints = routers.map( Endpoint::route ).collect( toList() );
Collections.shuffle( routeEndpoints );
return routeEndpoints;
}
private List<Endpoint> writeEndpoints()
{
return asList( leaderBoltAddress().map( Endpoint::write ) );
}
private List<Endpoint> readEndpoints()
{
List<AdvertisedSocketAddress> readReplicas = topologyService.readReplicas().allMemberInfo().stream()
.map( extractBoltAddress() ).collect( toList() );
boolean addFollowers = readReplicas.isEmpty() || config.get( cluster_allow_reads_on_followers );
Stream<AdvertisedSocketAddress> readCore = addFollowers ? coreReadEndPoints() : Stream.empty();
List<Endpoint> readEndPoints =
concat( readReplicas.stream(), readCore ).map( Endpoint::read ).collect( toList() );
Collections.shuffle( readEndPoints );
return readEndPoints;
}
private Stream<AdvertisedSocketAddress> coreReadEndPoints()
{
Optional<AdvertisedSocketAddress> leader = leaderBoltAddress();
Collection<CoreServerInfo> coreServerInfo = topologyService.coreServers().members().values();
Stream<AdvertisedSocketAddress> boltAddresses = topologyService.coreServers()
.members().values().stream().map( extractBoltAddress() );
// if the leader is present and it is not alone filter it out from the read end points
if ( leader.isPresent() && coreServerInfo.size() > 1 )
{
AdvertisedSocketAddress advertisedSocketAddress = leader.get();
return boltAddresses.filter( address -> !advertisedSocketAddress.equals( address ) );
}
// if there is only the leader return it as read end point
// or if we cannot locate the leader return all cores as read end points
return boltAddresses;
}
}