Search in sources :

Example 6 with CoreTopology

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

the class ClusterBinderTest method shouldTimeoutWhenNotBootrappableAndNobodyElsePublishesClusterId.

@Test
public void shouldTimeoutWhenNotBootrappableAndNobodyElsePublishesClusterId() throws Throwable {
    // given
    CoreTopology unboundTopology = new CoreTopology(null, false, emptyMap());
    CoreTopologyService topologyService = mock(CoreTopologyService.class);
    when(topologyService.coreServers()).thenReturn(unboundTopology);
    ClusterBinder binder = new ClusterBinder(new StubClusterIdStorage(), topologyService, NullLogProvider.getInstance(), clock, () -> clock.forward(1, TimeUnit.SECONDS), 3_000, coreBootstrapper);
    try {
        // when
        binder.bindToCluster(null);
        fail("Should have timed out");
    } catch (TimeoutException e) {
    // expected
    }
    // then
    verify(topologyService, atLeast(2)).coreServers();
}
Also used : CoreTopologyService(org.neo4j.causalclustering.discovery.CoreTopologyService) CoreTopology(org.neo4j.causalclustering.discovery.CoreTopology) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 7 with CoreTopology

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

the class ClusterBinderTest method shouldBootstrapWhenBootstrappable.

@Test
public void shouldBootstrapWhenBootstrappable() throws Throwable {
    // given
    CoreTopology bootstrappableTopology = new CoreTopology(null, true, emptyMap());
    CoreTopologyService topologyService = mock(CoreTopologyService.class);
    when(topologyService.coreServers()).thenReturn(bootstrappableTopology);
    when(topologyService.setClusterId(any())).thenReturn(true);
    ClusterBinder binder = new ClusterBinder(new StubClusterIdStorage(), topologyService, NullLogProvider.getInstance(), clock, () -> clock.forward(1, TimeUnit.SECONDS), 3_000, coreBootstrapper);
    ThrowingConsumer<CoreSnapshot, Throwable> snapshotInstaller = mock(ThrowingConsumer.class);
    // when
    binder.bindToCluster(snapshotInstaller);
    // then
    verify(coreBootstrapper).bootstrap(any());
    Optional<ClusterId> clusterId = binder.get();
    assertTrue(clusterId.isPresent());
    verify(topologyService).setClusterId(clusterId.get());
    verify(snapshotInstaller).accept(any());
}
Also used : CoreSnapshot(org.neo4j.causalclustering.core.state.snapshot.CoreSnapshot) CoreTopologyService(org.neo4j.causalclustering.discovery.CoreTopologyService) CoreTopology(org.neo4j.causalclustering.discovery.CoreTopology) Test(org.junit.Test)

Example 8 with CoreTopology

use of org.neo4j.causalclustering.discovery.CoreTopology 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 9 with CoreTopology

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

the class UpstreamDatabaseStrategySelectorTest method shouldDefaultToRandomCoreServerIfNoOtherStrategySpecified.

@Test
public void shouldDefaultToRandomCoreServerIfNoOtherStrategySpecified() throws Exception {
    // given
    TopologyService topologyService = mock(TopologyService.class);
    MemberId memberId = new MemberId(UUID.randomUUID());
    when(topologyService.coreServers()).thenReturn(new CoreTopology(new ClusterId(UUID.randomUUID()), false, mapOf(memberId, mock(CoreServerInfo.class))));
    ConnectToRandomCoreServerStrategy defaultStrategy = new ConnectToRandomCoreServerStrategy();
    defaultStrategy.setTopologyService(topologyService);
    UpstreamDatabaseStrategySelector selector = new UpstreamDatabaseStrategySelector(defaultStrategy);
    // when
    MemberId instance = selector.bestUpstreamDatabase();
    // then
    assertEquals(memberId, instance);
}
Also used : MemberId(org.neo4j.causalclustering.identity.MemberId) ClusterId(org.neo4j.causalclustering.identity.ClusterId) CoreServerInfo(org.neo4j.causalclustering.discovery.CoreServerInfo) CoreTopology(org.neo4j.causalclustering.discovery.CoreTopology) TopologyService(org.neo4j.causalclustering.discovery.TopologyService) Test(org.junit.Test)

Example 10 with CoreTopology

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

CoreTopology (org.neo4j.causalclustering.discovery.CoreTopology)22 MemberId (org.neo4j.causalclustering.identity.MemberId)16 Test (org.junit.Test)15 CoreServerInfo (org.neo4j.causalclustering.discovery.CoreServerInfo)15 CoreTopologyService (org.neo4j.causalclustering.discovery.CoreTopologyService)13 LeaderLocator (org.neo4j.causalclustering.core.consensus.LeaderLocator)12 ReadReplicaTopology (org.neo4j.causalclustering.discovery.ReadReplicaTopology)12 HashMap (java.util.HashMap)10 TopologyService (org.neo4j.causalclustering.discovery.TopologyService)4 Optional (java.util.Optional)3 Set (java.util.Set)3 NoLeaderFoundException (org.neo4j.causalclustering.core.consensus.NoLeaderFoundException)3 ClusterId (org.neo4j.causalclustering.identity.ClusterId)3 List (java.util.List)2 Map (java.util.Map)2 TimeoutException (java.util.concurrent.TimeoutException)2 Collectors (java.util.stream.Collectors)2 CausalClusteringSettings (org.neo4j.causalclustering.core.CausalClusteringSettings)2 CoreSnapshot (org.neo4j.causalclustering.core.state.snapshot.CoreSnapshot)2 ClientConnectorAddresses (org.neo4j.causalclustering.discovery.ClientConnectorAddresses)2