-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
CheckConsistencyCommand.java
254 lines (234 loc) · 10.8 KB
/
CheckConsistencyCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.consistency;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import org.neo4j.commandline.admin.AdminCommand;
import org.neo4j.commandline.admin.CommandFailed;
import org.neo4j.commandline.admin.IncorrectUsage;
import org.neo4j.commandline.admin.OutsideWorld;
import org.neo4j.commandline.arguments.Arguments;
import org.neo4j.commandline.arguments.OptionalBooleanArg;
import org.neo4j.commandline.arguments.common.OptionalCanonicalPath;
import org.neo4j.consistency.checking.full.CheckConsistencyConfig;
import org.neo4j.consistency.checking.full.ConsistencyCheckIncompleteException;
import org.neo4j.dbms.DatabaseManagementSystemSettings;
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.ConfigurableStandalonePageCacheFactory;
import org.neo4j.kernel.impl.recovery.RecoveryRequiredChecker;
import org.neo4j.logging.FormattedLogProvider;
import static java.lang.String.format;
import static org.neo4j.dbms.DatabaseManagementSystemSettings.database_path;
public class CheckConsistencyCommand implements AdminCommand
{
public static final String CHECK_GRAPH = "check-graph";
public static final String CHECK_INDEXES = "check-indexes";
public static final String CHECK_LABEL_SCAN_STORE = "check-label-scan-store";
public static final String CHECK_PROPERTY_OWNERS = "check-property-owners";
private static final Arguments arguments = new Arguments()
.withDatabase()
.withArgument( new OptionalCanonicalPath( "backup", "/path/to/backup", "",
"Path to backup to check consistency of. Cannot be used together with --database." ) )
.withArgument( new OptionalBooleanArg( "verbose", false, "Enable verbose output." ) )
.withArgument( new OptionalCanonicalPath( "report-dir", "directory", ".",
"Directory to write report file in." ) )
.withArgument( new OptionalCanonicalPath( "additional-config", "config-file-path", "",
"Configuration file to supply additional configuration in. This argument is DEPRECATED." ) )
.withArgument( new OptionalBooleanArg( CHECK_GRAPH, true,
"Perform checks between nodes, relationships, properties, types and tokens." ) )
.withArgument( new OptionalBooleanArg( CHECK_INDEXES, true,
"Perform checks on indexes." ) )
.withArgument( new OptionalBooleanArg( CHECK_LABEL_SCAN_STORE, true,
"Perform checks on the label scan store." ) )
.withArgument( new OptionalBooleanArg( CHECK_PROPERTY_OWNERS, false,
"Perform additional checks on property ownership. This check is *very* expensive in time and " +
"memory." ) );
private final Path homeDir;
private final Path configDir;
private final OutsideWorld outsideWorld;
private final ConsistencyCheckService consistencyCheckService;
public CheckConsistencyCommand( Path homeDir, Path configDir, OutsideWorld outsideWorld )
{
this( homeDir, configDir, outsideWorld, new ConsistencyCheckService() );
}
public CheckConsistencyCommand( Path homeDir, Path configDir, OutsideWorld outsideWorld,
ConsistencyCheckService consistencyCheckService )
{
this.homeDir = homeDir;
this.configDir = configDir;
this.outsideWorld = outsideWorld;
this.consistencyCheckService = consistencyCheckService;
}
@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( args ).get( "database" );
backupPath = arguments.getOptionalPath( "backup" );
verbose = arguments.getBoolean( "verbose" );
additionalConfigFile = arguments.getOptionalPath( "additional-config" );
reportDir = arguments.getOptionalPath( "report-dir" )
.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" ) )
{
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 ) )
{
checkGraph = arguments.getBoolean( CHECK_GRAPH );
}
else
{
checkGraph = config.get( ConsistencyCheckSettings.consistency_check_graph );
}
if ( arguments.has( CHECK_INDEXES ) )
{
checkIndexes = arguments.getBoolean( CHECK_INDEXES );
}
else
{
checkIndexes = config.get( ConsistencyCheckSettings.consistency_check_indexes );
}
if ( arguments.has( CHECK_LABEL_SCAN_STORE ) )
{
checkLabelScanStore = arguments.getBoolean( CHECK_LABEL_SCAN_STORE );
}
else
{
checkLabelScanStore = config.get( ConsistencyCheckSettings.consistency_check_label_scan_store );
}
if ( arguments.has( CHECK_PROPERTY_OWNERS ) )
{
checkPropertyOwners = arguments.getBoolean( CHECK_PROPERTY_OWNERS );
}
else
{
checkPropertyOwners = config.get( ConsistencyCheckSettings.consistency_check_property_owners );
}
}
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 );
}
}
private Map<String,String> loadAdditionalConfig( Optional<Path> additionalConfigFile )
{
if ( additionalConfigFile.isPresent() )
{
try
{
return MapUtil.load( additionalConfigFile.get().toFile() );
}
catch ( IOException e )
{
throw new IllegalArgumentException(
String.format( "Could not read configuration file [%s]", additionalConfigFile ), e );
}
}
return new HashMap<>();
}
private void checkDbState( File storeDir, Config additionalConfiguration ) throws CommandFailed
{
try ( FileSystemAbstraction fileSystem = new DefaultFileSystemAbstraction();
PageCache pageCache = ConfigurableStandalonePageCacheFactory
.createPageCache( fileSystem, additionalConfiguration ) )
{
if ( new RecoveryRequiredChecker( fileSystem, 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 )
{
additionalConfig.put( DatabaseManagementSystemSettings.active_database.name(), databaseName );
return Config.fromFile( configDir.resolve( Config.DEFAULT_CONFIG_FILE_NAME ) ).withHome( homeDir ).withConnectorsDisabled()
.withSettings( additionalConfig ).build();
}
public static Arguments arguments()
{
return arguments;
}
}