use of org.neo4j.causalclustering.discovery.ReadReplicaTopology in project neo4j by neo4j.
the class ServerPoliciesPlugin method readEndpoints.
private List<Endpoint> readEndpoints(CoreTopology coreTopology, ReadReplicaTopology rrTopology, Policy policy) {
Set<ServerInfo> possibleReaders = rrTopology.members().entrySet().stream().map(entry -> new ServerInfo(entry.getValue().connectors().boltAddress(), entry.getKey(), entry.getValue().groups())).collect(Collectors.toSet());
if (allowReadsOnFollowers || possibleReaders.size() == 0) {
Set<MemberId> validCores = coreTopology.members().keySet();
try {
MemberId leader = leaderLocator.getLeader();
validCores = validCores.stream().filter(memberId -> !memberId.equals(leader)).collect(Collectors.toSet());
} catch (NoLeaderFoundException ignored) {
// we might end up using the leader for reading during this ttl, should be fine in general
}
for (MemberId validCore : validCores) {
Optional<CoreServerInfo> coreServerInfo = coreTopology.find(validCore);
if (coreServerInfo.isPresent()) {
CoreServerInfo serverInfo = coreServerInfo.get();
possibleReaders.add(new ServerInfo(serverInfo.connectors().boltAddress(), validCore, serverInfo.groups()));
}
}
}
Set<ServerInfo> readers = policy.apply(possibleReaders);
return readers.stream().map(r -> Endpoint.read(r.boltAddress())).collect(Collectors.toList());
}
use of org.neo4j.causalclustering.discovery.ReadReplicaTopology in project neo4j by neo4j.
the class ServerPoliciesPlugin method run.
@Override
public Result run(Map<String, String> context) {
CoreTopology coreTopology = topologyService.coreServers();
ReadReplicaTopology rrTopology = topologyService.readReplicas();
return new LoadBalancingResult(routeEndpoints(coreTopology), writeEndpoints(coreTopology), readEndpoints(coreTopology, rrTopology, policies.selectFor(context)), timeToLive);
}
use of org.neo4j.causalclustering.discovery.ReadReplicaTopology in project neo4j by neo4j.
the class UserDefinedConfigurationStrategyTest method fakeReadReplicaTopology.
static ReadReplicaTopology fakeReadReplicaTopology(MemberId... readReplicaIds) {
assert readReplicaIds.length > 0;
Map<MemberId, ReadReplicaInfo> readReplicas = new HashMap<>();
int offset = 0;
for (MemberId memberId : readReplicaIds) {
readReplicas.put(memberId, new ReadReplicaInfo(new ClientConnectorAddresses(singletonList(new ClientConnectorAddresses.ConnectorUri(ClientConnectorAddresses.Scheme.bolt, new AdvertisedSocketAddress("localhost", 11000 + offset)))), new AdvertisedSocketAddress("localhost", 10000 + offset)));
offset++;
}
return new ReadReplicaTopology(readReplicas);
}
use of org.neo4j.causalclustering.discovery.ReadReplicaTopology in project neo4j by neo4j.
the class ClusterOverviewProcedureTest method shouldProvideOverviewOfCoreServersAndReadReplicas.
@Test
public void shouldProvideOverviewOfCoreServersAndReadReplicas() throws Exception {
// given
final CoreTopologyService topologyService = mock(CoreTopologyService.class);
Map<MemberId, CoreServerInfo> coreMembers = new HashMap<>();
MemberId theLeader = new MemberId(UUID.randomUUID());
MemberId follower1 = new MemberId(UUID.randomUUID());
MemberId follower2 = new MemberId(UUID.randomUUID());
coreMembers.put(theLeader, adressesForCore(0));
coreMembers.put(follower1, adressesForCore(1));
coreMembers.put(follower2, adressesForCore(2));
Map<MemberId, ReadReplicaInfo> replicaMembers = new HashMap<>();
MemberId replica4 = new MemberId(UUID.randomUUID());
MemberId replica5 = new MemberId(UUID.randomUUID());
replicaMembers.put(replica4, addressesForReadReplica(4));
replicaMembers.put(replica5, addressesForReadReplica(5));
when(topologyService.coreServers()).thenReturn(new CoreTopology(null, false, coreMembers));
when(topologyService.readReplicas()).thenReturn(new ReadReplicaTopology(replicaMembers));
LeaderLocator leaderLocator = mock(LeaderLocator.class);
when(leaderLocator.getLeader()).thenReturn(theLeader);
ClusterOverviewProcedure procedure = new ClusterOverviewProcedure(topologyService, leaderLocator, NullLogProvider.getInstance());
// when
final RawIterator<Object[], ProcedureException> members = procedure.apply(null, new Object[0]);
assertThat(members.next(), new IsRecord(theLeader.getUuid(), 5000, Role.LEADER, asSet("core", "core0")));
assertThat(members.next(), new IsRecord(follower1.getUuid(), 5001, Role.FOLLOWER, asSet("core", "core1")));
assertThat(members.next(), new IsRecord(follower2.getUuid(), 5002, Role.FOLLOWER, asSet("core", "core2")));
assertThat(members.next(), new IsRecord(replica4.getUuid(), 6004, Role.READ_REPLICA, asSet("replica", "replica4")));
assertThat(members.next(), new IsRecord(replica5.getUuid(), 6005, Role.READ_REPLICA, asSet("replica", "replica5")));
assertFalse(members.hasNext());
}
use of org.neo4j.causalclustering.discovery.ReadReplicaTopology in project neo4j by neo4j.
the class GetServersProcedureV1Test method shouldProvideReaderAndRouterForSingleCoreSetup.
@Test
public void shouldProvideReaderAndRouterForSingleCoreSetup() throws Exception {
// given
final CoreTopologyService coreTopologyService = mock(CoreTopologyService.class);
LeaderLocator leaderLocator = mock(LeaderLocator.class);
Map<MemberId, CoreServerInfo> coreMembers = new HashMap<>();
coreMembers.put(member(0), adressesForCore(0));
final CoreTopology clusterTopology = new CoreTopology(clusterId, false, coreMembers);
when(coreTopologyService.coreServers()).thenReturn(clusterTopology);
when(coreTopologyService.readReplicas()).thenReturn(new ReadReplicaTopology(emptyMap()));
final LegacyGetServersProcedure proc = new LegacyGetServersProcedure(coreTopologyService, leaderLocator, config, getInstance());
// when
ClusterView clusterView = run(proc);
// then
ClusterView.Builder builder = new ClusterView.Builder();
builder.readAddress(adressesForCore(0).connectors().boltAddress());
builder.routeAddress(adressesForCore(0).connectors().boltAddress());
assertEquals(builder.build(), clusterView);
}
Aggregations