Skip to content

Commit

Permalink
Fail fast on unsupported log pruning configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
davidegrohmann committed Sep 27, 2016
1 parent df20a2c commit 96e28e1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 20 deletions.
Expand Up @@ -19,21 +19,46 @@
*/
package org.neo4j.kernel.impl.transaction.log.pruning;

import java.util.Objects;

import static org.neo4j.kernel.configuration.Settings.parseLongWithUnit;

public class ThresholdConfigParser
{
public static final class ThresholdConfigValue
{
public static final ThresholdConfigValue NO_PRUNING = new ThresholdConfigValue( "false", -1 );
static final ThresholdConfigValue NO_PRUNING = new ThresholdConfigValue( "false", -1 );
static final ThresholdConfigValue KEEP_LAST_FILE = new ThresholdConfigValue( "entries", 1 );

public final String type;
public final long value;

public ThresholdConfigValue( String type, long value )
ThresholdConfigValue( String type, long value )
{
this.type = type;
this.value = value;
}

@Override
public boolean equals( Object o )
{
if ( this == o )
{
return true;
}
if ( o == null || getClass() != o.getClass() )
{
return false;
}
ThresholdConfigValue that = (ThresholdConfigValue) o;
return value == that.value && Objects.equals( type, that.type );
}

@Override
public int hashCode()
{
return Objects.hash( type, value );
}
}

public static ThresholdConfigValue parse( String configValue )
Expand All @@ -55,17 +80,18 @@ public static ThresholdConfigValue parse( String configValue )
return ThresholdConfigValue.NO_PRUNING;
case "keep_none":
case "false":
return new ThresholdConfigValue( "entries", 1 );
return ThresholdConfigValue.KEEP_LAST_FILE;
default:
throw new IllegalArgumentException( "Invalid log pruning configuration value '" + configValue +
"'. The form is 'all' or '<number><unit> <type>' for example '100k txs' " +
"for the latest 100 000 transactions" );
}
}

long thresholdValue = parseLongWithUnit( tokens[0] );
String thresholdType = tokens[1];

return new ThresholdConfigValue( thresholdType, thresholdValue );
else
{
long thresholdValue = parseLongWithUnit( boolOrNumber );
String thresholdType = tokens[1];
return new ThresholdConfigValue( thresholdType, thresholdValue );
}
}
}
Expand Up @@ -24,6 +24,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.neo4j.kernel.impl.transaction.log.pruning.ThresholdConfigParser.ThresholdConfigValue;
import static org.neo4j.kernel.impl.transaction.log.pruning.ThresholdConfigParser.ThresholdConfigValue.KEEP_LAST_FILE;
import static org.neo4j.kernel.impl.transaction.log.pruning.ThresholdConfigParser.ThresholdConfigValue.NO_PRUNING;
import static org.neo4j.kernel.impl.transaction.log.pruning.ThresholdConfigParser.parse;

Expand All @@ -32,17 +33,15 @@ public class ThresholdConfigParserTest
@Test
public void parseTrue() throws Exception
{
ThresholdConfigValue aTrue = parse( "true" );
assertEquals( NO_PRUNING.type, aTrue.type );
assertEquals( NO_PRUNING.value, aTrue.value );
ThresholdConfigValue configValue = parse( "true" );
assertEquals( NO_PRUNING, configValue );
}

@Test
public void parseFalse() throws Exception
{
ThresholdConfigValue aFalse = parse( "false" );
assertEquals( "entries", aFalse.type );
assertEquals( 1, aFalse.value );
ThresholdConfigValue configValue = parse( "false" );
assertEquals( KEEP_LAST_FILE, configValue );
}

@Test(expected=IllegalArgumentException.class)
Expand Down
Expand Up @@ -19,26 +19,26 @@
*/
package org.neo4j.coreedge.core.consensus.log.segmented;


import org.neo4j.logging.LogProvider;

import static org.neo4j.kernel.impl.transaction.log.pruning.ThresholdConfigParser.ThresholdConfigValue;
import static org.neo4j.kernel.impl.transaction.log.pruning.ThresholdConfigParser.parse;

public class SegmentedRaftLogPruner
class SegmentedRaftLogPruner
{
private final ThresholdConfigValue parsedConfigOption;
private final CoreLogPruningStrategy pruneStrategy;

public SegmentedRaftLogPruner( String pruningStrategyConfig, LogProvider logProvider )
SegmentedRaftLogPruner( String pruningStrategyConfig, LogProvider logProvider )
{
parsedConfigOption = parse( pruningStrategyConfig );
pruneStrategy = getPruneStrategy( parsedConfigOption, logProvider );
}

private CoreLogPruningStrategy getPruneStrategy( ThresholdConfigValue configValue, LogProvider logProvider )
{
switch ( configValue.type )
String type = configValue.type;
switch ( type )
{
case "size":
return new SizeBasedLogPruningStrategy( parsedConfigOption.value );
Expand All @@ -47,15 +47,17 @@ private CoreLogPruningStrategy getPruneStrategy( ThresholdConfigValue configValu
return new EntryBasedLogPruningStrategy( parsedConfigOption.value, logProvider );
case "hours": // hours and days are currently not supported as such, default to no prune
case "days":
throw new IllegalArgumentException(
"Time based pruning not supported yet for the segmented raft log, got '" + type + "'." );
case "false":
return new NoPruningPruningStrategy();
default:
throw new IllegalArgumentException( "Invalid log pruning configuration value '" + configValue.value +
"'. Invalid type '" + configValue.type + "', valid are files, size, txs, entries, hours, days." );
"'. Invalid type '" + type + "', valid are files, size, txs, entries, hours, days." );
}
}

public long getIndexToPruneFrom( long safeIndex, Segments segments )
long getIndexToPruneFrom( long safeIndex, Segments segments )
{
return Math.min( safeIndex, pruneStrategy.getIndexToKeep( segments ) );
}
Expand Down

0 comments on commit 96e28e1

Please sign in to comment.