Skip to content

Commit

Permalink
Complete the first draft implementation of PageList.tryEvict
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvest committed May 26, 2017
1 parent be415f1 commit 4e77999
Show file tree
Hide file tree
Showing 15 changed files with 421 additions and 80 deletions.
Expand Up @@ -23,6 +23,9 @@


import org.neo4j.io.pagecache.PageCursor; import org.neo4j.io.pagecache.PageCursor;
import org.neo4j.io.pagecache.PageSwapper; import org.neo4j.io.pagecache.PageSwapper;
import org.neo4j.io.pagecache.tracing.EvictionEvent;
import org.neo4j.io.pagecache.tracing.EvictionEventOpportunity;
import org.neo4j.io.pagecache.tracing.FlushEvent;
import org.neo4j.io.pagecache.tracing.PageFaultEvent; import org.neo4j.io.pagecache.tracing.PageFaultEvent;
import org.neo4j.unsafe.impl.internal.dragons.MemoryManager; import org.neo4j.unsafe.impl.internal.dragons.MemoryManager;
import org.neo4j.unsafe.impl.internal.dragons.UnsafeUtil; import org.neo4j.unsafe.impl.internal.dragons.UnsafeUtil;
Expand Down Expand Up @@ -117,7 +120,7 @@ private void clearMemory( long baseAddress, long pageCount )
*/ */
public long deref( long pageId ) public long deref( long pageId )
{ {
return baseAddress + pageId * META_DATA_BYTES_PER_PAGE; return baseAddress + (pageId * META_DATA_BYTES_PER_PAGE);
} }


private long offLock( long pageRef ) private long offLock( long pageRef )
Expand Down Expand Up @@ -337,32 +340,54 @@ private static IllegalStateException cannotFaultException( long pageRef, PageSwa
return new IllegalStateException( msg ); return new IllegalStateException( msg );
} }


public boolean tryEvict( long pageRef ) throws IOException public boolean tryEvict( long pageRef, EvictionEventOpportunity evictionOpportunity ) throws IOException
{ {
if ( tryExclusiveLock( pageRef ) ) if ( tryExclusiveLock( pageRef ) )
{ {
if ( isLoaded( pageRef ) ) if ( isLoaded( pageRef ) )
{ {
int swapperId = getSwapperId( pageRef ); try ( EvictionEvent evictionEvent = evictionOpportunity.beginEviction() )
SwapperSet.Allocation allocation = swappers.getAllocation( swapperId );
PageSwapper swapper = allocation.swapper;
long filePageId = getFilePageId( pageRef );
if ( isModified( pageRef ) )
{ {
long address = getAddress( pageRef ); evict( pageRef, evictionEvent );
swapper.write( filePageId, address, allocation.filePageSize ); return true;
explicitlyMarkPageUnmodifiedUnderExclusiveLock( pageRef );
} }
swapper.evicted( filePageId, null );
clearBinding( pageRef );
return true;
} }
else unlockExclusive( pageRef );
}
return false;
}

private void evict( long pageRef, EvictionEvent evictionEvent ) throws IOException
{
int swapperId = getSwapperId( pageRef );
SwapperSet.Allocation allocation = swappers.getAllocation( swapperId );
PageSwapper swapper = allocation.swapper;
long filePageId = getFilePageId( pageRef );
evictionEvent.setFilePageId( filePageId );
evictionEvent.setCachePageId( pageRef );
evictionEvent.setSwapper( swapper );
if ( isModified( pageRef ) )
{
FlushEvent flushEvent = evictionEvent.flushEventOpportunity().beginFlush( filePageId, pageRef, swapper );
try
{
long address = getAddress( pageRef );
long bytesWritten = swapper.write( filePageId, address, allocation.filePageSize );
explicitlyMarkPageUnmodifiedUnderExclusiveLock( pageRef );
flushEvent.addBytesWritten( bytesWritten );
flushEvent.addPagesFlushed( 1 );
flushEvent.done();
}
catch ( IOException e )
{ {
unlockExclusive( pageRef ); unlockExclusive( pageRef );
flushEvent.done( e );
evictionEvent.threwException( e );
throw e;
} }
} }
return false; swapper.evicted( filePageId, null );
clearBinding( pageRef );
} }


private void clearBinding( long pageRef ) private void clearBinding( long pageRef )
Expand Down
Expand Up @@ -95,7 +95,7 @@ public void threwException( IOException exception )
} }


@Override @Override
public void setCachePageId( int cachePageId ) public void setCachePageId( long cachePageId )
{ {
} }


Expand Down
Expand Up @@ -55,7 +55,7 @@ public void threwException( IOException exception )
} }


@Override @Override
public void setCachePageId( int cachePageId ) public void setCachePageId( long cachePageId )
{ {
} }


Expand Down Expand Up @@ -89,5 +89,5 @@ public void close()
/** /**
* The cache page id of the evicted page. * The cache page id of the evicted page.
*/ */
void setCachePageId( int cachePageId ); void setCachePageId( long cachePageId );
} }
@@ -0,0 +1,31 @@
/*
* 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 org.neo4j.io.pagecache.tracing;

/**
* Interface for any event that in turn presents the opportunity to evict a page.
*/
public interface EvictionEventOpportunity
{
/**
* Begin an eviction event.
*/
EvictionEvent beginEviction();
}
Expand Up @@ -24,14 +24,13 @@
* needs to evict a batch of pages. The dedicated eviction thread is * needs to evict a batch of pages. The dedicated eviction thread is
* mostly sleeping when it is not performing an eviction run. * mostly sleeping when it is not performing an eviction run.
*/ */
public interface EvictionRunEvent extends AutoCloseablePageCacheTracerEvent public interface EvictionRunEvent extends AutoCloseablePageCacheTracerEvent, EvictionEventOpportunity
{ {
/** /**
* An EvictionRunEvent that does nothing other than return the EvictionEvent.NULL. * An EvictionRunEvent that does nothing other than return the EvictionEvent.NULL.
*/ */
EvictionRunEvent NULL = new EvictionRunEvent() EvictionRunEvent NULL = new EvictionRunEvent()
{ {

@Override @Override
public EvictionEvent beginEviction() public EvictionEvent beginEviction()
{ {
Expand All @@ -43,9 +42,4 @@ public void close()
{ {
} }
}; };

/**
* An eviction is started as part of this eviction run.
*/
EvictionEvent beginEviction();
} }
Expand Up @@ -36,5 +36,5 @@ public interface FlushEventOpportunity
/** /**
* Begin flushing the given page. * Begin flushing the given page.
*/ */
FlushEvent beginFlush( long filePageId, int cachePageId, PageSwapper swapper ); FlushEvent beginFlush( long filePageId, long cachePageId, PageSwapper swapper );
} }
Expand Up @@ -22,7 +22,7 @@
/** /**
* Begin a page fault as part of a pin event. * Begin a page fault as part of a pin event.
*/ */
public interface PageFaultEvent public interface PageFaultEvent extends EvictionEventOpportunity
{ {
/** /**
* A PageFaultEvent that does nothing. * A PageFaultEvent that does nothing.
Expand Down Expand Up @@ -51,7 +51,7 @@ public EvictionEvent beginEviction()
} }


@Override @Override
public void setCachePageId( int cachePageId ) public void setCachePageId( long cachePageId )
{ {
} }
}; };
Expand All @@ -64,7 +64,7 @@ public void setCachePageId( int cachePageId )
/** /**
* The id of the cache page that is being faulted into. * The id of the cache page that is being faulted into.
*/ */
void setCachePageId( int cachePageId ); void setCachePageId( long cachePageId );


/** /**
* The page fault completed successfully. * The page fault completed successfully.
Expand All @@ -75,9 +75,4 @@ public void setCachePageId( int cachePageId )
* The page fault did not complete successfully, but instead caused the given Throwable to be thrown. * The page fault did not complete successfully, but instead caused the given Throwable to be thrown.
*/ */
void done( Throwable throwable ); void done( Throwable throwable );

/**
* Begin an eviction event caused by this page fault event.
*/
EvictionEvent beginEviction();
} }
Expand Up @@ -30,7 +30,7 @@ public interface PinEvent
PinEvent NULL = new PinEvent() PinEvent NULL = new PinEvent()
{ {
@Override @Override
public void setCachePageId( int cachePageId ) public void setCachePageId( long cachePageId )
{ {
} }


Expand All @@ -54,7 +54,7 @@ public void done()
/** /**
* The id of the cache page that holds the file page we pinned. * The id of the cache page that holds the file page we pinned.
*/ */
void setCachePageId( int cachePageId ); void setCachePageId( long cachePageId );


/** /**
* The page we want to pin is not in memory, so being a page fault to load it in. * The page we want to pin is not in memory, so being a page fault to load it in.
Expand Down
Expand Up @@ -191,7 +191,7 @@ public void threwException( IOException exception )
} }


@Override @Override
public void setCachePageId( int cachePageId ) public void setCachePageId( long cachePageId )
{ {
} }


Expand Down Expand Up @@ -229,15 +229,15 @@ public EvictionEvent beginEviction()
} }


@Override @Override
public void setCachePageId( int cachePageId ) public void setCachePageId( long cachePageId )
{ {
} }
}; };


private final FlushEventOpportunity flushEventOpportunity = new FlushEventOpportunity() private final FlushEventOpportunity flushEventOpportunity = new FlushEventOpportunity()
{ {
@Override @Override
public FlushEvent beginFlush( long filePageId, int cachePageId, PageSwapper swapper ) public FlushEvent beginFlush( long filePageId, long cachePageId, PageSwapper swapper )
{ {
return flushEvent; return flushEvent;
} }
Expand Down Expand Up @@ -274,7 +274,7 @@ private class DefaultPinEvent implements PinEvent
int eventHits = 1; int eventHits = 1;


@Override @Override
public void setCachePageId( int cachePageId ) public void setCachePageId( long cachePageId )
{ {
} }


Expand Down

0 comments on commit 4e77999

Please sign in to comment.