Search in sources :

Example 6 with Timeouts

use of org.neo4j.cluster.timeout.Timeouts 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 7 with Timeouts

use of org.neo4j.cluster.timeout.Timeouts 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 8 with Timeouts

use of org.neo4j.cluster.timeout.Timeouts in project neo4j by neo4j.

the class MultiPaxosContextTest method shouldDeepClone.

@Test
public void shouldDeepClone() throws Exception {
    // Given
    ObjectStreamFactory objStream = new ObjectStreamFactory();
    AcceptorInstanceStore acceptorInstances = mock(AcceptorInstanceStore.class);
    Executor executor = mock(Executor.class);
    Timeouts timeouts = mock(Timeouts.class);
    ClusterConfiguration clusterConfig = new ClusterConfiguration("myCluster", NullLogProvider.getInstance());
    ElectionCredentialsProvider electionCredentials = mock(ElectionCredentialsProvider.class);
    Config config = mock(Config.class);
    when(config.get(ClusterSettings.max_acceptors)).thenReturn(10);
    MultiPaxosContext ctx = new MultiPaxosContext(new InstanceId(1), Collections.<ElectionRole>emptyList(), clusterConfig, executor, NullLogProvider.getInstance(), objStream, objStream, acceptorInstances, timeouts, electionCredentials, config);
    // When
    MultiPaxosContext snapshot = ctx.snapshot(NullLogProvider.getInstance(), timeouts, executor, acceptorInstances, objStream, objStream, electionCredentials);
    // Then
    assertEquals(ctx, snapshot);
}
Also used : Executor(java.util.concurrent.Executor) ElectionCredentialsProvider(org.neo4j.cluster.protocol.election.ElectionCredentialsProvider) InstanceId(org.neo4j.cluster.InstanceId) Timeouts(org.neo4j.cluster.timeout.Timeouts) Config(org.neo4j.kernel.configuration.Config) MultiPaxosContext(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.context.MultiPaxosContext) ClusterConfiguration(org.neo4j.cluster.protocol.cluster.ClusterConfiguration) ObjectStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectStreamFactory) Test(org.junit.Test)

Example 9 with Timeouts

use of org.neo4j.cluster.timeout.Timeouts in project neo4j by neo4j.

the class ClusterContextImplTest method shouldGracefullyHandleEmptyDiscoveryHeader.

@Test
public void shouldGracefullyHandleEmptyDiscoveryHeader() throws Exception {
    // Given
    InstanceId me = new InstanceId(1);
    InstanceId joining = new InstanceId(2);
    CommonContextState commonContextState = mock(CommonContextState.class, RETURNS_MOCKS);
    Timeouts timeouts = mock(Timeouts.class);
    Executor executor = mock(Executor.class);
    HeartbeatContext heartbeatContext = mock(HeartbeatContext.class);
    ClusterContext context = new ClusterContextImpl(me, commonContextState, NullLogProvider.getInstance(), timeouts, executor, mock(ObjectOutputStreamFactory.class), mock(ObjectInputStreamFactory.class), mock(LearnerContext.class), heartbeatContext, mock(Config.class));
    ClusterMessage.ConfigurationRequestState request = mock(ClusterMessage.ConfigurationRequestState.class);
    when(request.getJoiningId()).thenReturn(joining);
    // When
    // Instance 2 contacts us with a request but it is empty
    context.addContactingInstance(request, "");
    // Then
    // The discovery header we generate should still contain that instance
    assertEquals("2", context.generateDiscoveryHeader());
}
Also used : InstanceId(org.neo4j.cluster.InstanceId) Timeouts(org.neo4j.cluster.timeout.Timeouts) Config(org.neo4j.kernel.configuration.Config) ClusterContext(org.neo4j.cluster.protocol.cluster.ClusterContext) LearnerContext(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.LearnerContext) Executor(java.util.concurrent.Executor) HeartbeatContext(org.neo4j.cluster.protocol.heartbeat.HeartbeatContext) ObjectInputStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectInputStreamFactory) ObjectOutputStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectOutputStreamFactory) ClusterMessage(org.neo4j.cluster.protocol.cluster.ClusterMessage) Test(org.junit.Test)

Example 10 with Timeouts

use of org.neo4j.cluster.timeout.Timeouts in project neo4j by neo4j.

the class ClusterContextImplTest method nonElectorFailingMustNotCauseElectorVersionToBeReset.

/*
     * This test ensures that an instance that is marked as failed has its elector version reset. This means that
     * the instance, once it comes back, will still be able to do elections even if it lost state
     */
@Test
public void nonElectorFailingMustNotCauseElectorVersionToBeReset() throws Exception {
    // Given
    InstanceId me = new InstanceId(1);
    InstanceId elector = new InstanceId(2);
    CommonContextState commonContextState = mock(CommonContextState.class, RETURNS_MOCKS);
    Timeouts timeouts = mock(Timeouts.class);
    Executor executor = mock(Executor.class);
    HeartbeatContext heartbeatContext = mock(HeartbeatContext.class);
    ArgumentCaptor<HeartbeatListener> listenerCaptor = ArgumentCaptor.forClass(HeartbeatListener.class);
    ClusterContext context = new ClusterContextImpl(me, commonContextState, NullLogProvider.getInstance(), timeouts, executor, mock(ObjectOutputStreamFactory.class), mock(ObjectInputStreamFactory.class), mock(LearnerContext.class), heartbeatContext, mock(Config.class));
    verify(heartbeatContext).addHeartbeatListener(listenerCaptor.capture());
    HeartbeatListener theListener = listenerCaptor.getValue();
    // This means instance 2 was the elector at version 8
    context.setLastElector(elector);
    context.setLastElectorVersion(8);
    // When
    theListener.failed(new InstanceId(3));
    // Then
    assertEquals(context.getLastElector(), elector);
    assertEquals(context.getLastElectorVersion(), 8);
}
Also used : InstanceId(org.neo4j.cluster.InstanceId) Timeouts(org.neo4j.cluster.timeout.Timeouts) Config(org.neo4j.kernel.configuration.Config) ClusterContext(org.neo4j.cluster.protocol.cluster.ClusterContext) LearnerContext(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.LearnerContext) HeartbeatListener(org.neo4j.cluster.protocol.heartbeat.HeartbeatListener) Executor(java.util.concurrent.Executor) HeartbeatContext(org.neo4j.cluster.protocol.heartbeat.HeartbeatContext) ObjectInputStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectInputStreamFactory) ObjectOutputStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectOutputStreamFactory) Test(org.junit.Test)

Aggregations

Timeouts (org.neo4j.cluster.timeout.Timeouts)11 Test (org.junit.Test)10 InstanceId (org.neo4j.cluster.InstanceId)10 HeartbeatContext (org.neo4j.cluster.protocol.heartbeat.HeartbeatContext)8 Executor (java.util.concurrent.Executor)7 Config (org.neo4j.kernel.configuration.Config)7 ObjectInputStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectInputStreamFactory)6 ObjectOutputStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectOutputStreamFactory)6 ClusterConfiguration (org.neo4j.cluster.protocol.cluster.ClusterConfiguration)6 LearnerContext (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.LearnerContext)5 ClusterContext (org.neo4j.cluster.protocol.cluster.ClusterContext)5 HeartbeatListener (org.neo4j.cluster.protocol.heartbeat.HeartbeatListener)5 DelayedDirectExecutor (org.neo4j.cluster.DelayedDirectExecutor)4 ArrayList (java.util.ArrayList)3 MultiPaxosContext (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.context.MultiPaxosContext)3 ClusterMessage (org.neo4j.cluster.protocol.cluster.ClusterMessage)3 ElectionCredentialsProvider (org.neo4j.cluster.protocol.election.ElectionCredentialsProvider)2 ElectionRole (org.neo4j.cluster.protocol.election.ElectionRole)2 HashSet (java.util.HashSet)1 StateMachines (org.neo4j.cluster.StateMachines)1