Search in sources :

Example 1 with StateMachine

use of org.neo4j.cluster.statemachine.StateMachine in project neo4j by neo4j.

the class ProtocolServer method toString.

@Override
public String toString() {
    StringBuilder builder = new StringBuilder();
    builder.append("Instance URI: ").append(boundAt.toString()).append("\n");
    for (StateMachine stateMachine : stateMachines.getStateMachines()) {
        builder.append("  ").append(stateMachine).append("\n");
    }
    return builder.toString();
}
Also used : StateMachine(org.neo4j.cluster.statemachine.StateMachine)

Example 2 with StateMachine

use of org.neo4j.cluster.statemachine.StateMachine in project neo4j by neo4j.

the class ClusterClient method dumpDiagnostics.

public void dumpDiagnostics(StringBuilder appendTo) {
    StateMachines stateMachines = protocolServer.getStateMachines();
    for (StateMachine stateMachine : stateMachines.getStateMachines()) {
        appendTo.append("   ").append(stateMachine.getMessageType().getSimpleName()).append(":").append(stateMachine.getState().toString()).append("\n");
    }
    appendTo.append("Current timeouts:\n");
    for (Map.Entry<Object, Timeouts.Timeout> objectTimeoutEntry : stateMachines.getTimeouts().getTimeouts().entrySet()) {
        appendTo.append(objectTimeoutEntry.getKey().toString()).append(":").append(objectTimeoutEntry.getValue().getTimeoutMessage().toString());
    }
}
Also used : StateMachine(org.neo4j.cluster.statemachine.StateMachine) StateMachines(org.neo4j.cluster.StateMachines) Map(java.util.Map)

Example 3 with StateMachine

use of org.neo4j.cluster.statemachine.StateMachine 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 4 with StateMachine

use of org.neo4j.cluster.statemachine.StateMachine in project neo4j by neo4j.

the class ClusterInstance method newCopy.

public ClusterInstance newCopy() {
    // A very invasive method of cloning a protocol server. Nonetheless, since this is mostly an experiment at this
    // point, it seems we can refactor later on to have a cleaner clone mechanism.
    // Because state machines share state, and are simultaneously conceptually unaware of each other, implementing
    // a clean snapshot mechanism is very hard. I've opted for having a dirty one here in the test code rather
    // than introducing a hack into the runtime code.
    ProverTimeouts timeoutsSnapshot = timeouts.snapshot();
    InMemoryAcceptorInstanceStore snapshotAcceptorInstances = acceptorInstanceStore.snapshot();
    ClusterInstanceOutput output = new ClusterInstanceOutput(uri);
    ClusterInstanceInput input = new ClusterInstanceInput();
    DelayedDirectExecutor executor = new DelayedDirectExecutor(logging);
    ObjectStreamFactory objectStreamFactory = new ObjectStreamFactory();
    MultiPaxosContext snapshotCtx = ctx.snapshot(logging, timeoutsSnapshot, executor, snapshotAcceptorInstances, objectStreamFactory, objectStreamFactory, new DefaultElectionCredentialsProvider(server.getServerId(), new StateVerifierLastTxIdGetter(), new MemberInfoProvider()));
    List<StateMachine> snapshotMachines = new ArrayList<>();
    for (StateMachine stateMachine : server.getStateMachines().getStateMachines()) {
        snapshotMachines.add(snapshotStateMachine(logging, snapshotCtx, stateMachine));
    }
    ProtocolServer snapshotProtocolServer = factory.constructSupportingInfrastructureFor(server.getServerId(), input, output, executor, timeoutsSnapshot, stateMachineExecutor, snapshotCtx, snapshotMachines.toArray(new StateMachine[snapshotMachines.size()]));
    return new ClusterInstance(stateMachineExecutor, logging, factory, snapshotProtocolServer, snapshotCtx, snapshotAcceptorInstances, timeoutsSnapshot, input, output, uri);
}
Also used : StateMachine(org.neo4j.cluster.statemachine.StateMachine) ArrayList(java.util.ArrayList) HighAvailabilityMemberInfoProvider(org.neo4j.kernel.ha.HighAvailabilityMemberInfoProvider) DelayedDirectExecutor(org.neo4j.cluster.DelayedDirectExecutor) DefaultElectionCredentialsProvider(org.neo4j.kernel.ha.cluster.DefaultElectionCredentialsProvider) ObjectStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectStreamFactory) ProtocolServer(org.neo4j.cluster.ProtocolServer) MultiPaxosContext(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.context.MultiPaxosContext) InMemoryAcceptorInstanceStore(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.InMemoryAcceptorInstanceStore)

Example 5 with StateMachine

use of org.neo4j.cluster.statemachine.StateMachine in project neo4j by neo4j.

the class StateMachinesTest method shouldAlwaysAddItsInstanceIdToOutgoingMessages.

@Test
public void shouldAlwaysAddItsInstanceIdToOutgoingMessages() throws Exception {
    InstanceId me = new InstanceId(42);
    final List<Message> sentOut = new LinkedList<Message>();
    /*
         * Lots of setup required. Must have a sender that keeps messages so we can see what the machine sent out.
         * We must have the StateMachines actually delegate the incoming message and retrieve the generated outgoing.
         * That means we need an actual StateMachine with a registered MessageType. And most of those are void
         * methods, which means lots of Answer objects.
         */
    // Given
    MessageSender sender = mock(MessageSender.class);
    // The sender, which adds messages outgoing to the list above.
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            sentOut.addAll((Collection<? extends Message>) invocation.getArguments()[0]);
            return null;
        }
    }).when(sender).process(Matchers.<List<Message<? extends MessageType>>>any());
    StateMachines stateMachines = new StateMachines(NullLogProvider.getInstance(), mock(StateMachines.Monitor.class), mock(MessageSource.class), sender, mock(Timeouts.class), mock(DelayedDirectExecutor.class), new Executor() {

        @Override
        public void execute(Runnable command) {
            command.run();
        }
    }, me);
    // The state machine, which has a TestMessage message type and simply adds a TO header to the messages it
    // is handed to handle.
    StateMachine machine = mock(StateMachine.class);
    when(machine.getMessageType()).then(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            return TestMessage.class;
        }
    });
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            Message message = (Message) invocation.getArguments()[0];
            MessageHolder holder = (MessageHolder) invocation.getArguments()[1];
            message.setHeader(Message.TO, "to://neverland");
            holder.offer(message);
            return null;
        }
    }).when(machine).handle(any(Message.class), any(MessageHolder.class));
    stateMachines.addStateMachine(machine);
    // When
    stateMachines.process(Message.internal(TestMessage.message1));
    // Then
    assertEquals("StateMachines should not make up messages from thin air", 1, sentOut.size());
    Message sent = sentOut.get(0);
    assertTrue("StateMachines should add the instance-id header", sent.hasHeader(Message.INSTANCE_ID));
    assertEquals("StateMachines should add instance-id header that has the correct value", me.toString(), sent.getHeader(Message.INSTANCE_ID));
}
Also used : Message(org.neo4j.cluster.com.message.Message) MessageSender(org.neo4j.cluster.com.message.MessageSender) Timeouts(org.neo4j.cluster.timeout.Timeouts) StateMachine(org.neo4j.cluster.statemachine.StateMachine) MessageSource(org.neo4j.cluster.com.message.MessageSource) LinkedList(java.util.LinkedList) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) MessageHolder(org.neo4j.cluster.com.message.MessageHolder) Executor(java.util.concurrent.Executor) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Collection(java.util.Collection) MessageType(org.neo4j.cluster.com.message.MessageType) Test(org.junit.Test)

Aggregations

StateMachine (org.neo4j.cluster.statemachine.StateMachine)8 Executor (java.util.concurrent.Executor)3 Test (org.junit.Test)3 MessageSender (org.neo4j.cluster.com.message.MessageSender)3 MessageSource (org.neo4j.cluster.com.message.MessageSource)3 Timeouts (org.neo4j.cluster.timeout.Timeouts)3 ArrayList (java.util.ArrayList)2 DelayedDirectExecutor (org.neo4j.cluster.DelayedDirectExecutor)2 StateMachines (org.neo4j.cluster.StateMachines)2 Message (org.neo4j.cluster.com.message.Message)2 LearnerMessage (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.LearnerMessage)2 MultiPaxosContext (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.context.MultiPaxosContext)2 URI (java.net.URI)1 Collection (java.util.Collection)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 Mockito.doAnswer (org.mockito.Mockito.doAnswer)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Answer (org.mockito.stubbing.Answer)1 InstanceId (org.neo4j.cluster.InstanceId)1