Search in sources :

Example 1 with CoreServerInfo

use of org.neo4j.causalclustering.discovery.CoreServerInfo in project neo4j by neo4j.

the class GetServersProcedureV1RoutingTest method shouldReturnEndpointsInDifferentOrders.

@Test
public void shouldReturnEndpointsInDifferentOrders() throws Exception {
    // given
    final CoreTopologyService coreTopologyService = mock(CoreTopologyService.class);
    LeaderLocator leaderLocator = mock(LeaderLocator.class);
    when(leaderLocator.getLeader()).thenReturn(member(0));
    Map<MemberId, CoreServerInfo> coreMembers = new HashMap<>();
    coreMembers.put(member(0), adressesForCore(0));
    coreMembers.put(member(1), adressesForCore(1));
    coreMembers.put(member(2), adressesForCore(2));
    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
    Object[] endpoints = getEndpoints(proc);
    //then
    Object[] endpointsInDifferentOrder = getEndpoints(proc);
    for (int i = 0; i < 100; i++) {
        if (Arrays.deepEquals(endpointsInDifferentOrder, endpoints)) {
            endpointsInDifferentOrder = getEndpoints(proc);
        } else {
            //Different order of servers, no need to retry.
            break;
        }
    }
    assertFalse(Arrays.deepEquals(endpoints, endpointsInDifferentOrder));
}
Also used : MemberId(org.neo4j.causalclustering.identity.MemberId) LeaderLocator(org.neo4j.causalclustering.core.consensus.LeaderLocator) CoreTopologyService(org.neo4j.causalclustering.discovery.CoreTopologyService) HashMap(java.util.HashMap) CoreServerInfo(org.neo4j.causalclustering.discovery.CoreServerInfo) ReadReplicaTopology(org.neo4j.causalclustering.discovery.ReadReplicaTopology) CoreTopology(org.neo4j.causalclustering.discovery.CoreTopology) Test(org.junit.Test)

Example 2 with CoreServerInfo

use of org.neo4j.causalclustering.discovery.CoreServerInfo in project neo4j by neo4j.

the class GetServersProcedureV1Test method shouldReturnNoWriteEndpointsIfThereIsNoAddressForTheLeader.

@Test
public void shouldReturnNoWriteEndpointsIfThereIsNoAddressForTheLeader() throws Exception {
    // given
    final CoreTopologyService topologyService = mock(CoreTopologyService.class);
    Map<MemberId, CoreServerInfo> coreMembers = new HashMap<>();
    coreMembers.put(member(0), adressesForCore(0));
    when(topologyService.coreServers()).thenReturn(new CoreTopology(clusterId, false, coreMembers));
    when(topologyService.readReplicas()).thenReturn(new ReadReplicaTopology(emptyMap()));
    LeaderLocator leaderLocator = mock(LeaderLocator.class);
    when(leaderLocator.getLeader()).thenReturn(member(1));
    LegacyGetServersProcedure procedure = new LegacyGetServersProcedure(topologyService, leaderLocator, config, getInstance());
    // when
    ClusterView clusterView = run(procedure);
    // then
    ClusterView.Builder builder = new ClusterView.Builder();
    builder.readAddress(adressesForCore(0).connectors().boltAddress());
    builder.routeAddress(adressesForCore(0).connectors().boltAddress());
    assertEquals(builder.build(), clusterView);
}
Also used : MemberId(org.neo4j.causalclustering.identity.MemberId) LeaderLocator(org.neo4j.causalclustering.core.consensus.LeaderLocator) CoreTopologyService(org.neo4j.causalclustering.discovery.CoreTopologyService) HashMap(java.util.HashMap) CoreServerInfo(org.neo4j.causalclustering.discovery.CoreServerInfo) ReadReplicaTopology(org.neo4j.causalclustering.discovery.ReadReplicaTopology) CoreTopology(org.neo4j.causalclustering.discovery.CoreTopology) Test(org.junit.Test)

Example 3 with CoreServerInfo

use of org.neo4j.causalclustering.discovery.CoreServerInfo in project neo4j by neo4j.

the class GetServersProcedureV1Test method shouldReturnNoWriteEndpointsIfThereIsNoLeader.

@Test
public void shouldReturnNoWriteEndpointsIfThereIsNoLeader() throws Exception {
    // given
    final CoreTopologyService topologyService = mock(CoreTopologyService.class);
    Map<MemberId, CoreServerInfo> coreMembers = new HashMap<>();
    coreMembers.put(member(0), adressesForCore(0));
    when(topologyService.coreServers()).thenReturn(new CoreTopology(clusterId, false, coreMembers));
    when(topologyService.readReplicas()).thenReturn(new ReadReplicaTopology(emptyMap()));
    LeaderLocator leaderLocator = mock(LeaderLocator.class);
    when(leaderLocator.getLeader()).thenThrow(new NoLeaderFoundException());
    LegacyGetServersProcedure procedure = new LegacyGetServersProcedure(topologyService, leaderLocator, config, getInstance());
    // when
    ClusterView clusterView = run(procedure);
    // then
    ClusterView.Builder builder = new ClusterView.Builder();
    builder.readAddress(adressesForCore(0).connectors().boltAddress());
    builder.routeAddress(adressesForCore(0).connectors().boltAddress());
    assertEquals(builder.build(), clusterView);
}
Also used : NoLeaderFoundException(org.neo4j.causalclustering.core.consensus.NoLeaderFoundException) HashMap(java.util.HashMap) CoreTopology(org.neo4j.causalclustering.discovery.CoreTopology) MemberId(org.neo4j.causalclustering.identity.MemberId) LeaderLocator(org.neo4j.causalclustering.core.consensus.LeaderLocator) CoreTopologyService(org.neo4j.causalclustering.discovery.CoreTopologyService) CoreServerInfo(org.neo4j.causalclustering.discovery.CoreServerInfo) ReadReplicaTopology(org.neo4j.causalclustering.discovery.ReadReplicaTopology) Test(org.junit.Test)

Example 4 with CoreServerInfo

use of org.neo4j.causalclustering.discovery.CoreServerInfo in project neo4j by neo4j.

the class ConnectToRandomCoreServerStrategyTest method fakeCoreTopology.

static CoreTopology fakeCoreTopology(MemberId... memberIds) {
    assert memberIds.length > 0;
    ClusterId clusterId = new ClusterId(UUID.randomUUID());
    Map<MemberId, CoreServerInfo> coreMembers = new HashMap<>();
    int offset = 0;
    for (MemberId memberId : memberIds) {
        coreMembers.put(memberId, new CoreServerInfo(new AdvertisedSocketAddress("localhost", 5000 + offset), new AdvertisedSocketAddress("localhost", 6000 + offset), new ClientConnectorAddresses(singletonList(new ClientConnectorAddresses.ConnectorUri(ClientConnectorAddresses.Scheme.bolt, new AdvertisedSocketAddress("localhost", 7000 + offset)))), asSet("core")));
        offset++;
    }
    return new CoreTopology(clusterId, false, coreMembers);
}
Also used : MemberId(org.neo4j.causalclustering.identity.MemberId) ClusterId(org.neo4j.causalclustering.identity.ClusterId) HashMap(java.util.HashMap) CoreServerInfo(org.neo4j.causalclustering.discovery.CoreServerInfo) AdvertisedSocketAddress(org.neo4j.helpers.AdvertisedSocketAddress) ClientConnectorAddresses(org.neo4j.causalclustering.discovery.ClientConnectorAddresses) CoreTopology(org.neo4j.causalclustering.discovery.CoreTopology)

Example 5 with CoreServerInfo

use of org.neo4j.causalclustering.discovery.CoreServerInfo 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)

Aggregations

CoreServerInfo (org.neo4j.causalclustering.discovery.CoreServerInfo)16 CoreTopology (org.neo4j.causalclustering.discovery.CoreTopology)13 MemberId (org.neo4j.causalclustering.identity.MemberId)13 LeaderLocator (org.neo4j.causalclustering.core.consensus.LeaderLocator)11 HashMap (java.util.HashMap)10 ReadReplicaTopology (org.neo4j.causalclustering.discovery.ReadReplicaTopology)10 Test (org.junit.Test)9 CoreTopologyService (org.neo4j.causalclustering.discovery.CoreTopologyService)9 Optional (java.util.Optional)3 Set (java.util.Set)3 NoLeaderFoundException (org.neo4j.causalclustering.core.consensus.NoLeaderFoundException)3 AdvertisedSocketAddress (org.neo4j.helpers.AdvertisedSocketAddress)3 List (java.util.List)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 CausalClusteringSettings (org.neo4j.causalclustering.core.CausalClusteringSettings)2 ClientConnectorAddresses (org.neo4j.causalclustering.discovery.ClientConnectorAddresses)2 ReadReplicaInfo (org.neo4j.causalclustering.discovery.ReadReplicaInfo)2 TopologyService (org.neo4j.causalclustering.discovery.TopologyService)2 ClusterId (org.neo4j.causalclustering.identity.ClusterId)2