Search in sources :

Example 1 with Endpoint

use of org.neo4j.causalclustering.load_balancing.Endpoint in project neo4j by neo4j.

the class ResultFormatV1Test method shouldSerializeToAndFromRecordFormat.

@Test
public void shouldSerializeToAndFromRecordFormat() throws Exception {
    // given
    List<Endpoint> writers = asList(Endpoint.write(new AdvertisedSocketAddress("write", 1)), Endpoint.write(new AdvertisedSocketAddress("write", 2)), Endpoint.write(new AdvertisedSocketAddress("write", 3)));
    List<Endpoint> readers = asList(Endpoint.read(new AdvertisedSocketAddress("read", 4)), Endpoint.read(new AdvertisedSocketAddress("read", 5)), Endpoint.read(new AdvertisedSocketAddress("read", 6)), Endpoint.read(new AdvertisedSocketAddress("read", 7)));
    List<Endpoint> routers = singletonList(Endpoint.route(new AdvertisedSocketAddress("route", 8)));
    long ttlSeconds = 5;
    LoadBalancingResult original = new LoadBalancingResult(routers, writers, readers, ttlSeconds * 1000);
    // when
    Object[] record = ResultFormatV1.build(original);
    // then
    LoadBalancingResult parsed = ResultFormatV1.parse(record);
    assertEquals(original, parsed);
}
Also used : LoadBalancingResult(org.neo4j.causalclustering.load_balancing.LoadBalancingResult) Endpoint(org.neo4j.causalclustering.load_balancing.Endpoint) AdvertisedSocketAddress(org.neo4j.helpers.AdvertisedSocketAddress) Test(org.junit.Test)

Example 2 with Endpoint

use of org.neo4j.causalclustering.load_balancing.Endpoint in project neo4j by neo4j.

the class ServerPoliciesPlugin method writeEndpoints.

private List<Endpoint> writeEndpoints(CoreTopology cores) {
    MemberId leader;
    try {
        leader = leaderLocator.getLeader();
    } catch (NoLeaderFoundException e) {
        return emptyList();
    }
    Optional<Endpoint> endPoint = cores.find(leader).map(extractBoltAddress()).map(Endpoint::write);
    return asList(endPoint);
}
Also used : MemberId(org.neo4j.causalclustering.identity.MemberId) NoLeaderFoundException(org.neo4j.causalclustering.core.consensus.NoLeaderFoundException) Endpoint(org.neo4j.causalclustering.load_balancing.Endpoint)

Example 3 with Endpoint

use of org.neo4j.causalclustering.load_balancing.Endpoint 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());
}
Also used : ReadReplicaTopology(org.neo4j.causalclustering.discovery.ReadReplicaTopology) Service(org.neo4j.helpers.Service) LoadBalancingResult(org.neo4j.causalclustering.load_balancing.LoadBalancingResult) Log(org.neo4j.logging.Log) CausalClusteringSettings(org.neo4j.causalclustering.core.CausalClusteringSettings) Util.asList(org.neo4j.causalclustering.load_balancing.Util.asList) LogProvider(org.neo4j.logging.LogProvider) FilteringPolicyLoader.load(org.neo4j.causalclustering.load_balancing.plugins.server_policies.FilteringPolicyLoader.load) Map(java.util.Map) MemberId(org.neo4j.causalclustering.identity.MemberId) Endpoint(org.neo4j.causalclustering.load_balancing.Endpoint) CoreServerInfo(org.neo4j.causalclustering.discovery.CoreServerInfo) LeaderLocator(org.neo4j.causalclustering.core.consensus.LeaderLocator) InvalidSettingException(org.neo4j.graphdb.config.InvalidSettingException) CoreTopology(org.neo4j.causalclustering.discovery.CoreTopology) LoadBalancingPlugin(org.neo4j.causalclustering.load_balancing.LoadBalancingPlugin) Config(org.neo4j.kernel.configuration.Config) Collections.emptyList(java.util.Collections.emptyList) TopologyService(org.neo4j.causalclustering.discovery.TopologyService) Set(java.util.Set) Collectors(java.util.stream.Collectors) List(java.util.List) NoLeaderFoundException(org.neo4j.causalclustering.core.consensus.NoLeaderFoundException) Optional(java.util.Optional) Util.extractBoltAddress(org.neo4j.causalclustering.load_balancing.Util.extractBoltAddress) MemberId(org.neo4j.causalclustering.identity.MemberId) NoLeaderFoundException(org.neo4j.causalclustering.core.consensus.NoLeaderFoundException) CoreServerInfo(org.neo4j.causalclustering.discovery.CoreServerInfo) CoreServerInfo(org.neo4j.causalclustering.discovery.CoreServerInfo)

Example 4 with Endpoint

use of org.neo4j.causalclustering.load_balancing.Endpoint in project neo4j by neo4j.

the class ResultFormatV1 method parse.

private static Map<Role, List<Endpoint>> parse(List<Map<String, Object>> result) {
    Map<Role, List<Endpoint>> endpoints = new HashMap<>();
    for (Map<String, Object> single : result) {
        Role role = Role.valueOf((String) single.get("role"));
        List<Endpoint> addresses = parse((Object[]) single.get("addresses"), role);
        endpoints.put(role, addresses);
    }
    Arrays.stream(Role.values()).forEach(r -> endpoints.putIfAbsent(r, Collections.emptyList()));
    return endpoints;
}
Also used : Role(org.neo4j.causalclustering.load_balancing.Role) Endpoint(org.neo4j.causalclustering.load_balancing.Endpoint) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList)

Example 5 with Endpoint

use of org.neo4j.causalclustering.load_balancing.Endpoint in project neo4j by neo4j.

the class ServerShufflingProcessorTest method shouldShuffleServers.

@Test
public void shouldShuffleServers() throws Exception {
    // given
    LoadBalancingProcessor delegate = mock(LoadBalancingPlugin.class);
    List<Endpoint> routers = asList(Endpoint.route(new AdvertisedSocketAddress("route", 1)), Endpoint.route(new AdvertisedSocketAddress("route", 2)));
    List<Endpoint> writers = asList(Endpoint.write(new AdvertisedSocketAddress("write", 3)), Endpoint.write(new AdvertisedSocketAddress("write", 4)), Endpoint.write(new AdvertisedSocketAddress("write", 5)));
    List<Endpoint> readers = asList(Endpoint.read(new AdvertisedSocketAddress("read", 6)), Endpoint.read(new AdvertisedSocketAddress("read", 7)), Endpoint.read(new AdvertisedSocketAddress("read", 8)), Endpoint.read(new AdvertisedSocketAddress("read", 9)));
    long ttl = 1000;
    LoadBalancingProcessor.Result result = new LoadBalancingResult(new ArrayList<>(routers), new ArrayList<>(writers), new ArrayList<>(readers), ttl);
    when(delegate.run(any())).thenReturn(result);
    ServerShufflingProcessor plugin = new ServerShufflingProcessor(delegate);
    boolean completeShuffle = false;
    for (// we try many times to make false negatives extremely unlikely
    int i = 0; // we try many times to make false negatives extremely unlikely
    i < 1000; // we try many times to make false negatives extremely unlikely
    i++) {
        // when
        LoadBalancingProcessor.Result shuffledResult = plugin.run(Collections.emptyMap());
        // then: should still contain the same endpoints
        assertThat(shuffledResult.routeEndpoints(), containsInAnyOrder(routers.toArray()));
        assertThat(shuffledResult.writeEndpoints(), containsInAnyOrder(writers.toArray()));
        assertThat(shuffledResult.readEndpoints(), containsInAnyOrder(readers.toArray()));
        assertEquals(shuffledResult.getTimeToLiveMillis(), ttl);
        // but possibly in a different order
        boolean readersEqual = shuffledResult.readEndpoints().equals(readers);
        boolean writersEqual = shuffledResult.writeEndpoints().equals(writers);
        boolean routersEqual = shuffledResult.routeEndpoints().equals(routers);
        if (!readersEqual && !writersEqual && !routersEqual) {
            // we don't stop until it is completely different
            completeShuffle = true;
            break;
        }
    }
    assertTrue(completeShuffle);
}
Also used : LoadBalancingResult(org.neo4j.causalclustering.load_balancing.LoadBalancingResult) Endpoint(org.neo4j.causalclustering.load_balancing.Endpoint) AdvertisedSocketAddress(org.neo4j.helpers.AdvertisedSocketAddress) LoadBalancingProcessor(org.neo4j.causalclustering.load_balancing.LoadBalancingProcessor) Endpoint(org.neo4j.causalclustering.load_balancing.Endpoint) Test(org.junit.Test)

Aggregations

Endpoint (org.neo4j.causalclustering.load_balancing.Endpoint)6 LoadBalancingResult (org.neo4j.causalclustering.load_balancing.LoadBalancingResult)4 Test (org.junit.Test)3 List (java.util.List)2 NoLeaderFoundException (org.neo4j.causalclustering.core.consensus.NoLeaderFoundException)2 MemberId (org.neo4j.causalclustering.identity.MemberId)2 AdvertisedSocketAddress (org.neo4j.helpers.AdvertisedSocketAddress)2 ArrayList (java.util.ArrayList)1 Collections.emptyList (java.util.Collections.emptyList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Collectors.toList (java.util.stream.Collectors.toList)1 CausalClusteringSettings (org.neo4j.causalclustering.core.CausalClusteringSettings)1 LeaderLocator (org.neo4j.causalclustering.core.consensus.LeaderLocator)1 CoreServerInfo (org.neo4j.causalclustering.discovery.CoreServerInfo)1 CoreTopology (org.neo4j.causalclustering.discovery.CoreTopology)1 ReadReplicaTopology (org.neo4j.causalclustering.discovery.ReadReplicaTopology)1