Search in sources :

Example 1 with DelayedDirectExecutor

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

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

the class HeartbeatContextImplTest method majorityOfNonSuspectedInstancesShouldBeEnoughToMarkAnInstanceAsFailed.

@Test
public void majorityOfNonSuspectedInstancesShouldBeEnoughToMarkAnInstanceAsFailed() throws Exception {
    // Given
    InstanceId me = new InstanceId(1);
    InstanceId member2 = new InstanceId(2);
    InstanceId member3 = new InstanceId(3);
    InstanceId member4 = new InstanceId(4);
    InstanceId member5 = new InstanceId(5);
    Timeouts timeouts = mock(Timeouts.class);
    CommonContextState commonState = mock(CommonContextState.class);
    ClusterConfiguration configuration = mock(ClusterConfiguration.class);
    when(commonState.configuration()).thenReturn(configuration);
    when(configuration.getMembers()).thenReturn(members(5));
    when(configuration.getMemberIds()).thenReturn(ids(5));
    DelayedDirectExecutor executor = new DelayedDirectExecutor(NullLogProvider.getInstance());
    HeartbeatContext context = new HeartbeatContextImpl(me, commonState, NullLogProvider.getInstance(), timeouts, executor);
    final List<InstanceId> failed = new ArrayList<>(4);
    HeartbeatListener listener = new HeartbeatListener() {

        @Override
        public void failed(InstanceId server) {
            failed.add(server);
        }

        @Override
        public void alive(InstanceId server) {
            failed.remove(server);
        }
    };
    context.addHeartbeatListener(listener);
    // when
    // just two suspicions come, no extra failing action should be taken since this is not majority
    context.suspect(member2);
    context.suspect(member3);
    executor.drain();
    // then
    assertEquals(0, failed.size());
    // when
    // the another instance suspects them, therefore have a majority of non suspected, then 2 and 3 must fail
    Set<InstanceId> suspicionsFrom5 = new HashSet<>();
    suspicionsFrom5.add(member2);
    suspicionsFrom5.add(member3);
    context.suspicions(member5, suspicionsFrom5);
    executor.drain();
    // then
    assertEquals(2, failed.size());
    assertTrue(failed.contains(member2));
    assertTrue(failed.contains(member3));
    // when
    // an instance sends a heartbeat, it should be set as alive
    context.alive(member2);
    executor.drain();
    // then
    assertEquals(1, failed.size());
    assertTrue(failed.contains(member3));
}
Also used : HeartbeatListener(org.neo4j.cluster.protocol.heartbeat.HeartbeatListener) InstanceId(org.neo4j.cluster.InstanceId) Timeouts(org.neo4j.cluster.timeout.Timeouts) HeartbeatContext(org.neo4j.cluster.protocol.heartbeat.HeartbeatContext) ArrayList(java.util.ArrayList) ClusterConfiguration(org.neo4j.cluster.protocol.cluster.ClusterConfiguration) DelayedDirectExecutor(org.neo4j.cluster.DelayedDirectExecutor) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with DelayedDirectExecutor

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

the class HeartbeatContextImplTest method shouldFailAllInstancesIfAllOtherInstancesAreSuspected.

@Test
public void shouldFailAllInstancesIfAllOtherInstancesAreSuspected() throws Exception {
    // Given
    InstanceId me = new InstanceId(1);
    InstanceId member2 = new InstanceId(2);
    InstanceId member3 = new InstanceId(3);
    Timeouts timeouts = mock(Timeouts.class);
    CommonContextState commonState = mock(CommonContextState.class);
    ClusterConfiguration configuration = mock(ClusterConfiguration.class);
    when(commonState.configuration()).thenReturn(configuration);
    when(configuration.getMembers()).thenReturn(members(3));
    when(configuration.getMemberIds()).thenReturn(ids(3));
    DelayedDirectExecutor executor = new DelayedDirectExecutor(NullLogProvider.getInstance());
    HeartbeatContext context = new HeartbeatContextImpl(me, commonState, NullLogProvider.getInstance(), timeouts, executor);
    List<InstanceId> failed = new ArrayList<>(2);
    HeartbeatListener listener = new HeartbeatListener() {

        @Override
        public void failed(InstanceId server) {
            failed.add(server);
        }

        @Override
        public void alive(InstanceId server) {
            failed.remove(server);
        }
    };
    context.addHeartbeatListener(listener);
    // when
    // just one suspicion comes, no extra failing action should be taken
    context.suspect(member2);
    executor.drain();
    // then
    assertEquals(0, failed.size());
    // when
    // the other instance is suspected, all instances must be marked as failed
    context.suspect(member3);
    executor.drain();
    // then
    assertEquals(2, failed.size());
    assertTrue(failed.contains(member2));
    assertTrue(failed.contains(member3));
    // when
    // one of them comes alive again, only that instance should be marked as alive
    context.alive(member2);
    executor.drain();
    // then
    assertEquals(1, failed.size());
    assertTrue(failed.contains(member3));
}
Also used : HeartbeatListener(org.neo4j.cluster.protocol.heartbeat.HeartbeatListener) InstanceId(org.neo4j.cluster.InstanceId) Timeouts(org.neo4j.cluster.timeout.Timeouts) HeartbeatContext(org.neo4j.cluster.protocol.heartbeat.HeartbeatContext) ArrayList(java.util.ArrayList) ClusterConfiguration(org.neo4j.cluster.protocol.cluster.ClusterConfiguration) DelayedDirectExecutor(org.neo4j.cluster.DelayedDirectExecutor) Test(org.junit.Test)

Example 4 with DelayedDirectExecutor

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

the class HeartbeatContextImplTest method shouldFailAndAliveBothNotifyHeartbeatListenerInDelayedDirectExecutor.

@Test
public void shouldFailAndAliveBothNotifyHeartbeatListenerInDelayedDirectExecutor() throws Throwable {
    // Given
    InstanceId me = new InstanceId(1);
    InstanceId failedMachine = new InstanceId(2);
    InstanceId goodMachine = new InstanceId(3);
    Timeouts timeouts = mock(Timeouts.class);
    CommonContextState commonState = mock(CommonContextState.class);
    ClusterConfiguration configuration = mock(ClusterConfiguration.class);
    when(commonState.configuration()).thenReturn(configuration);
    when(configuration.getMembers()).thenReturn(members(3));
    when(configuration.getMemberIds()).thenReturn(ids(3));
    final List<Runnable> runnables = new ArrayList<>();
    HeartbeatContext context = new HeartbeatContextImpl(me, commonState, NullLogProvider.getInstance(), timeouts, new DelayedDirectExecutor(NullLogProvider.getInstance()) {

        @Override
        public synchronized void execute(Runnable command) {
            runnables.add(command);
        }
    });
    context.addHeartbeatListener(mock(HeartbeatListener.class));
    context.suspicions(goodMachine, new HashSet<>(singletonList(failedMachine)));
    // fail
    context.suspect(failedMachine);
    // alive
    context.alive(failedMachine);
    // Then
    // fail + alive
    assertEquals(2, runnables.size());
}
Also used : InstanceId(org.neo4j.cluster.InstanceId) Timeouts(org.neo4j.cluster.timeout.Timeouts) ArrayList(java.util.ArrayList) ClusterConfiguration(org.neo4j.cluster.protocol.cluster.ClusterConfiguration) DelayedDirectExecutor(org.neo4j.cluster.DelayedDirectExecutor) HeartbeatListener(org.neo4j.cluster.protocol.heartbeat.HeartbeatListener) HeartbeatContext(org.neo4j.cluster.protocol.heartbeat.HeartbeatContext) Test(org.junit.Test)

Example 5 with DelayedDirectExecutor

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

the class ClusterInstance method newClusterInstance.

public static ClusterInstance newClusterInstance(InstanceId id, URI uri, Monitors monitors, ClusterConfiguration configuration, int maxSurvivableFailedMembers, LogProvider logging) {
    MultiPaxosServerFactory factory = new MultiPaxosServerFactory(configuration, logging, monitors.newMonitor(StateMachines.Monitor.class));
    ClusterInstanceInput input = new ClusterInstanceInput();
    ClusterInstanceOutput output = new ClusterInstanceOutput(uri);
    ObjectStreamFactory objStreamFactory = new ObjectStreamFactory();
    ProverTimeouts timeouts = new ProverTimeouts(uri);
    InMemoryAcceptorInstanceStore acceptorInstances = new InMemoryAcceptorInstanceStore();
    Config config = mock(Config.class);
    when(config.get(ClusterSettings.max_acceptors)).thenReturn(maxSurvivableFailedMembers);
    DelayedDirectExecutor executor = new DelayedDirectExecutor(logging);
    final MultiPaxosContext context = new MultiPaxosContext(id, Iterables.<ElectionRole, ElectionRole>iterable(new ElectionRole(ClusterConfiguration.COORDINATOR)), new ClusterConfiguration(configuration.getName(), logging, configuration.getMemberURIs()), executor, logging, objStreamFactory, objStreamFactory, acceptorInstances, timeouts, new DefaultElectionCredentialsProvider(id, new StateVerifierLastTxIdGetter(), new MemberInfoProvider()), config);
    context.getClusterContext().setBoundAt(uri);
    SnapshotContext snapshotContext = new SnapshotContext(context.getClusterContext(), context.getLearnerContext());
    DelayedDirectExecutor taskExecutor = new DelayedDirectExecutor(logging);
    ProtocolServer ps = factory.newProtocolServer(id, input, output, DIRECT_EXECUTOR, taskExecutor, timeouts, context, snapshotContext);
    return new ClusterInstance(DIRECT_EXECUTOR, logging, factory, ps, context, acceptorInstances, timeouts, input, output, uri);
}
Also used : Config(org.neo4j.kernel.configuration.Config) HighAvailabilityMemberInfoProvider(org.neo4j.kernel.ha.HighAvailabilityMemberInfoProvider) DelayedDirectExecutor(org.neo4j.cluster.DelayedDirectExecutor) ClusterConfiguration(org.neo4j.cluster.protocol.cluster.ClusterConfiguration) DefaultElectionCredentialsProvider(org.neo4j.kernel.ha.cluster.DefaultElectionCredentialsProvider) ElectionRole(org.neo4j.cluster.protocol.election.ElectionRole) ObjectStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectStreamFactory) SnapshotContext(org.neo4j.cluster.protocol.snapshot.SnapshotContext) ProtocolServer(org.neo4j.cluster.ProtocolServer) MultiPaxosContext(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.context.MultiPaxosContext) MultiPaxosServerFactory(org.neo4j.cluster.MultiPaxosServerFactory) InMemoryAcceptorInstanceStore(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.InMemoryAcceptorInstanceStore)

Aggregations

DelayedDirectExecutor (org.neo4j.cluster.DelayedDirectExecutor)5 ArrayList (java.util.ArrayList)4 ClusterConfiguration (org.neo4j.cluster.protocol.cluster.ClusterConfiguration)4 Test (org.junit.Test)3 InstanceId (org.neo4j.cluster.InstanceId)3 HeartbeatContext (org.neo4j.cluster.protocol.heartbeat.HeartbeatContext)3 HeartbeatListener (org.neo4j.cluster.protocol.heartbeat.HeartbeatListener)3 Timeouts (org.neo4j.cluster.timeout.Timeouts)3 ProtocolServer (org.neo4j.cluster.ProtocolServer)2 ObjectStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectStreamFactory)2 InMemoryAcceptorInstanceStore (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.InMemoryAcceptorInstanceStore)2 MultiPaxosContext (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.context.MultiPaxosContext)2 HighAvailabilityMemberInfoProvider (org.neo4j.kernel.ha.HighAvailabilityMemberInfoProvider)2 DefaultElectionCredentialsProvider (org.neo4j.kernel.ha.cluster.DefaultElectionCredentialsProvider)2 HashSet (java.util.HashSet)1 MultiPaxosServerFactory (org.neo4j.cluster.MultiPaxosServerFactory)1 ElectionRole (org.neo4j.cluster.protocol.election.ElectionRole)1 SnapshotContext (org.neo4j.cluster.protocol.snapshot.SnapshotContext)1 StateMachine (org.neo4j.cluster.statemachine.StateMachine)1 Config (org.neo4j.kernel.configuration.Config)1