Skip to content

Commit

Permalink
Count and allow some timeouts in election performance test.
Browse files Browse the repository at this point in the history
  • Loading branch information
martinfurmanski committed Feb 8, 2016
1 parent 6e65936 commit c89e9b5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
Expand Up @@ -35,31 +35,40 @@ public class DisconnectLeaderScenario
{
private final Fixture fixture;
private final long electionTimeout;
private final List<Long> electionResults = new ArrayList<>();
private final List<Long> electionTimeResults = new ArrayList<>();
private long timeoutCount;

public DisconnectLeaderScenario( Fixture fixture, long electionTimeout )
{
this.fixture = fixture;
this.electionTimeout = electionTimeout;
}

public void run( long iterations ) throws InterruptedException, TimeoutException
public void run( long iterations, long leaderStabilityMaxTimeMillis ) throws InterruptedException
{
for ( int i = 0; i < iterations; i++ )
{
long electionTime = oneIteration();
electionResults.add( electionTime );
long electionTime;
try
{
electionTime = oneIteration( leaderStabilityMaxTimeMillis );
electionTimeResults.add( electionTime );
}
catch ( TimeoutException e )
{
timeoutCount++;
}
Thread.sleep( ThreadLocalRandom.current().nextLong( electionTimeout ) );
}
}

private long oneIteration() throws InterruptedException, TimeoutException
private long oneIteration( long leaderStabilityMaxTimeMillis ) throws InterruptedException, TimeoutException
{
RaftTestMember oldLeader = ElectionUtil.waitForLeaderAgreement( fixture.rafts, 10 * electionTimeout );
RaftTestMember oldLeader = ElectionUtil.waitForLeaderAgreement( fixture.rafts, leaderStabilityMaxTimeMillis );
long startTime = System.currentTimeMillis();

fixture.net.disconnect( oldLeader );
RaftTestMember newLeader = ElectionUtil.waitForLeaderAgreement( new FilteringIterable<>( fixture.rafts, raft -> !raft.identity().equals( oldLeader ) ), 10 * electionTimeout );
RaftTestMember newLeader = ElectionUtil.waitForLeaderAgreement( new FilteringIterable<>( fixture.rafts, raft -> !raft.identity().equals( oldLeader ) ), leaderStabilityMaxTimeMillis );
assert !newLeader.equals( oldLeader ); // this should be guaranteed by the waitForLeaderAgreement call

long endTime = System.currentTimeMillis();
Expand All @@ -82,12 +91,13 @@ public class Result
double collidingAverage;
double collisionRate;
long collisionCount;
long timeoutCount;

@Override
public String toString()
{
return String.format( "Result{nonCollidingAverage=%s, collidingAverage=%s, collisionRate=%s, collisionCount=%d}",
nonCollidingAverage, collidingAverage, collisionRate, collisionCount );
return String.format( "Result{nonCollidingAverage=%s, collidingAverage=%s, collisionRate=%s, collisionCount=%d, timeoutCount=%d}",
nonCollidingAverage, collidingAverage, collisionRate, collisionCount, timeoutCount );
}
}

Expand All @@ -101,7 +111,7 @@ public Result result()
long nonCollidingRuns = 0;
long nonCollidingSum = 0;

for ( long electionTime : electionResults )
for ( long electionTime : electionTimeResults )
{
if ( hadOneOrMoreCollisions( electionTime ) )
{
Expand All @@ -117,8 +127,9 @@ public Result result()

result.collidingAverage = collidingSum / (double) collidingRuns;
result.nonCollidingAverage = nonCollidingSum / (double) nonCollidingRuns;
result.collisionRate = collidingRuns / (double) electionResults.size();
result.collisionRate = collidingRuns / (double) electionTimeResults.size();
result.collisionCount = collidingRuns;
result.timeoutCount = timeoutCount;

return result;
}
Expand Down
Expand Up @@ -24,8 +24,10 @@
import org.neo4j.coreedge.raft.RaftTestNetwork;
import org.neo4j.coreedge.server.RaftTestMember;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.lessThan;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.neo4j.helpers.collection.IteratorUtil.asSet;

/**
Expand Down Expand Up @@ -56,7 +58,7 @@ public void electionPerformance_NormalConditions() throws Throwable
{
// when running scenario
fixture.boot();
scenario.run( iterations );
scenario.run( iterations, 10 * electionTimeout );
}
finally
{
Expand All @@ -76,6 +78,7 @@ public void electionPerformance_NormalConditions() throws Throwable
{
assertThat( result.collidingAverage, lessThan( 3000d ) );
}
assertThat( result.timeoutCount, is( 0L ) );
}

@Test
Expand All @@ -95,7 +98,7 @@ public void electionPerformance_RapidConditions() throws Throwable
{
// when running scenario
fixture.boot();
scenario.run( iterations );
scenario.run( iterations, 10 * electionTimeout );
}
finally
{
Expand All @@ -117,5 +120,6 @@ public void electionPerformance_RapidConditions() throws Throwable
{
assertThat( result.collidingAverage, lessThan( 120d ) );
}
assertThat( result.timeoutCount, lessThanOrEqualTo( 1L ) ); // for GC or whatever reason
}
}

0 comments on commit c89e9b5

Please sign in to comment.