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();
}
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());
}
}
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)"));
}
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);
}
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));
}
Aggregations