Search in sources :

Example 1 with Timeouts

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);
}
Also used : Timeouts(org.neo4j.cluster.timeout.Timeouts) MultiPaxosContext(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.context.MultiPaxosContext) ClusterConfiguration(org.neo4j.cluster.protocol.cluster.ClusterConfiguration) ElectionRole(org.neo4j.cluster.protocol.election.ElectionRole) SnapshotContext(org.neo4j.cluster.protocol.snapshot.SnapshotContext)

Example 2 with Timeouts

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)"));
}
Also used : LearnerMessage(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.LearnerMessage) Message(org.neo4j.cluster.com.message.Message) InstanceId(org.neo4j.cluster.InstanceId) Timeouts(org.neo4j.cluster.timeout.Timeouts) Config(org.neo4j.kernel.configuration.Config) MessageSender(org.neo4j.cluster.com.message.MessageSender) StateMachine(org.neo4j.cluster.statemachine.StateMachine) StateMachines(org.neo4j.cluster.StateMachines) TimeoutStrategy(org.neo4j.cluster.timeout.TimeoutStrategy) MessageSource(org.neo4j.cluster.com.message.MessageSource) ClusterConfiguration(org.neo4j.cluster.protocol.cluster.ClusterConfiguration) DelayedDirectExecutor(org.neo4j.cluster.DelayedDirectExecutor) ElectionRole(org.neo4j.cluster.protocol.election.ElectionRole) Executor(java.util.concurrent.Executor) DelayedDirectExecutor(org.neo4j.cluster.DelayedDirectExecutor) ElectionCredentialsProvider(org.neo4j.cluster.protocol.election.ElectionCredentialsProvider) MultiPaxosContext(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.context.MultiPaxosContext) ObjectInputStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectInputStreamFactory) ObjectOutputStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectOutputStreamFactory) AcceptorInstanceStore(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.AcceptorInstanceStore) AssertableLogProvider(org.neo4j.logging.AssertableLogProvider) Test(org.junit.Test)

Example 3 with Timeouts

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);
}
Also used : InstanceId(org.neo4j.cluster.InstanceId) Timeouts(org.neo4j.cluster.timeout.Timeouts) Config(org.neo4j.kernel.configuration.Config) ClusterContext(org.neo4j.cluster.protocol.cluster.ClusterContext) LearnerContext(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.LearnerContext) HeartbeatListener(org.neo4j.cluster.protocol.heartbeat.HeartbeatListener) Executor(java.util.concurrent.Executor) HeartbeatContext(org.neo4j.cluster.protocol.heartbeat.HeartbeatContext) ObjectInputStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectInputStreamFactory) ObjectOutputStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectOutputStreamFactory) Test(org.junit.Test)

Example 4 with Timeouts

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());
}
Also used : InstanceId(org.neo4j.cluster.InstanceId) Timeouts(org.neo4j.cluster.timeout.Timeouts) Config(org.neo4j.kernel.configuration.Config) ClusterContext(org.neo4j.cluster.protocol.cluster.ClusterContext) LearnerContext(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.LearnerContext) Executor(java.util.concurrent.Executor) HeartbeatContext(org.neo4j.cluster.protocol.heartbeat.HeartbeatContext) ObjectInputStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectInputStreamFactory) ObjectOutputStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectOutputStreamFactory) ClusterMessage(org.neo4j.cluster.protocol.cluster.ClusterMessage) Test(org.junit.Test)

Example 5 with Timeouts

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));
}
Also used : InstanceId(org.neo4j.cluster.InstanceId) Timeouts(org.neo4j.cluster.timeout.Timeouts) Config(org.neo4j.kernel.configuration.Config) ClusterContext(org.neo4j.cluster.protocol.cluster.ClusterContext) LearnerContext(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.LearnerContext) Executor(java.util.concurrent.Executor) HeartbeatContext(org.neo4j.cluster.protocol.heartbeat.HeartbeatContext) ObjectInputStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectInputStreamFactory) ObjectOutputStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectOutputStreamFactory) ClusterMessage(org.neo4j.cluster.protocol.cluster.ClusterMessage) Test(org.junit.Test)

Aggregations

Timeouts (org.neo4j.cluster.timeout.Timeouts)11 Test (org.junit.Test)10 InstanceId (org.neo4j.cluster.InstanceId)10 HeartbeatContext (org.neo4j.cluster.protocol.heartbeat.HeartbeatContext)8 Executor (java.util.concurrent.Executor)7 Config (org.neo4j.kernel.configuration.Config)7 ObjectInputStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectInputStreamFactory)6 ObjectOutputStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectOutputStreamFactory)6 ClusterConfiguration (org.neo4j.cluster.protocol.cluster.ClusterConfiguration)6 LearnerContext (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.LearnerContext)5 ClusterContext (org.neo4j.cluster.protocol.cluster.ClusterContext)5 HeartbeatListener (org.neo4j.cluster.protocol.heartbeat.HeartbeatListener)5 DelayedDirectExecutor (org.neo4j.cluster.DelayedDirectExecutor)4 ArrayList (java.util.ArrayList)3 MultiPaxosContext (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.context.MultiPaxosContext)3 ClusterMessage (org.neo4j.cluster.protocol.cluster.ClusterMessage)3 ElectionCredentialsProvider (org.neo4j.cluster.protocol.election.ElectionCredentialsProvider)2 ElectionRole (org.neo4j.cluster.protocol.election.ElectionRole)2 HashSet (java.util.HashSet)1 StateMachines (org.neo4j.cluster.StateMachines)1