use of org.neo4j.cluster.timeout.Timeouts in project neo4j by neo4j.
the class MultiPaxosServerFactory method newProtocolServer.
@Override
public ProtocolServer newProtocolServer(InstanceId me, TimeoutStrategy timeoutStrategy, MessageSource input, MessageSender output, AcceptorInstanceStore acceptorInstanceStore, ElectionCredentialsProvider electionCredentialsProvider, Executor stateMachineExecutor, ObjectInputStreamFactory objectInputStreamFactory, ObjectOutputStreamFactory objectOutputStreamFactory, Config config) {
DelayedDirectExecutor executor = new DelayedDirectExecutor(logging);
// Create state machines
Timeouts timeouts = new Timeouts(timeoutStrategy);
final MultiPaxosContext context = new MultiPaxosContext(me, Iterables.iterable(new ElectionRole(ClusterConfiguration.COORDINATOR)), new ClusterConfiguration(initialConfig.getName(), logging, initialConfig.getMemberURIs()), executor, logging, objectInputStreamFactory, objectOutputStreamFactory, acceptorInstanceStore, timeouts, electionCredentialsProvider, config);
SnapshotContext snapshotContext = new SnapshotContext(context.getClusterContext(), context.getLearnerContext());
return newProtocolServer(me, input, output, stateMachineExecutor, executor, timeouts, context, snapshotContext);
}
use of org.neo4j.cluster.timeout.Timeouts in project neo4j by neo4j.
the class HeartbeatStateTest method shouldLogFirstHeartbeatAfterTimeout.
@Test
public void shouldLogFirstHeartbeatAfterTimeout() throws Throwable {
// given
InstanceId instanceId = new InstanceId(1), otherInstance = new InstanceId(2);
ClusterConfiguration configuration = new ClusterConfiguration("whatever", NullLogProvider.getInstance(), "cluster://1", "cluster://2");
configuration.getMembers().put(otherInstance, URI.create("cluster://2"));
AssertableLogProvider internalLog = new AssertableLogProvider(true);
TimeoutStrategy timeoutStrategy = mock(TimeoutStrategy.class);
Timeouts timeouts = new Timeouts(timeoutStrategy);
Config config = mock(Config.class);
when(config.get(ClusterSettings.max_acceptors)).thenReturn(10);
MultiPaxosContext context = new MultiPaxosContext(instanceId, iterable(new ElectionRole("coordinator")), configuration, mock(Executor.class), internalLog, mock(ObjectInputStreamFactory.class), mock(ObjectOutputStreamFactory.class), mock(AcceptorInstanceStore.class), timeouts, mock(ElectionCredentialsProvider.class), config);
StateMachines stateMachines = new StateMachines(internalLog, mock(StateMachines.Monitor.class), mock(MessageSource.class), mock(MessageSender.class), timeouts, mock(DelayedDirectExecutor.class), command -> command.run(), instanceId);
stateMachines.addStateMachine(new StateMachine(context.getHeartbeatContext(), HeartbeatMessage.class, HeartbeatState.start, internalLog));
timeouts.tick(0);
when(timeoutStrategy.timeoutFor(any(Message.class))).thenReturn(5L);
// when
stateMachines.process(Message.internal(HeartbeatMessage.join));
stateMachines.process(Message.internal(HeartbeatMessage.i_am_alive, new HeartbeatMessage.IAmAliveState(otherInstance)).setHeader(Message.CREATED_BY, otherInstance.toString()));
for (int i = 1; i <= 15; i++) {
timeouts.tick(i);
}
// then
verify(timeoutStrategy, times(3)).timeoutTriggered(argThat(new MessageArgumentMatcher<>().onMessageType(HeartbeatMessage.timed_out)));
internalLog.assertExactly(inLog(HeartbeatState.class).debug("Received timed out for server 2"), inLog(HeartbeatContext.class).info("1(me) is now suspecting 2"), inLog(HeartbeatState.class).debug("Received timed out for server 2"), inLog(HeartbeatState.class).debug("Received timed out for server 2"));
internalLog.clear();
// when
stateMachines.process(Message.internal(HeartbeatMessage.i_am_alive, new HeartbeatMessage.IAmAliveState(otherInstance)).setHeader(Message.CREATED_BY, otherInstance.toString()));
// then
internalLog.assertExactly(inLog(HeartbeatState.class).debug("Received i_am_alive[2] after missing 3 (15ms)"));
}
use of org.neo4j.cluster.timeout.Timeouts 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.timeout.Timeouts 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());
}
use of org.neo4j.cluster.timeout.Timeouts 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));
}
Aggregations