Search in sources :

Example 6 with ElectionRole

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

use of org.neo4j.cluster.protocol.election.ElectionRole in project neo4j by neo4j.

the class ClusterContextTest method testElectionVersionIsResetWhenElectorChangesFromOtherToMe.

@Test
public void testElectionVersionIsResetWhenElectorChangesFromOtherToMe() throws Exception {
    final String coordinatorRole = "coordinator";
    final InstanceId me = new InstanceId(1);
    final InstanceId winner = new InstanceId(2);
    final InstanceId elector = new InstanceId(2);
    HeartbeatContext heartbeatContext = mock(HeartbeatContext.class);
    when(heartbeatContext.getFailed()).thenReturn(Collections.<InstanceId>emptySet());
    Config config = mock(Config.class);
    when(config.get(ClusterSettings.max_acceptors)).thenReturn(10);
    MultiPaxosContext multiPaxosContext = new MultiPaxosContext(me, Iterables.iterable(new ElectionRole(coordinatorRole)), mock(ClusterConfiguration.class), new Executor() {

        @Override
        public void execute(Runnable command) {
            command.run();
        }
    }, NullLogProvider.getInstance(), mock(ObjectInputStreamFactory.class), mock(ObjectOutputStreamFactory.class), mock(AcceptorInstanceStore.class), mock(Timeouts.class), mock(ElectionCredentialsProvider.class), config);
    ClusterContext context = multiPaxosContext.getClusterContext();
    ElectionContext electionContext = multiPaxosContext.getElectionContext();
    ClusterListener listener = mock(ClusterListener.class);
    context.setLastElectorVersion(5);
    context.setLastElector(elector);
    context.addClusterListener(listener);
    context.elected(coordinatorRole, winner, elector, 6);
    verify(listener, times(1)).elected(coordinatorRole, winner, null);
    electionContext.forgetElection(coordinatorRole);
    long expectedVersion = electionContext.newConfigurationStateChange().getVersion();
    context.elected(coordinatorRole, winner, me, expectedVersion);
    verify(listener, times(2)).elected(coordinatorRole, winner, null);
}
Also used : ElectionContext(org.neo4j.cluster.protocol.election.ElectionContext) InstanceId(org.neo4j.cluster.InstanceId) Config(org.neo4j.kernel.configuration.Config) Timeouts(org.neo4j.cluster.timeout.Timeouts) ElectionRole(org.neo4j.cluster.protocol.election.ElectionRole) Executor(java.util.concurrent.Executor) ElectionCredentialsProvider(org.neo4j.cluster.protocol.election.ElectionCredentialsProvider) MultiPaxosContext(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.context.MultiPaxosContext) HeartbeatContext(org.neo4j.cluster.protocol.heartbeat.HeartbeatContext) 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 8 with ElectionRole

use of org.neo4j.cluster.protocol.election.ElectionRole in project neo4j by neo4j.

the class ClusterContextTest method testElectionVersionIsUpdatedOnElectionFromSelfAndProperlyIgnoredIfOld.

@Test
public void testElectionVersionIsUpdatedOnElectionFromSelfAndProperlyIgnoredIfOld() throws Exception {
    final String coordinatorRole = "coordinator";
    final InstanceId me = new InstanceId(1);
    final InstanceId winner = new InstanceId(2);
    HeartbeatContext heartbeatContext = mock(HeartbeatContext.class);
    when(heartbeatContext.getFailed()).thenReturn(Collections.<InstanceId>emptySet());
    Config config = mock(Config.class);
    when(config.get(ClusterSettings.max_acceptors)).thenReturn(10);
    MultiPaxosContext multiPaxosContext = new MultiPaxosContext(me, Iterables.<ElectionRole, ElectionRole>iterable(new ElectionRole(coordinatorRole)), mock(ClusterConfiguration.class), new Executor() {

        @Override
        public void execute(Runnable command) {
            command.run();
        }
    }, NullLogProvider.getInstance(), mock(ObjectInputStreamFactory.class), mock(ObjectOutputStreamFactory.class), mock(AcceptorInstanceStore.class), mock(Timeouts.class), mock(ElectionCredentialsProvider.class), config);
    ClusterContext context = multiPaxosContext.getClusterContext();
    ElectionContext electionContext = multiPaxosContext.getElectionContext();
    ClusterListener listener = mock(ClusterListener.class);
    context.addClusterListener(listener);
    electionContext.forgetElection(coordinatorRole);
    long expectedVersion = electionContext.newConfigurationStateChange().getVersion();
    context.elected(coordinatorRole, winner, me, expectedVersion);
    assertEquals(1, expectedVersion);
    verify(listener, times(1)).elected(coordinatorRole, winner, null);
    electionContext.forgetElection(coordinatorRole);
    expectedVersion = electionContext.newConfigurationStateChange().getVersion();
    context.elected(coordinatorRole, winner, me, expectedVersion);
    assertEquals(2, expectedVersion);
    verify(listener, times(2)).elected(coordinatorRole, winner, null);
    context.elected(coordinatorRole, winner, me, expectedVersion - 1);
    verifyNoMoreInteractions(listener);
}
Also used : ElectionContext(org.neo4j.cluster.protocol.election.ElectionContext) InstanceId(org.neo4j.cluster.InstanceId) Config(org.neo4j.kernel.configuration.Config) Timeouts(org.neo4j.cluster.timeout.Timeouts) ElectionRole(org.neo4j.cluster.protocol.election.ElectionRole) Executor(java.util.concurrent.Executor) ElectionCredentialsProvider(org.neo4j.cluster.protocol.election.ElectionCredentialsProvider) MultiPaxosContext(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.context.MultiPaxosContext) HeartbeatContext(org.neo4j.cluster.protocol.heartbeat.HeartbeatContext) 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 9 with ElectionRole

use of org.neo4j.cluster.protocol.election.ElectionRole in project neo4j by neo4j.

the class ClusterContextTest method testElectionVersionIsUpdatedOnElectionFromOtherAndIgnoredIfOld.

@Test
public void testElectionVersionIsUpdatedOnElectionFromOtherAndIgnoredIfOld() throws Exception {
    final String coordinatorRole = "coordinator";
    final InstanceId me = new InstanceId(1);
    final InstanceId winner = new InstanceId(2);
    final InstanceId elector = new InstanceId(2);
    HeartbeatContext heartbeatContext = mock(HeartbeatContext.class);
    when(heartbeatContext.getFailed()).thenReturn(Collections.<InstanceId>emptySet());
    Config config = mock(Config.class);
    when(config.get(ClusterSettings.max_acceptors)).thenReturn(10);
    MultiPaxosContext multiPaxosContext = new MultiPaxosContext(me, Iterables.<ElectionRole, ElectionRole>iterable(new ElectionRole(coordinatorRole)), mock(ClusterConfiguration.class), new Executor() {

        @Override
        public void execute(Runnable command) {
            command.run();
        }
    }, NullLogProvider.getInstance(), mock(ObjectInputStreamFactory.class), mock(ObjectOutputStreamFactory.class), mock(AcceptorInstanceStore.class), mock(Timeouts.class), mock(ElectionCredentialsProvider.class), config);
    ClusterContext context = multiPaxosContext.getClusterContext();
    ClusterListener listener = mock(ClusterListener.class);
    context.addClusterListener(listener);
    context.elected(coordinatorRole, winner, elector, 2);
    verify(listener, times(1)).elected(coordinatorRole, winner, null);
    context.elected(coordinatorRole, winner, elector, 3);
    verify(listener, times(2)).elected(coordinatorRole, winner, null);
    context.elected(coordinatorRole, winner, elector, 2);
    verifyNoMoreInteractions(listener);
}
Also used : InstanceId(org.neo4j.cluster.InstanceId) Config(org.neo4j.kernel.configuration.Config) Timeouts(org.neo4j.cluster.timeout.Timeouts) ElectionRole(org.neo4j.cluster.protocol.election.ElectionRole) Executor(java.util.concurrent.Executor) ElectionCredentialsProvider(org.neo4j.cluster.protocol.election.ElectionCredentialsProvider) MultiPaxosContext(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.context.MultiPaxosContext) HeartbeatContext(org.neo4j.cluster.protocol.heartbeat.HeartbeatContext) 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 10 with ElectionRole

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

MultiPaxosContext (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.context.MultiPaxosContext)11 ElectionRole (org.neo4j.cluster.protocol.election.ElectionRole)11 Timeouts (org.neo4j.cluster.timeout.Timeouts)10 Config (org.neo4j.kernel.configuration.Config)10 Executor (java.util.concurrent.Executor)9 InstanceId (org.neo4j.cluster.InstanceId)9 ObjectInputStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectInputStreamFactory)9 ObjectOutputStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectOutputStreamFactory)9 AcceptorInstanceStore (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.AcceptorInstanceStore)9 ElectionCredentialsProvider (org.neo4j.cluster.protocol.election.ElectionCredentialsProvider)9 Test (org.junit.Test)8 ClusterConfiguration (org.neo4j.cluster.protocol.cluster.ClusterConfiguration)7 DelayedDirectExecutor (org.neo4j.cluster.DelayedDirectExecutor)5 Message (org.neo4j.cluster.com.message.Message)4 LearnerMessage (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.LearnerMessage)4 HeartbeatContext (org.neo4j.cluster.protocol.heartbeat.HeartbeatContext)4 MessageHolder (org.neo4j.cluster.com.message.MessageHolder)3 ElectionContext (org.neo4j.cluster.protocol.election.ElectionContext)3 SnapshotContext (org.neo4j.cluster.protocol.snapshot.SnapshotContext)2 URI (java.net.URI)1