Search in sources :

Example 6 with Barrier

use of org.neo4j.test.Barrier in project neo4j by neo4j.

the class IdBufferingRoleSwitchIT method shouldNotSeeFreedIdsCrossRoleSwitch.

@Test
public void shouldNotSeeFreedIdsCrossRoleSwitch() throws Throwable {
    // GIVEN
    ManagedCluster cluster = clusterRule.startCluster();
    HighlyAvailableGraphDatabase firstMaster = cluster.getMaster();
    // WHEN
    // a node with a property
    Node node = createNodeWithProperties(firstMaster, 1);
    // sync cluster
    cluster.sync();
    // a transaction on master which deletes the property
    deleteNode(node, firstMaster);
    triggerIdMaintenance(firstMaster);
    // <-- this one reuses the same property id 0
    createNodeWithProperties(firstMaster, 1);
    // a transaction T on slave which will be kept open using a barrier
    GraphDatabaseAPI slave = cluster.getAnySlave();
    Barrier.Control barrier = new Barrier.Control();
    Future<Void> t = t2.execute(barrierControlledReadTransaction(slave, barrier));
    // pull updates on slave
    barrier.await();
    slave.getDependencyResolver().resolveDependency(UpdatePuller.class).pullUpdates();
    // a role switch
    cluster.shutdown(firstMaster);
    cluster.await(masterAvailable(firstMaster));
    // close T
    barrier.release();
    t.get();
    triggerIdMaintenance(slave);
    // THEN the deleted property record should now not be in freelist on new master
    // <-- this transaction should introduce inconsistencies
    createNodeWithProperties(slave, 10);
    // <-- CC will be run here since that's configured above ^^^
    cluster.stop();
}
Also used : GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) ManagedCluster(org.neo4j.kernel.impl.ha.ClusterManager.ManagedCluster) Node(org.neo4j.graphdb.Node) Barrier(org.neo4j.test.Barrier) Test(org.junit.Test)

Example 7 with Barrier

use of org.neo4j.test.Barrier in project neo4j by neo4j.

the class BuiltInProceduresInteractionTestBase method shouldTerminateQueriesEvenIfUsingPeriodicCommit.

@SuppressWarnings("unchecked")
@Test
public void shouldTerminateQueriesEvenIfUsingPeriodicCommit() throws Throwable {
    for (int batchSize = 8; batchSize <= 11; batchSize++) {
        // Spawns a throttled HTTP server, runs a PERIODIC COMMIT that fetches data from this server,
        // and checks that the query is visible when using listQueries()
        // Given
        final DoubleLatch latch = new DoubleLatch(3, true);
        final Barrier.Control barrier = new Barrier.Control();
        // Serve CSV via local web server, let Jetty find a random port for us
        Server server = createHttpServer(latch, barrier, batchSize, 50 - batchSize);
        server.start();
        int localPort = getLocalPort(server);
        // When
        ThreadedTransaction<S> write = new ThreadedTransaction<>(neo, latch);
        try {
            String writeQuery = write.executeEarly(threading, writeSubject, KernelTransaction.Type.implicit, format("USING PERIODIC COMMIT 10 LOAD CSV FROM 'http://localhost:%d' AS line ", localPort) + "CREATE (n:A {id: line[0], square: line[1]}) RETURN count(*)");
            latch.startAndWaitForAllToStart();
            // Then
            String writeQueryId = extractQueryId(writeQuery);
            assertSuccess(adminSubject, "CALL dbms.killQuery('" + writeQueryId + "') YIELD username " + "RETURN count(username) AS count, username", r -> {
                List<Map<String, Object>> actual = r.stream().collect(toList());
                Matcher<Map<String, Object>> mapMatcher = allOf((Matcher) hasEntry(equalTo("count"), anyOf(equalTo(1), equalTo(1L))), (Matcher) hasEntry(equalTo("username"), equalTo("writeSubject")));
                assertThat(actual, matchesOneToOneInAnyOrder(mapMatcher));
            });
        } finally {
            // When
            barrier.release();
            latch.finishAndWaitForAllToFinish();
            // Then
            write.closeAndAssertTransactionTermination();
            // stop server after assertion to avoid other kind of failures due to races (e.g., already closed
            // lock clients )
            server.stop();
        }
    }
}
Also used : Server(org.eclipse.jetty.server.Server) TransactionIT.createHttpServer(org.neo4j.bolt.v1.runtime.integration.TransactionIT.createHttpServer) Barrier(org.neo4j.test.Barrier) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) DoubleLatch(org.neo4j.test.DoubleLatch) Map(java.util.Map) MapUtil.stringMap(org.neo4j.helpers.collection.MapUtil.stringMap) Collections.emptyMap(java.util.Collections.emptyMap) Test(org.junit.Test)

Example 8 with Barrier

use of org.neo4j.test.Barrier in project neo4j by neo4j.

the class BackupServiceIT method shouldContainTransactionsThatHappenDuringBackupProcess.

@Test
public void shouldContainTransactionsThatHappenDuringBackupProcess() throws Throwable {
    // given
    defaultBackupPortHostParams();
    Config defaultConfig = dbRule.getConfigCopy();
    dbRule.setConfig(OnlineBackupSettings.online_backup_enabled, "false");
    Config withOnlineBackupDisabled = dbRule.getConfigCopy();
    final Barrier.Control barrier = new Barrier.Control();
    final GraphDatabaseAPI db = dbRule.getGraphDatabaseAPI();
    // create some data
    createAndIndexNode(db, 1);
    final DependencyResolver resolver = db.getDependencyResolver();
    long expectedLastTxId = resolver.resolveDependency(TransactionIdStore.class).getLastClosedTransactionId();
    // This monitor is added server-side...
    monitors.addMonitorListener(new StoreSnoopingMonitor(barrier));
    Dependencies dependencies = new Dependencies(resolver);
    dependencies.satisfyDependencies(defaultConfig, monitors, NullLogProvider.getInstance());
    OnlineBackupKernelExtension backup = (OnlineBackupKernelExtension) new OnlineBackupExtensionFactory().newInstance(new SimpleKernelContext(storeDir, DatabaseInfo.UNKNOWN, dependencies), DependenciesProxy.dependencies(dependencies, OnlineBackupExtensionFactory.Dependencies.class));
    backup.start();
    // when
    BackupService backupService = backupService();
    ExecutorService executor = Executors.newSingleThreadExecutor();
    executor.execute(() -> {
        barrier.awaitUninterruptibly();
        createAndIndexNode(db, 1);
        resolver.resolveDependency(StorageEngine.class).flushAndForce(limiter);
        barrier.release();
    });
    BackupService.BackupOutcome backupOutcome = backupService.doFullBackup(BACKUP_HOST, backupPort, backupDir.getAbsoluteFile(), ConsistencyCheck.FULL, withOnlineBackupDisabled, BackupClient.BIG_READ_TIMEOUT, false);
    backup.stop();
    executor.shutdown();
    executor.awaitTermination(30, TimeUnit.SECONDS);
    // then
    checkPreviousCommittedTxIdFromLog(0, expectedLastTxId);
    File neoStore = new File(storeDir, MetaDataStore.DEFAULT_NAME);
    long txIdFromOrigin = MetaDataStore.getRecord(resolver.resolveDependency(PageCache.class), neoStore, Position.LAST_TRANSACTION_ID);
    checkLastCommittedTxIdInLogAndNeoStore(expectedLastTxId + 1, txIdFromOrigin);
    assertEquals(DbRepresentation.of(db), getBackupDbRepresentation());
    assertTrue(backupOutcome.isConsistent());
}
Also used : TransactionIdStore(org.neo4j.kernel.impl.transaction.log.TransactionIdStore) Config(org.neo4j.kernel.configuration.Config) Barrier(org.neo4j.test.Barrier) StorageEngine(org.neo4j.storageengine.api.StorageEngine) DependencyResolver(org.neo4j.graphdb.DependencyResolver) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) SimpleKernelContext(org.neo4j.kernel.impl.spi.SimpleKernelContext) ExecutorService(java.util.concurrent.ExecutorService) Dependencies(org.neo4j.kernel.impl.util.Dependencies) LogFile(org.neo4j.kernel.impl.transaction.log.LogFile) StoreFile(org.neo4j.kernel.impl.storemigration.StoreFile) File(java.io.File) PageCache(org.neo4j.io.pagecache.PageCache) Test(org.junit.Test)

Example 9 with Barrier

use of org.neo4j.test.Barrier in project neo4j by neo4j.

the class DeferringLocksIT method removeNodeChangeNodeProperty.

@Test(timeout = TEST_TIMEOUT)
public void removeNodeChangeNodeProperty() throws Exception {
    // GIVEN
    final Barrier.Control barrier = new Barrier.Control();
    final long nodeId;
    try (Transaction tx = db.beginTx()) {
        Node node = db.createNode();
        nodeId = node.getId();
        node.setProperty(PROPERTY_KEY, VALUE_1);
        tx.success();
    }
    // WHEN
    Future<Void> future = t2.execute(new WorkerCommand<Void, Void>() {

        @Override
        public Void doWork(Void state) throws Exception {
            try (Transaction tx = db.beginTx()) {
                db.getNodeById(nodeId).delete();
                tx.success();
                barrier.reached();
            }
            return null;
        }
    });
    try {
        try (Transaction tx = db.beginTx()) {
            barrier.await();
            db.getNodeById(nodeId).setProperty(PROPERTY_KEY, VALUE_2);
            tx.success();
            barrier.release();
        }
    } catch (TransactionFailureException e) {
        // Node was already deleted, fine.
        assertThat(e.getCause(), instanceOf(InvalidRecordException.class));
    }
    future.get();
    try (Transaction tx = db.beginTx()) {
        try {
            db.getNodeById(nodeId);
            assertEquals(VALUE_2, db.getNodeById(nodeId).getProperty(PROPERTY_KEY, VALUE_2));
        } catch (NotFoundException e) {
        // Fine, its gone
        }
        tx.success();
    }
}
Also used : Node(org.neo4j.graphdb.Node) NotFoundException(org.neo4j.graphdb.NotFoundException) Barrier(org.neo4j.test.Barrier) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) NotFoundException(org.neo4j.graphdb.NotFoundException) InvalidRecordException(org.neo4j.kernel.impl.store.InvalidRecordException) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) Transaction(org.neo4j.graphdb.Transaction) Test(org.junit.Test)

Example 10 with Barrier

use of org.neo4j.test.Barrier in project neo4j by neo4j.

the class DeferringLocksIT method firstRemoveSecondChangeProperty.

@Test(timeout = TEST_TIMEOUT)
public void firstRemoveSecondChangeProperty() throws Exception {
    // GIVEN
    final Barrier.Control barrier = new Barrier.Control();
    final Node node;
    try (Transaction tx = db.beginTx()) {
        node = db.createNode();
        node.setProperty(PROPERTY_KEY, VALUE_1);
        tx.success();
    }
    // WHEN
    Future<Void> future = t2.execute(new WorkerCommand<Void, Void>() {

        @Override
        public Void doWork(Void state) throws Exception {
            try (Transaction tx = db.beginTx()) {
                node.removeProperty(PROPERTY_KEY);
                tx.success();
                barrier.reached();
            }
            return null;
        }
    });
    try (Transaction tx = db.beginTx()) {
        barrier.await();
        node.setProperty(PROPERTY_KEY, VALUE_2);
        tx.success();
        barrier.release();
    }
    future.get();
    try (Transaction tx = db.beginTx()) {
        assertEquals(VALUE_2, node.getProperty(PROPERTY_KEY, VALUE_2));
        tx.success();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Barrier(org.neo4j.test.Barrier) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) NotFoundException(org.neo4j.graphdb.NotFoundException) InvalidRecordException(org.neo4j.kernel.impl.store.InvalidRecordException) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)15 Barrier (org.neo4j.test.Barrier)15 Transaction (org.neo4j.graphdb.Transaction)7 Node (org.neo4j.graphdb.Node)6 ExecutionException (java.util.concurrent.ExecutionException)3 Server (org.eclipse.jetty.server.Server)3 NotFoundException (org.neo4j.graphdb.NotFoundException)3 TransactionFailureException (org.neo4j.graphdb.TransactionFailureException)3 InvalidRecordException (org.neo4j.kernel.impl.store.InvalidRecordException)3 File (java.io.File)2 IOException (java.io.IOException)2 Collections.emptyMap (java.util.Collections.emptyMap)2 Map (java.util.Map)2 Future (java.util.concurrent.Future)2 MILLISECONDS (java.util.concurrent.TimeUnit.MILLISECONDS)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 Assert.assertEquals (org.junit.Assert.assertEquals)2 Assert.assertSame (org.junit.Assert.assertSame)2 Assert.fail (org.junit.Assert.fail)2 Rule (org.junit.Rule)2