Search in sources :

Example 31 with CommandFailedException

use of org.neo4j.cli.CommandFailedException in project neo4j by neo4j.

the class CheckConsistencyCommand method execute.

@Override
public void execute() {
    options.warnOnUsageOfDeprecatedOptions(spec, ctx);
    if (target.backup != null) {
        target.backup = target.backup.toAbsolutePath();
        if (!Files.isDirectory(target.backup)) {
            throw new CommandFailedException("Report directory path doesn't exist or not a directory: " + target.backup);
        }
    }
    Config config = loadNeo4jConfig(ctx.homeDir(), ctx.confDir(), additionalConfig);
    var memoryTracker = EmptyMemoryTracker.INSTANCE;
    try (FileSystemAbstraction fileSystem = new DefaultFileSystemAbstraction()) {
        DatabaseLayout databaseLayout = Optional.ofNullable(target.backup).map(DatabaseLayout::ofFlat).orElseGet(() -> Neo4jLayout.of(config).databaseLayout(target.database.name()));
        checkDatabaseExistence(databaseLayout);
        try (Closeable ignored = LockChecker.checkDatabaseLock(databaseLayout)) {
            checkDbState(databaseLayout, config, memoryTracker);
            // Only output progress indicator if a console receives the output
            ProgressMonitorFactory progressMonitorFactory = ProgressMonitorFactory.NONE;
            if (System.console() != null) {
                progressMonitorFactory = ProgressMonitorFactory.textual(System.out);
            }
            ConsistencyCheckService.Result consistencyCheckResult;
            try (Log4jLogProvider logProvider = Util.configuredLogProvider(config, System.out)) {
                consistencyCheckResult = consistencyCheckService.runFullConsistencyCheck(databaseLayout, config, progressMonitorFactory, logProvider, fileSystem, verbose, options.getReportDir().normalize(), new ConsistencyFlags(options.isCheckGraph(), options.isCheckIndexes(), options.isCheckIndexStructure()));
            }
            if (!consistencyCheckResult.isSuccessful()) {
                throw new CommandFailedException(format("Inconsistencies found. See '%s' for details.", consistencyCheckResult.reportFile()));
            }
        } catch (FileLockException e) {
            throw new CommandFailedException("The database is in use. Stop database '" + databaseLayout.getDatabaseName() + "' and try again.", e);
        } catch (CannotWriteException e) {
            throw new CommandFailedException("You do not have permission to check database consistency.", e);
        }
    } catch (ConsistencyCheckIncompleteException | IOException e) {
        throw new CommandFailedException("Consistency checking failed." + e.getMessage(), e);
    }
}
Also used : CannotWriteException(org.neo4j.commandline.dbms.CannotWriteException) FileLockException(org.neo4j.kernel.internal.locker.FileLockException) DefaultFileSystemAbstraction(org.neo4j.io.fs.DefaultFileSystemAbstraction) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) DefaultFileSystemAbstraction(org.neo4j.io.fs.DefaultFileSystemAbstraction) ProgressMonitorFactory(org.neo4j.internal.helpers.progress.ProgressMonitorFactory) ConsistencyFlags(org.neo4j.consistency.checking.full.ConsistencyFlags) Config(org.neo4j.configuration.Config) Log4jLogProvider(org.neo4j.logging.log4j.Log4jLogProvider) Closeable(java.io.Closeable) ConsistencyCheckIncompleteException(org.neo4j.consistency.checking.full.ConsistencyCheckIncompleteException) IOException(java.io.IOException) CommandFailedException(org.neo4j.cli.CommandFailedException) DatabaseLayout(org.neo4j.io.layout.DatabaseLayout)

Example 32 with CommandFailedException

use of org.neo4j.cli.CommandFailedException in project neo4j by neo4j.

the class MemoryRecommendationsCommand method getConfig.

private Config getConfig(Path configFile) {
    if (!ctx.fs().fileExists(configFile)) {
        throw new CommandFailedException("Unable to find config file, tried: " + configFile.toAbsolutePath());
    }
    Config config = Config.newBuilder().fromFile(configFile).set(GraphDatabaseSettings.neo4j_home, ctx.homeDir().toAbsolutePath()).commandExpansion(allowCommandExpansion).build();
    ConfigUtils.disableAllConnectors(config);
    return config;
}
Also used : Config(org.neo4j.configuration.Config) CommandFailedException(org.neo4j.cli.CommandFailedException)

Example 33 with CommandFailedException

use of org.neo4j.cli.CommandFailedException in project neo4j by neo4j.

the class StoreInfoCommand method printInfo.

private static String printInfo(FileSystemAbstraction fs, DatabaseLayout databaseLayout, PageCache pageCache, StorageEngineFactory storageEngineFactory, Config config, boolean structured, boolean failSilently) {
    var memoryTracker = EmptyMemoryTracker.INSTANCE;
    try (var ignored = LockChecker.checkDatabaseLock(databaseLayout)) {
        var storeVersionCheck = storageEngineFactory.versionCheck(fs, databaseLayout, Config.defaults(), pageCache, NullLogService.getInstance(), PageCacheTracer.NULL);
        var storeVersion = storeVersionCheck.storeVersion(CursorContext.NULL).orElseThrow(() -> new CommandFailedException(format("Could not find version metadata in store '%s'", databaseLayout.databaseDirectory())));
        var versionInformation = storageEngineFactory.versionInformation(storeVersion);
        var recoveryRequired = checkRecoveryState(fs, databaseLayout, config, memoryTracker);
        var txIdStore = storageEngineFactory.readOnlyTransactionIdStore(fs, databaseLayout, pageCache, CursorContext.NULL);
        // Latest committed tx id found in metadata store. May be behind if recovery is required.
        var lastTxId = txIdStore.getLastCommittedTransactionId();
        var successorString = versionInformation.successor().map(StoreVersion::introductionNeo4jVersion).orElse(null);
        var storeInfo = StoreInfo.notInUseResult(databaseLayout.getDatabaseName(), storeVersion, versionInformation.introductionNeo4jVersion(), successorString, lastTxId, recoveryRequired);
        return storeInfo.print(structured);
    } catch (FileLockException e) {
        if (!failSilently) {
            throw new CommandFailedException(format("Failed to execute command as the database '%s' is in use. " + "Please stop it and try again.", databaseLayout.getDatabaseName()), e);
        }
        return StoreInfo.inUseResult(databaseLayout.getDatabaseName()).print(structured);
    } catch (CommandFailedException e) {
        throw e;
    } catch (Exception e) {
        throw new CommandFailedException(format("Failed to execute command: '%s'.", e.getMessage()), e);
    }
}
Also used : FileLockException(org.neo4j.kernel.internal.locker.FileLockException) CommandFailedException(org.neo4j.cli.CommandFailedException) FileLockException(org.neo4j.kernel.internal.locker.FileLockException) CommandFailedException(org.neo4j.cli.CommandFailedException)

Example 34 with CommandFailedException

use of org.neo4j.cli.CommandFailedException in project neo4j by neo4j.

the class StoreInfoCommand method execute.

@Override
public void execute() {
    var storageEngineFactory = StorageEngineFactory.defaultStorageEngine();
    var config = CommandHelpers.buildConfig(ctx, allowCommandExpansion);
    var neo4jLayout = Neo4jLayout.of(config);
    try (var fs = ctx.fs();
        var jobScheduler = createInitialisedScheduler();
        var pageCache = StandalonePageCacheFactory.createPageCache(fs, jobScheduler, PageCacheTracer.NULL)) {
        validatePath(fs, all, path, neo4jLayout);
        if (all) {
            var collector = structured ? Collectors.joining(",", "[", "]") : Collectors.joining(System.lineSeparator() + System.lineSeparator());
            var result = Arrays.stream(fs.listFiles(path)).sorted(comparing(Path::getFileName)).map(dbPath -> neo4jLayout.databaseLayout(dbPath.getFileName().toString())).filter(dbLayout -> Validators.isExistingDatabase(fs, dbLayout)).map(dbLayout -> printInfo(fs, dbLayout, pageCache, storageEngineFactory, config, structured, true)).collect(collector);
            ctx.out().println(result);
        } else {
            var databaseLayout = neo4jLayout.databaseLayout(path.getFileName().toString());
            ctx.out().println(printInfo(fs, databaseLayout, pageCache, storageEngineFactory, config, structured, false));
        }
    } catch (CommandFailedException e) {
        throw e;
    } catch (Exception e) {
        throw new CommandFailedException(format("Failed to execute command: '%s'.", e.getMessage()), e);
    }
}
Also used : Arrays(java.util.Arrays) Parameters(picocli.CommandLine.Parameters) NullLogService(org.neo4j.logging.internal.NullLogService) CursorContext(org.neo4j.io.pagecache.context.CursorContext) Config(org.neo4j.configuration.Config) EmptyMemoryTracker(org.neo4j.memory.EmptyMemoryTracker) JobSchedulerFactory.createInitialisedScheduler(org.neo4j.kernel.impl.scheduler.JobSchedulerFactory.createInitialisedScheduler) DatabaseLayout(org.neo4j.io.layout.DatabaseLayout) PageCacheTracer(org.neo4j.io.pagecache.tracing.PageCacheTracer) Recovery.isRecoveryRequired(org.neo4j.kernel.recovery.Recovery.isRecoveryRequired) AbstractCommand(org.neo4j.cli.AbstractCommand) Comparator.comparing(java.util.Comparator.comparing) Command(picocli.CommandLine.Command) Path(java.nio.file.Path) MemoryTracker(org.neo4j.memory.MemoryTracker) PageCache(org.neo4j.io.pagecache.PageCache) Validators(org.neo4j.kernel.impl.util.Validators) StandalonePageCacheFactory(org.neo4j.io.pagecache.impl.muninn.StandalonePageCacheFactory) Collectors(java.util.stream.Collectors) Neo4jLayout(org.neo4j.io.layout.Neo4jLayout) String.format(java.lang.String.format) ExecutionContext(org.neo4j.cli.ExecutionContext) Objects(java.util.Objects) StorageEngineFactory(org.neo4j.storageengine.api.StorageEngineFactory) Option(picocli.CommandLine.Option) List(java.util.List) FileLockException(org.neo4j.kernel.internal.locker.FileLockException) CommandFailedException(org.neo4j.cli.CommandFailedException) Pair(org.neo4j.internal.helpers.collection.Pair) StoreVersion(org.neo4j.storageengine.api.StoreVersion) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) CommandFailedException(org.neo4j.cli.CommandFailedException) FileLockException(org.neo4j.kernel.internal.locker.FileLockException) CommandFailedException(org.neo4j.cli.CommandFailedException)

Aggregations

CommandFailedException (org.neo4j.cli.CommandFailedException)34 Test (org.junit.jupiter.api.Test)20 IOException (java.io.IOException)10 Path (java.nio.file.Path)10 Config (org.neo4j.configuration.Config)9 DatabaseLayout (org.neo4j.io.layout.DatabaseLayout)9 FileSystemAbstraction (org.neo4j.io.fs.FileSystemAbstraction)8 DefaultFileSystemAbstraction (org.neo4j.io.fs.DefaultFileSystemAbstraction)6 Closeable (java.io.Closeable)5 ExecutionContext (org.neo4j.cli.ExecutionContext)5 FileLockException (org.neo4j.kernel.internal.locker.FileLockException)5 NoSuchFileException (java.nio.file.NoSuchFileException)3 CheckConsistencyCommand (org.neo4j.consistency.CheckConsistencyCommand)3 ConsistencyCheckService (org.neo4j.consistency.ConsistencyCheckService)3 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)2 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)2 ConsistencyFlags (org.neo4j.consistency.checking.full.ConsistencyFlags)2 ProgressMonitorFactory (org.neo4j.internal.helpers.progress.ProgressMonitorFactory)2 DatabaseLocker (org.neo4j.kernel.internal.locker.DatabaseLocker)2 Locker (org.neo4j.kernel.internal.locker.Locker)2