Skip to content

Commit

Permalink
Add an implementation of checkDbState from the original tool.
Browse files Browse the repository at this point in the history
  • Loading branch information
srbaker committed Sep 13, 2016
1 parent ee3c76b commit 2f51bf6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
Expand Up @@ -36,10 +36,15 @@
import org.neo4j.dbms.DatabaseManagementSystemSettings;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.helpers.Args;
import org.neo4j.helpers.Strings;
import org.neo4j.helpers.collection.MapUtil;
import org.neo4j.helpers.progress.ProgressMonitorFactory;
import org.neo4j.io.fs.DefaultFileSystemAbstraction;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.io.pagecache.PageCache;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.impl.pagecache.StandalonePageCacheFactory;
import org.neo4j.kernel.impl.recovery.RecoveryRequiredChecker;
import org.neo4j.kernel.impl.util.Converters;
import org.neo4j.logging.FormattedLogProvider;
import org.neo4j.server.configuration.ConfigLoader;
Expand Down Expand Up @@ -70,24 +75,29 @@ public String description()
@Override
public AdminCommand create( Path homeDir, Path configDir, OutsideWorld outsideWorld )
{
return new CheckConsistencyCommand( homeDir, configDir );
return new CheckConsistencyCommand( homeDir, configDir, outsideWorld );
}
}

private final Path homeDir;
private final Path configDir;
private final OutsideWorld outsideWorld;
private final ConsistencyCheckService consistencyCheckService;
private final FileSystemAbstraction fileSystemAbstraction;

public CheckConsistencyCommand( Path homeDir, Path configDir )
public CheckConsistencyCommand( Path homeDir, Path configDir, OutsideWorld outsideWorld )
{
this( homeDir, configDir, new ConsistencyCheckService() );
this( homeDir, configDir, outsideWorld, new ConsistencyCheckService() );
}

public CheckConsistencyCommand( Path homeDir, Path configDir, ConsistencyCheckService consistencyCheckService )
public CheckConsistencyCommand( Path homeDir, Path configDir, OutsideWorld outsideWorld, ConsistencyCheckService
consistencyCheckService )
{
this.homeDir = homeDir;
this.configDir = configDir;
this.outsideWorld = outsideWorld;
this.consistencyCheckService = consistencyCheckService;
this.fileSystemAbstraction = new DefaultFileSystemAbstraction();
}

@Override
Expand All @@ -114,9 +124,11 @@ public void execute( String[] args ) throws IncorrectUsage, CommandFailed

try
{
consistencyCheckService.runFullConsistencyCheck( config.get( database_path ), config,
File storeDir = config.get( database_path );
checkDbState( storeDir, config );
consistencyCheckService.runFullConsistencyCheck( storeDir, config,
ProgressMonitorFactory.textual( System.err ), FormattedLogProvider.toOutputStream( System.out ),
new DefaultFileSystemAbstraction(), verbose );
this.fileSystemAbstraction, verbose );
}
catch ( ConsistencyCheckIncompleteException | IOException e )
{
Expand All @@ -142,6 +154,26 @@ private Map<String,String> loadAdditionalConfig( File additionalConfigFile )
}
}

private void checkDbState( File storeDir, Config additionalConfiguration ) throws CommandFailed
{
try ( PageCache pageCache = StandalonePageCacheFactory.createPageCache( this.fileSystemAbstraction,
additionalConfiguration ) )
{
if ( new RecoveryRequiredChecker( this.fileSystemAbstraction, pageCache ).isRecoveryRequiredAt( storeDir ) )
{
throw new CommandFailed( Strings.joinAsLines(
"Active logical log detected, this might be a source of inconsistencies.",
"Please recover database before running the consistency check.",
"To perform recovery please start database and perform clean shutdown." ) );
}
}
catch ( IOException e )
{
outsideWorld.stdErrLine( "Failure when checking for recovery state: '%s', continuing as normal.%n" + e
.getMessage() );
}
}

private static Config loadNeo4jConfig( Path homeDir, Path configDir, String databaseName,
Map<String,String> additionalConfig )
{
Expand Down
Expand Up @@ -27,6 +27,7 @@
import java.nio.file.Path;

import org.neo4j.commandline.admin.IncorrectUsage;
import org.neo4j.commandline.admin.OutsideWorld;
import org.neo4j.helpers.progress.ProgressMonitorFactory;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.kernel.configuration.Config;
Expand Down Expand Up @@ -56,9 +57,10 @@ public class CheckConsistencyCommandTest
@Test
public void requiresDatabaseArgument() throws Exception
{
OutsideWorld outsideWorld = mock( OutsideWorld.class );
CheckConsistencyCommand checkConsistencyCommand =
new CheckConsistencyCommand( testDir.directory( "home" ).toPath(),
testDir.directory( "conf" ).toPath() );
testDir.directory( "conf" ).toPath(), outsideWorld );

String[] arguments = {""};
try
Expand All @@ -78,8 +80,10 @@ public void runsConsistencyChecker() 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(), consistencyCheckService );
new CheckConsistencyCommand( homeDir, testDir.directory( "conf" ).toPath(), outsideWorld,
consistencyCheckService );

File databasePath = new File( homeDir.toFile(), "data/databases/mydb" );
checkConsistencyCommand.execute( new String[]{"--database=mydb"} );
Expand All @@ -95,8 +99,10 @@ public void enablesVerbosity() 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(), consistencyCheckService );
new CheckConsistencyCommand( homeDir, testDir.directory( "conf" ).toPath(), outsideWorld,
consistencyCheckService );

File databasePath = new File( homeDir.toFile(), "data/databases/mydb" );

Expand Down

0 comments on commit 2f51bf6

Please sign in to comment.