Search in sources :

Example 21 with HeartbeatContext

use of org.neo4j.cluster.protocol.heartbeat.HeartbeatContext in project neo4j by neo4j.

the class AtomicBroadcastContextImplTest method shouldHasQuorumWhenOneMachineAliveInAClusterWithOneMachine.

@Test
public void shouldHasQuorumWhenOneMachineAliveInAClusterWithOneMachine() {
    //Given
    HeartbeatContext heartbeatContext = mock(HeartbeatContext.class);
    CommonContextState commonState = mock(CommonContextState.class);
    ClusterConfiguration configuration = mock(ClusterConfiguration.class);
    when(heartbeatContext.getAlive()).thenReturn(ids(1));
    when(commonState.configuration()).thenReturn(configuration);
    when(configuration.getMembers()).thenReturn(members(1));
    AtomicBroadcastContextImpl context = new AtomicBroadcastContextImpl(null, commonState, null, null, null, // we do not care about other args
    heartbeatContext);
    //When
    boolean hasQuorum = context.hasQuorum();
    //Then
    assertTrue(hasQuorum);
}
Also used : HeartbeatContext(org.neo4j.cluster.protocol.heartbeat.HeartbeatContext) ClusterConfiguration(org.neo4j.cluster.protocol.cluster.ClusterConfiguration) Test(org.junit.Test)

Example 22 with HeartbeatContext

use of org.neo4j.cluster.protocol.heartbeat.HeartbeatContext 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 23 with HeartbeatContext

use of org.neo4j.cluster.protocol.heartbeat.HeartbeatContext 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)

Example 24 with HeartbeatContext

use of org.neo4j.cluster.protocol.heartbeat.HeartbeatContext 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 25 with HeartbeatContext

use of org.neo4j.cluster.protocol.heartbeat.HeartbeatContext in project neo4j by neo4j.

the class ProposerContextImplTest method limitedAcceptors.

private int limitedAcceptors(int maxAcceptors, List<InstanceId> alive) {
    CommonContextState commonContextState = new CommonContextState(null, maxAcceptors);
    HeartbeatContext heartbeatContext = mock(HeartbeatContext.class);
    when(heartbeatContext.getAlive()).thenReturn(alive);
    // when
    ProposerContextImpl proposerContext = new ProposerContextImpl(new InstanceId(1), commonContextState, null, null, null, heartbeatContext);
    return proposerContext.getAcceptors().size();
}
Also used : InstanceId(org.neo4j.cluster.InstanceId) HeartbeatContext(org.neo4j.cluster.protocol.heartbeat.HeartbeatContext)

Aggregations

HeartbeatContext (org.neo4j.cluster.protocol.heartbeat.HeartbeatContext)26 InstanceId (org.neo4j.cluster.InstanceId)22 Test (org.junit.Test)21 Timeouts (org.neo4j.cluster.timeout.Timeouts)16 Config (org.neo4j.kernel.configuration.Config)15 Executor (java.util.concurrent.Executor)13 ObjectInputStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectInputStreamFactory)13 ObjectOutputStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectOutputStreamFactory)13 ClusterConfiguration (org.neo4j.cluster.protocol.cluster.ClusterConfiguration)11 MultiPaxosContext (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.context.MultiPaxosContext)10 ClusterContext (org.neo4j.cluster.protocol.cluster.ClusterContext)9 AcceptorInstanceStore (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.AcceptorInstanceStore)8 LinkedList (java.util.LinkedList)5 LearnerContext (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.LearnerContext)5 HeartbeatListener (org.neo4j.cluster.protocol.heartbeat.HeartbeatListener)5 URI (java.net.URI)4 ArrayList (java.util.ArrayList)4 ElectionCredentialsProvider (org.neo4j.cluster.protocol.election.ElectionCredentialsProvider)4 ElectionRole (org.neo4j.cluster.protocol.election.ElectionRole)4 DelayedDirectExecutor (org.neo4j.cluster.DelayedDirectExecutor)3