Search in sources :

Example 1 with CoreGraphDatabase

use of org.neo4j.causalclustering.core.CoreGraphDatabase in project neo4j by neo4j.

the class Cluster method leaderTx.

/**
     * Perform a transaction against the leader of the core cluster, retrying as necessary.
     */
private CoreClusterMember leaderTx(BiConsumer<CoreGraphDatabase, Transaction> op, int timeout, TimeUnit timeUnit) throws Exception {
    ThrowingSupplier<CoreClusterMember, Exception> supplier = () -> {
        CoreClusterMember member = awaitLeader(timeout, timeUnit);
        CoreGraphDatabase db = member.database();
        if (db == null) {
            throw new DatabaseShutdownException();
        }
        try (Transaction tx = db.beginTx()) {
            op.accept(db, tx);
            return member;
        } catch (Throwable e) {
            if (isTransientFailure(e)) {
                // this is not the best, but it helps in debugging
                System.err.println("Transient failure in leader transaction, trying again.");
                e.printStackTrace();
                return null;
            } else {
                throw e;
            }
        }
    };
    return awaitEx(supplier, notNull()::test, timeout, timeUnit);
}
Also used : Transaction(org.neo4j.graphdb.Transaction) DatabaseShutdownException(org.neo4j.graphdb.DatabaseShutdownException) CoreGraphDatabase(org.neo4j.causalclustering.core.CoreGraphDatabase) TimeoutException(java.util.concurrent.TimeoutException) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) IdGenerationException(org.neo4j.causalclustering.core.state.machines.id.IdGenerationException) ExecutionException(java.util.concurrent.ExecutionException) NoLeaderFoundException(org.neo4j.causalclustering.core.consensus.NoLeaderFoundException) WriteOperationsNotAllowedException(org.neo4j.graphdb.security.WriteOperationsNotAllowedException) DatabaseShutdownException(org.neo4j.graphdb.DatabaseShutdownException) AcquireLockTimeoutException(org.neo4j.storageengine.api.lock.AcquireLockTimeoutException)

Example 2 with CoreGraphDatabase

use of org.neo4j.causalclustering.core.CoreGraphDatabase in project neo4j by neo4j.

the class CoreToCoreCopySnapshotIT method shouldBeAbleToDownloadToNewInstanceAfterPruning.

@Test
public void shouldBeAbleToDownloadToNewInstanceAfterPruning() throws Exception {
    // given
    Map<String, String> params = stringMap(CausalClusteringSettings.state_machine_flush_window_size.name(), "1", CausalClusteringSettings.raft_log_pruning_strategy.name(), "3 entries", CausalClusteringSettings.raft_log_rotation_size.name(), "1K");
    Cluster cluster = clusterRule.withSharedCoreParams(params).startCluster();
    CoreClusterMember leader = cluster.coreTx((db, tx) -> {
        createData(db, 10000);
        tx.success();
    });
    // when
    for (CoreClusterMember coreDb : cluster.coreMembers()) {
        coreDb.coreState().prune();
    }
    // to force a change of leader
    cluster.removeCoreMember(leader);
    leader = cluster.awaitLeader();
    int newDbId = 3;
    cluster.addCoreMemberWithId(newDbId).start();
    CoreGraphDatabase newDb = cluster.getCoreMemberById(newDbId).database();
    // then
    assertEquals(DbRepresentation.of(leader.database()), DbRepresentation.of(newDb));
}
Also used : CoreClusterMember(org.neo4j.causalclustering.discovery.CoreClusterMember) Cluster(org.neo4j.causalclustering.discovery.Cluster) CoreGraphDatabase(org.neo4j.causalclustering.core.CoreGraphDatabase) Test(org.junit.Test)

Example 3 with CoreGraphDatabase

use of org.neo4j.causalclustering.core.CoreGraphDatabase in project neo4j by neo4j.

the class ReadReplicaReplicationIT method transactionsShouldNotAppearOnTheReadReplicaWhilePollingIsPaused.

@Test
public void transactionsShouldNotAppearOnTheReadReplicaWhilePollingIsPaused() throws Throwable {
    // given
    Cluster cluster = clusterRule.startCluster();
    ReadReplicaGraphDatabase readReplicaGraphDatabase = cluster.findAnyReadReplica().database();
    CatchupPollingProcess pollingClient = readReplicaGraphDatabase.getDependencyResolver().resolveDependency(CatchupPollingProcess.class);
    pollingClient.stop();
    cluster.coreTx((coreGraphDatabase, transaction) -> {
        coreGraphDatabase.createNode();
        transaction.success();
    });
    CoreGraphDatabase leaderDatabase = cluster.awaitLeader().database();
    long transactionVisibleOnLeader = transactionIdTracker(leaderDatabase).newestEncounteredTxId();
    // when the poller is paused, transaction doesn't make it to the read replica
    try {
        transactionIdTracker(readReplicaGraphDatabase).awaitUpToDate(transactionVisibleOnLeader, ofSeconds(3));
        fail("should have thrown exception");
    } catch (TransactionFailureException e) {
    // expected timeout
    }
    // when the poller is resumed, it does make it to the read replica
    pollingClient.start();
    transactionIdTracker(readReplicaGraphDatabase).awaitUpToDate(transactionVisibleOnLeader, ofSeconds(3));
}
Also used : TransactionFailureException(org.neo4j.kernel.api.exceptions.TransactionFailureException) CatchupPollingProcess(org.neo4j.causalclustering.catchup.tx.CatchupPollingProcess) Cluster(org.neo4j.causalclustering.discovery.Cluster) ReadReplicaGraphDatabase(org.neo4j.causalclustering.readreplica.ReadReplicaGraphDatabase) CoreGraphDatabase(org.neo4j.causalclustering.core.CoreGraphDatabase) Test(org.junit.Test)

Example 4 with CoreGraphDatabase

use of org.neo4j.causalclustering.core.CoreGraphDatabase in project neo4j by neo4j.

the class ClusterFormationIT method shouldBeAbleToAddAndRemoveCoreMembersUnderModestLoad.

@Test
public void shouldBeAbleToAddAndRemoveCoreMembersUnderModestLoad() throws Exception {
    // given
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    executorService.submit(() -> {
        CoreGraphDatabase leader = cluster.getDbWithRole(Role.LEADER).database();
        try (Transaction tx = leader.beginTx()) {
            leader.createNode();
            tx.success();
        }
    });
    // when
    cluster.getCoreMemberById(0).shutdown();
    cluster.getCoreMemberById(0).start();
    // then
    assertEquals(3, cluster.numberOfCoreMembersReportedByTopology());
    // when
    cluster.removeCoreMemberWithMemberId(0);
    // then
    assertEquals(2, cluster.numberOfCoreMembersReportedByTopology());
    // when
    cluster.addCoreMemberWithId(4).start();
    // then
    assertEquals(3, cluster.numberOfCoreMembersReportedByTopology());
    executorService.shutdown();
}
Also used : InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Transaction(org.neo4j.graphdb.Transaction) ExecutorService(java.util.concurrent.ExecutorService) CoreGraphDatabase(org.neo4j.causalclustering.core.CoreGraphDatabase) Test(org.junit.Test)

Example 5 with CoreGraphDatabase

use of org.neo4j.causalclustering.core.CoreGraphDatabase in project neo4j by neo4j.

the class CausalClusteringProceduresIT method coreProceduresShouldBeAvailable.

@Test
public void coreProceduresShouldBeAvailable() throws Throwable {
    String[] coreProcs = new String[] { // Server role
    "dbms.cluster.role", // Discover the cluster topology
    "dbms.cluster.routing.getServers", // Discover appropriate discovery service
    "dbms.cluster.overview", // Kernel built procedures
    "dbms.procedures", // Built in procedure from enterprise
    "dbms.listQueries" };
    for (String procedure : coreProcs) {
        Optional<CoreClusterMember> firstCore = cluster.coreMembers().stream().findFirst();
        assert firstCore.isPresent();
        CoreGraphDatabase database = firstCore.get().database();
        InternalTransaction tx = database.beginTransaction(KernelTransaction.Type.explicit, AUTH_DISABLED);
        Result coreResult = database.execute("CALL " + procedure + "()");
        assertTrue("core with procedure " + procedure, coreResult.hasNext());
        coreResult.close();
        tx.close();
    }
}
Also used : CoreClusterMember(org.neo4j.causalclustering.discovery.CoreClusterMember) CoreGraphDatabase(org.neo4j.causalclustering.core.CoreGraphDatabase) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) Result(org.neo4j.graphdb.Result) Test(org.junit.Test)

Aggregations

CoreGraphDatabase (org.neo4j.causalclustering.core.CoreGraphDatabase)15 Test (org.junit.Test)12 Transaction (org.neo4j.graphdb.Transaction)9 CoreClusterMember (org.neo4j.causalclustering.discovery.CoreClusterMember)7 File (java.io.File)5 Cluster (org.neo4j.causalclustering.discovery.Cluster)5 WriteOperationsNotAllowedException (org.neo4j.graphdb.security.WriteOperationsNotAllowedException)5 Node (org.neo4j.graphdb.Node)3 IOException (java.io.IOException)2 HazelcastDiscoveryServiceFactory (org.neo4j.causalclustering.discovery.HazelcastDiscoveryServiceFactory)2 ReadReplica (org.neo4j.causalclustering.discovery.ReadReplica)2 ReadReplicaGraphDatabase (org.neo4j.causalclustering.readreplica.ReadReplicaGraphDatabase)2 DefaultFileSystemAbstraction (org.neo4j.io.fs.DefaultFileSystemAbstraction)2 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)2 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 Clock (java.time.Clock)1 Duration.ofSeconds (java.time.Duration.ofSeconds)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1