-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
ConsistencyCheckServiceIntegrationTest.java
392 lines (338 loc) · 15 KB
/
ConsistencyCheckServiceIntegrationTest.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
* Copyright (c) 2002-2018 "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 org.apache.commons.lang3.StringUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.RuleChain;
import java.io.File;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.neo4j.consistency.ConsistencyCheckService.Result;
import org.neo4j.consistency.checking.GraphStoreFixture;
import org.neo4j.consistency.checking.full.ConsistencyCheckIncompleteException;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseBuilder;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.graphdb.schema.IndexDefinition;
import org.neo4j.helpers.progress.ProgressMonitorFactory;
import org.neo4j.io.fs.FileUtils;
import org.neo4j.kernel.api.exceptions.TransactionFailureException;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.configuration.Settings;
import org.neo4j.kernel.impl.storageengine.impl.recordstorage.RecordStorageEngine;
import org.neo4j.kernel.impl.store.NeoStores;
import org.neo4j.kernel.impl.store.RelationshipStore;
import org.neo4j.kernel.impl.store.record.NodeRecord;
import org.neo4j.kernel.impl.store.record.RecordLoad;
import org.neo4j.kernel.impl.store.record.RelationshipRecord;
import org.neo4j.kernel.internal.GraphDatabaseAPI;
import org.neo4j.logging.NullLogProvider;
import org.neo4j.test.TestGraphDatabaseFactory;
import org.neo4j.test.rule.TestDirectory;
import static java.lang.String.format;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.neo4j.helpers.collection.MapUtil.stringMap;
import static org.neo4j.test.Property.property;
import static org.neo4j.test.Property.set;
public class ConsistencyCheckServiceIntegrationTest
{
private final GraphStoreFixture fixture = new GraphStoreFixture( getRecordFormatName() )
{
@Override
protected void generateInitialData( GraphDatabaseService graphDb )
{
try ( org.neo4j.graphdb.Transaction tx = graphDb.beginTx() )
{
Node node1 = set( graphDb.createNode() );
Node node2 = set( graphDb.createNode(), property( "key", "exampleValue" ) );
node1.createRelationshipTo( node2, RelationshipType.withName( "C" ) );
tx.success();
}
}
};
private final TestDirectory testDirectory = TestDirectory.testDirectory();
@Rule
public final RuleChain chain = RuleChain.outerRule( testDirectory ).around( fixture );
@Test
public void reportNotUsedRelationshipReferencedInChain() throws Exception
{
prepareDbWithDeletedRelationshipPartOfTheChain();
Date timestamp = new Date();
ConsistencyCheckService service = new ConsistencyCheckService( timestamp );
Config configuration = Config.defaults( settings() );
ConsistencyCheckService.Result result = runFullConsistencyCheck( service, configuration );
assertFalse( result.isSuccessful() );
File reportFile = result.reportFile();
assertTrue( "Consistency check report file should be generated.", reportFile.exists() );
assertThat( "Expected to see report about not deleted relationship record present as part of a chain",
Files.readAllLines( reportFile.toPath() ).toString(),
containsString( "The relationship record is not in use, but referenced from relationships chain.") );
}
@Test
public void shouldSucceedIfStoreIsConsistent() throws Exception
{
// given
Date timestamp = new Date();
ConsistencyCheckService service = new ConsistencyCheckService( timestamp );
Config configuration = Config.defaults( settings() );
// when
ConsistencyCheckService.Result result = runFullConsistencyCheck( service, configuration );
// then
assertTrue( result.isSuccessful() );
File reportFile = result.reportFile();
assertFalse( "Unexpected generation of consistency check report file: " + reportFile, reportFile.exists() );
}
@Test
public void shouldFailIfTheStoreInNotConsistent() throws Exception
{
// given
breakNodeStore();
Date timestamp = new Date();
ConsistencyCheckService service = new ConsistencyCheckService( timestamp );
String logsDir = testDirectory.directory().getPath();
Config configuration = Config.defaults(
settings( GraphDatabaseSettings.logs_directory.name(), logsDir ) );
// when
ConsistencyCheckService.Result result = runFullConsistencyCheck( service, configuration );
// then
assertFalse( result.isSuccessful() );
String reportFile = format( "inconsistencies-%s.report",
new SimpleDateFormat( "yyyy-MM-dd.HH.mm.ss" ).format( timestamp ) );
assertEquals( new File( logsDir, reportFile ), result.reportFile() );
assertTrue( "Inconsistency report file not generated", result.reportFile().exists() );
}
@Test
public void shouldNotReportDuplicateForHugeLongValues() throws Exception
{
// given
ConsistencyCheckService service = new ConsistencyCheckService();
Config configuration = Config.defaults( settings() );
GraphDatabaseService db = new TestGraphDatabaseFactory().newEmbeddedDatabaseBuilder( testDirectory.graphDbDir() )
.setConfig( GraphDatabaseSettings.record_format, getRecordFormatName() )
.setConfig( "dbms.backup.enabled", "false" )
.newGraphDatabase();
String propertyKey = "itemId";
Label label = Label.label( "Item" );
try ( Transaction tx = db.beginTx() )
{
db.schema().constraintFor( label ).assertPropertyIsUnique( propertyKey ).create();
tx.success();
}
try ( Transaction tx = db.beginTx() )
{
set( db.createNode( label ), property( propertyKey, 973305894188596880L ) );
set( db.createNode( label ), property( propertyKey, 973305894188596864L ) );
tx.success();
}
db.shutdown();
// when
Result result = runFullConsistencyCheck( service, configuration );
// then
assertTrue( result.isSuccessful() );
}
@Test
public void shouldAllowGraphCheckDisabled() throws ConsistencyCheckIncompleteException
{
GraphDatabaseService gds = getGraphDatabaseService();
try ( Transaction tx = gds.beginTx() )
{
gds.createNode();
tx.success();
}
gds.shutdown();
ConsistencyCheckService service = new ConsistencyCheckService();
Config configuration = Config.defaults(
settings( ConsistencyCheckSettings.consistency_check_graph.name(), Settings.FALSE ) );
// when
Result result = runFullConsistencyCheck( service, configuration );
// then
assertTrue( result.isSuccessful() );
}
@Test
public void shouldReportMissingSchemaIndex() throws Exception
{
// given
File storeDir = testDirectory.absolutePath();
GraphDatabaseService gds = getGraphDatabaseService( storeDir );
Label label = Label.label( "label" );
String propKey = "propKey";
createIndex( gds, label, propKey );
gds.shutdown();
// when
File schemaDir = findFile( "schema", storeDir );
FileUtils.deleteRecursively( schemaDir );
ConsistencyCheckService service = new ConsistencyCheckService();
Config configuration = Config.defaults( settings() );
Result result = runFullConsistencyCheck( service, configuration, storeDir );
// then
assertTrue( result.isSuccessful() );
File reportFile = result.reportFile();
assertTrue( "Consistency check report file should be generated.", reportFile.exists() );
assertThat( "Expected to see report about schema index not being online",
Files.readAllLines( reportFile.toPath() ).toString(), allOf(
containsString( "schema rule" ),
containsString( "not online" )
) );
}
@Test
public void oldLuceneSchemaIndexShouldBeConsideredConsistentWithFusionProvider() throws Exception
{
File storeDir = testDirectory.graphDbDir();
String enableNativeIndex = GraphDatabaseSettings.enable_native_schema_index.name();
Label label = Label.label( "label" );
String propKey = "propKey";
// Given a lucene index
GraphDatabaseService db = getGraphDatabaseService( storeDir, enableNativeIndex, Settings.FALSE );
createIndex( db, label, propKey );
try ( Transaction tx = db.beginTx() )
{
db.createNode( label ).setProperty( propKey, 1 );
db.createNode( label ).setProperty( propKey, "string" );
tx.success();
}
db.shutdown();
ConsistencyCheckService service = new ConsistencyCheckService();
Config configuration =
Config.defaults( settings( enableNativeIndex, Settings.TRUE ) );
Result result = runFullConsistencyCheck( service, configuration, storeDir );
assertTrue( result.isSuccessful() );
}
private void createIndex( GraphDatabaseService gds, Label label, String propKey )
{
IndexDefinition indexDefinition;
try ( Transaction tx = gds.beginTx() )
{
indexDefinition = gds.schema().indexFor( label ).on( propKey ).create();
tx.success();
}
try ( Transaction tx = gds.beginTx() )
{
gds.schema().awaitIndexOnline( indexDefinition, 1, TimeUnit.MINUTES );
tx.success();
}
}
private File findFile( String targetFile, File directory )
{
File file = new File( directory, targetFile );
if ( !file.exists() )
{
fail( "Could not find file " + targetFile );
}
return file;
}
private GraphDatabaseService getGraphDatabaseService()
{
return getGraphDatabaseService( testDirectory.absolutePath() );
}
private GraphDatabaseService getGraphDatabaseService( File storeDir )
{
return getGraphDatabaseService( storeDir, new String[0] );
}
private GraphDatabaseService getGraphDatabaseService( File storeDir, String... settings )
{
GraphDatabaseBuilder builder = new TestGraphDatabaseFactory().newEmbeddedDatabaseBuilder( storeDir );
builder.setConfig( settings( settings ) );
return builder.newGraphDatabase();
}
private void prepareDbWithDeletedRelationshipPartOfTheChain()
{
GraphDatabaseAPI db = (GraphDatabaseAPI) new TestGraphDatabaseFactory().newEmbeddedDatabaseBuilder( testDirectory.graphDbDir() )
.setConfig( GraphDatabaseSettings.record_format, getRecordFormatName() )
.setConfig( "dbms.backup.enabled", "false" )
.newGraphDatabase();
try
{
RelationshipType relationshipType = RelationshipType.withName( "testRelationshipType" );
try ( Transaction tx = db.beginTx() )
{
Node node1 = set( db.createNode() );
Node node2 = set( db.createNode(), property( "key", "value" ) );
node1.createRelationshipTo( node2, relationshipType );
node1.createRelationshipTo( node2, relationshipType );
node1.createRelationshipTo( node2, relationshipType );
node1.createRelationshipTo( node2, relationshipType );
node1.createRelationshipTo( node2, relationshipType );
node1.createRelationshipTo( node2, relationshipType );
tx.success();
}
RecordStorageEngine recordStorageEngine = db.getDependencyResolver().resolveDependency( RecordStorageEngine.class );
NeoStores neoStores = recordStorageEngine.testAccessNeoStores();
RelationshipStore relationshipStore = neoStores.getRelationshipStore();
RelationshipRecord relationshipRecord = new RelationshipRecord( -1 );
RelationshipRecord record = relationshipStore.getRecord( 4, relationshipRecord, RecordLoad.FORCE );
record.setInUse( false );
relationshipStore.updateRecord( relationshipRecord );
}
finally
{
db.shutdown();
}
}
protected Map<String,String> settings( String... strings )
{
Map<String, String> defaults = new HashMap<>();
defaults.put( GraphDatabaseSettings.pagecache_memory.name(), "8m" );
defaults.put( GraphDatabaseSettings.record_format.name(), getRecordFormatName() );
defaults.put( "dbms.backup.enabled", "false" );
return stringMap( defaults, strings );
}
private void breakNodeStore() throws TransactionFailureException
{
fixture.apply( new GraphStoreFixture.Transaction()
{
@Override
protected void transactionData( GraphStoreFixture.TransactionDataBuilder tx,
GraphStoreFixture.IdGenerator next )
{
tx.create( new NodeRecord( next.node(), false, next.relationship(), -1 ) );
}
} );
}
private Result runFullConsistencyCheck( ConsistencyCheckService service, Config configuration )
throws ConsistencyCheckIncompleteException
{
return runFullConsistencyCheck( service, configuration, fixture.directory() );
}
private Result runFullConsistencyCheck( ConsistencyCheckService service, Config configuration, File storeDir )
throws ConsistencyCheckIncompleteException
{
return service.runFullConsistencyCheck( storeDir,
configuration, ProgressMonitorFactory.NONE, NullLogProvider.getInstance(), false );
}
protected String getRecordFormatName()
{
return StringUtils.EMPTY;
}
}