Search in sources :

Example 1 with TestProtocolServer

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

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

the class ClusterMockTest method testCluster.

protected void testCluster(int[] serverIds, VerifyInstanceConfiguration[] finalConfig, NetworkMock mock, ClusterTestScript script) throws ExecutionException, InterruptedException, URISyntaxException, TimeoutException {
    this.script = script;
    network = mock;
    servers.clear();
    out.clear();
    in.clear();
    for (int i = 0; i < serverIds.length; i++) {
        final URI uri = new URI("server" + (i + 1));
        members.put(serverIds[i], uri);
        TestProtocolServer server = network.addServer(serverIds[i], uri);
        final Cluster cluster = server.newClient(Cluster.class);
        clusterStateListener(uri, cluster);
        server.newClient(Heartbeat.class).addHeartbeatListener(new HeartbeatListener() {

            @Override
            public void failed(InstanceId server) {
                logger.getLogger().warning(uri + ": Failed:" + server);
            }

            @Override
            public void alive(InstanceId server) {
                logger.getLogger().fine(uri + ": Alive:" + server);
            }
        });
        server.newClient(AtomicBroadcast.class).addAtomicBroadcastListener(new AtomicBroadcastListener() {

            AtomicBroadcastSerializer serializer = new AtomicBroadcastSerializer(new ObjectStreamFactory(), new ObjectStreamFactory());

            @Override
            public void receive(Payload value) {
                try {
                    logger.getLogger().fine(uri + " received: " + serializer.receive(value));
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        });
        servers.add(server);
        out.add(cluster);
    }
    // Run test
    for (int i = 0; i < script.rounds(); i++) {
        logger.getLogger().fine("Round " + i + ", time:" + network.getTime());
        script.tick(network.getTime());
        network.tick();
    }
    // Let messages settle
    network.tick(100);
    if (finalConfig == null) {
        verifyConfigurations("final config");
    } else {
        verifyConfigurations(finalConfig);
    }
    logger.getLogger().fine("All nodes leave");
    // All leave
    for (Cluster cluster : new ArrayList<Cluster>(in)) {
        logger.getLogger().fine("Leaving:" + cluster);
        cluster.leave();
        in.remove(cluster);
        network.tick(400);
    }
    if (finalConfig != null) {
        verifyConfigurations(finalConfig);
    } else {
        verifyConfigurations("test over");
    }
}
Also used : AtomicBroadcastSerializer(org.neo4j.cluster.protocol.atomicbroadcast.AtomicBroadcastSerializer) InstanceId(org.neo4j.cluster.InstanceId) AtomicBroadcast(org.neo4j.cluster.protocol.atomicbroadcast.AtomicBroadcast) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URI(java.net.URI) AtomicBroadcastListener(org.neo4j.cluster.protocol.atomicbroadcast.AtomicBroadcastListener) ObjectStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectStreamFactory) HeartbeatListener(org.neo4j.cluster.protocol.heartbeat.HeartbeatListener) TestProtocolServer(org.neo4j.cluster.TestProtocolServer) Heartbeat(org.neo4j.cluster.protocol.heartbeat.Heartbeat) Payload(org.neo4j.cluster.protocol.atomicbroadcast.Payload)

Example 3 with TestProtocolServer

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

the class MultiPaxosTest method testFailure.

@Test
public void testFailure() throws Exception {
    ScriptableNetworkFailureLatencyStrategy networkLatency = new ScriptableNetworkFailureLatencyStrategy();
    NetworkMock network = new NetworkMock(NullLogService.getInstance(), new Monitors(), 50, new MultipleFailureLatencyStrategy(networkLatency), new MessageTimeoutStrategy(new FixedTimeoutStrategy(1000)));
    List<TestProtocolServer> nodes = new ArrayList<TestProtocolServer>();
    TestProtocolServer server = network.addServer(1, URI.create("cluster://server1"));
    server.newClient(Cluster.class).create("default");
    network.tickUntilDone();
    nodes.add(server);
    for (int i = 1; i < 3; i++) {
        TestProtocolServer protocolServer = network.addServer(i + 1, new URI("cluster://server" + (i + 1)));
        protocolServer.newClient(Cluster.class).join("default", new URI("cluster://server1"));
        network.tick(10);
        nodes.add(protocolServer);
    }
    final AtomicBroadcast atomicBroadcast = nodes.get(0).newClient(AtomicBroadcast.class);
    ObjectStreamFactory objectStreamFactory = new ObjectStreamFactory();
    final AtomicBroadcastSerializer serializer = new AtomicBroadcastSerializer(objectStreamFactory, objectStreamFactory);
    atomicBroadcast.broadcast(serializer.broadcast(new DaPayload()));
    networkLatency.nodeIsDown("cluster://server2");
    networkLatency.nodeIsDown("cluster://server3");
    atomicBroadcast.broadcast(serializer.broadcast(new DaPayload()));
    network.tick(100);
    networkLatency.nodeIsUp("cluster://server3");
    network.tick(1000);
    for (TestProtocolServer node : nodes) {
        node.newClient(Cluster.class).leave();
        network.tick(10);
    }
}
Also used : AtomicBroadcastSerializer(org.neo4j.cluster.protocol.atomicbroadcast.AtomicBroadcastSerializer) FixedTimeoutStrategy(org.neo4j.cluster.timeout.FixedTimeoutStrategy) NetworkMock(org.neo4j.cluster.NetworkMock) ScriptableNetworkFailureLatencyStrategy(org.neo4j.cluster.ScriptableNetworkFailureLatencyStrategy) MultipleFailureLatencyStrategy(org.neo4j.cluster.MultipleFailureLatencyStrategy) AtomicBroadcast(org.neo4j.cluster.protocol.atomicbroadcast.AtomicBroadcast) ArrayList(java.util.ArrayList) Cluster(org.neo4j.cluster.protocol.cluster.Cluster) URI(java.net.URI) ObjectStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectStreamFactory) TestProtocolServer(org.neo4j.cluster.TestProtocolServer) Monitors(org.neo4j.kernel.monitoring.Monitors) MessageTimeoutStrategy(org.neo4j.cluster.timeout.MessageTimeoutStrategy) Test(org.junit.Test)

Example 4 with TestProtocolServer

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

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

TestProtocolServer (org.neo4j.cluster.TestProtocolServer)5 URI (java.net.URI)4 ArrayList (java.util.ArrayList)3 InstanceId (org.neo4j.cluster.InstanceId)3 StateMachines (org.neo4j.cluster.StateMachines)3 LinkedList (java.util.LinkedList)2 AtomicBroadcast (org.neo4j.cluster.protocol.atomicbroadcast.AtomicBroadcast)2 AtomicBroadcastSerializer (org.neo4j.cluster.protocol.atomicbroadcast.AtomicBroadcastSerializer)2 ObjectStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectStreamFactory)2 HeartbeatContext (org.neo4j.cluster.protocol.heartbeat.HeartbeatContext)2 IOException (java.io.IOException)1 List (java.util.List)1 Test (org.junit.Test)1 MultipleFailureLatencyStrategy (org.neo4j.cluster.MultipleFailureLatencyStrategy)1 NetworkMock (org.neo4j.cluster.NetworkMock)1 ScriptableNetworkFailureLatencyStrategy (org.neo4j.cluster.ScriptableNetworkFailureLatencyStrategy)1 AtomicBroadcastListener (org.neo4j.cluster.protocol.atomicbroadcast.AtomicBroadcastListener)1 Payload (org.neo4j.cluster.protocol.atomicbroadcast.Payload)1 Cluster (org.neo4j.cluster.protocol.cluster.Cluster)1 Heartbeat (org.neo4j.cluster.protocol.heartbeat.Heartbeat)1