Search in sources :

Example 1 with ClusterContext

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

the class ElectionStateTest method electionShouldRemainLocalIfStartedBySingleInstanceWhichIsTheRoleHolder.

@Test
public void electionShouldRemainLocalIfStartedBySingleInstanceWhichIsTheRoleHolder() throws Throwable {
    /*
         * Ensures that when an instance is alone in the cluster, elections for roles that it holds do not set
         * timeouts or try to reach other instances.
         */
    // Given
    ElectionContext context = mock(ElectionContext.class);
    ClusterContext clusterContextMock = mock(ClusterContext.class);
    when(clusterContextMock.getLog(Matchers.<Class>any())).thenReturn(NullLog.getInstance());
    MessageHolder holder = mock(MessageHolder.class);
    // These mean the election can proceed normally, by us
    when(context.electionOk()).thenReturn(true);
    when(context.isInCluster()).thenReturn(true);
    when(context.isElector()).thenReturn(true);
    // Like it says on the box, we are the only instance
    final InstanceId myInstanceId = new InstanceId(1);
    Map<InstanceId, URI> members = new HashMap<InstanceId, URI>();
    members.put(myInstanceId, URI.create("ha://me"));
    when(context.getMembers()).thenReturn(members);
    // Any role would do, just make sure we have it
    final String role = "master";
    ElectionContext.VoteRequest voteRequest = new ElectionContext.VoteRequest(role, 13);
    when(context.getPossibleRoles()).thenReturn(Collections.<ElectionRole>singletonList(new ElectionRole(role)));
    when(context.getElected(role)).thenReturn(myInstanceId);
    when(context.voteRequestForRole(new ElectionRole(role))).thenReturn(voteRequest);
    // Required for logging
    when(context.getLog(Mockito.<Class>any())).thenReturn(NullLog.getInstance());
    // When
    election.handle(context, Message.<ElectionMessage>internal(performRoleElections), holder);
    // Then
    // Make sure that we asked ourselves to vote for that role and that no timer was set
    verify(holder, times(1)).offer(Matchers.argThat(new MessageArgumentMatcher<ElectionMessage>().onMessageType(ElectionMessage.vote).withPayload(voteRequest)));
    verify(context, times(0)).setTimeout(Matchers.<String>any(), Matchers.<Message>any());
}
Also used : MessageHolder(org.neo4j.cluster.com.message.MessageHolder) MessageArgumentMatcher(org.neo4j.cluster.protocol.MessageArgumentMatcher) InstanceId(org.neo4j.cluster.InstanceId) HashMap(java.util.HashMap) ClusterContext(org.neo4j.cluster.protocol.cluster.ClusterContext) URI(java.net.URI) Test(org.junit.Test)

Example 2 with ClusterContext

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

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

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

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

the class ClusterContextImplTest method electorFailingMustCauseElectorVersionToBeReset.

/*
     * This test ensures that an instance that is marked as failed has its elector version reset. This means that
     * the instance, once it comes back, will still be able to do elections even if it lost state
     */
@Test
public void electorFailingMustCauseElectorVersionToBeReset() throws Exception {
    // Given
    InstanceId me = new InstanceId(1);
    InstanceId elector = new InstanceId(2);
    CommonContextState commonContextState = mock(CommonContextState.class, RETURNS_MOCKS);
    Timeouts timeouts = mock(Timeouts.class);
    Executor executor = mock(Executor.class);
    HeartbeatContext heartbeatContext = mock(HeartbeatContext.class);
    ArgumentCaptor<HeartbeatListener> listenerCaptor = ArgumentCaptor.forClass(HeartbeatListener.class);
    ClusterContext context = new ClusterContextImpl(me, commonContextState, NullLogProvider.getInstance(), timeouts, executor, mock(ObjectOutputStreamFactory.class), mock(ObjectInputStreamFactory.class), mock(LearnerContext.class), heartbeatContext, mock(Config.class));
    verify(heartbeatContext).addHeartbeatListener(listenerCaptor.capture());
    HeartbeatListener theListener = listenerCaptor.getValue();
    // This means instance 2 was the elector at version 8
    context.setLastElector(elector);
    context.setLastElectorVersion(8);
    // When
    theListener.failed(elector);
    // Then
    assertEquals(context.getLastElector(), InstanceId.NONE);
    assertEquals(context.getLastElectorVersion(), ClusterContextImpl.NO_ELECTOR_VERSION);
}
Also used : InstanceId(org.neo4j.cluster.InstanceId) Timeouts(org.neo4j.cluster.timeout.Timeouts) Config(org.neo4j.kernel.configuration.Config) ClusterContext(org.neo4j.cluster.protocol.cluster.ClusterContext) LearnerContext(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.LearnerContext) HeartbeatListener(org.neo4j.cluster.protocol.heartbeat.HeartbeatListener) Executor(java.util.concurrent.Executor) HeartbeatContext(org.neo4j.cluster.protocol.heartbeat.HeartbeatContext) ObjectInputStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectInputStreamFactory) ObjectOutputStreamFactory(org.neo4j.cluster.protocol.atomicbroadcast.ObjectOutputStreamFactory) Test(org.junit.Test)

Aggregations

ClusterContext (org.neo4j.cluster.protocol.cluster.ClusterContext)30 Test (org.junit.Test)27 InstanceId (org.neo4j.cluster.InstanceId)27 ClusterConfiguration (org.neo4j.cluster.protocol.cluster.ClusterConfiguration)17 Executor (java.util.concurrent.Executor)16 ObjectInputStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectInputStreamFactory)16 ObjectOutputStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectOutputStreamFactory)16 Timeouts (org.neo4j.cluster.timeout.Timeouts)16 Config (org.neo4j.kernel.configuration.Config)16 URI (java.net.URI)13 HeartbeatContext (org.neo4j.cluster.protocol.heartbeat.HeartbeatContext)13 MessageHolder (org.neo4j.cluster.com.message.MessageHolder)10 LearnerContext (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.LearnerContext)9 HashMap (java.util.HashMap)7 AcceptorInstanceStore (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.AcceptorInstanceStore)7 MultiPaxosContext (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.context.MultiPaxosContext)7 Message (org.neo4j.cluster.com.message.Message)6 LinkedList (java.util.LinkedList)4 MessageType (org.neo4j.cluster.com.message.MessageType)4 HashSet (java.util.HashSet)3