use of org.neo4j.cluster.protocol.heartbeat.HeartbeatContext in project neo4j by neo4j.
the class ClusterContextTest method testElectionVersionIsResetWhenElectorChangesFromMeToOther.
@Test
public void testElectionVersionIsResetWhenElectorChangesFromMeToOther() 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.<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.setLastElectorVersion(5);
context.setLastElector(me);
context.addClusterListener(listener);
long expectedVersion = electionContext.newConfigurationStateChange().getVersion();
context.elected(coordinatorRole, winner, me, expectedVersion);
verify(listener, times(1)).elected(coordinatorRole, winner, null);
context.elected(coordinatorRole, winner, elector, 2);
verify(listener, times(2)).elected(coordinatorRole, winner, null);
context.elected(coordinatorRole, winner, elector, 3);
verify(listener, times(3)).elected(coordinatorRole, winner, null);
context.elected(coordinatorRole, winner, elector, 2);
verifyNoMoreInteractions(listener);
}
use of org.neo4j.cluster.protocol.heartbeat.HeartbeatContext in project neo4j by neo4j.
the class ClusterMockTest method verifyConfigurations.
public void verifyConfigurations(VerifyInstanceConfiguration[] toCheckAgainst) {
logger.getLogger().fine("Verify configurations against given");
List<URI> members;
Map<String, InstanceId> roles;
Set<InstanceId> failed;
List<AssertionError> errors = new LinkedList<AssertionError>();
List<TestProtocolServer> protocolServers = network.getServers();
assertEquals("You must provide a configuration for all instances", protocolServers.size(), toCheckAgainst.length);
for (int j = 0; j < protocolServers.size(); j++) {
members = toCheckAgainst[j].members;
roles = toCheckAgainst[j].roles;
failed = toCheckAgainst[j].failed;
StateMachines stateMachines = protocolServers.get(j).getServer().getStateMachines();
State<?, ?> clusterState = stateMachines.getStateMachine(ClusterMessage.class).getState();
if (!clusterState.equals(ClusterState.entered)) {
logger.getLogger().warning("Instance " + (j + 1) + " is not in the cluster (" + clusterState + ")");
continue;
}
ClusterContext context = (ClusterContext) stateMachines.getStateMachine(ClusterMessage.class).getContext();
HeartbeatContext heartbeatContext = (HeartbeatContext) stateMachines.getStateMachine(HeartbeatMessage.class).getContext();
ClusterConfiguration clusterConfiguration = context.getConfiguration();
if (!clusterConfiguration.getMemberURIs().isEmpty()) {
logger.getLogger().fine(" Server " + (j + 1) + ": Cluster:" + clusterConfiguration.getMemberURIs() + ", Roles:" + clusterConfiguration.getRoles() + ", Failed:" + heartbeatContext.getFailed());
verifyConfigurations(stateMachines, members, roles, failed, errors);
}
}
if (!errors.isEmpty()) {
for (AssertionError error : errors) {
logger.getLogger().severe(error.toString());
}
throw errors.get(0);
}
}
use of org.neo4j.cluster.protocol.heartbeat.HeartbeatContext in project neo4j by neo4j.
the class AtomicBroadcastContextImplTest method shouldHasQuorumWhenTwoMachinesAliveInAClusterWithThreeMachines.
@Test
public void shouldHasQuorumWhenTwoMachinesAliveInAClusterWithThreeMachines() {
//Given
HeartbeatContext heartbeatContext = mock(HeartbeatContext.class);
CommonContextState commonState = mock(CommonContextState.class);
ClusterConfiguration configuration = mock(ClusterConfiguration.class);
when(heartbeatContext.getAlive()).thenReturn(ids(2));
when(commonState.configuration()).thenReturn(configuration);
when(configuration.getMembers()).thenReturn(members(3));
AtomicBroadcastContextImpl context = new AtomicBroadcastContextImpl(null, commonState, null, null, null, // we do not care about other args
heartbeatContext);
//When
boolean hasQuorum = context.hasQuorum();
//Then
assertTrue(hasQuorum);
}
use of org.neo4j.cluster.protocol.heartbeat.HeartbeatContext in project neo4j by neo4j.
the class ClusterContextImplTest method electorFailingMustCauseElectorVersionToBeReset.
/*
* This test ensures that an instance that is marked as failed has its elector version reset. This means that
* the instance, once it comes back, will still be able to do elections even if it lost state
*/
@Test
public void electorFailingMustCauseElectorVersionToBeReset() throws Exception {
// Given
InstanceId me = new InstanceId(1);
InstanceId elector = new InstanceId(2);
CommonContextState commonContextState = mock(CommonContextState.class, RETURNS_MOCKS);
Timeouts timeouts = mock(Timeouts.class);
Executor executor = mock(Executor.class);
HeartbeatContext heartbeatContext = mock(HeartbeatContext.class);
ArgumentCaptor<HeartbeatListener> listenerCaptor = ArgumentCaptor.forClass(HeartbeatListener.class);
ClusterContext context = new ClusterContextImpl(me, commonContextState, NullLogProvider.getInstance(), timeouts, executor, mock(ObjectOutputStreamFactory.class), mock(ObjectInputStreamFactory.class), mock(LearnerContext.class), heartbeatContext, mock(Config.class));
verify(heartbeatContext).addHeartbeatListener(listenerCaptor.capture());
HeartbeatListener theListener = listenerCaptor.getValue();
// This means instance 2 was the elector at version 8
context.setLastElector(elector);
context.setLastElectorVersion(8);
// When
theListener.failed(elector);
// Then
assertEquals(context.getLastElector(), InstanceId.NONE);
assertEquals(context.getLastElectorVersion(), ClusterContextImpl.NO_ELECTOR_VERSION);
}
use of org.neo4j.cluster.protocol.heartbeat.HeartbeatContext in project neo4j by neo4j.
the class ClusterContextImplTest method shouldUpdateDiscoveryHeaderWithContactingInstances.
@Test
public void shouldUpdateDiscoveryHeaderWithContactingInstances() throws Exception {
// Given
InstanceId me = new InstanceId(1);
InstanceId joiningOne = new InstanceId(2);
InstanceId joiningTwo = new InstanceId(3);
CommonContextState commonContextState = mock(CommonContextState.class, RETURNS_MOCKS);
Timeouts timeouts = mock(Timeouts.class);
Executor executor = mock(Executor.class);
HeartbeatContext heartbeatContext = mock(HeartbeatContext.class);
ClusterContext context = new ClusterContextImpl(me, commonContextState, NullLogProvider.getInstance(), timeouts, executor, mock(ObjectOutputStreamFactory.class), mock(ObjectInputStreamFactory.class), mock(LearnerContext.class), heartbeatContext, mock(Config.class));
ClusterMessage.ConfigurationRequestState requestOne = mock(ClusterMessage.ConfigurationRequestState.class);
when(requestOne.getJoiningId()).thenReturn(joiningOne);
ClusterMessage.ConfigurationRequestState requestTwo = mock(ClusterMessage.ConfigurationRequestState.class);
when(requestTwo.getJoiningId()).thenReturn(joiningTwo);
// When
// Instance 2 contacts us twice and Instance 3 contacts us once
// discovery headers are random here
context.addContactingInstance(requestOne, "4, 5");
context.addContactingInstance(requestOne, "4, 5");
context.addContactingInstance(requestTwo, "2, 5");
// Then
// The discovery header we generate should still contain one copy of each instance
assertEquals("2,3", context.generateDiscoveryHeader());
}
Aggregations