Skip to content

Commit

Permalink
Merge in the restartable PageCacheWarmer from upstream.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvest committed Apr 4, 2018
1 parent 5f9bed0 commit 294d82c
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 12 deletions.
Expand Up @@ -69,10 +69,10 @@ public class PageCacheWarmer implements NeoStoreFileListing.StoreFileProvider
private final FileSystemAbstraction fs;
private final PageCache pageCache;
private final JobScheduler scheduler;
private volatile boolean stopOngoingWarming;
private final ExecutorService executor;
private final PageLoaderFactory pageLoaderFactory;
private final ProfileRefCounts refCounts;
private volatile boolean stopOngoingWarming;
private ExecutorService executor;
private PageLoaderFactory pageLoaderFactory;

PageCacheWarmer( FileSystemAbstraction fs, PageCache pageCache, JobScheduler scheduler )
{
Expand Down Expand Up @@ -104,16 +104,28 @@ public synchronized Resource addFilesTo( Collection<StoreFileMetadata> coll ) th
};
}

public synchronized void start()
{
stopOngoingWarming = false;
executor = buildExecutorService( scheduler );
pageLoaderFactory = new PageLoaderFactory( executor, pageCache );
}

public void stop()
{
stopOngoingWarming = true;
synchronized ( this )
stopWarmer();
}

/**
* Stopping warmer process, needs to be synchronised to prevent racing with profiling and heating
*/
private synchronized void stopWarmer()
{
if ( executor != null )
{
// This synchronised block means we'll wait for any reheat() or profile() to notice our raised stopped flag,
// before we return to the caller. This helps avoid races, e.g. if the page cache is closed while this page
// cache warmer is still holding on to some mapped pages.
executor.shutdown();
}
executor.shutdown();
}

/**
Expand Down
Expand Up @@ -49,6 +49,7 @@ class PageCacheWarmerKernelExtension extends LifecycleAdapter
private final Config config;
private final PageCacheWarmer pageCacheWarmer;
private volatile JobScheduler.JobHandle profileHandle;
private volatile boolean started;

PageCacheWarmerKernelExtension(
JobScheduler scheduler, AvailabilityGuard availabilityGuard, PageCache pageCache, FileSystemAbstraction fs,
Expand All @@ -68,8 +69,10 @@ public void start()
{
if ( ENABLED )
{
pageCacheWarmer.start();
scheduleTryReheat();
fileListing.get().registerStoreFileProvider( pageCacheWarmer );
started = true;
}
}

Expand Down Expand Up @@ -141,11 +144,14 @@ private void doProfile()
@Override
public void stop() throws Throwable
{
JobScheduler.JobHandle handle = profileHandle;
if ( handle != null )
if ( started )
{
handle.cancel( false );
JobScheduler.JobHandle handle = profileHandle;
if ( handle != null )
{
handle.cancel( false );
}
pageCacheWarmer.stop();
}
pageCacheWarmer.stop();
}
}
Expand Up @@ -32,6 +32,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.OptionalLong;
import java.util.concurrent.ThreadLocalRandom;

import org.neo4j.collection.primitive.Primitive;
Expand All @@ -57,6 +58,8 @@
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

Expand Down Expand Up @@ -102,6 +105,52 @@ private void clearTracerCounts()
cursorTracer.get().init( cacheTracer );
}

@Test
public void doNotReheatAfterStop() throws IOException
{
try ( PageCache pageCache = pageCacheRule.getPageCache( fs, cfg );
PagedFile ignore = pageCache.map( file, pageCache.pageSize(), StandardOpenOption.CREATE ) )
{
PageCacheWarmer warmer = new PageCacheWarmer( fs, pageCache, scheduler );
warmer.start();
warmer.stop();
assertSame( OptionalLong.empty(), warmer.reheat() );
}
}

@Test
public void doNoProfileAfterStop() throws IOException
{
try ( PageCache pageCache = pageCacheRule.getPageCache( fs, cfg );
PagedFile ignore = pageCache.map( file, pageCache.pageSize(), StandardOpenOption.CREATE ) )
{
PageCacheWarmer warmer = new PageCacheWarmer( fs, pageCache, scheduler );
warmer.start();
warmer.stop();
assertSame( OptionalLong.empty(), warmer.profile() );
}
}

@Test
public void profileAndReheatAfterRestart() throws IOException
{
try ( PageCache pageCache = pageCacheRule.getPageCache( fs, cfg );
PagedFile pf = pageCache.map( file, pageCache.pageSize(), StandardOpenOption.CREATE ) )
{
PageCacheWarmer warmer = new PageCacheWarmer( fs, pageCache, scheduler );
warmer.start();
warmer.stop();
warmer.start();
try ( PageCursor writer = pf.io( 0, PagedFile.PF_SHARED_WRITE_LOCK ) )
{
assertTrue( writer.next( 1 ) );
assertTrue( writer.next( 3 ) );
}
warmer.profile();
assertNotSame( OptionalLong.empty(), warmer.reheat() );
}
}

@Test
public void mustDoNothingWhenReheatingUnprofiledPageCache() throws Exception
{
Expand Down Expand Up @@ -138,6 +187,7 @@ public void mustReheatProfiledPageCache() throws Exception
PagedFile pf = pageCache.map( file, pageCache.pageSize() ) )
{
PageCacheWarmer warmer = new PageCacheWarmer( fs, pageCache, scheduler );
warmer.start();
warmer.reheat();

pageCache.reportEvents();
Expand Down Expand Up @@ -183,6 +233,7 @@ public void reheatingMustWorkOnLargeNumberOfPages() throws Exception
PagedFile pf = pageCache.map( file, pageCache.pageSize() ) )
{
PageCacheWarmer warmer = new PageCacheWarmer( fs, pageCache, scheduler );
warmer.start();
warmer.reheat();

pageCache.reportEvents();
Expand Down Expand Up @@ -216,6 +267,7 @@ public void profileMustNotDeleteFilesCurrentlyExposedViaFileListing() throws Exc
}
pf.flushAndForce();
PageCacheWarmer warmer = new PageCacheWarmer( fs, pageCache, scheduler );
warmer.start();
warmer.profile();
warmer.profile();
warmer.profile();
Expand All @@ -242,6 +294,7 @@ public void profileMustNotDeleteFilesCurrentlyExposedViaFileListing() throws Exc
}
// Once we are done with the file listing, profile should remove those files.
warmer.profile();
warmer.stop();
assertFilesNotExists( fileListing );
}
}
Expand Down

0 comments on commit 294d82c

Please sign in to comment.