Search in sources :

Example 1 with AtomicBroadcastListener

use of org.neo4j.cluster.protocol.atomicbroadcast.AtomicBroadcastListener in project neo4j by neo4j.

the class HardKillIT method testMasterSwitchHappensOnKillMinus9.

@Test
public void testMasterSwitchHappensOnKillMinus9() throws Exception {
    Process proc = null;
    HighlyAvailableGraphDatabase dbWithId2 = null, dbWithId3 = null, oldMaster = null;
    try {
        proc = run(1);
        dbWithId2 = startDb(2, path(2));
        dbWithId3 = startDb(3, path(3));
        waitUntilAllProperlyAvailable(dbWithId2, 1, MASTER, 2, SLAVE, 3, SLAVE);
        waitUntilAllProperlyAvailable(dbWithId3, 1, MASTER, 2, SLAVE, 3, SLAVE);
        final CountDownLatch newMasterAvailableLatch = new CountDownLatch(1);
        dbWithId2.getDependencyResolver().resolveDependency(ClusterClient.class).addAtomicBroadcastListener(new AtomicBroadcastListener() {

            @Override
            public void receive(Payload value) {
                try {
                    Object event = new AtomicBroadcastSerializer(new ObjectStreamFactory(), new ObjectStreamFactory()).receive(value);
                    if (event instanceof MemberIsAvailable) {
                        if (HighAvailabilityModeSwitcher.MASTER.equals(((MemberIsAvailable) event).getRole())) {
                            newMasterAvailableLatch.countDown();
                        }
                    }
                } catch (Exception e) {
                    fail(e.toString());
                }
            }
        });
        proc.destroy();
        proc = null;
        assertTrue(newMasterAvailableLatch.await(60, SECONDS));
        assertTrue(dbWithId2.isMaster());
        assertTrue(!dbWithId3.isMaster());
        // Ensure that everyone has marked the killed instance as failed, otherwise it cannot rejoin
        waitUntilAllProperlyAvailable(dbWithId2, 1, UNKNOWN, 2, MASTER, 3, SLAVE);
        waitUntilAllProperlyAvailable(dbWithId3, 1, UNKNOWN, 2, MASTER, 3, SLAVE);
        oldMaster = startDb(1, path(1));
        long oldMasterNode = createNamedNode(oldMaster, "Old master");
        assertEquals(oldMasterNode, getNamedNode(dbWithId2, "Old master"));
    } finally {
        if (proc != null) {
            proc.destroy();
        }
        if (oldMaster != null) {
            oldMaster.shutdown();
        }
        if (dbWithId2 != null) {
            dbWithId2.shutdown();
        }
        if (dbWithId3 != null) {
            dbWithId3.shutdown();
        }
    }
}
Also used : AtomicBroadcastSerializer(org.neo4j.cluster.protocol.atomicbroadcast.AtomicBroadcastSerializer) MemberIsAvailable(org.neo4j.cluster.member.paxos.MemberIsAvailable) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBroadcastListener(org.neo4j.cluster.protocol.atomicbroadcast.AtomicBroadcastListener) ObjectStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectStreamFactory) IOException(java.io.IOException) ClusterClient(org.neo4j.cluster.client.ClusterClient) Payload(org.neo4j.cluster.protocol.atomicbroadcast.Payload) Test(org.junit.Test)

Example 2 with AtomicBroadcastListener

use of org.neo4j.cluster.protocol.atomicbroadcast.AtomicBroadcastListener 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 AtomicBroadcastListener

use of org.neo4j.cluster.protocol.atomicbroadcast.AtomicBroadcastListener in project neo4j by neo4j.

the class MultiPaxosServer method start.

public void start() throws IOException {
    broadcastSerializer = new AtomicBroadcastSerializer(new ObjectStreamFactory(), new ObjectStreamFactory());
    final LifeSupport life = new LifeSupport();
    try {
        MessageTimeoutStrategy timeoutStrategy = new MessageTimeoutStrategy(new FixedTimeoutStrategy(5000)).timeout(HeartbeatMessage.sendHeartbeat, 200);
        Monitors monitors = new Monitors();
        NetworkedServerFactory serverFactory = new NetworkedServerFactory(life, new MultiPaxosServerFactory(new ClusterConfiguration("default", NullLogProvider.getInstance()), NullLogProvider.getInstance(), monitors.newMonitor(StateMachines.Monitor.class)), timeoutStrategy, NullLogProvider.getInstance(), new ObjectStreamFactory(), new ObjectStreamFactory(), monitors.newMonitor(NetworkReceiver.Monitor.class), monitors.newMonitor(NetworkSender.Monitor.class), monitors.newMonitor(NamedThreadFactory.Monitor.class));
        ServerIdElectionCredentialsProvider electionCredentialsProvider = new ServerIdElectionCredentialsProvider();
        server = serverFactory.newNetworkedServer(Config.embeddedDefaults(), new InMemoryAcceptorInstanceStore(), electionCredentialsProvider);
        server.addBindingListener(electionCredentialsProvider);
        server.addBindingListener(new BindingListener() {

            @Override
            public void listeningAt(URI me) {
                System.out.println("Listening at:" + me);
            }
        });
        cluster = server.newClient(Cluster.class);
        cluster.addClusterListener(new ClusterListener() {

            @Override
            public void enteredCluster(ClusterConfiguration clusterConfiguration) {
                System.out.println("Entered cluster:" + clusterConfiguration);
            }

            @Override
            public void joinedCluster(InstanceId instanceId, URI member) {
                System.out.println("Joined cluster:" + instanceId + " (at URI " + member + ")");
            }

            @Override
            public void leftCluster(InstanceId instanceId, URI member) {
                System.out.println("Left cluster:" + instanceId);
            }

            @Override
            public void leftCluster() {
                System.out.println("Left cluster");
            }

            @Override
            public void elected(String role, InstanceId instanceId, URI electedMember) {
                System.out.println(instanceId + " at URI " + electedMember + " was elected as " + role);
            }

            @Override
            public void unelected(String role, InstanceId instanceId, URI electedMember) {
                System.out.println(instanceId + " at URI " + electedMember + " was removed from " + role);
            }
        });
        Heartbeat heartbeat = server.newClient(Heartbeat.class);
        heartbeat.addHeartbeatListener(new HeartbeatListener() {

            @Override
            public void failed(InstanceId server) {
                System.out.println(server + " failed");
            }

            @Override
            public void alive(InstanceId server) {
                System.out.println(server + " alive");
            }
        });
        broadcast = server.newClient(AtomicBroadcast.class);
        broadcast.addAtomicBroadcastListener(new AtomicBroadcastListener() {

            @Override
            public void receive(Payload value) {
                try {
                    System.out.println(broadcastSerializer.receive(value));
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        });
        life.start();
        String command;
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        while (!(command = reader.readLine()).equals("quit")) {
            String[] arguments = command.split(" ");
            Method method = getCommandMethod(arguments[0]);
            if (method != null) {
                String[] realArgs = new String[arguments.length - 1];
                System.arraycopy(arguments, 1, realArgs, 0, realArgs.length);
                try {
                    method.invoke(this, (Object[]) realArgs);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
        cluster.leave();
    } finally {
        life.shutdown();
        System.out.println("Done");
    }
}
Also used : FixedTimeoutStrategy(org.neo4j.cluster.timeout.FixedTimeoutStrategy) NetworkedServerFactory(org.neo4j.cluster.NetworkedServerFactory) ClusterConfiguration(org.neo4j.cluster.protocol.cluster.ClusterConfiguration) URI(java.net.URI) AtomicBroadcastListener(org.neo4j.cluster.protocol.atomicbroadcast.AtomicBroadcastListener) BindingListener(org.neo4j.cluster.BindingListener) LifeSupport(org.neo4j.kernel.lifecycle.LifeSupport) ServerIdElectionCredentialsProvider(org.neo4j.cluster.protocol.election.ServerIdElectionCredentialsProvider) AtomicBroadcastSerializer(org.neo4j.cluster.protocol.atomicbroadcast.AtomicBroadcastSerializer) InputStreamReader(java.io.InputStreamReader) InstanceId(org.neo4j.cluster.InstanceId) AtomicBroadcast(org.neo4j.cluster.protocol.atomicbroadcast.AtomicBroadcast) StateMachines(org.neo4j.cluster.StateMachines) Cluster(org.neo4j.cluster.protocol.cluster.Cluster) IOException(java.io.IOException) Method(java.lang.reflect.Method) ObjectStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectStreamFactory) InvocationTargetException(java.lang.reflect.InvocationTargetException) HeartbeatListener(org.neo4j.cluster.protocol.heartbeat.HeartbeatListener) ClusterListener(org.neo4j.cluster.protocol.cluster.ClusterListener) Heartbeat(org.neo4j.cluster.protocol.heartbeat.Heartbeat) Monitors(org.neo4j.kernel.monitoring.Monitors) MultiPaxosServerFactory(org.neo4j.cluster.MultiPaxosServerFactory) BufferedReader(java.io.BufferedReader) MessageTimeoutStrategy(org.neo4j.cluster.timeout.MessageTimeoutStrategy) Payload(org.neo4j.cluster.protocol.atomicbroadcast.Payload)

Aggregations

IOException (java.io.IOException)3 AtomicBroadcastListener (org.neo4j.cluster.protocol.atomicbroadcast.AtomicBroadcastListener)3 AtomicBroadcastSerializer (org.neo4j.cluster.protocol.atomicbroadcast.AtomicBroadcastSerializer)3 ObjectStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectStreamFactory)3 Payload (org.neo4j.cluster.protocol.atomicbroadcast.Payload)3 URI (java.net.URI)2 InstanceId (org.neo4j.cluster.InstanceId)2 AtomicBroadcast (org.neo4j.cluster.protocol.atomicbroadcast.AtomicBroadcast)2 Heartbeat (org.neo4j.cluster.protocol.heartbeat.Heartbeat)2 HeartbeatListener (org.neo4j.cluster.protocol.heartbeat.HeartbeatListener)2 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Test (org.junit.Test)1 BindingListener (org.neo4j.cluster.BindingListener)1 MultiPaxosServerFactory (org.neo4j.cluster.MultiPaxosServerFactory)1 NetworkedServerFactory (org.neo4j.cluster.NetworkedServerFactory)1