Skip to content

Commit

Permalink
Fix buffer size evaluation in EphemeralFileSystemAbstraction for stor…
Browse files Browse the repository at this point in the history
…es that over exceed standard sizes.

Fix calculation of store size index for stores that over exceed standard sizes.
Before in some of the cases newly evaluated capacity was actually less then requested.
  • Loading branch information
MishaDemianenko committed Oct 26, 2016
1 parent 05ed97e commit c90d819
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
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
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 c90d819

Please sign in to comment.