Search in sources :

Example 1 with StateMachines

use of org.neo4j.cluster.StateMachines 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 2 with StateMachines

use of org.neo4j.cluster.StateMachines 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 StateMachines

use of org.neo4j.cluster.StateMachines in project neo4j by neo4j.

the class ClusterMockTest method verifyConfigurations.

public void verifyConfigurations(VerifyInstanceConfiguration[] toCheckAgainst) {
    logger.getLogger().fine("Verify configurations against given");
    List<URI> members;
    Map<String, InstanceId> roles;
    Set<InstanceId> failed;
    List<AssertionError> errors = new LinkedList<AssertionError>();
    List<TestProtocolServer> protocolServers = network.getServers();
    assertEquals("You must provide a configuration for all instances", protocolServers.size(), toCheckAgainst.length);
    for (int j = 0; j < protocolServers.size(); j++) {
        members = toCheckAgainst[j].members;
        roles = toCheckAgainst[j].roles;
        failed = toCheckAgainst[j].failed;
        StateMachines stateMachines = protocolServers.get(j).getServer().getStateMachines();
        State<?, ?> clusterState = stateMachines.getStateMachine(ClusterMessage.class).getState();
        if (!clusterState.equals(ClusterState.entered)) {
            logger.getLogger().warning("Instance " + (j + 1) + " is not in the cluster (" + clusterState + ")");
            continue;
        }
        ClusterContext context = (ClusterContext) stateMachines.getStateMachine(ClusterMessage.class).getContext();
        HeartbeatContext heartbeatContext = (HeartbeatContext) stateMachines.getStateMachine(HeartbeatMessage.class).getContext();
        ClusterConfiguration clusterConfiguration = context.getConfiguration();
        if (!clusterConfiguration.getMemberURIs().isEmpty()) {
            logger.getLogger().fine("   Server " + (j + 1) + ": Cluster:" + clusterConfiguration.getMemberURIs() + ", Roles:" + clusterConfiguration.getRoles() + ", Failed:" + heartbeatContext.getFailed());
            verifyConfigurations(stateMachines, members, roles, failed, errors);
        }
    }
    if (!errors.isEmpty()) {
        for (AssertionError error : errors) {
            logger.getLogger().severe(error.toString());
        }
        throw errors.get(0);
    }
}
Also used : InstanceId(org.neo4j.cluster.InstanceId) StateMachines(org.neo4j.cluster.StateMachines) URI(java.net.URI) LinkedList(java.util.LinkedList) TestProtocolServer(org.neo4j.cluster.TestProtocolServer) HeartbeatContext(org.neo4j.cluster.protocol.heartbeat.HeartbeatContext)

Example 4 with StateMachines

use of org.neo4j.cluster.StateMachines in project neo4j by neo4j.

the class ClusterMockTest method verifyConfigurations.

public void verifyConfigurations(String description) {
    logger.getLogger().fine("Verify configurations");
    List<URI> members = null;
    Map<String, InstanceId> roles = null;
    Set<InstanceId> failed = null;
    List<AssertionError> errors = new LinkedList<AssertionError>();
    List<TestProtocolServer> protocolServers = network.getServers();
    for (int j = 0; j < protocolServers.size(); j++) {
        StateMachines stateMachines = protocolServers.get(j).getServer().getStateMachines();
        State<?, ?> clusterState = stateMachines.getStateMachine(ClusterMessage.class).getState();
        if (!clusterState.equals(ClusterState.entered)) {
            logger.getLogger().fine("Instance " + (j + 1) + " is not in the cluster (" + clusterState + ")");
            continue;
        }
        ClusterContext context = (ClusterContext) stateMachines.getStateMachine(ClusterMessage.class).getContext();
        HeartbeatContext heartbeatContext = (HeartbeatContext) stateMachines.getStateMachine(HeartbeatMessage.class).getContext();
        ClusterConfiguration clusterConfiguration = context.getConfiguration();
        if (!clusterConfiguration.getMemberURIs().isEmpty()) {
            logger.getLogger().fine("   Server " + (j + 1) + ": Cluster:" + clusterConfiguration.getMemberURIs() + ", Roles:" + clusterConfiguration.getRoles() + ", Failed:" + heartbeatContext.getFailed());
            if (members == null) {
                members = clusterConfiguration.getMemberURIs();
                roles = clusterConfiguration.getRoles();
                failed = heartbeatContext.getFailed();
            } else {
                verifyConfigurations(stateMachines, members, roles, failed, errors);
            }
        }
    }
    assertEquals(description + ": In:" + in + ", Out:" + out, protocolServers.size(), Iterables.count(Iterables.<Cluster, List<Cluster>>flatten(in, out)));
    if (!errors.isEmpty()) {
        for (AssertionError error : errors) {
            logger.getLogger().severe(error.toString());
        }
        throw errors.get(0);
    }
}
Also used : InstanceId(org.neo4j.cluster.InstanceId) StateMachines(org.neo4j.cluster.StateMachines) URI(java.net.URI) LinkedList(java.util.LinkedList) TestProtocolServer(org.neo4j.cluster.TestProtocolServer) HeartbeatContext(org.neo4j.cluster.protocol.heartbeat.HeartbeatContext) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List)

Example 5 with StateMachines

use of org.neo4j.cluster.StateMachines in project neo4j by neo4j.

the class ClusterMockTest method getRoles.

private void getRoles(Map<String, InstanceId> roles) {
    List<TestProtocolServer> protocolServers = network.getServers();
    for (int j = 0; j < protocolServers.size(); j++) {
        StateMachines stateMachines = protocolServers.get(j).getServer().getStateMachines();
        State<?, ?> clusterState = stateMachines.getStateMachine(ClusterMessage.class).getState();
        if (!clusterState.equals(ClusterState.entered)) {
            logger.getLogger().warning("Instance " + (j + 1) + " is not in the cluster (" + clusterState + ")");
            continue;
        }
        ClusterContext context = (ClusterContext) stateMachines.getStateMachine(ClusterMessage.class).getContext();
        ClusterConfiguration clusterConfiguration = context.getConfiguration();
        roles.putAll(clusterConfiguration.getRoles());
    }
}
Also used : TestProtocolServer(org.neo4j.cluster.TestProtocolServer) StateMachines(org.neo4j.cluster.StateMachines)

Aggregations

StateMachines (org.neo4j.cluster.StateMachines)5 InstanceId (org.neo4j.cluster.InstanceId)3 TestProtocolServer (org.neo4j.cluster.TestProtocolServer)3 URI (java.net.URI)2 LinkedList (java.util.LinkedList)2 HeartbeatContext (org.neo4j.cluster.protocol.heartbeat.HeartbeatContext)2 StateMachine (org.neo4j.cluster.statemachine.StateMachine)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 Executor (java.util.concurrent.Executor)1 Test (org.junit.Test)1 DelayedDirectExecutor (org.neo4j.cluster.DelayedDirectExecutor)1 Message (org.neo4j.cluster.com.message.Message)1 MessageSender (org.neo4j.cluster.com.message.MessageSender)1 MessageSource (org.neo4j.cluster.com.message.MessageSource)1 ObjectInputStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectInputStreamFactory)1 ObjectOutputStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectOutputStreamFactory)1 AcceptorInstanceStore (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.AcceptorInstanceStore)1 LearnerMessage (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.LearnerMessage)1