Search in sources :

Example 21 with ThreadPoolJobScheduler

use of org.neo4j.test.scheduler.ThreadPoolJobScheduler in project neo4j by neo4j.

the class CrashGenerationCleanerCrashTest method mustNotLeakTasksOnCrash.

@Test
void mustNotLeakTasksOnCrash() {
    // Given
    String exceptionMessage = "When there's no more room in hell, the dead will walk the earth";
    CrashGenerationCleaner cleaner = newCrashingCrashGenerationCleaner(exceptionMessage);
    ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
    ThreadPoolJobScheduler threadPoolJobScheduler = new ThreadPoolJobScheduler(executorService);
    try {
        var executor = new CleanupJob.Executor() {

            @Override
            public <T> CleanupJob.JobResult<T> submit(String jobDescription, Callable<T> job) {
                var jobHandle = threadPoolJobScheduler.schedule(Group.TESTING, JobMonitoringParams.NOT_MONITORED, job);
                return jobHandle::get;
            }
        };
        Throwable exception = assertThrows(Throwable.class, () -> cleaner.clean(executor));
        Throwable rootCause = getRootCause(exception);
        assertTrue(rootCause instanceof IOException);
        assertEquals(exceptionMessage, rootCause.getMessage());
    } finally {
        List<Runnable> tasks = executorService.shutdownNow();
        assertEquals(0, tasks.size());
    }
}
Also used : IOException(java.io.IOException) Callable(java.util.concurrent.Callable) ExecutorService(java.util.concurrent.ExecutorService) ThreadPoolJobScheduler(org.neo4j.test.scheduler.ThreadPoolJobScheduler) Test(org.junit.jupiter.api.Test)

Example 22 with ThreadPoolJobScheduler

use of org.neo4j.test.scheduler.ThreadPoolJobScheduler in project neo4j by neo4j.

the class DatabaseStartupTest method startTheDatabaseWithWrongVersionShouldFailAlsoWhenUpgradeIsAllowed.

@Test
void startTheDatabaseWithWrongVersionShouldFailAlsoWhenUpgradeIsAllowed() throws Throwable {
    // given
    // create a store
    DatabaseManagementService managementService = new TestDatabaseManagementServiceBuilder(databaseLayout).build();
    GraphDatabaseService db = managementService.database(DEFAULT_DATABASE_NAME);
    try (Transaction tx = db.beginTx()) {
        tx.createNode();
        tx.commit();
    }
    managementService.shutdown();
    // mess up the version in the metadatastore
    String badStoreVersion = "bad";
    try (FileSystemAbstraction fileSystem = new DefaultFileSystemAbstraction();
        ThreadPoolJobScheduler scheduler = new ThreadPoolJobScheduler();
        PageCache pageCache = createPageCache(fileSystem, scheduler, PageCacheTracer.NULL)) {
        MetaDataStore.setRecord(pageCache, databaseLayout.metadataStore(), MetaDataStore.Position.STORE_VERSION, MetaDataStore.versionStringToLong(badStoreVersion), databaseLayout.getDatabaseName(), NULL);
    }
    managementService = new TestDatabaseManagementServiceBuilder(databaseLayout).setConfig(GraphDatabaseSettings.allow_upgrade, true).build();
    GraphDatabaseAPI databaseService = (GraphDatabaseAPI) managementService.database(DEFAULT_DATABASE_NAME);
    try {
        assertThrows(DatabaseShutdownException.class, databaseService::beginTx);
        DatabaseStateService dbStateService = databaseService.getDependencyResolver().resolveDependency(DatabaseStateService.class);
        assertTrue(dbStateService.causeOfFailure(databaseService.databaseId()).isPresent());
        Optional<Throwable> upgradeException = findCauseOrSuppressed(dbStateService.causeOfFailure(databaseService.databaseId()).get(), e -> e instanceof StoreUpgrader.UnexpectedUpgradingStoreVersionException);
        assertTrue(upgradeException.isPresent());
    } finally {
        managementService.shutdown();
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) DefaultFileSystemAbstraction(org.neo4j.io.fs.DefaultFileSystemAbstraction) EphemeralFileSystemAbstraction(org.neo4j.io.fs.EphemeralFileSystemAbstraction) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) DefaultFileSystemAbstraction(org.neo4j.io.fs.DefaultFileSystemAbstraction) DatabaseStateService(org.neo4j.dbms.DatabaseStateService) TestDatabaseManagementServiceBuilder(org.neo4j.test.TestDatabaseManagementServiceBuilder) Transaction(org.neo4j.graphdb.Transaction) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) StoreUpgrader(org.neo4j.kernel.impl.storemigration.StoreUpgrader) ThreadPoolJobScheduler(org.neo4j.test.scheduler.ThreadPoolJobScheduler) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService) StandalonePageCacheFactory.createPageCache(org.neo4j.io.pagecache.impl.muninn.StandalonePageCacheFactory.createPageCache) PageCache(org.neo4j.io.pagecache.PageCache) Test(org.junit.jupiter.api.Test)

Example 23 with ThreadPoolJobScheduler

use of org.neo4j.test.scheduler.ThreadPoolJobScheduler in project neo4j by neo4j.

the class DatabaseStartupTest method startDatabaseWithWrongTransactionFilesShouldFail.

@Test
void startDatabaseWithWrongTransactionFilesShouldFail() throws IOException {
    // Create a store
    DatabaseManagementService managementService = new TestDatabaseManagementServiceBuilder(databaseLayout).build();
    GraphDatabaseAPI db = (GraphDatabaseAPI) managementService.database(DEFAULT_DATABASE_NAME);
    DatabaseLayout databaseLayout = db.databaseLayout();
    try (Transaction tx = db.beginTx()) {
        tx.createNode();
        tx.commit();
    }
    managementService.shutdown();
    // Change store id component
    try (FileSystemAbstraction fileSystem = new DefaultFileSystemAbstraction();
        ThreadPoolJobScheduler scheduler = new ThreadPoolJobScheduler();
        PageCache pageCache = createPageCache(fileSystem, scheduler, PageCacheTracer.NULL)) {
        long newTime = System.currentTimeMillis() + 1;
        MetaDataStore.setRecord(pageCache, databaseLayout.metadataStore(), MetaDataStore.Position.TIME, newTime, databaseLayout.getDatabaseName(), NULL);
    }
    // Try to start
    managementService = new TestDatabaseManagementServiceBuilder(databaseLayout).build();
    try {
        db = (GraphDatabaseAPI) managementService.database(DEFAULT_DATABASE_NAME);
        assertFalse(db.isAvailable(10));
        DatabaseStateService dbStateService = db.getDependencyResolver().resolveDependency(DatabaseStateService.class);
        Optional<Throwable> cause = dbStateService.causeOfFailure(db.databaseId());
        assertTrue(cause.isPresent());
        assertTrue(cause.get().getCause().getMessage().contains("Mismatching store id"));
    } finally {
        managementService.shutdown();
    }
}
Also used : DefaultFileSystemAbstraction(org.neo4j.io.fs.DefaultFileSystemAbstraction) EphemeralFileSystemAbstraction(org.neo4j.io.fs.EphemeralFileSystemAbstraction) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) DefaultFileSystemAbstraction(org.neo4j.io.fs.DefaultFileSystemAbstraction) DatabaseStateService(org.neo4j.dbms.DatabaseStateService) TestDatabaseManagementServiceBuilder(org.neo4j.test.TestDatabaseManagementServiceBuilder) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) Transaction(org.neo4j.graphdb.Transaction) DatabaseLayout(org.neo4j.io.layout.DatabaseLayout) ThreadPoolJobScheduler(org.neo4j.test.scheduler.ThreadPoolJobScheduler) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService) StandalonePageCacheFactory.createPageCache(org.neo4j.io.pagecache.impl.muninn.StandalonePageCacheFactory.createPageCache) PageCache(org.neo4j.io.pagecache.PageCache) Test(org.junit.jupiter.api.Test)

Example 24 with ThreadPoolJobScheduler

use of org.neo4j.test.scheduler.ThreadPoolJobScheduler in project neo4j by neo4j.

the class DatabaseStartupTest method startTheDatabaseWithWrongVersionShouldFailWithUpgradeNotAllowed.

@Test
void startTheDatabaseWithWrongVersionShouldFailWithUpgradeNotAllowed() throws Throwable {
    // given
    // create a store
    DatabaseManagementService managementService = new TestDatabaseManagementServiceBuilder(databaseLayout).build();
    GraphDatabaseAPI db = (GraphDatabaseAPI) managementService.database(DEFAULT_DATABASE_NAME);
    try (Transaction tx = db.beginTx()) {
        tx.createNode();
        tx.commit();
    }
    managementService.shutdown();
    // mess up the version in the metadatastore
    try (FileSystemAbstraction fileSystem = new DefaultFileSystemAbstraction();
        ThreadPoolJobScheduler scheduler = new ThreadPoolJobScheduler();
        PageCache pageCache = createPageCache(fileSystem, scheduler, PageCacheTracer.NULL)) {
        MetaDataStore.setRecord(pageCache, databaseLayout.metadataStore(), MetaDataStore.Position.STORE_VERSION, MetaDataStore.versionStringToLong("bad"), databaseLayout.getDatabaseName(), NULL);
    }
    managementService = new TestDatabaseManagementServiceBuilder(databaseLayout).build();
    GraphDatabaseAPI databaseService = (GraphDatabaseAPI) managementService.database(DEFAULT_DATABASE_NAME);
    try {
        assertThrows(DatabaseShutdownException.class, databaseService::beginTx);
        DatabaseStateService dbStateService = databaseService.getDependencyResolver().resolveDependency(DatabaseStateService.class);
        assertTrue(dbStateService.causeOfFailure(databaseService.databaseId()).isPresent());
        Throwable throwable = findCauseOrSuppressed(dbStateService.causeOfFailure(databaseService.databaseId()).get(), e -> e instanceof IllegalArgumentException).get();
        assertEquals("Unknown store version 'bad'", throwable.getMessage());
    } finally {
        managementService.shutdown();
    }
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) GraphDatabaseSettings(org.neo4j.configuration.GraphDatabaseSettings) LogAssertions.assertThat(org.neo4j.logging.LogAssertions.assertThat) StandalonePageCacheFactory.createPageCache(org.neo4j.io.pagecache.impl.muninn.StandalonePageCacheFactory.createPageCache) Config(org.neo4j.configuration.Config) DatabaseLayout(org.neo4j.io.layout.DatabaseLayout) DEFAULT_DATABASE_NAME(org.neo4j.configuration.GraphDatabaseSettings.DEFAULT_DATABASE_NAME) TestDatabaseManagementServiceBuilder(org.neo4j.test.TestDatabaseManagementServiceBuilder) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Inject(org.neo4j.test.extension.Inject) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) AssertableLogProvider(org.neo4j.logging.AssertableLogProvider) PageCacheTracer(org.neo4j.io.pagecache.tracing.PageCacheTracer) NULL(org.neo4j.io.pagecache.context.CursorContext.NULL) GraphDatabaseInternalSettings(org.neo4j.configuration.GraphDatabaseInternalSettings) DbmsInfo(org.neo4j.kernel.impl.factory.DbmsInfo) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Transaction(org.neo4j.graphdb.Transaction) Path(java.nio.file.Path) PageCache(org.neo4j.io.pagecache.PageCache) Exceptions.findCauseOrSuppressed(org.neo4j.internal.helpers.Exceptions.findCauseOrSuppressed) DatabaseManagementServiceBuilder(org.neo4j.dbms.api.DatabaseManagementServiceBuilder) IOException(java.io.IOException) Neo4jLayoutExtension(org.neo4j.test.extension.Neo4jLayoutExtension) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) Test(org.junit.jupiter.api.Test) DatabaseStateService(org.neo4j.dbms.DatabaseStateService) ExternalDependencies(org.neo4j.graphdb.facade.ExternalDependencies) DefaultFileSystemAbstraction(org.neo4j.io.fs.DefaultFileSystemAbstraction) EphemeralFileSystemAbstraction(org.neo4j.io.fs.EphemeralFileSystemAbstraction) MetaDataStore(org.neo4j.kernel.impl.store.MetaDataStore) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) CommunityEditionModule(org.neo4j.graphdb.factory.module.edition.CommunityEditionModule) ThreadPoolJobScheduler(org.neo4j.test.scheduler.ThreadPoolJobScheduler) DatabaseShutdownException(org.neo4j.graphdb.DatabaseShutdownException) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService) DatabaseManagementServiceFactory(org.neo4j.graphdb.facade.DatabaseManagementServiceFactory) StoreUpgrader(org.neo4j.kernel.impl.storemigration.StoreUpgrader) GlobalModule(org.neo4j.graphdb.factory.module.GlobalModule) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) DefaultFileSystemAbstraction(org.neo4j.io.fs.DefaultFileSystemAbstraction) EphemeralFileSystemAbstraction(org.neo4j.io.fs.EphemeralFileSystemAbstraction) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) DefaultFileSystemAbstraction(org.neo4j.io.fs.DefaultFileSystemAbstraction) DatabaseStateService(org.neo4j.dbms.DatabaseStateService) TestDatabaseManagementServiceBuilder(org.neo4j.test.TestDatabaseManagementServiceBuilder) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) Transaction(org.neo4j.graphdb.Transaction) ThreadPoolJobScheduler(org.neo4j.test.scheduler.ThreadPoolJobScheduler) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService) StandalonePageCacheFactory.createPageCache(org.neo4j.io.pagecache.impl.muninn.StandalonePageCacheFactory.createPageCache) PageCache(org.neo4j.io.pagecache.PageCache) Test(org.junit.jupiter.api.Test)

Example 25 with ThreadPoolJobScheduler

use of org.neo4j.test.scheduler.ThreadPoolJobScheduler in project neo4j by neo4j.

the class ParallelBatchImporterTest method shouldImportCsvData.

@ParameterizedTest
@MethodSource("params")
void shouldImportCsvData(InputIdGenerator inputIdGenerator, IdType idType) throws Exception {
    this.inputIdGenerator = inputIdGenerator;
    // GIVEN
    ExecutionMonitor processorAssigner = ProcessorAssignmentStrategies.eagerRandomSaturation(config.maxNumberOfProcessors());
    CapturingMonitor monitor = new CapturingMonitor(processorAssigner);
    boolean successful = false;
    Groups groups = new Groups();
    IdGroupDistribution groupDistribution = new IdGroupDistribution(NODE_COUNT, NUMBER_OF_ID_GROUPS, random.random(), groups);
    long nodeRandomSeed = random.nextLong();
    long relationshipRandomSeed = random.nextLong();
    var pageCacheTracer = new DefaultPageCacheTracer();
    JobScheduler jobScheduler = new ThreadPoolJobScheduler();
    // This will have statistically half the nodes be considered dense
    Config dbConfig = Config.defaults(GraphDatabaseSettings.dense_node_threshold, RELATIONSHIPS_PER_NODE * 2);
    IndexImporterFactoryImpl indexImporterFactory = new IndexImporterFactoryImpl(dbConfig);
    final BatchImporter inserter = new ParallelBatchImporter(databaseLayout, fs, pageCacheTracer, config, NullLogService.getInstance(), monitor, EMPTY, dbConfig, getFormat(), ImportLogic.NO_MONITOR, jobScheduler, Collector.EMPTY, TransactionLogInitializer.getLogFilesInitializer(), indexImporterFactory, INSTANCE);
    LongAdder propertyCount = new LongAdder();
    LongAdder relationshipCount = new LongAdder();
    try {
        // WHEN
        inserter.doImport(Input.input(nodes(nodeRandomSeed, NODE_COUNT, config.batchSize(), inputIdGenerator, groupDistribution, propertyCount), relationships(relationshipRandomSeed, RELATIONSHIP_COUNT, config.batchSize(), inputIdGenerator, groupDistribution, propertyCount, relationshipCount), idType, knownEstimates(NODE_COUNT, RELATIONSHIP_COUNT, NODE_COUNT * TOKENS.length / 2, RELATIONSHIP_COUNT * TOKENS.length / 2, NODE_COUNT * TOKENS.length / 2 * Long.BYTES, RELATIONSHIP_COUNT * TOKENS.length / 2 * Long.BYTES, NODE_COUNT * TOKENS.length / 2), groups));
        assertThat(pageCacheTracer.pins()).isGreaterThan(0);
        assertThat(pageCacheTracer.pins()).isEqualTo(pageCacheTracer.unpins());
        assertThat(pageCacheTracer.pins()).isEqualTo(Math.addExact(pageCacheTracer.faults(), pageCacheTracer.hits()));
        // THEN
        DatabaseManagementService managementService = getDBMSBuilder(databaseLayout).build();
        GraphDatabaseService db = managementService.database(DEFAULT_DATABASE_NAME);
        try (Transaction tx = db.beginTx()) {
            inputIdGenerator.reset();
            verifyData(NODE_COUNT, RELATIONSHIP_COUNT, db, tx, groupDistribution, nodeRandomSeed, relationshipRandomSeed);
            tx.commit();
        } finally {
            managementService.shutdown();
        }
        assertConsistent(databaseLayout);
        successful = true;
    } finally {
        jobScheduler.close();
        if (!successful) {
            Path failureFile = databaseLayout.databaseDirectory().resolve("input");
            try (PrintStream out = new PrintStream(Files.newOutputStream(failureFile))) {
                out.println("Seed used in this failing run: " + random.seed());
                out.println(inputIdGenerator);
                inputIdGenerator.reset();
                out.println();
                out.println("Processor assignments");
                out.println(processorAssigner.toString());
            }
            System.err.println("Additional debug information stored in " + failureFile);
        }
    }
}
Also used : JobScheduler(org.neo4j.scheduler.JobScheduler) ThreadPoolJobScheduler(org.neo4j.test.scheduler.ThreadPoolJobScheduler) Path(java.nio.file.Path) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) PrintStream(java.io.PrintStream) Config(org.neo4j.configuration.Config) LongAdder(java.util.concurrent.atomic.LongAdder) Transaction(org.neo4j.graphdb.Transaction) Groups(org.neo4j.internal.batchimport.input.Groups) IndexImporterFactoryImpl(org.neo4j.kernel.impl.index.schema.IndexImporterFactoryImpl) ExecutionMonitor(org.neo4j.internal.batchimport.staging.ExecutionMonitor) ThreadPoolJobScheduler(org.neo4j.test.scheduler.ThreadPoolJobScheduler) DatabaseManagementService(org.neo4j.dbms.api.DatabaseManagementService) DefaultPageCacheTracer(org.neo4j.io.pagecache.tracing.DefaultPageCacheTracer) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

ThreadPoolJobScheduler (org.neo4j.test.scheduler.ThreadPoolJobScheduler)26 JobScheduler (org.neo4j.scheduler.JobScheduler)15 Test (org.junit.jupiter.api.Test)14 FileSystemAbstraction (org.neo4j.io.fs.FileSystemAbstraction)9 PageCache (org.neo4j.io.pagecache.PageCache)9 DefaultFileSystemAbstraction (org.neo4j.io.fs.DefaultFileSystemAbstraction)8 Path (java.nio.file.Path)7 Config (org.neo4j.configuration.Config)7 IOException (java.io.IOException)4 DatabaseManagementService (org.neo4j.dbms.api.DatabaseManagementService)4 Transaction (org.neo4j.graphdb.Transaction)4 ParallelBatchImporter (org.neo4j.internal.batchimport.ParallelBatchImporter)4 EphemeralFileSystemAbstraction (org.neo4j.io.fs.EphemeralFileSystemAbstraction)4 SingleFilePageSwapperFactory (org.neo4j.io.pagecache.impl.SingleFilePageSwapperFactory)4 MuninnPageCache (org.neo4j.io.pagecache.impl.muninn.MuninnPageCache)4 DatabaseStateService (org.neo4j.dbms.DatabaseStateService)3 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)3 Input (org.neo4j.internal.batchimport.input.Input)3 DatabaseLayout (org.neo4j.io.layout.DatabaseLayout)3 StandalonePageCacheFactory.createPageCache (org.neo4j.io.pagecache.impl.muninn.StandalonePageCacheFactory.createPageCache)3