Search in sources :

Example 11 with Message

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

the class ProposerStateTest method something.

@Test
public void something() throws Throwable {
    Object acceptorValue = new Object();
    Object bookedValue = new Object();
    org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.InstanceId instanceId = new org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.InstanceId(42);
    PaxosInstanceStore paxosInstanceStore = new PaxosInstanceStore();
    ProposerContext context = Mockito.mock(ProposerContext.class);
    when(context.getPaxosInstance(instanceId)).thenReturn(paxosInstanceStore.getPaxosInstance(instanceId));
    when(context.getMinimumQuorumSize(Mockito.anyList())).thenReturn(2);
    // The instance is closed
    // the instance
    PaxosInstance paxosInstance = new PaxosInstance(paxosInstanceStore, instanceId);
    paxosInstance.propose(2001, Iterables.asList(Iterables.<URI, URI>iterable(create("http://something1"), create("http://something2"), create("http://something3"))));
    Message message = Message.to(ProposerMessage.promise, create("http://something1"), new ProposerMessage.PromiseState(2001, acceptorValue));
    message.setHeader(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.InstanceId.INSTANCE, instanceId.toString());
    MessageHolder mockHolder = mock(MessageHolder.class);
    ProposerState.proposer.handle(context, message, mockHolder);
}
Also used : Message(org.neo4j.cluster.com.message.Message) URI(java.net.URI) TrackingMessageHolder(org.neo4j.cluster.com.message.TrackingMessageHolder) MessageHolder(org.neo4j.cluster.com.message.MessageHolder) Test(org.junit.Test)

Example 12 with Message

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

the class AtomicBroadcastStateTest method shouldNotBroadcastWhenHavingNoQuorumNoCoordinator.

@Test
public void shouldNotBroadcastWhenHavingNoQuorumNoCoordinator() throws Throwable {
    // GIVEN
    AtomicBroadcastContext context = mock(AtomicBroadcastContext.class);
    when(context.hasQuorum()).thenReturn(false);
    InstanceId coordinator = id(1);
    when(context.getCoordinator()).thenReturn(coordinator);
    when(context.getUriForId(coordinator)).thenReturn(uri(1));
    when(context.getLog(AtomicBroadcastState.class)).thenReturn(NullLog.getInstance());
    final List<Message<?>> messages = new ArrayList<>(1);
    MessageHolder outgoing = new MessageHolder() {

        @Override
        public void offer(Message<? extends MessageType> message) {
            messages.add(message);
        }
    };
    // WHEN
    broadcasting.handle(context, message(1), outgoing);
    // THEN
    assertEquals(0, messages.size());
}
Also used : MessageHolder(org.neo4j.cluster.com.message.MessageHolder) Message(org.neo4j.cluster.com.message.Message) InstanceId(org.neo4j.cluster.InstanceId) ArrayList(java.util.ArrayList) MessageType(org.neo4j.cluster.com.message.MessageType) Test(org.junit.Test)

Example 13 with Message

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

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

the class HeartbeatStateTest method shouldIgnoreSuspicionsForOurselvesButKeepTheRest.

@Test
public void shouldIgnoreSuspicionsForOurselvesButKeepTheRest() throws Throwable {
    // Given
    InstanceId myId = new InstanceId(1);
    InstanceId foreignId = new InstanceId(3);
    HeartbeatState heartbeat = HeartbeatState.heartbeat;
    ClusterConfiguration configuration = new ClusterConfiguration("whatever", NullLogProvider.getInstance(), "cluster://1", "cluster://2");
    configuration.joined(myId, URI.create("cluster://1"));
    configuration.joined(new InstanceId(2), URI.create("cluster://2"));
    Config config = mock(Config.class);
    when(config.get(ClusterSettings.max_acceptors)).thenReturn(10);
    MultiPaxosContext context = new MultiPaxosContext(myId, 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);
    HeartbeatContext heartbeatContext = context.getHeartbeatContext();
    Message received = Message.internal(HeartbeatMessage.suspicions, new HeartbeatMessage.SuspicionsState(asSet(iterable(myId, foreignId))));
    received.setHeader(Message.FROM, "cluster://2").setHeader(Message.INSTANCE_ID, "2");
    // When
    heartbeat.handle(heartbeatContext, received, mock(MessageHolder.class));
    // Then
    assertThat(heartbeatContext.getSuspicionsOf(myId).size(), equalTo(0));
    assertThat(heartbeatContext.getSuspicionsOf(foreignId).size(), equalTo(1));
}
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) 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)

Example 15 with Message

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

the class ElectionStateTest method voteResponseShouldHaveSameVersionAsVoteRequest.

@Test
public void voteResponseShouldHaveSameVersionAsVoteRequest() throws Throwable {
    final List<Message> messages = new ArrayList<Message>(1);
    MessageHolder holder = new MessageHolder() {

        @Override
        public void offer(Message<? extends MessageType> message) {
            messages.add(message);
        }
    };
    ElectionContext context = mock(ElectionContext.class);
    final int version = 14;
    Message voteRequest = Message.to(ElectionMessage.vote, URI.create("some://instance"), new ElectionContext.VoteRequest("coordinator", version));
    voteRequest.setHeader(Message.FROM, "some://other");
    election.handle(context, voteRequest, holder);
    assertEquals(1, messages.size());
    Message response = messages.get(0);
    assertEquals(ElectionMessage.voted, response.getMessageType());
    ElectionMessage.VersionedVotedData payload = (ElectionMessage.VersionedVotedData) response.getPayload();
    assertEquals(version, payload.getVersion());
}
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) ArrayList(java.util.ArrayList) 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