Search in sources :

Example 11 with CoreTopology

use of org.neo4j.causalclustering.discovery.CoreTopology 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);
}
Also used : LoadBalancingResult(org.neo4j.causalclustering.load_balancing.LoadBalancingResult) ReadReplicaTopology(org.neo4j.causalclustering.discovery.ReadReplicaTopology) CoreTopology(org.neo4j.causalclustering.discovery.CoreTopology)

Example 12 with CoreTopology

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

the class ConnectToRandomCoreServerStrategy method upstreamDatabase.

@Override
public Optional<MemberId> upstreamDatabase() throws UpstreamDatabaseSelectionException {
    final CoreTopology coreTopology = topologyService.coreServers();
    if (coreTopology.members().size() == 0) {
        throw new UpstreamDatabaseSelectionException("No core servers available");
    }
    int skippedServers = random.nextInt(coreTopology.members().size());
    final Iterator<MemberId> iterator = coreTopology.members().keySet().iterator();
    MemberId member;
    do {
        member = iterator.next();
    } while (skippedServers-- > 0);
    return Optional.ofNullable(member);
}
Also used : MemberId(org.neo4j.causalclustering.identity.MemberId) CoreTopology(org.neo4j.causalclustering.discovery.CoreTopology)

Example 13 with CoreTopology

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

the class UserDefinedConfigurationStrategy method upstreamDatabase.

@Override
public Optional<MemberId> upstreamDatabase() throws UpstreamDatabaseSelectionException {
    try {
        Filter<ServerInfo> filters = FilterConfigParser.parse(config.get(CausalClusteringSettings.user_defined_upstream_selection_strategy));
        Set<ServerInfo> possibleReaders = topologyService.readReplicas().members().entrySet().stream().map(entry -> new ServerInfo(entry.getValue().connectors().boltAddress(), entry.getKey(), entry.getValue().groups())).collect(Collectors.toSet());
        CoreTopology coreTopology = topologyService.coreServers();
        for (MemberId validCore : coreTopology.members().keySet()) {
            Optional<CoreServerInfo> coreServerInfo = coreTopology.find(validCore);
            if (coreServerInfo.isPresent()) {
                CoreServerInfo serverInfo = coreServerInfo.get();
                possibleReaders.add(new ServerInfo(serverInfo.connectors().boltAddress(), validCore, serverInfo.groups()));
            }
        }
        return filters.apply(possibleReaders).stream().map(ServerInfo::memberId).findAny();
    } catch (InvalidFilterSpecification invalidFilterSpecification) {
        return Optional.empty();
    }
}
Also used : Service(org.neo4j.helpers.Service) InvalidFilterSpecification(org.neo4j.causalclustering.load_balancing.plugins.server_policies.InvalidFilterSpecification) CausalClusteringSettings(org.neo4j.causalclustering.core.CausalClusteringSettings) Optional(java.util.Optional) Set(java.util.Set) MemberId(org.neo4j.causalclustering.identity.MemberId) CoreServerInfo(org.neo4j.causalclustering.discovery.CoreServerInfo) Filter(org.neo4j.causalclustering.load_balancing.filters.Filter) ServerInfo(org.neo4j.causalclustering.load_balancing.plugins.server_policies.ServerInfo) Collectors(java.util.stream.Collectors) CoreTopology(org.neo4j.causalclustering.discovery.CoreTopology) FilterConfigParser(org.neo4j.causalclustering.load_balancing.plugins.server_policies.FilterConfigParser) MemberId(org.neo4j.causalclustering.identity.MemberId) CoreServerInfo(org.neo4j.causalclustering.discovery.CoreServerInfo) ServerInfo(org.neo4j.causalclustering.load_balancing.plugins.server_policies.ServerInfo) CoreServerInfo(org.neo4j.causalclustering.discovery.CoreServerInfo) InvalidFilterSpecification(org.neo4j.causalclustering.load_balancing.plugins.server_policies.InvalidFilterSpecification) CoreTopology(org.neo4j.causalclustering.discovery.CoreTopology)

Example 14 with CoreTopology

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

the class ClusterOverviewProcedure method apply.

@Override
public RawIterator<Object[], ProcedureException> apply(Context ctx, Object[] input) throws ProcedureException {
    List<ReadWriteEndPoint> endpoints = new ArrayList<>();
    CoreTopology coreTopology = topologyService.coreServers();
    Set<MemberId> coreMembers = coreTopology.members().keySet();
    MemberId leader = null;
    try {
        leader = leaderLocator.getLeader();
    } catch (NoLeaderFoundException e) {
        log.debug("No write server found. This can happen during a leader switch.");
    }
    for (MemberId memberId : coreMembers) {
        Optional<CoreServerInfo> coreServerInfo = coreTopology.find(memberId);
        if (coreServerInfo.isPresent()) {
            Role role = memberId.equals(leader) ? Role.LEADER : Role.FOLLOWER;
            endpoints.add(new ReadWriteEndPoint(coreServerInfo.get().connectors(), role, memberId.getUuid(), asList(coreServerInfo.get().groups())));
        } else {
            log.debug("No Address found for " + memberId);
        }
    }
    for (Map.Entry<MemberId, ReadReplicaInfo> readReplica : topologyService.readReplicas().members().entrySet()) {
        ReadReplicaInfo readReplicaInfo = readReplica.getValue();
        endpoints.add(new ReadWriteEndPoint(readReplicaInfo.connectors(), Role.READ_REPLICA, readReplica.getKey().getUuid(), asList(readReplicaInfo.groups())));
    }
    endpoints.sort(comparing(o -> o.addresses().toString()));
    return map((endpoint) -> new Object[] { endpoint.memberId().toString(), endpoint.addresses().uriList().stream().map(URI::toString).toArray(), endpoint.role().name(), endpoint.groups() }, asRawIterator(endpoints.iterator()));
}
Also used : ProcedureException(org.neo4j.kernel.api.exceptions.ProcedureException) Neo4jTypes(org.neo4j.kernel.api.proc.Neo4jTypes) Log(org.neo4j.logging.Log) RawIterator(org.neo4j.collection.RawIterator) CallableProcedure(org.neo4j.kernel.api.proc.CallableProcedure) ClientConnectorAddresses(org.neo4j.causalclustering.discovery.ClientConnectorAddresses) LogProvider(org.neo4j.logging.LogProvider) ArrayList(java.util.ArrayList) Iterators.asRawIterator(org.neo4j.helpers.collection.Iterators.asRawIterator) Map(java.util.Map) MemberId(org.neo4j.causalclustering.identity.MemberId) URI(java.net.URI) ReadReplicaInfo(org.neo4j.causalclustering.discovery.ReadReplicaInfo) Comparator.comparing(java.util.Comparator.comparing) ProcedureSignature.procedureSignature(org.neo4j.kernel.api.proc.ProcedureSignature.procedureSignature) CoreServerInfo(org.neo4j.causalclustering.discovery.CoreServerInfo) LeaderLocator(org.neo4j.causalclustering.core.consensus.LeaderLocator) CoreTopology(org.neo4j.causalclustering.discovery.CoreTopology) Iterators.map(org.neo4j.helpers.collection.Iterators.map) TopologyService(org.neo4j.causalclustering.discovery.TopologyService) Set(java.util.Set) UUID(java.util.UUID) Iterables.asList(org.neo4j.helpers.collection.Iterables.asList) List(java.util.List) NoLeaderFoundException(org.neo4j.causalclustering.core.consensus.NoLeaderFoundException) QualifiedName(org.neo4j.kernel.api.proc.QualifiedName) Optional(java.util.Optional) Context(org.neo4j.kernel.api.proc.Context) NoLeaderFoundException(org.neo4j.causalclustering.core.consensus.NoLeaderFoundException) ReadReplicaInfo(org.neo4j.causalclustering.discovery.ReadReplicaInfo) ArrayList(java.util.ArrayList) URI(java.net.URI) CoreTopology(org.neo4j.causalclustering.discovery.CoreTopology) MemberId(org.neo4j.causalclustering.identity.MemberId) CoreServerInfo(org.neo4j.causalclustering.discovery.CoreServerInfo) Map(java.util.Map)

Example 15 with CoreTopology

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

the class ClusterBinder method bindToCluster.

/**
     * The cluster binding process tries to establish a common cluster ID. If there is no common cluster ID
     * then a single instance will eventually create one and publish it through the underlying topology service.
     *
     * @throws IOException If there is an issue with I/O.
     * @throws InterruptedException If the process gets interrupted.
     * @throws TimeoutException If the process times out.
     */
public void bindToCluster(ThrowingConsumer<CoreSnapshot, Throwable> snapshotInstaller) throws Throwable {
    if (clusterIdStorage.exists()) {
        clusterId = clusterIdStorage.readState();
        publishClusterId(clusterId);
        log.info("Already bound to cluster: " + clusterId);
        return;
    }
    CoreTopology topology;
    long endTime = clock.millis() + timeoutMillis;
    do {
        topology = topologyService.coreServers();
        if (topology.clusterId() != null) {
            clusterId = topology.clusterId();
            log.info("Bound to cluster: " + clusterId);
        } else if (topology.canBeBootstrapped()) {
            clusterId = new ClusterId(UUID.randomUUID());
            CoreSnapshot snapshot = coreBootstrapper.bootstrap(topology.members().keySet());
            log.info(String.format("Bootstrapped with snapshot: %s and clusterId: %s", snapshot, clusterId));
            snapshotInstaller.accept(snapshot);
            publishClusterId(clusterId);
        } else {
            retryWaiter.apply();
        }
    } while (clusterId == null && clock.millis() < endTime);
    if (clusterId == null) {
        throw new TimeoutException(String.format("Failed to join a cluster with members %s. Another member should have published " + "a clusterId but none was detected. Please restart the cluster.", topology));
    }
    clusterIdStorage.writeState(clusterId);
}
Also used : CoreSnapshot(org.neo4j.causalclustering.core.state.snapshot.CoreSnapshot) CoreTopology(org.neo4j.causalclustering.discovery.CoreTopology) TimeoutException(java.util.concurrent.TimeoutException)

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