Skip to content

Commit

Permalink
Merge pull request #8258 from MishaDemianenko/3.1-ephemeral-fs-resize
Browse files Browse the repository at this point in the history
Fix buffer size evaluation in EphemeralFileSystemAbstraction for big stores.
  • Loading branch information
burqen committed Oct 31, 2016
2 parents d0a6eab + c90d819 commit 1f093fa
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1184,8 +1184,7 @@ private void copyByteBufferContents( ByteBuffer from, ByteBuffer to )
*/
private ByteBuffer allocate( int sizeIndex )
{
int capacity = (sizeIndex < SIZES.length) ?
SIZES[sizeIndex] : ((sizeIndex - SIZES.length + 1) * SIZES[SIZES.length - 1]);
int capacity = capacity( sizeIndex );
try
{
return ByteBuffer.allocateDirect( capacity );
Expand All @@ -1204,6 +1203,12 @@ private ByteBuffer allocate( int sizeIndex )
}
}

private int capacity( int sizeIndex )
{
return (sizeIndex < SIZES.length) ?
SIZES[sizeIndex] : ((sizeIndex - SIZES.length + 1) * SIZES[SIZES.length - 1]);
}

void free()
{
assertNotFreed();
Expand Down Expand Up @@ -1269,7 +1274,7 @@ private void verifySize( int totalAmount )
// Double size each time, but after 1M only increase by 1M at a time, until required amount is reached.
int newSize = buf.capacity();
int sizeIndex = sizeIndexFor( newSize );
while ( newSize < totalAmount )
while ( capacity( sizeIndex ) < totalAmount )
{
newSize += Math.min( newSize, 1024 * 1024 );
sizeIndex++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
*/
package org.neo4j.graphdb.mockfs;

import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.nio.BufferUnderflowException;
Expand All @@ -31,24 +34,48 @@
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.junit.Test;

import org.neo4j.io.ByteUnit;
import org.neo4j.io.fs.StoreChannel;

import static java.nio.ByteBuffer.allocateDirect;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

public class EphemeralFileSystemAbstractionCrashTest
{

private EphemeralFileSystemAbstraction fs;

@Before
public void setUp()
{
fs = new EphemeralFileSystemAbstraction();
}

@Test
public void allowStoreThatExceedPredefinedSizes() throws IOException
{
File aFile = new File( "test" );
StoreChannel channel = fs.open( aFile, "rw" );

ByteBuffer buffer = allocateDirect( Long.BYTES );
int mebiBytes = (int) ByteUnit.mebiBytes( 1 );
for ( int position = mebiBytes + 42; position < 10_000_000; position += mebiBytes )
{
buffer.putLong( 1 );
buffer.flip();
channel.write( buffer, position );
buffer.clear();
}
channel.close();
}

@Test
public void shouldNotLoseDataForcedBeforeFileSystemCrashes() throws Exception
{
// given
int numberOfBytesForced = 8;

EphemeralFileSystemAbstraction fs = new EphemeralFileSystemAbstraction();
File aFile = new File( "yo" );

StoreChannel channel = fs.open( aFile, "rw" );
Expand All @@ -69,7 +96,6 @@ public void shouldNotLoseDataForcedBeforeFileSystemCrashes() throws Exception
@Test
public void shouldBeConsistentAfterConcurrentWritesAndCrashes() throws Exception
{
EphemeralFileSystemAbstraction fs = new EphemeralFileSystemAbstraction();
File aFile = new File( "contendedFile" );

ExecutorService executorService = Executors.newCachedThreadPool();
Expand Down

0 comments on commit 1f093fa

Please sign in to comment.