Search in sources :

Example 6 with ClusterConfiguration

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

the class HeartbeatContextTest method setup.

@Before
public void setup() {
    Map<InstanceId, URI> members = new HashMap<InstanceId, URI>();
    for (int i = 0; i < instanceIds.length; i++) {
        members.put(instanceIds[i], URI.create(initialHosts[i]));
    }
    ClusterConfiguration config = new ClusterConfiguration("clusterName", NullLogProvider.getInstance(), initialHosts);
    config.setMembers(members);
    context = mock(ClusterContext.class);
    Config configuration = mock(Config.class);
    when(configuration.get(ClusterSettings.max_acceptors)).thenReturn(10);
    when(context.getConfiguration()).thenReturn(config);
    when(context.getMyId()).thenReturn(instanceIds[0]);
    MultiPaxosContext context = new MultiPaxosContext(instanceIds[0], Iterables.iterable(new ElectionRole("coordinator")), config, 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), configuration);
    toTest = context.getHeartbeatContext();
}
Also used : HashMap(java.util.HashMap) InstanceId(org.neo4j.cluster.InstanceId) Config(org.neo4j.kernel.configuration.Config) Timeouts(org.neo4j.cluster.timeout.Timeouts) ClusterContext(org.neo4j.cluster.protocol.cluster.ClusterContext) ClusterConfiguration(org.neo4j.cluster.protocol.cluster.ClusterConfiguration) ElectionRole(org.neo4j.cluster.protocol.election.ElectionRole) URI(java.net.URI) Executor(java.util.concurrent.Executor) 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) Before(org.junit.Before)

Example 7 with ClusterConfiguration

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

the class HeartbeatIAmAliveProcessorTest method shouldNotGenerateHeartbeatsForSuspicions.

@Test
public void shouldNotGenerateHeartbeatsForSuspicions() throws Exception {
    URI to = URI.create("ha://1");
    // 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(HeartbeatMessage.suspicions, to).setHeader(Message.FROM, to.toASCIIString()).setHeader(Message.INSTANCE_ID, "1");
    assertEquals(HeartbeatMessage.suspicions, incoming.getMessageType());
    // 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) Test(org.junit.Test)

Example 8 with ClusterConfiguration

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

the class HeartbeatIAmAliveProcessorTest method shouldNotGenerateHeartbeatsForHeartbeats.

@Test
public void shouldNotGenerateHeartbeatsForHeartbeats() throws Exception {
    URI to = URI.create("ha://1");
    // 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(HeartbeatMessage.i_am_alive, to).setHeader(Message.FROM, to.toASCIIString()).setHeader(Message.INSTANCE_ID, "1");
    assertEquals(HeartbeatMessage.i_am_alive, incoming.getMessageType());
    // 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) Test(org.junit.Test)

Example 9 with ClusterConfiguration

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

the class HeartbeatIAmAliveProcessorTest method shouldRevertToInverseUriLookupIfNoInstanceIdHeader.

/*
     * This test is required to ensure compatibility with the previous version. If we fail on non existing INSTANCE_ID
     * header then heartbeats may pause during rolling upgrades and cause timeouts, which we don't want.
     */
@Test
public void shouldRevertToInverseUriLookupIfNoInstanceIdHeader() throws Exception {
    final List<Message> sentOut = new LinkedList<Message>();
    String instance2UriString = "ha://2";
    // 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.getIdForUri(URI.create(instance2UriString))).thenReturn(new InstanceId(2));
    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.FROM, instance2UriString);
    // 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)

Example 10 with ClusterConfiguration

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

the class HeartbeatStateTest method shouldLogFirstHeartbeatAfterTimeout.

@Test
public void shouldLogFirstHeartbeatAfterTimeout() throws Throwable {
    // given
    InstanceId instanceId = new InstanceId(1), otherInstance = new InstanceId(2);
    ClusterConfiguration configuration = new ClusterConfiguration("whatever", NullLogProvider.getInstance(), "cluster://1", "cluster://2");
    configuration.getMembers().put(otherInstance, URI.create("cluster://2"));
    AssertableLogProvider internalLog = new AssertableLogProvider(true);
    TimeoutStrategy timeoutStrategy = mock(TimeoutStrategy.class);
    Timeouts timeouts = new Timeouts(timeoutStrategy);
    Config config = mock(Config.class);
    when(config.get(ClusterSettings.max_acceptors)).thenReturn(10);
    MultiPaxosContext context = new MultiPaxosContext(instanceId, iterable(new ElectionRole("coordinator")), configuration, mock(Executor.class), internalLog, mock(ObjectInputStreamFactory.class), mock(ObjectOutputStreamFactory.class), mock(AcceptorInstanceStore.class), timeouts, mock(ElectionCredentialsProvider.class), config);
    StateMachines stateMachines = new StateMachines(internalLog, mock(StateMachines.Monitor.class), mock(MessageSource.class), mock(MessageSender.class), timeouts, mock(DelayedDirectExecutor.class), command -> command.run(), instanceId);
    stateMachines.addStateMachine(new StateMachine(context.getHeartbeatContext(), HeartbeatMessage.class, HeartbeatState.start, internalLog));
    timeouts.tick(0);
    when(timeoutStrategy.timeoutFor(any(Message.class))).thenReturn(5L);
    // when
    stateMachines.process(Message.internal(HeartbeatMessage.join));
    stateMachines.process(Message.internal(HeartbeatMessage.i_am_alive, new HeartbeatMessage.IAmAliveState(otherInstance)).setHeader(Message.CREATED_BY, otherInstance.toString()));
    for (int i = 1; i <= 15; i++) {
        timeouts.tick(i);
    }
    // then
    verify(timeoutStrategy, times(3)).timeoutTriggered(argThat(new MessageArgumentMatcher<>().onMessageType(HeartbeatMessage.timed_out)));
    internalLog.assertExactly(inLog(HeartbeatState.class).debug("Received timed out for server 2"), inLog(HeartbeatContext.class).info("1(me) is now suspecting 2"), inLog(HeartbeatState.class).debug("Received timed out for server 2"), inLog(HeartbeatState.class).debug("Received timed out for server 2"));
    internalLog.clear();
    // when
    stateMachines.process(Message.internal(HeartbeatMessage.i_am_alive, new HeartbeatMessage.IAmAliveState(otherInstance)).setHeader(Message.CREATED_BY, otherInstance.toString()));
    // then
    internalLog.assertExactly(inLog(HeartbeatState.class).debug("Received i_am_alive[2] after missing 3 (15ms)"));
}
Also used : LearnerMessage(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.LearnerMessage) Message(org.neo4j.cluster.com.message.Message) InstanceId(org.neo4j.cluster.InstanceId) Timeouts(org.neo4j.cluster.timeout.Timeouts) Config(org.neo4j.kernel.configuration.Config) MessageSender(org.neo4j.cluster.com.message.MessageSender) StateMachine(org.neo4j.cluster.statemachine.StateMachine) StateMachines(org.neo4j.cluster.StateMachines) TimeoutStrategy(org.neo4j.cluster.timeout.TimeoutStrategy) MessageSource(org.neo4j.cluster.com.message.MessageSource) ClusterConfiguration(org.neo4j.cluster.protocol.cluster.ClusterConfiguration) DelayedDirectExecutor(org.neo4j.cluster.DelayedDirectExecutor) ElectionRole(org.neo4j.cluster.protocol.election.ElectionRole) 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) AssertableLogProvider(org.neo4j.logging.AssertableLogProvider) 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