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());
}
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"))));
}
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);
}
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);
}
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")));
}
Aggregations