Search in sources :

Example 1 with Message

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

the class ElectionStateTest method shouldSendAtomicBroadcastOnJoiningAClusterWithAnEstablishedCoordinator.

@Test
public void shouldSendAtomicBroadcastOnJoiningAClusterWithAnEstablishedCoordinator() throws Throwable {
    // Given
    String winnerURI = "some://winner";
    InstanceId winner = new InstanceId(2);
    final List<Message<?>> messages = new ArrayList<>(1);
    MessageHolder holder = new MessageHolder() {

        @Override
        public void offer(Message<? extends MessageType> message) {
            messages.add(message);
        }
    };
    ElectionCredentials voteCredentialComparable = mock(ElectionCredentials.class);
    ElectionContext electionContext = mock(ElectionContext.class);
    when(electionContext.voted(eq(COORDINATOR), eq(new InstanceId(1)), eq(voteCredentialComparable), anyLong())).thenReturn(true);
    when(electionContext.getVoteCount(COORDINATOR)).thenReturn(3);
    when(electionContext.getNeededVoteCount()).thenReturn(3);
    when(electionContext.getElectionWinner(COORDINATOR)).thenReturn(winner);
    when(electionContext.getLog(any(Class.class))).thenReturn(NullLog.getInstance());
    when(electionContext.newConfigurationStateChange()).thenReturn(mock(ClusterMessage.VersionedConfigurationStateChange.class));
    when(electionContext.getUriForId(winner)).thenReturn(URI.create(winnerURI));
    // When
    Message<ElectionMessage> votedMessage = Message.to(ElectionMessage.voted, URI.create("some://instance"), new ElectionMessage.VotedData(COORDINATOR, new InstanceId(1), voteCredentialComparable));
    votedMessage.setHeader(Message.FROM, "some://other");
    election.handle(electionContext, votedMessage, holder);
    // Then
    assertEquals(1, messages.size());
    Message<?> message = messages.get(0);
    assertEquals(AtomicBroadcastMessage.broadcast, message.getMessageType());
}
Also used : AtomicBroadcastMessage(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.AtomicBroadcastMessage) Message(org.neo4j.cluster.com.message.Message) ClusterMessage(org.neo4j.cluster.protocol.cluster.ClusterMessage) InstanceId(org.neo4j.cluster.InstanceId) ArrayList(java.util.ArrayList) MessageHolder(org.neo4j.cluster.com.message.MessageHolder) MessageType(org.neo4j.cluster.com.message.MessageType) Test(org.junit.Test)

Example 2 with Message

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

the class ElectionStateTest method timeoutMakesElectionBeForgotten.

@Test
public void timeoutMakesElectionBeForgotten() throws Throwable {
    // Given
    String coordinatorRole = "coordinator";
    ElectionContext context = mock(ElectionContext.class);
    when(context.getLog(Mockito.<Class>any())).thenReturn(NullLog.getInstance());
    MessageHolder holder = mock(MessageHolder.class);
    Message timeout = Message.timeout(ElectionMessage.electionTimeout, Message.internal(performRoleElections), new ElectionState.ElectionTimeoutData(coordinatorRole, null));
    // When
    election.handle(context, timeout, holder);
    // Then
    verify(context, times(1)).forgetElection(coordinatorRole);
}
Also used : MessageHolder(org.neo4j.cluster.com.message.MessageHolder) AtomicBroadcastMessage(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.AtomicBroadcastMessage) Message(org.neo4j.cluster.com.message.Message) ClusterMessage(org.neo4j.cluster.protocol.cluster.ClusterMessage) Test(org.junit.Test)

Example 3 with Message

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

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

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

Aggregations

Message (org.neo4j.cluster.com.message.Message)24 Test (org.junit.Test)22 MessageHolder (org.neo4j.cluster.com.message.MessageHolder)21 InstanceId (org.neo4j.cluster.InstanceId)14 MessageType (org.neo4j.cluster.com.message.MessageType)12 ClusterConfiguration (org.neo4j.cluster.protocol.cluster.ClusterConfiguration)9 URI (java.net.URI)8 ArrayList (java.util.ArrayList)7 ClusterContext (org.neo4j.cluster.protocol.cluster.ClusterContext)6 AtomicBroadcastMessage (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.AtomicBroadcastMessage)5 ClusterMessage (org.neo4j.cluster.protocol.cluster.ClusterMessage)5 Executor (java.util.concurrent.Executor)4 Timeouts (org.neo4j.cluster.timeout.Timeouts)4 LinkedList (java.util.LinkedList)3 Mockito.doAnswer (org.mockito.Mockito.doAnswer)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 Answer (org.mockito.stubbing.Answer)3 DelayedDirectExecutor (org.neo4j.cluster.DelayedDirectExecutor)3 ObjectInputStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectInputStreamFactory)3 ObjectOutputStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectOutputStreamFactory)3