From c90d8191cb632d23794df6705c13e740c1e8f184 Mon Sep 17 00:00:00 2001 From: MishaDemianenko Date: Tue, 25 Oct 2016 19:01:13 +0200 Subject: [PATCH] Fix buffer size evaluation in EphemeralFileSystemAbstraction for stores 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. --- .../EphemeralFileSystemAbstraction.java | 11 ++++-- ...hemeralFileSystemAbstractionCrashTest.java | 36 ++++++++++++++++--- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/community/io/src/test/java/org/neo4j/graphdb/mockfs/EphemeralFileSystemAbstraction.java b/community/io/src/test/java/org/neo4j/graphdb/mockfs/EphemeralFileSystemAbstraction.java index 98acd26522089..3ce4d42615c86 100644 --- a/community/io/src/test/java/org/neo4j/graphdb/mockfs/EphemeralFileSystemAbstraction.java +++ b/community/io/src/test/java/org/neo4j/graphdb/mockfs/EphemeralFileSystemAbstraction.java @@ -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 ); @@ -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(); @@ -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++; diff --git a/community/io/src/test/java/org/neo4j/graphdb/mockfs/EphemeralFileSystemAbstractionCrashTest.java b/community/io/src/test/java/org/neo4j/graphdb/mockfs/EphemeralFileSystemAbstractionCrashTest.java index 7ef9f302fee92..ba198d2cb75e3 100644 --- a/community/io/src/test/java/org/neo4j/graphdb/mockfs/EphemeralFileSystemAbstractionCrashTest.java +++ b/community/io/src/test/java/org/neo4j/graphdb/mockfs/EphemeralFileSystemAbstractionCrashTest.java @@ -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; @@ -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" ); @@ -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();