Search in sources :

Example 81 with DefaultFileSystemAbstraction

use of org.neo4j.io.fs.DefaultFileSystemAbstraction 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 82 with DefaultFileSystemAbstraction

use of org.neo4j.io.fs.DefaultFileSystemAbstraction in project neo4j by neo4j.

the class RotatingLogFileWriter method setupLogFile.

private static Neo4jLoggerContext setupLogFile(FileSystemAbstraction fileSystemAbstraction, Path logPath, long rotationThreshold, int maxArchives, String fileSuffix, String header) {
    try {
        Closeable additionalCloseable = null;
        Configuration configuration = new DefaultConfiguration() {

            @Override
            protected void setToDefault() {
            // no defaults
            }
        };
        // Just adds a header to the beginning of each file - no transformation will be done on the log messages.
        PatternLayout layout = PatternLayout.newBuilder().withConfiguration(configuration).withHeader(header).build();
        Appender appender;
        if (fileSystemAbstraction instanceof DefaultFileSystemAbstraction) {
            appender = RollingFileAppender.newBuilder().setName(APPENDER_NAME).setLayout(layout).withFileName(logPath.toString()).withFilePattern(logPath + ".%i" + fileSuffix).withPolicy(SizeBasedTriggeringPolicy.createPolicy(String.valueOf(rotationThreshold))).withStrategy(DefaultRolloverStrategy.newBuilder().withMax(String.valueOf(maxArchives)).withFileIndex("min").build()).build();
        } else {
            // When using a different file system than DefaultFileSystemAbstraction for tests, we cannot use log4j file appenders since
            // it will create files directly in the real filesystem ignoring our abstraction.
            fileSystemAbstraction.mkdirs(logPath.getParent());
            OutputStream outputStream = fileSystemAbstraction.openAsOutputStream(logPath, true);
            additionalCloseable = outputStream;
            appender = ((OutputStreamAppender.Builder<?>) OutputStreamAppender.newBuilder().setName(APPENDER_NAME).setLayout(layout)).setTarget(outputStream).build();
        }
        appender.start();
        configuration.addAppender(appender);
        LoggerConfig rootLogger = configuration.getRootLogger();
        rootLogger.addAppender(appender, null, null);
        rootLogger.setLevel(Level.DEBUG);
        LoggerContext context = new LoggerContext("loggercontext");
        context.setConfiguration(configuration);
        return new Neo4jLoggerContext(context, additionalCloseable);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : Appender(org.apache.logging.log4j.core.Appender) OutputStreamAppender(org.apache.logging.log4j.core.appender.OutputStreamAppender) RollingFileAppender(org.apache.logging.log4j.core.appender.RollingFileAppender) DefaultFileSystemAbstraction(org.neo4j.io.fs.DefaultFileSystemAbstraction) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) Configuration(org.apache.logging.log4j.core.config.Configuration) Closeable(java.io.Closeable) PatternLayout(org.apache.logging.log4j.core.layout.PatternLayout) OutputStream(java.io.OutputStream) DefaultConfiguration(org.apache.logging.log4j.core.config.DefaultConfiguration) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) LoggerContext(org.apache.logging.log4j.core.LoggerContext) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Aggregations

DefaultFileSystemAbstraction (org.neo4j.io.fs.DefaultFileSystemAbstraction)82 FileSystemAbstraction (org.neo4j.io.fs.FileSystemAbstraction)43 File (java.io.File)24 Path (java.nio.file.Path)21 PageCache (org.neo4j.io.pagecache.PageCache)21 Test (org.junit.Test)14 Test (org.junit.jupiter.api.Test)13 IOException (java.io.IOException)12 DatabaseLayout (org.neo4j.io.layout.DatabaseLayout)11 Config (org.neo4j.kernel.configuration.Config)11 Config (org.neo4j.configuration.Config)9 ThreadPoolJobScheduler (org.neo4j.test.scheduler.ThreadPoolJobScheduler)8 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)7 Transaction (org.neo4j.graphdb.Transaction)7 PrintStream (java.io.PrintStream)6 Before (org.junit.Before)6 CommandFailed (org.neo4j.commandline.admin.CommandFailed)6 Args (org.neo4j.helpers.Args)6 StandalonePageCacheFactory.createPageCache (org.neo4j.io.pagecache.impl.muninn.StandalonePageCacheFactory.createPageCache)6 JobScheduler (org.neo4j.scheduler.JobScheduler)6