Skip to content

Commit

Permalink
GBPTreeIT use check-pointing and avoid performing consistency check in
Browse files Browse the repository at this point in the history
…@after

Checkpointing exposes a lot of new code paths to be executed and should naturally
be part of concurrency testing.

If consistency check failed in @after, no random seed would be printed.
  • Loading branch information
burqen committed Feb 14, 2017
1 parent e96da35 commit 68769c6
Showing 1 changed file with 58 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package org.neo4j.index.internal.gbptree;

import org.apache.commons.lang3.mutable.MutableLong;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.RuleChain;
Expand All @@ -34,6 +33,7 @@
import java.util.concurrent.Executors;

import org.neo4j.cursor.RawCursor;
import org.neo4j.io.pagecache.IOLimiter;
import org.neo4j.io.pagecache.PageCache;
import org.neo4j.test.rule.PageCacheRule;
import org.neo4j.test.rule.RandomRule;
Expand All @@ -44,7 +44,6 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.rules.RuleChain.outerRule;

import static org.neo4j.index.internal.gbptree.GBPTree.NO_MONITOR;
import static org.neo4j.test.rule.PageCacheRule.config;

Expand Down Expand Up @@ -77,78 +76,80 @@ private GBPTree<MutableLong,MutableLong> createIndex( int pageSize, GBPTree.Moni
layout, 0/*use whatever page cache says*/, monitor );
}

@After
public void consistencyCheckAndClose() throws IOException
{
threadPool.shutdownNow();
index.consistencyCheck();
index.close();
}

@Test
public void shouldStayCorrectAfterRandomModifications() throws Exception
{
// GIVEN
GBPTree<MutableLong,MutableLong> index = createIndex( 256 );
Comparator<MutableLong> keyComparator = layout;
Map<MutableLong,MutableLong> data = new TreeMap<>( keyComparator );
int count = 100;
int totalNumberOfRounds = 10;
for ( int i = 0; i < count; i++ )
{
data.put( randomKey( random.random() ), randomKey( random.random() ) );
}

// WHEN
try ( Writer<MutableLong,MutableLong> writer = index.writer() )
try
{
for ( Map.Entry<MutableLong,MutableLong> entry : data.entrySet() )
// GIVEN
GBPTree<MutableLong,MutableLong> index = createIndex( 256 );
Comparator<MutableLong> keyComparator = layout;
Map<MutableLong,MutableLong> data = new TreeMap<>( keyComparator );
int count = 100;
int totalNumberOfRounds = 10;
for ( int i = 0; i < count; i++ )
{
writer.put( entry.getKey(), entry.getValue() );
data.put( randomKey( random.random() ), randomKey( random.random() ) );
}
}

for ( int round = 0; round < totalNumberOfRounds; round++ )
{
// THEN
for ( int i = 0; i < count; i++ )
// WHEN
try ( Writer<MutableLong,MutableLong> writer = index.writer() )
{
MutableLong first = randomKey( random.random() );
MutableLong second = randomKey( random.random() );
MutableLong from, to;
if ( first.longValue() < second.longValue() )
for ( Map.Entry<MutableLong,MutableLong> entry : data.entrySet() )
{
from = first;
to = second;
writer.put( entry.getKey(), entry.getValue() );
}
else
{
from = second;
to = first;
}
Map<MutableLong,MutableLong> expectedHits = expectedHits( data, from, to, keyComparator );
try ( RawCursor<Hit<MutableLong,MutableLong>,IOException> result = index.seek( from, to ) )
}

for ( int round = 0; round < totalNumberOfRounds; round++ )
{
// THEN
for ( int i = 0; i < count; i++ )
{
while ( result.next() )
MutableLong first = randomKey( random.random() );
MutableLong second = randomKey( random.random() );
MutableLong from, to;
if ( first.longValue() < second.longValue() )
{
MutableLong key = result.get().key();
if ( expectedHits.remove( key ) == null )
{
fail( "Unexpected hit " + key + " when searching for " + from + " - " + to );
}

assertTrue( keyComparator.compare( key, from ) >= 0 );
assertTrue( keyComparator.compare( key, to ) < 0 );
from = first;
to = second;
}
if ( !expectedHits.isEmpty() )
else
{
fail( "There were results which were expected to be returned, but weren't:" + expectedHits +
" when searching range " + from + " - " + to );
from = second;
to = first;
}
Map<MutableLong,MutableLong> expectedHits = expectedHits( data, from, to, keyComparator );
try ( RawCursor<Hit<MutableLong,MutableLong>,IOException> result = index.seek( from, to ) )
{
while ( result.next() )
{
MutableLong key = result.get().key();
if ( expectedHits.remove( key ) == null )
{
fail( "Unexpected hit " + key + " when searching for " + from + " - " + to );
}

assertTrue( keyComparator.compare( key, from ) >= 0 );
assertTrue( keyComparator.compare( key, to ) < 0 );
}
if ( !expectedHits.isEmpty() )
{
fail( "There were results which were expected to be returned, but weren't:" + expectedHits +
" when searching range " + from + " - " + to );
}
}
}
}

randomlyModifyIndex( index, data, random.random(), (double) round / totalNumberOfRounds );
index.checkpoint( IOLimiter.unlimited() );
randomlyModifyIndex( index, data, random.random(), (double) round / totalNumberOfRounds );
}
}
finally
{
threadPool.shutdownNow();
index.consistencyCheck();
index.close();
}
}

Expand Down

0 comments on commit 68769c6

Please sign in to comment.