use of org.neo4j.cluster.protocol.heartbeat.HeartbeatContext in project neo4j by neo4j.
the class ClusterContextImplTest method shouldKeepTrackOfInstancesWeHaveContacted.
@Test
public void shouldKeepTrackOfInstancesWeHaveContacted() 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 two contacts us but we are not in the header
context.addContactingInstance(requestOne, "4, 5");
// Then we haven't contacted instance 2
assertFalse(context.haveWeContactedInstance(requestOne));
// When
// Instance 2 reports that we have contacted it after all
context.addContactingInstance(requestOne, "4, 5, 1");
// Then
assertTrue(context.haveWeContactedInstance(requestOne));
// When
// Instance 3 says we have contacted it
context.addContactingInstance(requestTwo, "2, 5, 1");
// Then
assertTrue(context.haveWeContactedInstance(requestTwo));
// When
// For some reason we are not in the header of 3 in subsequent responses (a delayed one, for example)
context.addContactingInstance(requestTwo, "2, 5");
// Then
// The state should still keep the fact we've contacted it already
assertTrue(context.haveWeContactedInstance(requestTwo));
}
use of org.neo4j.cluster.protocol.heartbeat.HeartbeatContext in project neo4j by neo4j.
the class HeartbeatContextImplTest method majorityOfNonSuspectedInstancesShouldBeEnoughToMarkAnInstanceAsFailed.
@Test
public void majorityOfNonSuspectedInstancesShouldBeEnoughToMarkAnInstanceAsFailed() throws Exception {
// Given
InstanceId me = new InstanceId(1);
InstanceId member2 = new InstanceId(2);
InstanceId member3 = new InstanceId(3);
InstanceId member4 = new InstanceId(4);
InstanceId member5 = new InstanceId(5);
Timeouts timeouts = mock(Timeouts.class);
CommonContextState commonState = mock(CommonContextState.class);
ClusterConfiguration configuration = mock(ClusterConfiguration.class);
when(commonState.configuration()).thenReturn(configuration);
when(configuration.getMembers()).thenReturn(members(5));
when(configuration.getMemberIds()).thenReturn(ids(5));
DelayedDirectExecutor executor = new DelayedDirectExecutor(NullLogProvider.getInstance());
HeartbeatContext context = new HeartbeatContextImpl(me, commonState, NullLogProvider.getInstance(), timeouts, executor);
final List<InstanceId> failed = new ArrayList<>(4);
HeartbeatListener listener = new HeartbeatListener() {
@Override
public void failed(InstanceId server) {
failed.add(server);
}
@Override
public void alive(InstanceId server) {
failed.remove(server);
}
};
context.addHeartbeatListener(listener);
// when
// just two suspicions come, no extra failing action should be taken since this is not majority
context.suspect(member2);
context.suspect(member3);
executor.drain();
// then
assertEquals(0, failed.size());
// when
// the another instance suspects them, therefore have a majority of non suspected, then 2 and 3 must fail
Set<InstanceId> suspicionsFrom5 = new HashSet<>();
suspicionsFrom5.add(member2);
suspicionsFrom5.add(member3);
context.suspicions(member5, suspicionsFrom5);
executor.drain();
// then
assertEquals(2, failed.size());
assertTrue(failed.contains(member2));
assertTrue(failed.contains(member3));
// when
// an instance sends a heartbeat, it should be set as alive
context.alive(member2);
executor.drain();
// then
assertEquals(1, failed.size());
assertTrue(failed.contains(member3));
}
use of org.neo4j.cluster.protocol.heartbeat.HeartbeatContext in project neo4j by neo4j.
the class HeartbeatContextImplTest method shouldFailAllInstancesIfAllOtherInstancesAreSuspected.
@Test
public void shouldFailAllInstancesIfAllOtherInstancesAreSuspected() throws Exception {
// Given
InstanceId me = new InstanceId(1);
InstanceId member2 = new InstanceId(2);
InstanceId member3 = new InstanceId(3);
Timeouts timeouts = mock(Timeouts.class);
CommonContextState commonState = mock(CommonContextState.class);
ClusterConfiguration configuration = mock(ClusterConfiguration.class);
when(commonState.configuration()).thenReturn(configuration);
when(configuration.getMembers()).thenReturn(members(3));
when(configuration.getMemberIds()).thenReturn(ids(3));
DelayedDirectExecutor executor = new DelayedDirectExecutor(NullLogProvider.getInstance());
HeartbeatContext context = new HeartbeatContextImpl(me, commonState, NullLogProvider.getInstance(), timeouts, executor);
List<InstanceId> failed = new ArrayList<>(2);
HeartbeatListener listener = new HeartbeatListener() {
@Override
public void failed(InstanceId server) {
failed.add(server);
}
@Override
public void alive(InstanceId server) {
failed.remove(server);
}
};
context.addHeartbeatListener(listener);
// when
// just one suspicion comes, no extra failing action should be taken
context.suspect(member2);
executor.drain();
// then
assertEquals(0, failed.size());
// when
// the other instance is suspected, all instances must be marked as failed
context.suspect(member3);
executor.drain();
// then
assertEquals(2, failed.size());
assertTrue(failed.contains(member2));
assertTrue(failed.contains(member3));
// when
// one of them comes alive again, only that instance should be marked as alive
context.alive(member2);
executor.drain();
// then
assertEquals(1, failed.size());
assertTrue(failed.contains(member3));
}
use of org.neo4j.cluster.protocol.heartbeat.HeartbeatContext in project neo4j by neo4j.
the class MultiPaxosServer method config.
public void config() {
ClusterConfiguration configuration = ((ClusterContext) server.getStateMachines().getStateMachine(ClusterMessage.class).getContext()).getConfiguration();
Collection<InstanceId> failed = ((HeartbeatContext) server.getStateMachines().getStateMachine(HeartbeatMessage.class).getContext()).getFailed();
}
use of org.neo4j.cluster.protocol.heartbeat.HeartbeatContext in project neo4j by neo4j.
the class ElectionContextTest method electionBeingForgottenMustIncreaseElectionId.
@Test
public void electionBeingForgottenMustIncreaseElectionId() throws Exception {
// Given
final String coordinatorRole = "coordinator";
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);
ElectionContext context = new MultiPaxosContext(new InstanceId(1), Iterables.iterable(new ElectionRole(coordinatorRole)), mock(ClusterConfiguration.class), mock(Executor.class), NullLogProvider.getInstance(), mock(ObjectInputStreamFactory.class), mock(ObjectOutputStreamFactory.class), mock(AcceptorInstanceStore.class), mock(Timeouts.class), mock(ElectionCredentialsProvider.class), config).getElectionContext();
ElectionContext.VoteRequest voteRequestBefore = context.voteRequestForRole(new ElectionRole(coordinatorRole));
context.forgetElection(coordinatorRole);
ElectionContext.VoteRequest voteRequestAfter = context.voteRequestForRole(new ElectionRole(coordinatorRole));
assertEquals(voteRequestBefore.getVersion() + 1, voteRequestAfter.getVersion());
}
Aggregations