Search in sources :

Example 16 with MultiPaxosContext

use of org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.context.MultiPaxosContext in project neo4j by neo4j.

the class ElectionContextTest method twoVotesFromSameInstanceForSameRoleShouldBeConsolidated.

@Test
public void twoVotesFromSameInstanceForSameRoleShouldBeConsolidated() throws Exception {
    // Given
    final String coordinatorRole = "coordinator";
    HeartbeatContext heartbeatContext = mock(HeartbeatContext.class);
    when(heartbeatContext.getFailed()).thenReturn(Collections.<InstanceId>emptySet());
    Map<InstanceId, URI> members = new HashMap<InstanceId, URI>();
    members.put(new InstanceId(1), URI.create("server1"));
    members.put(new InstanceId(2), URI.create("server2"));
    members.put(new InstanceId(3), URI.create("server3"));
    Config config = mock(Config.class);
    when(config.get(ClusterSettings.max_acceptors)).thenReturn(10);
    ClusterConfiguration clusterConfiguration = mock(ClusterConfiguration.class);
    when(clusterConfiguration.getMembers()).thenReturn(members);
    ClusterContext clusterContext = mock(ClusterContext.class);
    when(clusterContext.getConfiguration()).thenReturn(clusterConfiguration);
    MultiPaxosContext context = new MultiPaxosContext(new InstanceId(1), Iterables.iterable(new ElectionRole(coordinatorRole)), clusterConfiguration, mock(Executor.class), NullLogProvider.getInstance(), mock(ObjectInputStreamFactory.class), mock(ObjectOutputStreamFactory.class), mock(AcceptorInstanceStore.class), mock(Timeouts.class), mock(ElectionCredentialsProvider.class), config);
    ElectionContext toTest = context.getElectionContext();
    // When
    toTest.startElectionProcess(coordinatorRole);
    toTest.voted(coordinatorRole, new InstanceId(1), new IntegerElectionCredentials(100), ClusterContext.NO_ELECTOR_VERSION);
    toTest.voted(coordinatorRole, new InstanceId(2), new IntegerElectionCredentials(100), ClusterContext.NO_ELECTOR_VERSION);
    toTest.voted(coordinatorRole, new InstanceId(2), new IntegerElectionCredentials(101), ClusterContext.NO_ELECTOR_VERSION);
    // Then
    assertNull(toTest.getElectionWinner(coordinatorRole));
    assertEquals(2, toTest.getVoteCount(coordinatorRole));
}
Also used : HashMap(java.util.HashMap) InstanceId(org.neo4j.cluster.InstanceId) Config(org.neo4j.kernel.configuration.Config) Timeouts(org.neo4j.cluster.timeout.Timeouts) ClusterContext(org.neo4j.cluster.protocol.cluster.ClusterContext) ClusterConfiguration(org.neo4j.cluster.protocol.cluster.ClusterConfiguration) URI(java.net.URI) Executor(java.util.concurrent.Executor) 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 17 with MultiPaxosContext

use of org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.context.MultiPaxosContext in project neo4j by neo4j.

the class ElectionContextTest method instanceFailingShouldHaveItsVotesInvalidated.

@Test
public void instanceFailingShouldHaveItsVotesInvalidated() throws Exception {
    // Given
    final String role1 = "coordinator1";
    final String role2 = "coordinator2";
    InstanceId me = new InstanceId(1);
    InstanceId failingInstance = new InstanceId(2);
    InstanceId otherInstance = new InstanceId(3);
    Config config = mock(Config.class);
    when(config.get(ClusterSettings.max_acceptors)).thenReturn(10);
    ClusterConfiguration clusterConfiguration = mock(ClusterConfiguration.class);
    List<InstanceId> clusterMemberIds = new LinkedList<InstanceId>();
    clusterMemberIds.add(failingInstance);
    clusterMemberIds.add(otherInstance);
    clusterMemberIds.add(me);
    when(clusterConfiguration.getMemberIds()).thenReturn(clusterMemberIds);
    MultiPaxosContext context = new MultiPaxosContext(me, Iterables.<ElectionRole, ElectionRole>iterable(new ElectionRole(role1), new ElectionRole(role2)), clusterConfiguration, 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);
    HeartbeatContext heartbeatContext = context.getHeartbeatContext();
    ElectionContext electionContext = context.getElectionContext();
    electionContext.startElectionProcess(role1);
    electionContext.startElectionProcess(role2);
    electionContext.voted(role1, failingInstance, mock(ElectionCredentials.class), 2);
    electionContext.voted(role2, failingInstance, mock(ElectionCredentials.class), 2);
    electionContext.voted(role1, otherInstance, mock(ElectionCredentials.class), 2);
    electionContext.voted(role2, otherInstance, mock(ElectionCredentials.class), 2);
    heartbeatContext.suspect(failingInstance);
    assertEquals(1, electionContext.getVoteCount(role1));
    assertEquals(1, electionContext.getVoteCount(role2));
}
Also used : InstanceId(org.neo4j.cluster.InstanceId) Config(org.neo4j.kernel.configuration.Config) Timeouts(org.neo4j.cluster.timeout.Timeouts) ClusterConfiguration(org.neo4j.cluster.protocol.cluster.ClusterConfiguration) LinkedList(java.util.LinkedList) Executor(java.util.concurrent.Executor) 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 18 with MultiPaxosContext

use of org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.context.MultiPaxosContext in project neo4j by neo4j.

the class ElectionContextTest method testElectionNotOkQuorumFailedTwoInstances.

@Test
public void testElectionNotOkQuorumFailedTwoInstances() {
    Set<InstanceId> failed = new HashSet<InstanceId>();
    failed.add(new InstanceId(2));
    Map<InstanceId, URI> members = new HashMap<InstanceId, URI>();
    members.put(new InstanceId(1), URI.create("server1"));
    members.put(new InstanceId(2), URI.create("server2"));
    Config config = mock(Config.class);
    when(config.get(ClusterSettings.max_acceptors)).thenReturn(10);
    ClusterConfiguration clusterConfiguration = mock(ClusterConfiguration.class);
    when(clusterConfiguration.getMembers()).thenReturn(members);
    ClusterContext clusterContext = mock(ClusterContext.class);
    when(clusterContext.getConfiguration()).thenReturn(clusterConfiguration);
    MultiPaxosContext context = new MultiPaxosContext(new InstanceId(1), Iterables.<ElectionRole, ElectionRole>iterable(new ElectionRole("coordinator")), clusterConfiguration, mock(Executor.class), NullLogProvider.getInstance(), mock(ObjectInputStreamFactory.class), mock(ObjectOutputStreamFactory.class), mock(AcceptorInstanceStore.class), mock(Timeouts.class), mock(ElectionCredentialsProvider.class), config);
    context.getHeartbeatContext().getFailed().addAll(failed);
    ElectionContext toTest = context.getElectionContext();
    assertFalse(toTest.electionOk());
}
Also used : InstanceId(org.neo4j.cluster.InstanceId) HashMap(java.util.HashMap) Config(org.neo4j.kernel.configuration.Config) Timeouts(org.neo4j.cluster.timeout.Timeouts) ClusterContext(org.neo4j.cluster.protocol.cluster.ClusterContext) ClusterConfiguration(org.neo4j.cluster.protocol.cluster.ClusterConfiguration) URI(java.net.URI) Executor(java.util.concurrent.Executor) 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) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 19 with MultiPaxosContext

use of org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.context.MultiPaxosContext in project neo4j by neo4j.

the class ElectionContextTest method baseTestForElectionOk.

private void baseTestForElectionOk(Set<InstanceId> failed, boolean moreThanQuorum) {
    Map<InstanceId, URI> members = new HashMap<InstanceId, URI>();
    members.put(new InstanceId(1), URI.create("server1"));
    members.put(new InstanceId(2), URI.create("server2"));
    members.put(new InstanceId(3), URI.create("server3"));
    Config config = mock(Config.class);
    when(config.get(ClusterSettings.max_acceptors)).thenReturn(10);
    ClusterConfiguration clusterConfiguration = mock(ClusterConfiguration.class);
    when(clusterConfiguration.getMembers()).thenReturn(members);
    ClusterContext clusterContext = mock(ClusterContext.class);
    when(clusterContext.getConfiguration()).thenReturn(clusterConfiguration);
    MultiPaxosContext context = new MultiPaxosContext(new InstanceId(1), Iterables.iterable(new ElectionRole("coordinator")), clusterConfiguration, mock(Executor.class), NullLogProvider.getInstance(), mock(ObjectInputStreamFactory.class), mock(ObjectOutputStreamFactory.class), mock(AcceptorInstanceStore.class), mock(Timeouts.class), mock(ElectionCredentialsProvider.class), config);
    context.getHeartbeatContext().getFailed().addAll(failed);
    ElectionContext toTest = context.getElectionContext();
    assertEquals(moreThanQuorum, !toTest.electionOk());
}
Also used : HashMap(java.util.HashMap) InstanceId(org.neo4j.cluster.InstanceId) Config(org.neo4j.kernel.configuration.Config) Timeouts(org.neo4j.cluster.timeout.Timeouts) ClusterContext(org.neo4j.cluster.protocol.cluster.ClusterContext) ClusterConfiguration(org.neo4j.cluster.protocol.cluster.ClusterConfiguration) URI(java.net.URI) Executor(java.util.concurrent.Executor) 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)

Example 20 with MultiPaxosContext

use of org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.context.MultiPaxosContext 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)

Aggregations

MultiPaxosContext (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.context.MultiPaxosContext)27 Config (org.neo4j.kernel.configuration.Config)25 InstanceId (org.neo4j.cluster.InstanceId)24 Test (org.junit.Test)22 ClusterConfiguration (org.neo4j.cluster.protocol.cluster.ClusterConfiguration)20 Timeouts (org.neo4j.cluster.timeout.Timeouts)20 Executor (java.util.concurrent.Executor)19 ObjectInputStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectInputStreamFactory)17 ObjectOutputStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectOutputStreamFactory)17 AcceptorInstanceStore (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.AcceptorInstanceStore)17 ElectionCredentialsProvider (org.neo4j.cluster.protocol.election.ElectionCredentialsProvider)11 ElectionRole (org.neo4j.cluster.protocol.election.ElectionRole)11 HeartbeatContext (org.neo4j.cluster.protocol.heartbeat.HeartbeatContext)10 ClusterContext (org.neo4j.cluster.protocol.cluster.ClusterContext)8 URI (java.net.URI)7 HashMap (java.util.HashMap)6 DelayedDirectExecutor (org.neo4j.cluster.DelayedDirectExecutor)6 Message (org.neo4j.cluster.com.message.Message)4 ObjectStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectStreamFactory)4 LearnerMessage (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.LearnerMessage)4