Search in sources :

Example 1 with CommandFailed

use of org.neo4j.commandline.admin.CommandFailed in project neo4j by neo4j.

the class CheckConsistencyCommandTest method failsWhenInconsistenciesAreFound.

@Test
public void failsWhenInconsistenciesAreFound() throws Exception {
    ConsistencyCheckService consistencyCheckService = mock(ConsistencyCheckService.class);
    Path homeDir = testDir.directory("home").toPath();
    OutsideWorld outsideWorld = mock(OutsideWorld.class);
    CheckConsistencyCommand checkConsistencyCommand = new CheckConsistencyCommand(homeDir, testDir.directory("conf").toPath(), outsideWorld, consistencyCheckService);
    File databasePath = new File(homeDir.toFile(), "data/databases/mydb");
    when(consistencyCheckService.runFullConsistencyCheck(eq(databasePath), any(Config.class), any(ProgressMonitorFactory.class), any(LogProvider.class), any(FileSystemAbstraction.class), eq(true), anyObject(), any(CheckConsistencyConfig.class))).thenReturn(ConsistencyCheckService.Result.failure(new File("/the/report/path")));
    try {
        checkConsistencyCommand.execute(new String[] { "--database=mydb", "--verbose" });
    } catch (CommandFailed e) {
        assertThat(e.getMessage(), containsString(new File("/the/report/path").toString()));
    }
}
Also used : Path(java.nio.file.Path) LogProvider(org.neo4j.logging.LogProvider) OutsideWorld(org.neo4j.commandline.admin.OutsideWorld) CheckConsistencyConfig(org.neo4j.consistency.checking.full.CheckConsistencyConfig) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) ProgressMonitorFactory(org.neo4j.helpers.progress.ProgressMonitorFactory) CheckConsistencyConfig(org.neo4j.consistency.checking.full.CheckConsistencyConfig) Config(org.neo4j.kernel.configuration.Config) CommandFailed(org.neo4j.commandline.admin.CommandFailed) File(java.io.File) Test(org.junit.Test)

Example 2 with CommandFailed

use of org.neo4j.commandline.admin.CommandFailed in project neo4j by neo4j.

the class RestoreDatabaseCli method execute.

@Override
public void execute(String[] incomingArguments) throws IncorrectUsage, CommandFailed {
    String databaseName;
    String fromPath;
    boolean forceOverwrite;
    try {
        databaseName = arguments.parse("database", incomingArguments);
        fromPath = arguments.parse("from", incomingArguments);
        forceOverwrite = arguments.parseBoolean("force", incomingArguments);
    } catch (IllegalArgumentException e) {
        throw new IncorrectUsage(e.getMessage());
    }
    Config config = loadNeo4jConfig(homeDir, configDir, databaseName);
    try (FileSystemAbstraction fileSystem = new DefaultFileSystemAbstraction()) {
        RestoreDatabaseCommand restoreDatabaseCommand = new RestoreDatabaseCommand(fileSystem, new File(fromPath), config, databaseName, forceOverwrite);
        restoreDatabaseCommand.execute();
    } catch (IOException e) {
        throw new CommandFailed("Failed to restore database", e);
    }
}
Also used : DefaultFileSystemAbstraction(org.neo4j.io.fs.DefaultFileSystemAbstraction) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) DefaultFileSystemAbstraction(org.neo4j.io.fs.DefaultFileSystemAbstraction) IncorrectUsage(org.neo4j.commandline.admin.IncorrectUsage) Config(org.neo4j.kernel.configuration.Config) CommandFailed(org.neo4j.commandline.admin.CommandFailed) IOException(java.io.IOException) File(java.io.File)

Example 3 with CommandFailed

use of org.neo4j.commandline.admin.CommandFailed in project neo4j by neo4j.

the class OnlineBackupCommand method execute.

@Override
public void execute(String[] args) throws IncorrectUsage, CommandFailed {
    final HostnamePort address;
    final Path folder;
    final String name;
    final boolean fallbackToFull;
    final boolean doConsistencyCheck;
    final Optional<Path> additionalConfig;
    final Path reportDir;
    final long timeout;
    final boolean checkGraph;
    final boolean checkIndexes;
    final boolean checkLabelScanStore;
    final boolean checkPropertyOwners;
    try {
        address = toHostnamePort(new HostnamePort("localhost", 6362)).apply(arguments.parse("from", args));
        folder = arguments.parseMandatoryPath("backup-dir", args);
        name = arguments.parse("name", args);
        fallbackToFull = arguments.parseBoolean("fallback-to-full", args);
        doConsistencyCheck = arguments.parseBoolean("check-consistency", args);
        timeout = parseTimeout(args);
        additionalConfig = arguments.parseOptionalPath("additional-config", args);
        reportDir = arguments.parseOptionalPath("cc-report-dir", args).orElseThrow(() -> new IllegalArgumentException("cc-report-dir must be a path"));
    } catch (IllegalArgumentException e) {
        throw new IncorrectUsage(e.getMessage());
    }
    // Make sure destination exists
    if (!outsideWorld.fileSystem().isDirectory(folder.toFile())) {
        throw new CommandFailed(String.format("Directory '%s' does not exist.", folder));
    }
    if (!outsideWorld.fileSystem().isDirectory(reportDir.toFile())) {
        throw new CommandFailed(String.format("Directory '%s' does not exist.", reportDir));
    }
    File destination = folder.resolve(name).toFile();
    Config config = loadConfig(additionalConfig);
    boolean done = false;
    try {
        // We can remove the loading from config file in 4.0
        if (arguments.has("cc-graph", args)) {
            checkGraph = arguments.parseBoolean("cc-graph", args);
        } else {
            checkGraph = ConsistencyCheckSettings.consistency_check_graph.from(config);
        }
        if (arguments.has("cc-indexes", args)) {
            checkIndexes = arguments.parseBoolean("cc-indexes", args);
        } else {
            checkIndexes = ConsistencyCheckSettings.consistency_check_indexes.from(config);
        }
        if (arguments.has("cc-label-scan-store", args)) {
            checkLabelScanStore = arguments.parseBoolean("cc-label-scan-store", args);
        } else {
            checkLabelScanStore = ConsistencyCheckSettings.consistency_check_label_scan_store.from(config);
        }
        if (arguments.has("cc-property-owners", args)) {
            checkPropertyOwners = arguments.parseBoolean("cc-property-owners", args);
        } else {
            checkPropertyOwners = ConsistencyCheckSettings.consistency_check_property_owners.from(config);
        }
    } catch (IllegalArgumentException e) {
        throw new IncorrectUsage(e.getMessage());
    }
    File[] listFiles = outsideWorld.fileSystem().listFiles(destination);
    if (listFiles != null && listFiles.length > 0) {
        outsideWorld.stdOutLine("Destination is not empty, doing incremental backup...");
        try {
            backupService.doIncrementalBackup(address.getHost(), address.getPort(), destination, timeout, config);
            done = true;
        } catch (Exception e) {
            if (fallbackToFull) {
                outsideWorld.stdErrLine("Incremental backup failed: " + e.getMessage());
                String renamed = renameExistingBackup(folder, name);
                outsideWorld.stdErrLine(String.format("Old backup renamed to '%s'.", renamed));
            } else {
                throw new CommandFailed("Backup failed: " + e.getMessage(), e);
            }
        }
    }
    if (!done) {
        outsideWorld.stdOutLine("Doing full backup...");
        try {
            backupService.doFullBackup(address.getHost(), address.getPort(), destination, ConsistencyCheck.NONE, config, timeout, false);
        } catch (Exception e) {
            throw new CommandFailed("Backup failed: " + e.getMessage(), e);
        }
    }
    if (doConsistencyCheck) {
        try {
            outsideWorld.stdOutLine("Doing consistency check...");
            ConsistencyCheckService.Result ccResult = consistencyCheckService.runFullConsistencyCheck(destination, config, ProgressMonitorFactory.textual(outsideWorld.errorStream()), FormattedLogProvider.toOutputStream(outsideWorld.outStream()), outsideWorld.fileSystem(), false, reportDir.toFile(), new CheckConsistencyConfig(checkGraph, checkIndexes, checkLabelScanStore, checkPropertyOwners));
            if (!ccResult.isSuccessful()) {
                throw new CommandFailed(String.format("Inconsistencies found. See '%s' for details.", ccResult.reportFile()), STATUS_CC_INCONSISTENT);
            }
        } catch (Throwable e) {
            if (e instanceof CommandFailed) {
                throw (CommandFailed) e;
            }
            throw new CommandFailed("Failed to do consistency check on backup: " + e.getMessage(), e, STATUS_CC_ERROR);
        }
    }
    outsideWorld.stdOutLine("Backup complete.");
}
Also used : Path(java.nio.file.Path) OptionalCanonicalPath(org.neo4j.commandline.arguments.common.OptionalCanonicalPath) MandatoryCanonicalPath(org.neo4j.commandline.arguments.common.MandatoryCanonicalPath) CheckConsistencyConfig(org.neo4j.consistency.checking.full.CheckConsistencyConfig) CheckConsistencyConfig(org.neo4j.consistency.checking.full.CheckConsistencyConfig) Config(org.neo4j.kernel.configuration.Config) HostnamePort(org.neo4j.helpers.HostnamePort) Converters.toHostnamePort(org.neo4j.kernel.impl.util.Converters.toHostnamePort) IOException(java.io.IOException) IncorrectUsage(org.neo4j.commandline.admin.IncorrectUsage) CommandFailed(org.neo4j.commandline.admin.CommandFailed) ConsistencyCheckService(org.neo4j.consistency.ConsistencyCheckService) File(java.io.File)

Example 4 with CommandFailed

use of org.neo4j.commandline.admin.CommandFailed in project neo4j by neo4j.

the class CheckConsistencyCommand method execute.

@Override
public void execute(String[] args) throws IncorrectUsage, CommandFailed {
    final String database;
    final boolean verbose;
    final Optional<Path> additionalConfigFile;
    final Path reportDir;
    final Optional<Path> backupPath;
    final boolean checkGraph;
    final boolean checkIndexes;
    final boolean checkLabelScanStore;
    final boolean checkPropertyOwners;
    try {
        database = arguments.parse("database", args);
        backupPath = arguments.parseOptionalPath("backup", args);
        verbose = arguments.parseBoolean("verbose", args);
        additionalConfigFile = arguments.parseOptionalPath("additional-config", args);
        reportDir = arguments.parseOptionalPath("report-dir", args).orElseThrow(() -> new IllegalArgumentException("report-dir must be a valid path"));
    } catch (IllegalArgumentException e) {
        throw new IncorrectUsage(e.getMessage());
    }
    if (backupPath.isPresent()) {
        if (arguments.has("database", args)) {
            throw new IncorrectUsage("Only one of '--database' and '--backup' can be specified.");
        }
        if (!backupPath.get().toFile().isDirectory()) {
            throw new CommandFailed(format("Specified backup should be a directory: %s", backupPath.get()));
        }
    }
    Config config = loadNeo4jConfig(homeDir, configDir, database, loadAdditionalConfig(additionalConfigFile));
    try {
        // We can remove the loading from config file in 4.0
        if (arguments.has(CHECK_GRAPH, args)) {
            checkGraph = arguments.parseBoolean(CHECK_GRAPH, args);
        } else {
            checkGraph = ConsistencyCheckSettings.consistency_check_graph.from(config);
        }
        if (arguments.has(CHECK_INDEXES, args)) {
            checkIndexes = arguments.parseBoolean(CHECK_INDEXES, args);
        } else {
            checkIndexes = ConsistencyCheckSettings.consistency_check_indexes.from(config);
        }
        if (arguments.has(CHECK_LABEL_SCAN_STORE, args)) {
            checkLabelScanStore = arguments.parseBoolean(CHECK_LABEL_SCAN_STORE, args);
        } else {
            checkLabelScanStore = ConsistencyCheckSettings.consistency_check_label_scan_store.from(config);
        }
        if (arguments.has(CHECK_PROPERTY_OWNERS, args)) {
            checkPropertyOwners = arguments.parseBoolean(CHECK_PROPERTY_OWNERS, args);
        } else {
            checkPropertyOwners = ConsistencyCheckSettings.consistency_check_property_owners.from(config);
        }
    } catch (IllegalArgumentException e) {
        throw new IncorrectUsage(e.getMessage());
    }
    try (FileSystemAbstraction fileSystem = new DefaultFileSystemAbstraction()) {
        File storeDir = backupPath.map(Path::toFile).orElse(config.get(database_path));
        checkDbState(storeDir, config);
        ConsistencyCheckService.Result consistencyCheckResult = consistencyCheckService.runFullConsistencyCheck(storeDir, config, ProgressMonitorFactory.textual(System.err), FormattedLogProvider.toOutputStream(System.out), fileSystem, verbose, reportDir.toFile(), new CheckConsistencyConfig(checkGraph, checkIndexes, checkLabelScanStore, checkPropertyOwners));
        if (!consistencyCheckResult.isSuccessful()) {
            throw new CommandFailed(format("Inconsistencies found. See '%s' for details.", consistencyCheckResult.reportFile()));
        }
    } catch (ConsistencyCheckIncompleteException | IOException e) {
        throw new CommandFailed("Consistency checking failed." + e.getMessage(), e);
    }
}
Also used : Path(java.nio.file.Path) OptionalCanonicalPath(org.neo4j.commandline.arguments.common.OptionalCanonicalPath) CheckConsistencyConfig(org.neo4j.consistency.checking.full.CheckConsistencyConfig) DefaultFileSystemAbstraction(org.neo4j.io.fs.DefaultFileSystemAbstraction) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) DefaultFileSystemAbstraction(org.neo4j.io.fs.DefaultFileSystemAbstraction) CheckConsistencyConfig(org.neo4j.consistency.checking.full.CheckConsistencyConfig) Config(org.neo4j.kernel.configuration.Config) ConsistencyCheckIncompleteException(org.neo4j.consistency.checking.full.ConsistencyCheckIncompleteException) IOException(java.io.IOException) IncorrectUsage(org.neo4j.commandline.admin.IncorrectUsage) CommandFailed(org.neo4j.commandline.admin.CommandFailed) File(java.io.File)

Example 5 with CommandFailed

use of org.neo4j.commandline.admin.CommandFailed in project neo4j by neo4j.

the class DumpCommand method execute.

@Override
public void execute(String[] args) throws IncorrectUsage, CommandFailed {
    String database = arguments.parse("database", args);
    Path archive = calculateArchive(database, arguments.parseMandatoryPath("to", args));
    Path databaseDirectory = canonicalPath(toDatabaseDirectory(database));
    try {
        Validators.CONTAINS_EXISTING_DATABASE.validate(databaseDirectory.toFile());
    } catch (IllegalArgumentException e) {
        throw new CommandFailed("database does not exist: " + database, e);
    }
    try (Closeable ignored = StoreLockChecker.check(databaseDirectory)) {
        dump(database, databaseDirectory, archive);
    } catch (StoreLockException e) {
        throw new CommandFailed("the database is in use -- stop Neo4j and try again", e);
    } catch (IOException e) {
        wrapIOException(e);
    } catch (CannotWriteException e) {
        throw new CommandFailed("you do not have permission to dump the database -- is Neo4j running as a " + "different user?", e);
    }
}
Also used : Util.canonicalPath(org.neo4j.commandline.Util.canonicalPath) Path(java.nio.file.Path) Closeable(java.io.Closeable) CommandFailed(org.neo4j.commandline.admin.CommandFailed) StoreLockException(org.neo4j.kernel.StoreLockException) IOException(java.io.IOException)

Aggregations

CommandFailed (org.neo4j.commandline.admin.CommandFailed)18 Test (org.junit.Test)9 Path (java.nio.file.Path)8 FileSystemAbstraction (org.neo4j.io.fs.FileSystemAbstraction)8 File (java.io.File)7 IOException (java.io.IOException)7 Config (org.neo4j.kernel.configuration.Config)7 DefaultFileSystemAbstraction (org.neo4j.io.fs.DefaultFileSystemAbstraction)6 IncorrectUsage (org.neo4j.commandline.admin.IncorrectUsage)5 CheckConsistencyConfig (org.neo4j.consistency.checking.full.CheckConsistencyConfig)3 StoreLocker (org.neo4j.kernel.internal.StoreLocker)3 Closeable (java.io.Closeable)2 OutsideWorld (org.neo4j.commandline.admin.OutsideWorld)2 MandatoryCanonicalPath (org.neo4j.commandline.arguments.common.MandatoryCanonicalPath)2 OptionalCanonicalPath (org.neo4j.commandline.arguments.common.OptionalCanonicalPath)2 StoreLockException (org.neo4j.kernel.StoreLockException)2 FileUserRepository (org.neo4j.server.security.auth.FileUserRepository)2 FileLock (java.nio.channels.FileLock)1 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)1 FileSystemException (java.nio.file.FileSystemException)1