Skip to content

Commit

Permalink
Cleanup cycling and configuration of page swappers
Browse files Browse the repository at this point in the history
Build page cache factory only page cache is actually created.
Move configure to PageSwapper and remove ConfigurablePageSwapperFactory.
  • Loading branch information
MishaDemianenko committed May 4, 2017
1 parent 5bfa413 commit 51ef0bd
Show file tree
Hide file tree
Showing 14 changed files with 103 additions and 101 deletions.
Expand Up @@ -33,4 +33,17 @@ public interface Configuration
* of the given property.
*/
<T> T get( Setting<T> setting );

/**
* Empty configuration without any settings.
*/
Configuration EMPTY = new Configuration()
{
@Override
public <T> T get( Setting<T> setting )
{
return null;
}
};

}
5 changes: 5 additions & 0 deletions community/io/pom.xml
Expand Up @@ -64,6 +64,11 @@ the relevant Commercial Agreement.
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-graphdb-api</artifactId>
<version>${project.version}</version>
</dependency>

<!-- Test dependencies -->
<dependency>
Expand Down
Expand Up @@ -24,6 +24,7 @@
import java.nio.file.NoSuchFileException;
import java.util.stream.Stream;

import org.neo4j.graphdb.config.Configuration;
import org.neo4j.io.fs.FileSystemAbstraction;

/**
Expand All @@ -33,17 +34,27 @@
* <p>
* The PageSwapperFactory presumably knows about what file system to use.
* <p>
* To be able to create PageSwapper factory need to be configured first using appropriate configure call.
* Note that this API is <em>only</em> intended to be used by a {@link PageCache} implementation.
* It should never be used directly by user code.
*/
public interface PageSwapperFactory
{
/**
* Configure the FileSystemAbstraction to use.
* <p>
* This must be called before the first PageSwapper is created.
* Configure page swapper factory with filesystem and config
* @param fs file system to use in page swappers
* @param config custom page swapper configuration
*/
void configure( FileSystemAbstraction fs, Configuration config );

/**
* Configure swapper with filesystem to use.
* @param fs file system to use in page swappers
*/
void setFileSystemAbstraction( FileSystemAbstraction fs );
default void configure( FileSystemAbstraction fs )
{
this.configure( fs, Configuration.EMPTY );
}

/**
* Get the name of this PageSwapperFactory implementation, for configuration purpose.
Expand Down
Expand Up @@ -27,6 +27,7 @@
import java.util.List;
import java.util.stream.Stream;

import org.neo4j.graphdb.config.Configuration;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.io.pagecache.FileHandle;
import org.neo4j.io.pagecache.PageEvictionCallback;
Expand All @@ -45,7 +46,7 @@ public class SingleFilePageSwapperFactory implements PageSwapperFactory
private FileSystemAbstraction fs;

@Override
public void setFileSystemAbstraction( FileSystemAbstraction fs )
public void configure( FileSystemAbstraction fs, Configuration config )
{
this.fs = fs;
}
Expand Down
Expand Up @@ -54,7 +54,7 @@ public static PageCache createPageCache( FileSystemAbstraction fileSystem, Integ
PageCacheTracer tracer, PageCursorTracerSupplier cursorTracerSupplier )
{
SingleFilePageSwapperFactory factory = new SingleFilePageSwapperFactory();
factory.setFileSystemAbstraction( fileSystem );
factory.configure( fileSystem );

int cachePageSize = pageSize != null ? pageSize : factory.getCachePageSizeHint();
long pageCacheMemory = ByteUnit.mebiBytes( 8 );
Expand Down
Expand Up @@ -3744,7 +3744,7 @@ public void force() throws IOException
};
}
};
factory.setFileSystemAbstraction( fs );
factory.configure( fs );
return factory;
}

Expand Down
Expand Up @@ -42,8 +42,8 @@
import org.neo4j.io.fs.StoreChannel;
import org.neo4j.io.pagecache.impl.SingleFilePageSwapperFactory;
import org.neo4j.io.pagecache.tracing.PageCacheTracer;
import org.neo4j.test.rule.RepeatRule;
import org.neo4j.io.pagecache.tracing.cursor.PageCursorTracerSupplier;
import org.neo4j.test.rule.RepeatRule;

import static org.junit.Assert.assertThat;
import static org.neo4j.test.matchers.ByteArrayMatcher.byteArray;
Expand Down Expand Up @@ -117,7 +117,7 @@ protected T createPageCache( FileSystemAbstraction fs, int maxPages, int pageSiz
PageCursorTracerSupplier cursorTracerSupplier )
{
PageSwapperFactory swapperFactory = new SingleFilePageSwapperFactory();
swapperFactory.setFileSystemAbstraction( fs );
swapperFactory.configure( fs );
return createPageCache( swapperFactory, maxPages, pageSize, tracer, cursorTracerSupplier );
}

Expand Down
Expand Up @@ -38,6 +38,7 @@

import org.neo4j.adversaries.RandomAdversary;
import org.neo4j.adversaries.fs.AdversarialFileSystemAbstraction;
import org.neo4j.graphdb.config.Configuration;
import org.neo4j.graphdb.mockfs.DelegatingFileSystemAbstraction;
import org.neo4j.graphdb.mockfs.DelegatingStoreChannel;
import org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction;
Expand Down Expand Up @@ -81,7 +82,7 @@ public void tearDown() throws Exception
protected PageSwapperFactory swapperFactory()
{
SingleFilePageSwapperFactory factory = new SingleFilePageSwapperFactory();
factory.setFileSystemAbstraction( getFs() );
factory.configure( getFs(), Configuration.EMPTY );
return factory;
}

Expand Down Expand Up @@ -235,7 +236,7 @@ public void creatingSwapperForFileMustTakeLockOnFile() throws Exception
assumeFalse( "No file locking on Windows", SystemUtils.IS_OS_WINDOWS );

PageSwapperFactory factory = createSwapperFactory();
factory.setFileSystemAbstraction( fileSystem );
factory.configure( fileSystem );
File file = testDir.file( "file" );
fileSystem.create( file ).close();

Expand All @@ -259,7 +260,7 @@ public void creatingSwapperForInternallyLockedFileMustThrow() throws Exception
assumeFalse( "No file locking on Windows", SystemUtils.IS_OS_WINDOWS ); // no file locking on Windows.

PageSwapperFactory factory = createSwapperFactory();
factory.setFileSystemAbstraction( fileSystem );
factory.configure( fileSystem );
File file = testDir.file( "file" );

StoreFileChannel channel = fileSystem.create( file );
Expand All @@ -278,7 +279,7 @@ public void creatingSwapperForExternallyLockedFileMustThrow() throws Exception
assumeFalse( "No file locking on Windows", SystemUtils.IS_OS_WINDOWS ); // no file locking on Windows.

PageSwapperFactory factory = createSwapperFactory();
factory.setFileSystemAbstraction( fileSystem );
factory.configure( fileSystem );
File file = testDir.file( "file" );

fileSystem.create( file ).close();
Expand Down Expand Up @@ -312,7 +313,7 @@ public void mustUnlockFileWhenThePageSwapperIsClosed() throws Exception
assumeFalse( "No file locking on Windows", SystemUtils.IS_OS_WINDOWS ); // no file locking on Windows.

PageSwapperFactory factory = createSwapperFactory();
factory.setFileSystemAbstraction( fileSystem );
factory.configure( fileSystem );
File file = testDir.file( "file" );
fileSystem.create( file ).close();

Expand All @@ -331,7 +332,7 @@ public void fileMustRemainLockedEvenIfChannelIsClosedByStrayInterrupt() throws E
assumeFalse( "No file locking on Windows", SystemUtils.IS_OS_WINDOWS ); // no file locking on Windows.

PageSwapperFactory factory = createSwapperFactory();
factory.setFileSystemAbstraction( fileSystem );
factory.configure( fileSystem );
File file = testDir.file( "file" );
fileSystem.create( file ).close();

Expand Down Expand Up @@ -360,7 +361,7 @@ public void mustCloseFilesIfTakingFileLockThrows() throws Exception

final AtomicInteger openFilesCounter = new AtomicInteger();
PageSwapperFactory factory = createSwapperFactory();
factory.setFileSystemAbstraction( new DelegatingFileSystemAbstraction( fileSystem )
factory.configure( new DelegatingFileSystemAbstraction( fileSystem )
{
@Override
public StoreChannel open( File fileName, String mode ) throws IOException
Expand Down Expand Up @@ -421,7 +422,7 @@ public void mustHandleMischiefInPositionedRead() throws Exception
ThreadLocalRandom.current().nextBytes( data );

PageSwapperFactory factory = createSwapperFactory();
factory.setFileSystemAbstraction( getFs() );
factory.configure( getFs() );
File file = getFile();
PageSwapper swapper = createSwapper( factory, file, bytesTotal, NO_CALLBACK, true );
try
Expand All @@ -434,7 +435,7 @@ public void mustHandleMischiefInPositionedRead() throws Exception
}

RandomAdversary adversary = new RandomAdversary( 0.5, 0.0, 0.0 );
factory.setFileSystemAbstraction( new AdversarialFileSystemAbstraction( adversary, getFs() ) );
factory.configure( new AdversarialFileSystemAbstraction( adversary, getFs() ) );
swapper = createSwapper( factory, file, bytesTotal, NO_CALLBACK, false );

ByteBufferPage page = createPage( bytesTotal );
Expand Down Expand Up @@ -466,7 +467,7 @@ public void mustHandleMischiefInPositionedWrite() throws Exception
File file = getFile();
PageSwapperFactory factory = createSwapperFactory();
RandomAdversary adversary = new RandomAdversary( 0.5, 0.0, 0.0 );
factory.setFileSystemAbstraction( new AdversarialFileSystemAbstraction( adversary, getFs() ) );
factory.configure( new AdversarialFileSystemAbstraction( adversary, getFs() ) );
PageSwapper swapper = createSwapper( factory, file, bytesTotal, NO_CALLBACK, true );

ByteBufferPage page = createPage( bytesTotal );
Expand Down Expand Up @@ -502,7 +503,7 @@ public void mustHandleMischiefInPositionedVectoredRead() throws Exception
ThreadLocalRandom.current().nextBytes( data );

PageSwapperFactory factory = createSwapperFactory();
factory.setFileSystemAbstraction( getFs() );
factory.configure( getFs() );
File file = getFile();
PageSwapper swapper = createSwapper( factory, file, bytesTotal, NO_CALLBACK, true );
try
Expand All @@ -515,7 +516,7 @@ public void mustHandleMischiefInPositionedVectoredRead() throws Exception
}

RandomAdversary adversary = new RandomAdversary( 0.5, 0.0, 0.0 );
factory.setFileSystemAbstraction( new AdversarialFileSystemAbstraction( adversary, getFs() ) );
factory.configure( new AdversarialFileSystemAbstraction( adversary, getFs() ) );
swapper = createSwapper( factory, file, bytesPerPage, NO_CALLBACK, false );

ByteBufferPage[] pages = new ByteBufferPage[pageCount];
Expand Down Expand Up @@ -561,7 +562,7 @@ public void mustHandleMischiefInPositionedVectoredWrite() throws Exception
File file = getFile();
PageSwapperFactory factory = createSwapperFactory();
RandomAdversary adversary = new RandomAdversary( 0.5, 0.0, 0.0 );
factory.setFileSystemAbstraction( new AdversarialFileSystemAbstraction( adversary, getFs() ) );
factory.configure( new AdversarialFileSystemAbstraction( adversary, getFs() ) );
PageSwapper swapper = createSwapper( factory, file, bytesPerPage, NO_CALLBACK, true );

ByteBufferPage[] writePages = new ByteBufferPage[pageCount];
Expand Down
Expand Up @@ -382,7 +382,7 @@ private void runIteration( long timeout, TimeUnit unit ) throws Exception
}

PageSwapperFactory swapperFactory = new SingleFilePageSwapperFactory();
swapperFactory.setFileSystemAbstraction( fs );
swapperFactory.configure( fs );
MuninnPageCache cache = new MuninnPageCache( swapperFactory, cachePageCount, cachePageSize, tracer,
cursorTracerSupplier );
cache.setPrintExceptionsOnClose( false );
Expand Down
Expand Up @@ -87,7 +87,7 @@ public void run() throws Exception
try ( FileSystemAbstraction fs = new DefaultFileSystemAbstraction() )
{
PageSwapperFactory swapperFactory = new SingleFilePageSwapperFactory();
swapperFactory.setFileSystemAbstraction( fs );
swapperFactory.configure( fs );

try ( PageCache pageCacheUnderTest = new MuninnPageCache( swapperFactory, numberOfCachePages, cachePageSize,
tracer, pageCursorTracerSupplier ) )
Expand Down

This file was deleted.

Expand Up @@ -41,7 +41,8 @@
public class ConfiguringPageCacheFactory
{
private static final int pageSize = getInteger( ConfiguringPageCacheFactory.class, "pageSize", 8192 );
private final PageSwapperFactory swapperFactory;
private PageSwapperFactory swapperFactory;
private final FileSystemAbstraction fs;
private final Config config;
private final PageCacheTracer pageCacheTracer;
private final Log log;
Expand All @@ -60,46 +61,19 @@ public class ConfiguringPageCacheFactory
public ConfiguringPageCacheFactory( FileSystemAbstraction fs, Config config, PageCacheTracer pageCacheTracer,
PageCursorTracerSupplier pageCursorTracerSupplier, Log log )
{
this.swapperFactory = createAndConfigureSwapperFactory( fs, config, log );
this.fs = fs;
this.config = config;
this.pageCacheTracer = pageCacheTracer;
this.log = log;
this.pageCursorTracerSupplier = pageCursorTracerSupplier;
}

private PageSwapperFactory createAndConfigureSwapperFactory( FileSystemAbstraction fs, Config config, Log log )
{
String desiredImplementation = config.get( pagecache_swapper );

if ( desiredImplementation != null )
{
for ( PageSwapperFactory factory : Service.load( PageSwapperFactory.class ) )
{
if ( factory.implementationName().equals( desiredImplementation ) )
{
factory.setFileSystemAbstraction( fs );
if ( factory instanceof ConfigurablePageSwapperFactory )
{
ConfigurablePageSwapperFactory configurableFactory = (ConfigurablePageSwapperFactory) factory;
configurableFactory.configure( config );
}
log.info( "Configured " + pagecache_swapper.name() + ": " + desiredImplementation );
return factory;
}
}
throw new IllegalArgumentException( "Cannot find PageSwapperFactory: " + desiredImplementation );
}

SingleFilePageSwapperFactory factory = new SingleFilePageSwapperFactory();
factory.setFileSystemAbstraction( fs );
return factory;
}

public synchronized PageCache getOrCreatePageCache()
{
if ( pageCache == null )
{
pageCache = createPageCache();
this.swapperFactory = createAndConfigureSwapperFactory( fs, config, log );
this.pageCache = createPageCache();
}
return pageCache;
}
Expand Down Expand Up @@ -218,4 +192,30 @@ public void dumpConfiguration()

log.info( msg );
}

private static PageSwapperFactory createAndConfigureSwapperFactory( FileSystemAbstraction fs, Config config, Log log )
{
PageSwapperFactory factory = getPageSwapperFactory( config, log );
factory.configure( fs, config );
return factory;
}

private static PageSwapperFactory getPageSwapperFactory( Config config, Log log )
{
String desiredImplementation = config.get( pagecache_swapper );
if ( desiredImplementation != null )
{
for ( PageSwapperFactory factory : Service.load( PageSwapperFactory.class ) )
{
if ( factory.implementationName().equals( desiredImplementation ) )
{
log.info( "Configured " + pagecache_swapper.name() + ": " + desiredImplementation );
return factory;
}
}
throw new IllegalArgumentException( "Cannot find PageSwapperFactory: " + desiredImplementation );
}
return new SingleFilePageSwapperFactory();
}

}

0 comments on commit 51ef0bd

Please sign in to comment.