use of org.neo4j.cluster.InstanceId in project neo4j by neo4j.
the class ElectionContextTest method testElectionNotOkQuorumFailedTwoInstances.
@Test
public void testElectionNotOkQuorumFailedTwoInstances() {
Set<InstanceId> failed = new HashSet<InstanceId>();
failed.add(new InstanceId(2));
Map<InstanceId, URI> members = new HashMap<InstanceId, URI>();
members.put(new InstanceId(1), URI.create("server1"));
members.put(new InstanceId(2), URI.create("server2"));
Config config = mock(Config.class);
when(config.get(ClusterSettings.max_acceptors)).thenReturn(10);
ClusterConfiguration clusterConfiguration = mock(ClusterConfiguration.class);
when(clusterConfiguration.getMembers()).thenReturn(members);
ClusterContext clusterContext = mock(ClusterContext.class);
when(clusterContext.getConfiguration()).thenReturn(clusterConfiguration);
MultiPaxosContext context = new MultiPaxosContext(new InstanceId(1), Iterables.<ElectionRole, ElectionRole>iterable(new ElectionRole("coordinator")), clusterConfiguration, mock(Executor.class), NullLogProvider.getInstance(), mock(ObjectInputStreamFactory.class), mock(ObjectOutputStreamFactory.class), mock(AcceptorInstanceStore.class), mock(Timeouts.class), mock(ElectionCredentialsProvider.class), config);
context.getHeartbeatContext().getFailed().addAll(failed);
ElectionContext toTest = context.getElectionContext();
assertFalse(toTest.electionOk());
}
use of org.neo4j.cluster.InstanceId in project neo4j by neo4j.
the class ElectionContextTest method baseTestForElectionOk.
private void baseTestForElectionOk(Set<InstanceId> failed, boolean moreThanQuorum) {
Map<InstanceId, URI> members = new HashMap<InstanceId, URI>();
members.put(new InstanceId(1), URI.create("server1"));
members.put(new InstanceId(2), URI.create("server2"));
members.put(new InstanceId(3), URI.create("server3"));
Config config = mock(Config.class);
when(config.get(ClusterSettings.max_acceptors)).thenReturn(10);
ClusterConfiguration clusterConfiguration = mock(ClusterConfiguration.class);
when(clusterConfiguration.getMembers()).thenReturn(members);
ClusterContext clusterContext = mock(ClusterContext.class);
when(clusterContext.getConfiguration()).thenReturn(clusterConfiguration);
MultiPaxosContext context = new MultiPaxosContext(new InstanceId(1), Iterables.iterable(new ElectionRole("coordinator")), clusterConfiguration, mock(Executor.class), NullLogProvider.getInstance(), mock(ObjectInputStreamFactory.class), mock(ObjectOutputStreamFactory.class), mock(AcceptorInstanceStore.class), mock(Timeouts.class), mock(ElectionCredentialsProvider.class), config);
context.getHeartbeatContext().getFailed().addAll(failed);
ElectionContext toTest = context.getElectionContext();
assertEquals(moreThanQuorum, !toTest.electionOk());
}
use of org.neo4j.cluster.InstanceId in project neo4j by neo4j.
the class ElectionStateTest method electionCompletingMakesItBeForgotten.
@Test
public void electionCompletingMakesItBeForgotten() throws Throwable {
// Given
String coordinatorRole = "coordinator";
InstanceId votingInstance = new InstanceId(2);
ElectionCredentials voteCredentialComparable = mock(ElectionCredentials.class);
ElectionContext context = mock(ElectionContext.class);
when(context.getLog(Mockito.<Class>any())).thenReturn(NullLog.getInstance());
when(context.getNeededVoteCount()).thenReturn(3);
when(context.getVoteCount(coordinatorRole)).thenReturn(3);
when(context.voted(coordinatorRole, votingInstance, voteCredentialComparable, 4)).thenReturn(true);
MessageHolder holder = mock(MessageHolder.class);
Message vote = Message.to(ElectionMessage.voted, URI.create("cluster://elector"), new ElectionMessage.VersionedVotedData(coordinatorRole, votingInstance, voteCredentialComparable, 4));
// When
election.handle(context, vote, holder);
// Then
verify(context, times(1)).forgetElection(coordinatorRole);
}
use of org.neo4j.cluster.InstanceId in project neo4j by neo4j.
the class ClusterContextTest method testElectionVersionIsResetWhenElectorChangesFromOtherToMe.
@Test
public void testElectionVersionIsResetWhenElectorChangesFromOtherToMe() throws Exception {
final String coordinatorRole = "coordinator";
final InstanceId me = new InstanceId(1);
final InstanceId winner = new InstanceId(2);
final InstanceId elector = new InstanceId(2);
HeartbeatContext heartbeatContext = mock(HeartbeatContext.class);
when(heartbeatContext.getFailed()).thenReturn(Collections.<InstanceId>emptySet());
Config config = mock(Config.class);
when(config.get(ClusterSettings.max_acceptors)).thenReturn(10);
MultiPaxosContext multiPaxosContext = new MultiPaxosContext(me, Iterables.iterable(new ElectionRole(coordinatorRole)), mock(ClusterConfiguration.class), new Executor() {
@Override
public void execute(Runnable command) {
command.run();
}
}, NullLogProvider.getInstance(), mock(ObjectInputStreamFactory.class), mock(ObjectOutputStreamFactory.class), mock(AcceptorInstanceStore.class), mock(Timeouts.class), mock(ElectionCredentialsProvider.class), config);
ClusterContext context = multiPaxosContext.getClusterContext();
ElectionContext electionContext = multiPaxosContext.getElectionContext();
ClusterListener listener = mock(ClusterListener.class);
context.setLastElectorVersion(5);
context.setLastElector(elector);
context.addClusterListener(listener);
context.elected(coordinatorRole, winner, elector, 6);
verify(listener, times(1)).elected(coordinatorRole, winner, null);
electionContext.forgetElection(coordinatorRole);
long expectedVersion = electionContext.newConfigurationStateChange().getVersion();
context.elected(coordinatorRole, winner, me, expectedVersion);
verify(listener, times(2)).elected(coordinatorRole, winner, null);
}
use of org.neo4j.cluster.InstanceId in project neo4j by neo4j.
the class ClusterContextTest method testElectionVersionIsUpdatedOnElectionFromSelfAndProperlyIgnoredIfOld.
@Test
public void testElectionVersionIsUpdatedOnElectionFromSelfAndProperlyIgnoredIfOld() throws Exception {
final String coordinatorRole = "coordinator";
final InstanceId me = new InstanceId(1);
final InstanceId winner = new InstanceId(2);
HeartbeatContext heartbeatContext = mock(HeartbeatContext.class);
when(heartbeatContext.getFailed()).thenReturn(Collections.<InstanceId>emptySet());
Config config = mock(Config.class);
when(config.get(ClusterSettings.max_acceptors)).thenReturn(10);
MultiPaxosContext multiPaxosContext = new MultiPaxosContext(me, Iterables.<ElectionRole, ElectionRole>iterable(new ElectionRole(coordinatorRole)), mock(ClusterConfiguration.class), new Executor() {
@Override
public void execute(Runnable command) {
command.run();
}
}, NullLogProvider.getInstance(), mock(ObjectInputStreamFactory.class), mock(ObjectOutputStreamFactory.class), mock(AcceptorInstanceStore.class), mock(Timeouts.class), mock(ElectionCredentialsProvider.class), config);
ClusterContext context = multiPaxosContext.getClusterContext();
ElectionContext electionContext = multiPaxosContext.getElectionContext();
ClusterListener listener = mock(ClusterListener.class);
context.addClusterListener(listener);
electionContext.forgetElection(coordinatorRole);
long expectedVersion = electionContext.newConfigurationStateChange().getVersion();
context.elected(coordinatorRole, winner, me, expectedVersion);
assertEquals(1, expectedVersion);
verify(listener, times(1)).elected(coordinatorRole, winner, null);
electionContext.forgetElection(coordinatorRole);
expectedVersion = electionContext.newConfigurationStateChange().getVersion();
context.elected(coordinatorRole, winner, me, expectedVersion);
assertEquals(2, expectedVersion);
verify(listener, times(2)).elected(coordinatorRole, winner, null);
context.elected(coordinatorRole, winner, me, expectedVersion - 1);
verifyNoMoreInteractions(listener);
}
Aggregations