Skip to content

Commit

Permalink
Rename attemp => retry, and add feature toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
fickludd authored and chrisvest committed Oct 26, 2018
1 parent 570e0a1 commit c0b96f4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
Expand Up @@ -35,6 +35,7 @@
import org.neo4j.kernel.impl.locking.LockWrapper;
import org.neo4j.kernel.impl.store.UnderlyingStorageException;
import org.neo4j.kernel.lifecycle.LifecycleAdapter;
import org.neo4j.util.FeatureToggles;

import static org.neo4j.kernel.impl.locking.LockWrapper.readLock;
import static org.neo4j.kernel.impl.locking.LockWrapper.writeLock;
Expand All @@ -48,6 +49,8 @@
@State( State.Strategy.CONCURRENT_HASH_MAP )
public abstract class AbstractKeyValueStore<Key> extends LifecycleAdapter
{
static final long MAX_LOOKUP_RETRY_COUNT = FeatureToggles.getLong( AbstractKeyValueStore.class, "maxLookupRetryCount", 1024 );

private final ReadWriteLock updateLock = new ReentrantReadWriteLock( /*fair=*/true );
private final Format format;
final RotationStrategy rotationStrategy;
Expand Down Expand Up @@ -92,8 +95,8 @@ public String toString()
protected final <Value> Value lookup( Key key, Reader<Value> reader ) throws IOException
{
ValueLookup<Value> lookup = new ValueLookup<>( reader );
int attemptsLeft = 1024;
while ( attemptsLeft > 0 )
long retriesLeft = MAX_LOOKUP_RETRY_COUNT;
while ( retriesLeft > 0 )
{
ProgressiveState<Key> originalState = this.state;
try
Expand All @@ -109,9 +112,9 @@ protected final <Value> Value lookup( Key key, Reader<Value> reader ) throws IOE
throw e;
}
}
attemptsLeft--;
retriesLeft--;
}
throw new IOException( String.format( "Failed to lookup `%s` in key value store", key ) );
throw new IOException( String.format( "Failed to lookup `%s` in key value store, after %d retries", key, MAX_LOOKUP_RETRY_COUNT ) );
}

/** Introspective feature, not thread safe. */
Expand Down
Expand Up @@ -28,6 +28,7 @@
import org.neo4j.kernel.impl.store.UnderlyingStorageException;

import static org.neo4j.io.pagecache.PagedFile.PF_SHARED_READ_LOCK;
import static org.neo4j.kernel.impl.store.kvstore.AbstractKeyValueStore.MAX_LOOKUP_RETRY_COUNT;
import static org.neo4j.kernel.impl.store.kvstore.BigEndianByteArrayBuffer.compare;
import static org.neo4j.kernel.impl.store.kvstore.BigEndianByteArrayBuffer.newBuffer;

Expand All @@ -36,7 +37,6 @@
*/
public class KeyValueStoreFile implements Closeable
{
private static final int ALLOWED_READ_ATTEMPTS = 1024;
private final PagedFile file;
private final int keySize;
private final int valueSize;
Expand Down Expand Up @@ -214,29 +214,29 @@ private static boolean visitable( BigEndianByteArrayBuffer key, boolean acceptZe
static void readKeyValuePair( PageCursor cursor, int offset, WritableBuffer key, WritableBuffer value )
throws IOException
{
int attemptsLeft = ALLOWED_READ_ATTEMPTS;
long retriesLeft = MAX_LOOKUP_RETRY_COUNT;
do
{
cursor.setOffset( offset );
key.getFrom( cursor );
value.getFrom( cursor );
}
while ( cursor.shouldRetry() && (--attemptsLeft) > 0 );
while ( cursor.shouldRetry() && (--retriesLeft) > 0 );

if ( cursor.checkAndClearBoundsFlag() )
{
throwOutOfBounds( cursor, offset );
}

if ( attemptsLeft == 0 )
if ( retriesLeft == 0 )
{
throwFailedRead( cursor, offset );
}
}

private static void throwFailedRead( PageCursor cursor, int offset )
{
throwReadError( cursor, offset, "Failed to read after " + ALLOWED_READ_ATTEMPTS + " attempts" );
throwReadError( cursor, offset, "Failed to read after " + MAX_LOOKUP_RETRY_COUNT + " retries" );
}

private static void throwOutOfBounds( PageCursor cursor, int offset )
Expand Down

0 comments on commit c0b96f4

Please sign in to comment.