-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
StoreUpgraderTest.java
482 lines (423 loc) · 21 KB
/
StoreUpgraderTest.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/*
* Copyright (c) 2002-2016 "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 upgrade;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.mockito.Mockito;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.neo4j.consistency.checking.full.ConsistencyCheckIncompleteException;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.helpers.collection.MapUtil;
import org.neo4j.io.fs.DefaultFileSystemAbstraction;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.io.pagecache.PageCache;
import org.neo4j.kernel.api.index.SchemaIndexProvider;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.impl.api.index.inmemory.InMemoryIndexProvider;
import org.neo4j.kernel.impl.api.scan.InMemoryLabelScanStore;
import org.neo4j.kernel.impl.api.scan.LabelScanStoreProvider;
import org.neo4j.kernel.impl.logging.NullLogService;
import org.neo4j.kernel.impl.logging.StoreLogService;
import org.neo4j.kernel.impl.store.MetaDataStore;
import org.neo4j.kernel.impl.store.NeoStores;
import org.neo4j.kernel.impl.store.StoreFactory;
import org.neo4j.kernel.impl.store.format.RecordFormats;
import org.neo4j.kernel.impl.store.format.standard.StandardV2_0;
import org.neo4j.kernel.impl.store.format.standard.StandardV2_1;
import org.neo4j.kernel.impl.store.format.standard.StandardV2_2;
import org.neo4j.kernel.impl.store.format.standard.StandardV2_3;
import org.neo4j.kernel.impl.store.format.standard.StandardV3_0;
import org.neo4j.kernel.impl.storemigration.StoreMigrationParticipant;
import org.neo4j.kernel.impl.storemigration.StoreUpgrader;
import org.neo4j.kernel.impl.storemigration.StoreUpgrader.UnableToUpgradeException;
import org.neo4j.kernel.impl.storemigration.StoreVersionCheck;
import org.neo4j.kernel.impl.storemigration.UpgradableDatabase;
import org.neo4j.kernel.impl.storemigration.UpgradeNotAllowedByConfigurationException;
import org.neo4j.kernel.impl.storemigration.legacystore.LegacyStoreVersionCheck;
import org.neo4j.kernel.impl.storemigration.monitoring.MigrationProgressMonitor;
import org.neo4j.kernel.impl.storemigration.monitoring.SilentMigrationProgressMonitor;
import org.neo4j.kernel.impl.storemigration.participant.AbstractStoreMigrationParticipant;
import org.neo4j.kernel.impl.storemigration.participant.SchemaIndexMigrator;
import org.neo4j.kernel.impl.storemigration.participant.StoreMigrator;
import org.neo4j.logging.NullLogProvider;
import org.neo4j.test.rule.PageCacheRule;
import org.neo4j.test.rule.TargetDirectory;
import org.neo4j.test.rule.TargetDirectory.TestDirectory;
import static java.util.concurrent.TimeUnit.MINUTES;
import static org.hamcrest.Matchers.emptyCollectionOf;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.verify;
import static org.neo4j.consistency.store.StoreAssertions.assertConsistentStore;
import static org.neo4j.kernel.impl.storemigration.MigrationTestUtils.allLegacyStoreFilesHaveVersion;
import static org.neo4j.kernel.impl.storemigration.MigrationTestUtils.allStoreFilesHaveNoTrailer;
import static org.neo4j.kernel.impl.storemigration.MigrationTestUtils.changeVersionNumber;
import static org.neo4j.kernel.impl.storemigration.MigrationTestUtils.containsAnyStoreFiles;
import static org.neo4j.kernel.impl.storemigration.MigrationTestUtils.isolatedMigrationDirectoryOf;
import static org.neo4j.kernel.impl.storemigration.MigrationTestUtils.prepareSampleLegacyDatabase;
import static org.neo4j.kernel.impl.storemigration.MigrationTestUtils.removeCheckPointFromTxLog;
import static org.neo4j.kernel.impl.storemigration.MigrationTestUtils.truncateAllFiles;
import static org.neo4j.kernel.impl.storemigration.MigrationTestUtils.truncateFile;
import static org.neo4j.kernel.impl.storemigration.MigrationTestUtils.verifyFilesHaveSameContent;
@RunWith(Parameterized.class)
public class StoreUpgraderTest
{
@Rule
public final TestDirectory directory = TargetDirectory.testDirForTest( getClass() );
@Rule
public final PageCacheRule pageCacheRule = new PageCacheRule();
private File dbDirectory;
private final FileSystemAbstraction fileSystem = new DefaultFileSystemAbstraction();
private StoreVersionCheck check;
private final String version;
private final SchemaIndexProvider schemaIndexProvider = new InMemoryIndexProvider();
private final LabelScanStoreProvider labelScanStoreProvider = new LabelScanStoreProvider( new
InMemoryLabelScanStore(), 2 );
private final Config allowMigrateConfig = new Config( MapUtil.stringMap( GraphDatabaseSettings
.allow_store_upgrade.name(), "true" ) );
public StoreUpgraderTest( String version )
{
this.version = version;
}
@Parameterized.Parameters( name = "{0}" )
public static Collection<String> versions()
{
return Arrays.asList(
StandardV2_0.STORE_VERSION,
StandardV2_1.STORE_VERSION,
StandardV2_2.STORE_VERSION,
StandardV2_3.STORE_VERSION
);
}
@Before
public void prepareDb() throws IOException
{
dbDirectory = directory.directory( "db_" + version );
File prepareDirectory = directory.directory( "prepare_" + version );
prepareSampleLegacyDatabase( version, fileSystem, dbDirectory, prepareDirectory );
}
@Test
public void shouldUpgradeAnOldFormatStore() throws IOException, ConsistencyCheckIncompleteException
{
// Given
PageCache pageCache = pageCacheRule.getPageCache( fileSystem );
UpgradableDatabase upgradableDatabase = new UpgradableDatabase( fileSystem,
new StoreVersionCheck( pageCache ), new LegacyStoreVersionCheck( fileSystem ),
getRecordFormats() );
assertEquals( !StandardV2_3.STORE_VERSION.equals( version ),
allLegacyStoreFilesHaveVersion( fileSystem, dbDirectory, version ) );
// When
newUpgrader( upgradableDatabase, pageCache ).migrateIfNeeded( dbDirectory );
// Then
assertCorrectStoreVersion( getRecordFormats().storeVersion(), check, dbDirectory );
assertTrue( allStoreFilesHaveNoTrailer( fileSystem, dbDirectory ) );
// We leave logical logs in place since the new version can read the old
assertFalse( containsAnyStoreFiles( fileSystem, isolatedMigrationDirectoryOf( dbDirectory ) ) );
assertConsistentStore( dbDirectory );
}
@Test
public void shouldHaltUpgradeIfUpgradeConfigurationVetoesTheProcess()
{
PageCache pageCache = pageCacheRule.getPageCache( fileSystem );
Config deniedMigrationConfig = new Config( MapUtil.stringMap( GraphDatabaseSettings
.allow_store_upgrade.name(), "false" ) );
UpgradableDatabase upgradableDatabase = new UpgradableDatabase( fileSystem,
new StoreVersionCheck( pageCache ), new LegacyStoreVersionCheck( fileSystem ),
getRecordFormats() );
try
{
newUpgrader( upgradableDatabase, deniedMigrationConfig, pageCache ).migrateIfNeeded( dbDirectory );
fail( "Should throw exception" );
}
catch ( UpgradeNotAllowedByConfigurationException e )
{
// expected
}
}
@Test
public void shouldLeaveAllFilesUntouchedIfWrongVersionNumberFound()
throws IOException
{
Assume.assumeFalse( StandardV2_3.STORE_VERSION.equals( version ) );
File comparisonDirectory = new File( "target/" + StoreUpgraderTest.class.getSimpleName()
+ "shouldLeaveAllFilesUntouchedIfWrongVersionNumberFound-comparison" );
changeVersionNumber( fileSystem, new File( dbDirectory, "neostore.nodestore.db" ), "v0.9.5" );
fileSystem.deleteRecursively( comparisonDirectory );
fileSystem.copyRecursively( dbDirectory, comparisonDirectory );
PageCache pageCache = pageCacheRule.getPageCache( fileSystem );
UpgradableDatabase upgradableDatabase = new UpgradableDatabase( fileSystem,
new StoreVersionCheck( pageCache ), new LegacyStoreVersionCheck( fileSystem ),
getRecordFormats() );
try
{
newUpgrader( upgradableDatabase, pageCache ).migrateIfNeeded( dbDirectory );
fail( "Should throw exception" );
}
catch ( StoreUpgrader.UnexpectedUpgradingStoreVersionException e )
{
// expected
}
verifyFilesHaveSameContent( fileSystem, comparisonDirectory, dbDirectory );
}
@Test
public void shouldRefuseToUpgradeIfAnyOfTheStoresWeNotShutDownCleanly()
throws IOException
{
File comparisonDirectory = new File( "target/" + StoreUpgraderTest.class.getSimpleName()
+ "shouldRefuseToUpgradeIfAnyOfTheStoresWeNotShutDownCleanly-comparison" );
makeDbNotCleanlyShutdown( false );
fileSystem.deleteRecursively( comparisonDirectory );
fileSystem.copyRecursively( dbDirectory, comparisonDirectory );
PageCache pageCache = pageCacheRule.getPageCache( fileSystem );
UpgradableDatabase upgradableDatabase = new UpgradableDatabase( fileSystem,
new StoreVersionCheck( pageCache ), new LegacyStoreVersionCheck( fileSystem ),
getRecordFormats() );
try
{
newUpgrader( upgradableDatabase, pageCache ).migrateIfNeeded( dbDirectory );
fail( "Should throw exception" );
}
catch ( StoreUpgrader.UnableToUpgradeException e )
{
// expected
}
verifyFilesHaveSameContent( fileSystem, comparisonDirectory, dbDirectory );
}
@Test
public void shouldRefuseToUpgradeIfAllOfTheStoresWereNotShutDownCleanly()
throws IOException
{
File comparisonDirectory = new File( "target/" + StoreUpgraderTest.class.getSimpleName()
+ "shouldRefuseToUpgradeIfAllOfTheStoresWeNotShutDownCleanly-comparison" );
makeDbNotCleanlyShutdown( true );
fileSystem.deleteRecursively( comparisonDirectory );
fileSystem.copyRecursively( dbDirectory, comparisonDirectory );
PageCache pageCache = pageCacheRule.getPageCache( fileSystem );
UpgradableDatabase upgradableDatabase = new UpgradableDatabase( fileSystem,
new StoreVersionCheck( pageCache ), new LegacyStoreVersionCheck( fileSystem ),
getRecordFormats() );
try
{
newUpgrader( upgradableDatabase, pageCache ).migrateIfNeeded( dbDirectory );
fail( "Should throw exception" );
}
catch ( StoreUpgrader.UnableToUpgradeException e )
{
// expected
}
verifyFilesHaveSameContent( fileSystem, comparisonDirectory, dbDirectory );
}
@Test
public void shouldContinueMovingFilesIfUpgradeCancelledWhileMoving() throws Exception
{
PageCache pageCache = pageCacheRule.getPageCache( fileSystem );
UpgradableDatabase upgradableDatabase = new UpgradableDatabase( fileSystem,
new StoreVersionCheck( pageCache ), new LegacyStoreVersionCheck( fileSystem ),
getRecordFormats() );
String versionToMigrateTo = upgradableDatabase.currentVersion();
String versionToMigrateFrom = upgradableDatabase.checkUpgradeable( dbDirectory ).storeVersion();
// GIVEN
{
StoreUpgrader upgrader = newUpgrader( upgradableDatabase, allowMigrateConfig, pageCache );
String failureMessage = "Just failing";
upgrader.addParticipant( participantThatWillFailWhenMoving( failureMessage ) );
// WHEN
try
{
upgrader.migrateIfNeeded( dbDirectory );
fail( "should have thrown" );
}
catch ( UnableToUpgradeException e )
{ // THEN
assertTrue( e.getCause() instanceof IOException );
assertEquals( failureMessage, e.getCause().getMessage() );
}
}
// AND WHEN
{
StoreUpgrader upgrader = newUpgrader( upgradableDatabase, pageCache );
StoreMigrationParticipant observingParticipant = Mockito.mock( StoreMigrationParticipant.class );
upgrader.addParticipant( observingParticipant );
upgrader.migrateIfNeeded( dbDirectory );
// THEN
verify( observingParticipant, Mockito.times( 0 ) ).migrate( any( File.class ), any( File.class ),
any( MigrationProgressMonitor.Section.class ), eq( versionToMigrateFrom ), eq( versionToMigrateTo ) );
verify( observingParticipant, Mockito.times( 1 ) ).
moveMigratedFiles( any( File.class ), any( File.class ), eq( versionToMigrateFrom ),
eq( versionToMigrateTo ) );
verify( observingParticipant, Mockito.times( 1 ) ).cleanup( any( File.class ) );
}
}
@Test
public void upgradedNeoStoreShouldHaveNewUpgradeTimeAndUpgradeId() throws Exception
{
// Given
fileSystem.deleteFile( new File( dbDirectory, StoreLogService.INTERNAL_LOG_NAME ) );
PageCache pageCache = pageCacheRule.getPageCache( fileSystem );
UpgradableDatabase upgradableDatabase = new UpgradableDatabase( fileSystem,
new StoreVersionCheck( pageCache ), new LegacyStoreVersionCheck( fileSystem ),
getRecordFormats() );
// When
newUpgrader( upgradableDatabase, allowMigrateConfig, pageCache ).migrateIfNeeded( dbDirectory );
// Then
StoreFactory factory = new StoreFactory( dbDirectory, pageCache, fileSystem, NullLogProvider.getInstance() );
try ( NeoStores neoStores = factory.openAllNeoStores() )
{
assertThat( neoStores.getMetaDataStore().getUpgradeTransaction(),
equalTo( neoStores.getMetaDataStore().getLastCommittedTransaction() ) );
assertThat( neoStores.getMetaDataStore().getUpgradeTime(),
not( equalTo( MetaDataStore.FIELD_NOT_INITIALIZED ) ) );
long minuteAgo = System.currentTimeMillis() - MINUTES.toMillis( 1 );
assertThat( neoStores.getMetaDataStore().getUpgradeTime(), greaterThan( minuteAgo ) );
}
}
@Test
public void upgradeShouldNotLeaveLeftoverAndMigrationDirs() throws Exception
{
// Given
fileSystem.deleteFile( new File( dbDirectory, StoreLogService.INTERNAL_LOG_NAME ) );
PageCache pageCache = pageCacheRule.getPageCache( fileSystem );
UpgradableDatabase upgradableDatabase = new UpgradableDatabase( fileSystem,
new StoreVersionCheck( pageCache ), new LegacyStoreVersionCheck( fileSystem ),
getRecordFormats() );
// When
newUpgrader( upgradableDatabase, allowMigrateConfig, pageCache ).migrateIfNeeded( dbDirectory );
// Then
assertThat( migrationHelperDirs(), is( emptyCollectionOf( File.class ) ) );
}
@Test
public void upgraderShouldCleanupLegacyLeftoverAndMigrationDirs() throws Exception
{
// Given
fileSystem.deleteFile( new File( dbDirectory, StoreLogService.INTERNAL_LOG_NAME ) );
fileSystem.mkdir( new File( dbDirectory, StoreUpgrader.MIGRATION_DIRECTORY ) );
fileSystem.mkdir( new File( dbDirectory, StoreUpgrader.MIGRATION_LEFT_OVERS_DIRECTORY ) );
fileSystem.mkdir( new File( dbDirectory, StoreUpgrader.MIGRATION_LEFT_OVERS_DIRECTORY + "_1" ) );
fileSystem.mkdir( new File( dbDirectory, StoreUpgrader.MIGRATION_LEFT_OVERS_DIRECTORY + "_2" ) );
fileSystem.mkdir( new File( dbDirectory, StoreUpgrader.MIGRATION_LEFT_OVERS_DIRECTORY + "_42" ) );
PageCache pageCache = pageCacheRule.getPageCache( fileSystem );
// When
UpgradableDatabase upgradableDatabase = new UpgradableDatabase( fileSystem,
new StoreVersionCheck( pageCache ), new LegacyStoreVersionCheck( fileSystem ),
getRecordFormats() );
StoreUpgrader storeUpgrader = newUpgrader( upgradableDatabase, pageCache );
storeUpgrader.migrateIfNeeded( dbDirectory );
// Then
assertThat( migrationHelperDirs(), is( emptyCollectionOf( File.class ) ) );
}
private static void assertCorrectStoreVersion( String expectedStoreVersion, StoreVersionCheck check, File storeDir )
{
File neoStoreFile = new File( storeDir, MetaDataStore.DEFAULT_NAME );
StoreVersionCheck.Result result = check.hasVersion( neoStoreFile, expectedStoreVersion );
assertTrue( "Unexpected store version", result.outcome.isSuccessful() );
}
private StoreMigrationParticipant participantThatWillFailWhenMoving( final String failureMessage )
{
return new AbstractStoreMigrationParticipant( "Failing" )
{
@Override
public void moveMigratedFiles( File migrationDir, File storeDir, String versionToUpgradeFrom,
String versionToMigrateTo ) throws IOException
{
throw new IOException( failureMessage );
}
};
}
private StoreUpgrader newUpgrader( UpgradableDatabase upgradableDatabase, Config config, PageCache pageCache )
{
return newUpgrader( upgradableDatabase, pageCache, config );
}
private StoreUpgrader newUpgrader( UpgradableDatabase upgradableDatabase, PageCache pageCache )
{
return newUpgrader( upgradableDatabase, pageCache, allowMigrateConfig );
}
private StoreUpgrader newUpgrader( UpgradableDatabase upgradableDatabase, PageCache pageCache, Config config )
{
check = new StoreVersionCheck( pageCache );
SilentMigrationProgressMonitor progressMonitor = new SilentMigrationProgressMonitor();
NullLogService instance = NullLogService.getInstance();
StoreMigrator defaultMigrator = new StoreMigrator( fileSystem, pageCache, getTuningConfig(), instance,
schemaIndexProvider );
SchemaIndexMigrator indexMigrator =
new SchemaIndexMigrator( fileSystem, schemaIndexProvider, labelScanStoreProvider );
StoreUpgrader upgrader = new StoreUpgrader( upgradableDatabase, progressMonitor, config, fileSystem,
NullLogProvider.getInstance() );
upgrader.addParticipant( indexMigrator );
upgrader.addParticipant( defaultMigrator );
return upgrader;
}
private List<File> migrationHelperDirs()
{
File[] tmpDirs = dbDirectory.listFiles( ( file, name ) -> file.isDirectory() &&
(name.equals( StoreUpgrader.MIGRATION_DIRECTORY ) ||
name.startsWith( StoreUpgrader.MIGRATION_LEFT_OVERS_DIRECTORY )) );
assertNotNull( "Some IO errors occurred", tmpDirs );
return Arrays.asList( tmpDirs );
}
private void makeDbNotCleanlyShutdown( boolean truncateAll ) throws IOException
{
if ( StandardV2_3.STORE_VERSION.equals( version ) )
{
removeCheckPointFromTxLog( fileSystem, dbDirectory );
}
else
{
if ( truncateAll )
{
truncateAllFiles( fileSystem, dbDirectory, version );
}
else
{
File storeFile = new File( dbDirectory, "neostore.propertystore.db.index.keys" );
truncateFile( fileSystem, storeFile, "StringPropertyStore " + version );
}
}
}
private Config getTuningConfig()
{
return new Config( MapUtil.stringMap( GraphDatabaseSettings.record_format.name(), getRecordFormatsName() ) );
}
protected RecordFormats getRecordFormats()
{
return StandardV3_0.RECORD_FORMATS;
}
protected String getRecordFormatsName()
{
return StandardV3_0.NAME;
}
}