Search in sources :

Example 1 with ObjectStreamFactory

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

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

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

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

the class MemberIsUnavailableTest method deserialize.

private static MemberIsUnavailable deserialize(byte[] serialized) throws Exception {
    ObjectInputStream inputStream = null;
    try {
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(serialized);
        inputStream = new ObjectStreamFactory().create(byteArrayInputStream);
        return (MemberIsUnavailable) inputStream.readObject();
    } finally {
        if (inputStream != null) {
            inputStream.close();
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectStreamFactory) ObjectInputStream(java.io.ObjectInputStream)

Example 5 with ObjectStreamFactory

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

the class NetworkMock method newTestProtocolServer.

protected TestProtocolServer newTestProtocolServer(int serverId, URI serverUri) {
    ProtocolServerFactory protocolServerFactory = new MultiPaxosServerFactory(new ClusterConfiguration("default", logService.getInternalLogProvider()), logService.getInternalLogProvider(), monitors.newMonitor(StateMachines.Monitor.class));
    ServerIdElectionCredentialsProvider electionCredentialsProvider = new ServerIdElectionCredentialsProvider();
    electionCredentialsProvider.listeningAt(serverUri);
    TestProtocolServer protocolServer = new TestProtocolServer(logService.getInternalLogProvider(), timeoutStrategy, protocolServerFactory, serverUri, new InstanceId(serverId), new InMemoryAcceptorInstanceStore(), electionCredentialsProvider);
    protocolServer.addStateTransitionListener(new StateTransitionLogger(logService.getInternalLogProvider(), new AtomicBroadcastSerializer(new ObjectStreamFactory(), new ObjectStreamFactory())));
    return protocolServer;
}
Also used : AtomicBroadcastSerializer(org.neo4j.cluster.protocol.atomicbroadcast.AtomicBroadcastSerializer) StateTransitionLogger(org.neo4j.cluster.statemachine.StateTransitionLogger) ServerIdElectionCredentialsProvider(org.neo4j.cluster.protocol.election.ServerIdElectionCredentialsProvider) ClusterConfiguration(org.neo4j.cluster.protocol.cluster.ClusterConfiguration) InMemoryAcceptorInstanceStore(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.InMemoryAcceptorInstanceStore) ObjectStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectStreamFactory)

Aggregations

ObjectStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectStreamFactory)12 AtomicBroadcastSerializer (org.neo4j.cluster.protocol.atomicbroadcast.AtomicBroadcastSerializer)6 Test (org.junit.Test)5 ClusterConfiguration (org.neo4j.cluster.protocol.cluster.ClusterConfiguration)5 URI (java.net.URI)4 InstanceId (org.neo4j.cluster.InstanceId)4 MultiPaxosContext (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.context.MultiPaxosContext)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 AtomicBroadcast (org.neo4j.cluster.protocol.atomicbroadcast.AtomicBroadcast)3 AtomicBroadcastListener (org.neo4j.cluster.protocol.atomicbroadcast.AtomicBroadcastListener)3 Payload (org.neo4j.cluster.protocol.atomicbroadcast.Payload)3 InMemoryAcceptorInstanceStore (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.InMemoryAcceptorInstanceStore)3 Config (org.neo4j.kernel.configuration.Config)3 Executor (java.util.concurrent.Executor)2 DelayedDirectExecutor (org.neo4j.cluster.DelayedDirectExecutor)2 MultiPaxosServerFactory (org.neo4j.cluster.MultiPaxosServerFactory)2 ProtocolServer (org.neo4j.cluster.ProtocolServer)2 TestProtocolServer (org.neo4j.cluster.TestProtocolServer)2 Cluster (org.neo4j.cluster.protocol.cluster.Cluster)2