Search in sources :

Example 31 with ClusterConfiguration

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

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

the class ClusterContextImplTest method nonElectorLeavingTheClusterMustNotAffectElectorInformation.

/*
     * This test ensures that an instance that cleanly leaves the cluster but is not the elector has no effect on
     * elector id and last version
     */
@Test
public void nonElectorLeavingTheClusterMustNotAffectElectorInformation() throws Throwable {
    // Given
    InstanceId me = new InstanceId(1);
    InstanceId elector = new InstanceId(2);
    InstanceId other = new InstanceId(3);
    ClusterConfiguration clusterConfiguration = mock(ClusterConfiguration.class);
    when(clusterConfiguration.getUriForId(other)).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(other);
    // Then
    assertEquals(context.getLastElector(), elector);
    assertEquals(context.getLastElectorVersion(), 8);
}
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 33 with ClusterConfiguration

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

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

the class HeartbeatIAmAliveProcessorTest method shouldNotProcessMessagesWithEqualFromAndToHeaders.

@Test
public void shouldNotProcessMessagesWithEqualFromAndToHeaders() throws Exception {
    URI to = URI.create("ha://someAwesomeInstanceInJapan");
    // GIVEN
    MessageHolder outgoing = mock(MessageHolder.class);
    ClusterContext mockContext = mock(ClusterContext.class);
    ClusterConfiguration mockConfiguration = mock(ClusterConfiguration.class);
    when(mockConfiguration.getMembers()).thenReturn(new HashMap<InstanceId, URI>() {

        {
            put(new InstanceId(1), URI.create("ha://1"));
            put(new InstanceId(2), URI.create("ha://2"));
        }
    });
    when(mockContext.getConfiguration()).thenReturn(mockConfiguration);
    HeartbeatIAmAliveProcessor processor = new HeartbeatIAmAliveProcessor(outgoing, mockContext);
    Message incoming = Message.to(mock(MessageType.class), to).setHeader(Message.FROM, to.toASCIIString()).setHeader(Message.INSTANCE_ID, "1");
    // WHEN
    processor.process(incoming);
    // THEN
    verifyZeroInteractions(outgoing);
}
Also used : MessageHolder(org.neo4j.cluster.com.message.MessageHolder) Message(org.neo4j.cluster.com.message.Message) InstanceId(org.neo4j.cluster.InstanceId) ClusterContext(org.neo4j.cluster.protocol.cluster.ClusterContext) ClusterConfiguration(org.neo4j.cluster.protocol.cluster.ClusterConfiguration) URI(java.net.URI) MessageType(org.neo4j.cluster.com.message.MessageType) Test(org.junit.Test)

Example 35 with ClusterConfiguration

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

the class HeartbeatIAmAliveProcessorTest method shouldCorrectlySetTheInstanceIdHeaderInTheGeneratedHeartbeat.

@Test
public void shouldCorrectlySetTheInstanceIdHeaderInTheGeneratedHeartbeat() throws Exception {
    final List<Message> sentOut = new LinkedList<Message>();
    // Given
    MessageHolder holder = mock(MessageHolder.class);
    // The sender, which adds messages outgoing to the list above.
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            sentOut.add((Message) invocation.getArguments()[0]);
            return null;
        }
    }).when(holder).offer(Matchers.<Message<MessageType>>any());
    ClusterContext mockContext = mock(ClusterContext.class);
    ClusterConfiguration mockConfiguration = mock(ClusterConfiguration.class);
    when(mockConfiguration.getMembers()).thenReturn(new HashMap<InstanceId, URI>() {

        {
            put(new InstanceId(1), URI.create("ha://1"));
            put(new InstanceId(2), URI.create("ha://2"));
        }
    });
    when(mockContext.getConfiguration()).thenReturn(mockConfiguration);
    HeartbeatIAmAliveProcessor processor = new HeartbeatIAmAliveProcessor(holder, mockContext);
    Message incoming = Message.to(mock(MessageType.class), URI.create("ha://someAwesomeInstanceInJapan")).setHeader(Message.INSTANCE_ID, "2").setHeader(Message.FROM, "ha://2");
    // WHEN
    processor.process(incoming);
    // THEN
    assertEquals(1, sentOut.size());
    assertEquals(HeartbeatMessage.i_am_alive, sentOut.get(0).getMessageType());
    assertEquals(new InstanceId(2), ((HeartbeatMessage.IAmAliveState) sentOut.get(0).getPayload()).getServer());
}
Also used : Message(org.neo4j.cluster.com.message.Message) InstanceId(org.neo4j.cluster.InstanceId) ClusterContext(org.neo4j.cluster.protocol.cluster.ClusterContext) ClusterConfiguration(org.neo4j.cluster.protocol.cluster.ClusterConfiguration) URI(java.net.URI) LinkedList(java.util.LinkedList) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) MessageHolder(org.neo4j.cluster.com.message.MessageHolder) InvocationOnMock(org.mockito.invocation.InvocationOnMock) MessageType(org.neo4j.cluster.com.message.MessageType) 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