Skip to content

Commit

Permalink
Better use of read / write locks when reading meta page and state pages
Browse files Browse the repository at this point in the history
  • Loading branch information
burqen committed Dec 13, 2016
1 parent 34212ab commit 80458f3
Showing 1 changed file with 30 additions and 23 deletions.
53 changes: 30 additions & 23 deletions community/index/src/main/java/org/neo4j/index/gbptree/GBPTree.java
Expand Up @@ -297,7 +297,7 @@ private PagedFile openOrCreate( PageCache pageCache, File indexFile,
{ {
readMeta( indexFile, layout, pagedFile ); readMeta( indexFile, layout, pagedFile );
pagedFile = mapWithCorrectPageSize( pageCache, indexFile, pagedFile ); pagedFile = mapWithCorrectPageSize( pageCache, indexFile, pagedFile );
readState( pagedFile ); loadState( pagedFile );
return pagedFile; return pagedFile;
} }
catch ( Throwable t ) catch ( Throwable t )
Expand Down Expand Up @@ -332,39 +332,46 @@ private PagedFile openOrCreate( PageCache pageCache, File indexFile,
} }
} }


private void readState( PagedFile pagedFile ) throws IOException private void loadState( PagedFile pagedFile ) throws IOException
{ {
try ( PageCursor cursor = pagedFile.io( 0L /*ignored*/, PagedFile.PF_SHARED_WRITE_LOCK ) ) Pair<TreeState,TreeState> states = readStatePages( pagedFile );
TreeState state = TreeStatePair.selectNewestValidState( states );
rootId = state.rootId();
lastId = state.lastId();
generation = Generation.generation( state.stableGeneration(), state.unstableGeneration() );
}

private void writeState( PagedFile pagedFile ) throws IOException
{
Pair<TreeState,TreeState> states = readStatePages( pagedFile );
TreeState oldestState = TreeStatePair.selectOldestOrInvalid( states );
long pageToOverwrite = oldestState.pageId();
try ( PageCursor cursor = pagedFile.io( pageToOverwrite, PagedFile.PF_SHARED_WRITE_LOCK ) )
{ {
Pair<TreeState,TreeState> states = TreeStatePair.readStatePages( PageCursorUtil.goTo( cursor, "state page", pageToOverwrite );
cursor, IdSpace.STATE_PAGE_A, IdSpace.STATE_PAGE_B ); TreeState.write( cursor, stableGeneration( generation ), unstableGeneration( generation ), rootId, lastId );
checkOutOfBounds( cursor ); checkOutOfBounds( cursor );

TreeState state = TreeStatePair.selectNewestValidState( states );
rootId = state.rootId();
lastId = state.lastId();
generation = Generation.generation( state.stableGeneration(), state.unstableGeneration() );
} }
} }


private void writeState( PagedFile pagedFile ) throws IOException private Pair<TreeState,TreeState> readStatePages( PagedFile pagedFile ) throws IOException
{ {
try ( PageCursor cursor = pagedFile.io( 0L /*ignored*/, PagedFile.PF_SHARED_WRITE_LOCK ) ) Pair<TreeState,TreeState> states;
try ( PageCursor cursor = pagedFile.io( 0L /*ignored*/, PagedFile.PF_SHARED_READ_LOCK ) )
{ {
Pair<TreeState,TreeState> states = TreeStatePair.readStatePages( do
cursor, IdSpace.STATE_PAGE_A, IdSpace.STATE_PAGE_B ); {
states = TreeStatePair.readStatePages(
cursor, IdSpace.STATE_PAGE_A, IdSpace.STATE_PAGE_B );
} while ( cursor.shouldRetry() );
checkOutOfBounds( cursor ); checkOutOfBounds( cursor );
TreeState oldestState = TreeStatePair.selectOldestOrInvalid( states );
long pageToOverwrite = oldestState.pageId();
PageCursorUtil.goTo( cursor, "state page", pageToOverwrite );
cursor.setOffset( 0 );
TreeState.write( cursor, stableGeneration( generation ), unstableGeneration( generation ), rootId, lastId );
} }
return states;
} }


private static PageCursor openMetaPageCursor( PagedFile pagedFile ) throws IOException private static PageCursor openMetaPageCursor( PagedFile pagedFile, int pfFlags ) throws IOException
{ {
PageCursor metaCursor = pagedFile.io( IdSpace.META_PAGE_ID, PagedFile.PF_SHARED_WRITE_LOCK ); PageCursor metaCursor = pagedFile.io( IdSpace.META_PAGE_ID, pfFlags );
PageCursorUtil.goTo( metaCursor, "meta page", IdSpace.META_PAGE_ID ); PageCursorUtil.goTo( metaCursor, "meta page", IdSpace.META_PAGE_ID );
return metaCursor; return metaCursor;
} }
Expand All @@ -376,7 +383,7 @@ private void readMeta( File indexFile, Layout<KEY,VALUE> layout, PagedFile paged
long layoutIdentifier; long layoutIdentifier;
int majorVersion; int majorVersion;
int minorVersion; int minorVersion;
try ( PageCursor metaCursor = openMetaPageCursor( pagedFile ) ) try ( PageCursor metaCursor = openMetaPageCursor( pagedFile, PagedFile.PF_SHARED_READ_LOCK ) )
{ {
do do
{ {
Expand Down Expand Up @@ -405,7 +412,7 @@ private void readMeta( File indexFile, Layout<KEY,VALUE> layout, PagedFile paged


private void writeMeta( Layout<KEY,VALUE> layout, PagedFile pagedFile ) throws IOException private void writeMeta( Layout<KEY,VALUE> layout, PagedFile pagedFile ) throws IOException
{ {
try ( PageCursor metaCursor = openMetaPageCursor( pagedFile ) ) try ( PageCursor metaCursor = openMetaPageCursor( pagedFile, PagedFile.PF_SHARED_WRITE_LOCK ) )
{ {
metaCursor.putInt( pageSize ); metaCursor.putInt( pageSize );
metaCursor.putLong( layout.identifier() ); metaCursor.putLong( layout.identifier() );
Expand Down

0 comments on commit 80458f3

Please sign in to comment.