Search in sources :

Example 11 with ClusterConfiguration

use of org.neo4j.cluster.protocol.cluster.ClusterConfiguration in project neo4j by neo4j.

the class HeartbeatStateTest method shouldIgnoreSuspicionsForOurselves.

@Test
public void shouldIgnoreSuspicionsForOurselves() throws Throwable {
    // Given
    InstanceId instanceId = new InstanceId(1);
    HeartbeatState heartbeat = HeartbeatState.heartbeat;
    ClusterConfiguration configuration = new ClusterConfiguration("whatever", NullLogProvider.getInstance(), "cluster://1", "cluster://2");
    configuration.joined(instanceId, URI.create("cluster://1"));
    configuration.joined(new InstanceId(2), URI.create("cluster://2"));
    Config config = mock(Config.class);
    when(config.get(ClusterSettings.max_acceptors)).thenReturn(10);
    MultiPaxosContext context = new MultiPaxosContext(instanceId, iterable(new ElectionRole("coordinator")), configuration, Mockito.mock(Executor.class), NullLogProvider.getInstance(), Mockito.mock(ObjectInputStreamFactory.class), Mockito.mock(ObjectOutputStreamFactory.class), Mockito.mock(AcceptorInstanceStore.class), Mockito.mock(Timeouts.class), mock(ElectionCredentialsProvider.class), config);
    HeartbeatContext heartbeatContext = context.getHeartbeatContext();
    Message received = Message.internal(HeartbeatMessage.suspicions, new HeartbeatMessage.SuspicionsState(asSet(iterable(instanceId))));
    received.setHeader(Message.FROM, "cluster://2").setHeader(Message.INSTANCE_ID, "2");
    // When
    heartbeat.handle(heartbeatContext, received, mock(MessageHolder.class));
    // Then
    assertThat(heartbeatContext.getSuspicionsOf(instanceId).size(), equalTo(0));
}
Also used : LearnerMessage(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.LearnerMessage) Message(org.neo4j.cluster.com.message.Message) InstanceId(org.neo4j.cluster.InstanceId) Config(org.neo4j.kernel.configuration.Config) Timeouts(org.neo4j.cluster.timeout.Timeouts) ClusterConfiguration(org.neo4j.cluster.protocol.cluster.ClusterConfiguration) ElectionRole(org.neo4j.cluster.protocol.election.ElectionRole) MessageHolder(org.neo4j.cluster.com.message.MessageHolder) Executor(java.util.concurrent.Executor) DelayedDirectExecutor(org.neo4j.cluster.DelayedDirectExecutor) ElectionCredentialsProvider(org.neo4j.cluster.protocol.election.ElectionCredentialsProvider) MultiPaxosContext(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.context.MultiPaxosContext) ObjectInputStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectInputStreamFactory) ObjectOutputStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectOutputStreamFactory) AcceptorInstanceStore(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.AcceptorInstanceStore) Test(org.junit.Test)

Example 12 with ClusterConfiguration

use of org.neo4j.cluster.protocol.cluster.ClusterConfiguration in project neo4j by neo4j.

the class AtomicBroadcastContextImplTest method shouldHasQuorumWhenTwoMachinesAliveInAClusterWithThreeMachines.

@Test
public void shouldHasQuorumWhenTwoMachinesAliveInAClusterWithThreeMachines() {
    //Given
    HeartbeatContext heartbeatContext = mock(HeartbeatContext.class);
    CommonContextState commonState = mock(CommonContextState.class);
    ClusterConfiguration configuration = mock(ClusterConfiguration.class);
    when(heartbeatContext.getAlive()).thenReturn(ids(2));
    when(commonState.configuration()).thenReturn(configuration);
    when(configuration.getMembers()).thenReturn(members(3));
    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 13 with ClusterConfiguration

use of org.neo4j.cluster.protocol.cluster.ClusterConfiguration in project neo4j by neo4j.

the class ClusterContextImplTest method electorLeavingTheClusterMustBeRemovedAsElector.

/*
     * This test ensures that an instance that cleanly leaves the cluster is no longer assumed to be an elector. This
     * has the effect that when it rejoins its elector version will be reset and its results will go through
     */
@Test
public void electorLeavingTheClusterMustBeRemovedAsElector() throws Throwable {
    // Given
    InstanceId me = new InstanceId(1);
    InstanceId elector = new InstanceId(2);
    ClusterConfiguration clusterConfiguration = mock(ClusterConfiguration.class);
    when(clusterConfiguration.getUriForId(elector)).thenReturn(URI.create("cluster://instance2"));
    CommonContextState commonContextState = mock(CommonContextState.class);
    when(commonContextState.configuration()).thenReturn(clusterConfiguration);
    ClusterContext context = new ClusterContextImpl(me, commonContextState, NullLogProvider.getInstance(), mock(Timeouts.class), mock(Executor.class), mock(ObjectOutputStreamFactory.class), mock(ObjectInputStreamFactory.class), mock(LearnerContext.class), mock(HeartbeatContext.class), mock(Config.class));
    // This means instance 2 was the elector at version 8
    context.setLastElector(elector);
    context.setLastElectorVersion(8);
    // When
    context.left(elector);
    // Then
    assertEquals(context.getLastElector(), InstanceId.NONE);
    assertEquals(context.getLastElectorVersion(), -1);
}
Also used : Executor(java.util.concurrent.Executor) InstanceId(org.neo4j.cluster.InstanceId) Timeouts(org.neo4j.cluster.timeout.Timeouts) Config(org.neo4j.kernel.configuration.Config) HeartbeatContext(org.neo4j.cluster.protocol.heartbeat.HeartbeatContext) ClusterContext(org.neo4j.cluster.protocol.cluster.ClusterContext) ObjectInputStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectInputStreamFactory) LearnerContext(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.LearnerContext) ClusterConfiguration(org.neo4j.cluster.protocol.cluster.ClusterConfiguration) ObjectOutputStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectOutputStreamFactory) Test(org.junit.Test)

Example 14 with ClusterConfiguration

use of org.neo4j.cluster.protocol.cluster.ClusterConfiguration 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 15 with ClusterConfiguration

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

Aggregations

ClusterConfiguration (org.neo4j.cluster.protocol.cluster.ClusterConfiguration)42 InstanceId (org.neo4j.cluster.InstanceId)33 Test (org.junit.Test)29 Timeouts (org.neo4j.cluster.timeout.Timeouts)20 URI (java.net.URI)18 ClusterContext (org.neo4j.cluster.protocol.cluster.ClusterContext)18 Config (org.neo4j.kernel.configuration.Config)17 Executor (java.util.concurrent.Executor)16 MultiPaxosContext (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.context.MultiPaxosContext)16 ObjectInputStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectInputStreamFactory)15 ObjectOutputStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectOutputStreamFactory)15 AcceptorInstanceStore (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.AcceptorInstanceStore)13 HeartbeatContext (org.neo4j.cluster.protocol.heartbeat.HeartbeatContext)13 Message (org.neo4j.cluster.com.message.Message)10 MessageHolder (org.neo4j.cluster.com.message.MessageHolder)10 DelayedDirectExecutor (org.neo4j.cluster.DelayedDirectExecutor)8 HashMap (java.util.HashMap)7 ElectionRole (org.neo4j.cluster.protocol.election.ElectionRole)7 ElectionCredentialsProvider (org.neo4j.cluster.protocol.election.ElectionCredentialsProvider)6 LinkedList (java.util.LinkedList)5