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()));
}
}
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);
}
}
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.");
}
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);
}
}
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);
}
}
Aggregations