Search in sources :

Example 56 with InstanceId

use of org.neo4j.cluster.InstanceId 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 57 with InstanceId

use of org.neo4j.cluster.InstanceId 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 58 with InstanceId

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

the class LearnerContextImplTest method shouldOnlyLogLearnMissOnce.

@Test
public void shouldOnlyLogLearnMissOnce() throws Exception {
    // Given
    final AssertableLogProvider logProvider = new AssertableLogProvider();
    LearnerContextImpl ctx = new LearnerContextImpl(new InstanceId(1), mock(CommonContextState.class), logProvider, mock(Timeouts.class), mock(PaxosInstanceStore.class), mock(AcceptorInstanceStore.class), mock(ObjectInputStreamFactory.class), mock(ObjectOutputStreamFactory.class), mock(HeartbeatContextImpl.class));
    // When
    ctx.notifyLearnMiss(new org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.InstanceId(1L));
    ctx.notifyLearnMiss(new org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.InstanceId(1L));
    ctx.notifyLearnMiss(new org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.InstanceId(2L));
    ctx.notifyLearnMiss(new org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.InstanceId(2L));
    ctx.notifyLearnMiss(new org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.InstanceId(1L));
    // Then
    logProvider.assertExactly(inLog(LearnerState.class).warn(containsString("Did not have learned value for Paxos instance 1.")), inLog(LearnerState.class).warn(containsString("Did not have learned value for Paxos instance 2.")), inLog(LearnerState.class).warn(containsString("Did not have learned value for Paxos instance 1.")));
}
Also used : InstanceId(org.neo4j.cluster.InstanceId) Timeouts(org.neo4j.cluster.timeout.Timeouts) PaxosInstanceStore(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.PaxosInstanceStore) ObjectInputStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectInputStreamFactory) AcceptorInstanceStore(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.AcceptorInstanceStore) ObjectOutputStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectOutputStreamFactory) AssertableLogProvider(org.neo4j.logging.AssertableLogProvider) Test(org.junit.Test)

Example 59 with InstanceId

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

the class ClusterConfiguration method removeElected.

public void removeElected(String roleName) {
    Map<String, InstanceId> newRoles = new HashMap<>(roles);
    InstanceId removed = newRoles.remove(roleName);
    roles = newRoles;
    log.info("Removed role " + roleName + " from instance " + removed);
}
Also used : HashMap(java.util.HashMap) InstanceId(org.neo4j.cluster.InstanceId)

Example 60 with InstanceId

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

the class HeartbeatIAmAliveProcessor method process.

@Override
public boolean process(Message<? extends MessageType> message) {
    if (!message.isInternal() && !message.getMessageType().equals(HeartbeatMessage.i_am_alive) && !message.getMessageType().equals(HeartbeatMessage.suspicions)) {
        // We assume the FROM header always exists.
        String from = message.getHeader(Message.FROM);
        if (!from.equals(message.getHeader(Message.TO))) {
            InstanceId theId;
            if (message.hasHeader(Message.INSTANCE_ID)) {
                // INSTANCE_ID is there since after 1.9.6
                theId = new InstanceId(Integer.parseInt(message.getHeader(Message.INSTANCE_ID)));
            } else {
                theId = clusterContext.getConfiguration().getIdForUri(URI.create(from));
            }
            if (theId != null && clusterContext.getConfiguration().getMembers().containsKey(theId) && !clusterContext.isMe(theId)) {
                Message<HeartbeatMessage> heartbeatMessage = message.copyHeadersTo(Message.internal(HeartbeatMessage.i_am_alive, new HeartbeatMessage.IAmAliveState(theId)), Message.FROM, Message.INSTANCE_ID);
                output.offer(heartbeatMessage);
            }
        }
    }
    return true;
}
Also used : InstanceId(org.neo4j.cluster.InstanceId)

Aggregations

InstanceId (org.neo4j.cluster.InstanceId)154 Test (org.junit.Test)129 URI (java.net.URI)47 Config (org.neo4j.kernel.configuration.Config)37 ClusterConfiguration (org.neo4j.cluster.protocol.cluster.ClusterConfiguration)36 Timeouts (org.neo4j.cluster.timeout.Timeouts)32 Executor (java.util.concurrent.Executor)28 ClusterContext (org.neo4j.cluster.protocol.cluster.ClusterContext)28 ObjectInputStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectInputStreamFactory)27 ObjectOutputStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectOutputStreamFactory)27 HeartbeatContext (org.neo4j.cluster.protocol.heartbeat.HeartbeatContext)27 MultiPaxosContext (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.context.MultiPaxosContext)24 AcceptorInstanceStore (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.AcceptorInstanceStore)18 MessageHolder (org.neo4j.cluster.com.message.MessageHolder)16 Message (org.neo4j.cluster.com.message.Message)15 ArrayList (java.util.ArrayList)13 HashMap (java.util.HashMap)13 ClusterMemberAvailability (org.neo4j.cluster.member.ClusterMemberAvailability)13 ClusterMemberEvents (org.neo4j.cluster.member.ClusterMemberEvents)12 ClusterMemberListener (org.neo4j.cluster.member.ClusterMemberListener)12