Skip to content

Commit

Permalink
Makes opening/creating BatchingNeoStores more robust
Browse files Browse the repository at this point in the history
There were some cases w/ restart shown by RestartableImportIT
that these additions takes care of.
  • Loading branch information
tinwelint authored and ragadeeshu committed Apr 5, 2018
1 parent b1ee5c5 commit d0142d2
Showing 1 changed file with 19 additions and 7 deletions.
Expand Up @@ -73,6 +73,7 @@
import static org.neo4j.helpers.collection.MapUtil.stringMap;
import static org.neo4j.io.IOUtils.closeAll;
import static org.neo4j.io.pagecache.IOLimiter.unlimited;
import static org.neo4j.kernel.impl.index.labelscan.NativeLabelScanStore.getLabelScanStoreFile;
import static org.neo4j.kernel.impl.store.MetaDataStore.DEFAULT_NAME;
import static org.neo4j.kernel.impl.store.StoreType.PROPERTY;
import static org.neo4j.kernel.impl.store.StoreType.PROPERTY_ARRAY;
Expand Down Expand Up @@ -133,7 +134,7 @@ private BatchingNeoStores( FileSystemAbstraction fileSystem, PageCache pageCache
this.externalPageCache = externalPageCache;
}

private boolean databaseExistsAndContainsData( PageCache pageCache, File storeDir )
private boolean databaseExistsAndContainsData()
{
File metaDataFile = new File( storeDir, StoreType.META_DATA.getStoreFile().fileName( StoreFileType.STORE ) );
try ( PagedFile pagedFile = pageCache.map( metaDataFile, pageCache.pageSize(), StandardOpenOption.READ ) )
Expand Down Expand Up @@ -161,10 +162,12 @@ private boolean databaseExistsAndContainsData( PageCache pageCache, File storeDi
*/
public void createNew() throws IOException
{
if ( databaseExistsAndContainsData( pageCache, storeDir ) )
{
throw new IllegalStateException( storeDir + " already contains data, cannot do import here" );
}
assertDatabaseIsEmptyOrNonExistent();

// There may have been a previous import which was killed before it even started, where the label scan store could
// be in a semi-initialized state. Better to be on the safe side and deleted it. We get her after determining that
// the db is either completely empty or non-existent anyway, so deleting this file is OK.
fileSystem.deleteFile( getLabelScanStoreFile( storeDir ) );

instantiateStores();
neoStores.getMetaDataStore().setLastCommittedAndClosedTransactionId(
Expand All @@ -173,6 +176,14 @@ public void createNew() throws IOException
initialIds.lastCommittedTransactionLogVersion() );
}

public void assertDatabaseIsEmptyOrNonExistent()
{
if ( databaseExistsAndContainsData() )
{
throw new IllegalStateException( storeDir + " already contains data, cannot do import here" );
}
}

/**
* Called when expecting a previous attempt/state of a database to open, where some store files should be kept,
* but others deleted. All temporary stores will be deleted in this call.
Expand All @@ -186,8 +197,6 @@ public void pruneAndOpenExistingStore( Predicate<StoreType> mainStoresToKeep, Pr
deleteStoreFiles( TEMP_NEOSTORE_NAME, tempStoresToKeep );
deleteStoreFiles( DEFAULT_NAME, mainStoresToKeep );
instantiateStores();
neoStores.makeStoreOk();
temporaryNeoStores.makeStoreOk();
}

private void deleteStoreFiles( String storeName, Predicate<StoreType> storesToKeep )
Expand Down Expand Up @@ -227,6 +236,9 @@ private void instantiateStores() throws IOException
neoStores.getRelationshipTypeTokenStore() );
temporaryNeoStores = instantiateTempStores();
instantiateKernelExtensions();

neoStores.makeStoreOk();
temporaryNeoStores.makeStoreOk();
}

private NeoStores instantiateTempStores()
Expand Down

0 comments on commit d0142d2

Please sign in to comment.