Search in sources :

Example 6 with MessageHolder

use of org.neo4j.cluster.com.message.MessageHolder 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 7 with MessageHolder

use of org.neo4j.cluster.com.message.MessageHolder in project neo4j by neo4j.

the class ClusterStateTest method discoveredInstancesShouldNotFilterByDefault.

@Test
public void discoveredInstancesShouldNotFilterByDefault() throws Throwable {
    // GIVEN
    ClusterContext context = mock(ClusterContext.class);
    when(context.getLog(any(Class.class))).thenReturn(NullLog.getInstance());
    when(context.getUriForId(id(2))).thenReturn(uri(2));
    when(context.getUriForId(id(3))).thenReturn(uri(3));
    List<ConfigurationRequestState> discoveredInstances = new LinkedList<>();
    when(context.getDiscoveredInstances()).thenReturn(discoveredInstances);
    MessageHolder outgoing = mock(MessageHolder.class);
    ConfigurationRequestState configurationRequestFromTwo = configuration(2);
    Message<ClusterMessage> messageFromTwo = to(configurationRequest, uri(1), configurationRequestFromTwo).setHeader(Message.FROM, uri(2).toString());
    ConfigurationRequestState configurationRequestFromThree = configuration(3);
    Message<ClusterMessage> messageFromThree = to(configurationRequest, uri(1), configurationRequestFromThree).setHeader(Message.FROM, uri(3).toString());
    // WHEN
    // We receive a configuration request from an instance which we haven't contacted
    ClusterState.discovery.handle(context, messageFromTwo, outgoing);
    // THEN
    // Since the setting is on, it should be added to the list anyway
    assertTrue(discoveredInstances.contains(configurationRequestFromTwo));
    // WHEN
    // Another contacts us as well
    ClusterState.discovery.handle(context, messageFromThree, outgoing);
    // Then
    // That should be in as well
    assertTrue(discoveredInstances.contains(configurationRequestFromTwo));
    assertTrue(discoveredInstances.contains(configurationRequestFromThree));
}
Also used : TrackingMessageHolder(org.neo4j.cluster.com.message.TrackingMessageHolder) MessageHolder(org.neo4j.cluster.com.message.MessageHolder) ConfigurationRequestState(org.neo4j.cluster.protocol.cluster.ClusterMessage.ConfigurationRequestState) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 8 with MessageHolder

use of org.neo4j.cluster.com.message.MessageHolder in project neo4j by neo4j.

the class StateMachinesTest method shouldAlwaysAddItsInstanceIdToOutgoingMessages.

@Test
public void shouldAlwaysAddItsInstanceIdToOutgoingMessages() throws Exception {
    InstanceId me = new InstanceId(42);
    final List<Message> sentOut = new LinkedList<Message>();
    /*
         * Lots of setup required. Must have a sender that keeps messages so we can see what the machine sent out.
         * We must have the StateMachines actually delegate the incoming message and retrieve the generated outgoing.
         * That means we need an actual StateMachine with a registered MessageType. And most of those are void
         * methods, which means lots of Answer objects.
         */
    // Given
    MessageSender sender = mock(MessageSender.class);
    // The sender, which adds messages outgoing to the list above.
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            sentOut.addAll((Collection<? extends Message>) invocation.getArguments()[0]);
            return null;
        }
    }).when(sender).process(Matchers.<List<Message<? extends MessageType>>>any());
    StateMachines stateMachines = new StateMachines(NullLogProvider.getInstance(), mock(StateMachines.Monitor.class), mock(MessageSource.class), sender, mock(Timeouts.class), mock(DelayedDirectExecutor.class), new Executor() {

        @Override
        public void execute(Runnable command) {
            command.run();
        }
    }, me);
    // The state machine, which has a TestMessage message type and simply adds a TO header to the messages it
    // is handed to handle.
    StateMachine machine = mock(StateMachine.class);
    when(machine.getMessageType()).then(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            return TestMessage.class;
        }
    });
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            Message message = (Message) invocation.getArguments()[0];
            MessageHolder holder = (MessageHolder) invocation.getArguments()[1];
            message.setHeader(Message.TO, "to://neverland");
            holder.offer(message);
            return null;
        }
    }).when(machine).handle(any(Message.class), any(MessageHolder.class));
    stateMachines.addStateMachine(machine);
    // When
    stateMachines.process(Message.internal(TestMessage.message1));
    // Then
    assertEquals("StateMachines should not make up messages from thin air", 1, sentOut.size());
    Message sent = sentOut.get(0);
    assertTrue("StateMachines should add the instance-id header", sent.hasHeader(Message.INSTANCE_ID));
    assertEquals("StateMachines should add instance-id header that has the correct value", me.toString(), sent.getHeader(Message.INSTANCE_ID));
}
Also used : Message(org.neo4j.cluster.com.message.Message) MessageSender(org.neo4j.cluster.com.message.MessageSender) Timeouts(org.neo4j.cluster.timeout.Timeouts) StateMachine(org.neo4j.cluster.statemachine.StateMachine) MessageSource(org.neo4j.cluster.com.message.MessageSource) LinkedList(java.util.LinkedList) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) MessageHolder(org.neo4j.cluster.com.message.MessageHolder) Executor(java.util.concurrent.Executor) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Collection(java.util.Collection) MessageType(org.neo4j.cluster.com.message.MessageType) Test(org.junit.Test)

Example 9 with MessageHolder

use of org.neo4j.cluster.com.message.MessageHolder in project neo4j by neo4j.

the class LearnerStateTest method learnerServingOldInstanceShouldNotLogErrorIfItDoesNotHaveIt.

@Test
public void learnerServingOldInstanceShouldNotLogErrorIfItDoesNotHaveIt() throws Throwable {
    // Given
    LearnerState state = LearnerState.learner;
    LearnerContext ctx = mock(LearnerContext.class);
    MessageHolder outgoing = mock(MessageHolder.class);
    // The instance will be asked for paxos instance 4...
    InstanceId paxosInstanceIdIDontHave = new InstanceId(4);
    Message<LearnerMessage> messageRequestingId = Message.to(LearnerMessage.learnRequest, URI.create("c:/1")).setHeader(Message.FROM, "c:/2").setHeader(InstanceId.INSTANCE, "4");
    // ...but it does not have it yet
    when(ctx.getPaxosInstance(paxosInstanceIdIDontHave)).thenReturn(new PaxosInstance(mock(PaxosInstanceStore.class), paxosInstanceIdIDontHave));
    // When
    state.handle(ctx, messageRequestingId, outgoing);
    // Then
    // verify there is no logging of the failure
    verify(ctx, times(0)).notifyLearnMiss(paxosInstanceIdIDontHave);
    // but the learn failed went out anyway
    verify(outgoing, times(1)).offer(Matchers.<Message<? extends MessageType>>argThat(new MessageArgumentMatcher().onMessageType(LearnerMessage.learnFailed).to(URI.create("c:/2"))));
}
Also used : MessageHolder(org.neo4j.cluster.com.message.MessageHolder) MessageArgumentMatcher(org.neo4j.cluster.protocol.MessageArgumentMatcher) Test(org.junit.Test)

Example 10 with MessageHolder

use of org.neo4j.cluster.com.message.MessageHolder in project neo4j by neo4j.

the class LearnerStateTest method learnerShouldAskAllAliveInstancesAndTheseOnlyForMissingValue.

@Test
public void learnerShouldAskAllAliveInstancesAndTheseOnlyForMissingValue() throws Throwable {
    // Given
    List<URI> allMembers = new ArrayList<URI>(3);
    // this one is failed
    URI instance1 = URI.create("c:/1");
    // this one is ok and will respond
    URI instance2 = URI.create("c:/2");
    // this one is the requesting instance
    URI instance3 = URI.create("c:/3");
    // and this one is ok and will respond too
    URI instance4 = URI.create("c:/4");
    allMembers.add(instance1);
    allMembers.add(instance2);
    allMembers.add(instance3);
    allMembers.add(instance4);
    Set<org.neo4j.cluster.InstanceId> aliveInstanceIds = new HashSet<org.neo4j.cluster.InstanceId>();
    org.neo4j.cluster.InstanceId id2 = new org.neo4j.cluster.InstanceId(2);
    org.neo4j.cluster.InstanceId id4 = new org.neo4j.cluster.InstanceId(4);
    aliveInstanceIds.add(id2);
    aliveInstanceIds.add(id4);
    LearnerState state = LearnerState.learner;
    LearnerContext ctx = mock(LearnerContext.class);
    MessageHolder outgoing = mock(MessageHolder.class);
    InstanceId paxosInstanceIdIAskedFor = new InstanceId(4);
    when(ctx.getLastDeliveredInstanceId()).thenReturn(3L);
    when(ctx.getLastKnownLearnedInstanceInCluster()).thenReturn(5L);
    when(ctx.getMemberURIs()).thenReturn(allMembers);
    when(ctx.getAlive()).thenReturn(aliveInstanceIds);
    when(ctx.getUriForId(id2)).thenReturn(instance2);
    when(ctx.getUriForId(id4)).thenReturn(instance4);
    when(ctx.getPaxosInstance(paxosInstanceIdIAskedFor)).thenReturn(new PaxosInstance(mock(PaxosInstanceStore.class), paxosInstanceIdIAskedFor));
    // could be anything, really
    Message<LearnerMessage> theCause = Message.to(LearnerMessage.catchUp, instance2);
    // When
    state.handle(ctx, Message.timeout(LearnerMessage.learnTimedout, theCause), outgoing);
    // Then
    verify(outgoing, times(1)).offer(Matchers.<Message<? extends MessageType>>argThat(new MessageArgumentMatcher().onMessageType(LearnerMessage.learnRequest).to(instance2)));
    verify(outgoing, times(1)).offer(Matchers.<Message<? extends MessageType>>argThat(new MessageArgumentMatcher().onMessageType(LearnerMessage.learnRequest).to(instance4)));
    verifyNoMoreInteractions(outgoing);
}
Also used : ArrayList(java.util.ArrayList) URI(java.net.URI) MessageHolder(org.neo4j.cluster.com.message.MessageHolder) MessageArgumentMatcher(org.neo4j.cluster.protocol.MessageArgumentMatcher) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

MessageHolder (org.neo4j.cluster.com.message.MessageHolder)29 Test (org.junit.Test)28 Message (org.neo4j.cluster.com.message.Message)19 InstanceId (org.neo4j.cluster.InstanceId)14 URI (java.net.URI)11 MessageType (org.neo4j.cluster.com.message.MessageType)11 ClusterContext (org.neo4j.cluster.protocol.cluster.ClusterContext)10 ClusterConfiguration (org.neo4j.cluster.protocol.cluster.ClusterConfiguration)8 ArrayList (java.util.ArrayList)7 LinkedList (java.util.LinkedList)5 MessageArgumentMatcher (org.neo4j.cluster.protocol.MessageArgumentMatcher)5 AtomicBroadcastMessage (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.AtomicBroadcastMessage)5 ClusterMessage (org.neo4j.cluster.protocol.cluster.ClusterMessage)5 TrackingMessageHolder (org.neo4j.cluster.com.message.TrackingMessageHolder)4 Mockito.doAnswer (org.mockito.Mockito.doAnswer)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 Answer (org.mockito.stubbing.Answer)3 HashMap (java.util.HashMap)2 Executor (java.util.concurrent.Executor)2 ConfigurationRequestState (org.neo4j.cluster.protocol.cluster.ClusterMessage.ConfigurationRequestState)2