Search in sources :

Example 1 with MessageArgumentMatcher

use of org.neo4j.cluster.protocol.MessageArgumentMatcher 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 MessageArgumentMatcher

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

use of org.neo4j.cluster.protocol.MessageArgumentMatcher 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)

Example 4 with MessageArgumentMatcher

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

the class ProposerStateTest method ifProposingWithClosedInstanceThenRetryWithNextInstance.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void ifProposingWithClosedInstanceThenRetryWithNextInstance() throws Throwable {
    ProposerContext context = Mockito.mock(ProposerContext.class);
    when(context.getLog(any(Class.class))).thenReturn(NullLog.getInstance());
    org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.InstanceId instanceId = new org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.InstanceId(42);
    PaxosInstanceStore paxosInstanceStore = new PaxosInstanceStore();
    // The instance is closed
    // the instance
    PaxosInstance paxosInstance = new PaxosInstance(paxosInstanceStore, instanceId);
    // is closed for that conversation, not really important
    paxosInstance.closed(instanceId, "1/15#");
    when(context.unbookInstance(instanceId)).thenReturn(Message.internal(ProposerMessage.accepted, "the closed payload"));
    // required for
    when(context.getPaxosInstance(instanceId)).thenReturn(paxosInstance);
    // But in the meantime it was reused and has now (of course) timed out
    String theTimedoutPayload = "the timed out payload";
    Message message = Message.internal(ProposerMessage.phase1Timeout, theTimedoutPayload);
    message.setHeader(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.InstanceId.INSTANCE, instanceId.toString());
    // Handle it
    MessageHolder mockHolder = mock(MessageHolder.class);
    ProposerState.proposer.handle(context, message, mockHolder);
    // Verify it was resent as a propose with the same value
    verify(mockHolder, times(1)).offer(Matchers.<Message<? extends MessageType>>argThat(new MessageArgumentMatcher().onMessageType(ProposerMessage.propose).withPayload(theTimedoutPayload)));
    verify(context, times(1)).unbookInstance(instanceId);
}
Also used : Message(org.neo4j.cluster.com.message.Message) TrackingMessageHolder(org.neo4j.cluster.com.message.TrackingMessageHolder) MessageHolder(org.neo4j.cluster.com.message.MessageHolder) MessageArgumentMatcher(org.neo4j.cluster.protocol.MessageArgumentMatcher) Test(org.junit.Test)

Example 5 with MessageArgumentMatcher

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

the class HeartbeatStateTest method shouldAddInstanceIdHeaderInCatchUpMessages.

@Test
public void shouldAddInstanceIdHeaderInCatchUpMessages() throws Throwable {
    // Given
    InstanceId instanceId = new InstanceId(1);
    HeartbeatState heartbeat = HeartbeatState.heartbeat;
    ClusterConfiguration configuration = new ClusterConfiguration("whatever", NullLogProvider.getInstance(), "cluster://1", "cluster://2");
    configuration.joined(instanceId, URI.create("cluster://1"));
    InstanceId otherInstance = new InstanceId(2);
    configuration.joined(otherInstance, URI.create("cluster://2"));
    Config config = mock(Config.class);
    when(config.get(ClusterSettings.max_acceptors)).thenReturn(10);
    MultiPaxosContext context = new MultiPaxosContext(instanceId, iterable(new ElectionRole("coordinator")), configuration, 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), config);
    int lastDeliveredInstanceId = 100;
    context.getLearnerContext().setLastDeliveredInstanceId(lastDeliveredInstanceId);
    // This gap will trigger the catchUp message that we'll test against
    lastDeliveredInstanceId += 20;
    HeartbeatContext heartbeatContext = context.getHeartbeatContext();
    Message received = Message.internal(HeartbeatMessage.i_am_alive, new HeartbeatMessage.IAmAliveState(otherInstance));
    received.setHeader(Message.FROM, "cluster://2").setHeader(Message.INSTANCE_ID, "2").setHeader("last-learned", Integer.toString(lastDeliveredInstanceId));
    // When
    MessageHolder holder = mock(MessageHolder.class);
    heartbeat.handle(heartbeatContext, received, holder);
    // Then
    verify(holder, times(1)).offer(Matchers.argThat(new MessageArgumentMatcher<LearnerMessage>().onMessageType(LearnerMessage.catchUp).withHeader(Message.INSTANCE_ID, "2")));
}
Also used : LearnerMessage(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.LearnerMessage) Message(org.neo4j.cluster.com.message.Message) InstanceId(org.neo4j.cluster.InstanceId) Config(org.neo4j.kernel.configuration.Config) Timeouts(org.neo4j.cluster.timeout.Timeouts) ClusterConfiguration(org.neo4j.cluster.protocol.cluster.ClusterConfiguration) ElectionRole(org.neo4j.cluster.protocol.election.ElectionRole) MessageHolder(org.neo4j.cluster.com.message.MessageHolder) Executor(java.util.concurrent.Executor) DelayedDirectExecutor(org.neo4j.cluster.DelayedDirectExecutor) ElectionCredentialsProvider(org.neo4j.cluster.protocol.election.ElectionCredentialsProvider) MessageArgumentMatcher(org.neo4j.cluster.protocol.MessageArgumentMatcher) 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) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)5 MessageHolder (org.neo4j.cluster.com.message.MessageHolder)5 MessageArgumentMatcher (org.neo4j.cluster.protocol.MessageArgumentMatcher)5 URI (java.net.URI)2 InstanceId (org.neo4j.cluster.InstanceId)2 Message (org.neo4j.cluster.com.message.Message)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Executor (java.util.concurrent.Executor)1 DelayedDirectExecutor (org.neo4j.cluster.DelayedDirectExecutor)1 TrackingMessageHolder (org.neo4j.cluster.com.message.TrackingMessageHolder)1 ObjectInputStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectInputStreamFactory)1 ObjectOutputStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectOutputStreamFactory)1 AcceptorInstanceStore (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.AcceptorInstanceStore)1 LearnerMessage (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.LearnerMessage)1 MultiPaxosContext (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.context.MultiPaxosContext)1 ClusterConfiguration (org.neo4j.cluster.protocol.cluster.ClusterConfiguration)1 ClusterContext (org.neo4j.cluster.protocol.cluster.ClusterContext)1 ElectionCredentialsProvider (org.neo4j.cluster.protocol.election.ElectionCredentialsProvider)1