Search in sources :

Example 11 with HeartbeatContext

use of org.neo4j.cluster.protocol.heartbeat.HeartbeatContext in project neo4j by neo4j.

the class AtomicBroadcastContextImplTest method shouldHasNoQuorumWhenOneMachineAliveInAClusterWithThreeMachines.

@Test
public void shouldHasNoQuorumWhenOneMachineAliveInAClusterWithThreeMachines() {
    //Given
    HeartbeatContext heartbeatContext = mock(HeartbeatContext.class);
    CommonContextState commonState = mock(CommonContextState.class);
    ClusterConfiguration configuration = mock(ClusterConfiguration.class);
    when(heartbeatContext.getAlive()).thenReturn(ids(1));
    when(commonState.configuration()).thenReturn(configuration);
    when(configuration.getMembers()).thenReturn(members(3));
    AtomicBroadcastContextImpl context = new AtomicBroadcastContextImpl(null, commonState, null, null, null, // we do not care about other args
    heartbeatContext);
    //When
    boolean hasQuorum = context.hasQuorum();
    //Then
    assertFalse(hasQuorum);
}
Also used : HeartbeatContext(org.neo4j.cluster.protocol.heartbeat.HeartbeatContext) ClusterConfiguration(org.neo4j.cluster.protocol.cluster.ClusterConfiguration) Test(org.junit.Test)

Example 12 with HeartbeatContext

use of org.neo4j.cluster.protocol.heartbeat.HeartbeatContext in project neo4j by neo4j.

the class ElectionContextTest method electorLeavingAndRejoiningWithNoElectionsInBetweenMustStillHaveElectionsGoThrough.

/*
     * This assumes an instance leaves the cluster normally and then rejoins, without any elections in between. The
     * expected result is that it will succeed in sending election results.
     */
@Test
public void electorLeavingAndRejoiningWithNoElectionsInBetweenMustStillHaveElectionsGoThrough() throws Exception {
    // Given
    final String role1 = "coordinator1";
    InstanceId me = new InstanceId(1);
    InstanceId leavingInstance = new InstanceId(2);
    InstanceId forQuorum = 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(leavingInstance);
    clusterMemberIds.add(me);
    clusterMemberIds.add(forQuorum);
    when(clusterConfiguration.getMemberIds()).thenReturn(clusterMemberIds);
    MultiPaxosContext context = new MultiPaxosContext(me, Iterables.<ElectionRole, ElectionRole>iterable(new ElectionRole(role1)), 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();
    ClusterContext clusterContext = context.getClusterContext();
    clusterContext.setLastElector(leavingInstance);
    clusterContext.setLastElectorVersion(8);
    // When the elector leaves the cluster
    clusterContext.left(leavingInstance);
    // Then the elector is reset to defaults
    assertEquals(clusterContext.getLastElector(), InstanceId.NONE);
    assertEquals(clusterContext.getLastElectorVersion(), ClusterContext.NO_ELECTOR_VERSION);
    // When the elector comes back with an election result
    // We don't need to join, election results do not check for elector membership
    clusterContext.elected(role1, forQuorum, leavingInstance, 9);
    // Then the result is actually respected
    assertEquals(clusterContext.getLastElector(), leavingInstance);
    assertEquals(clusterContext.getLastElectorVersion(), 9);
}
Also used : 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) 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 13 with HeartbeatContext

use of org.neo4j.cluster.protocol.heartbeat.HeartbeatContext in project neo4j by neo4j.

the class ElectionContextTest method voteFromPreviousSuccessfulElectionMustNotBeCounted.

@Test
public void voteFromPreviousSuccessfulElectionMustNotBeCounted() throws Exception {
    // Given
    final String coordinatorRole = "coordinator";
    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);
    ElectionContext context = new MultiPaxosContext(new InstanceId(1), Iterables.iterable(new ElectionRole(coordinatorRole)), mock(ClusterConfiguration.class), mock(Executor.class), NullLogProvider.getInstance(), mock(ObjectInputStreamFactory.class), mock(ObjectOutputStreamFactory.class), mock(AcceptorInstanceStore.class), mock(Timeouts.class), mock(ElectionCredentialsProvider.class), config).getElectionContext();
    // When
    ElectionContext.VoteRequest voteRequestBefore = context.voteRequestForRole(new ElectionRole(coordinatorRole));
    context.forgetElection(coordinatorRole);
    // Then
    assertFalse(context.voted(coordinatorRole, new InstanceId(2), null, voteRequestBefore.getVersion() - 1));
}
Also used : InstanceId(org.neo4j.cluster.InstanceId) Config(org.neo4j.kernel.configuration.Config) MultiPaxosContext(org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.context.MultiPaxosContext) HeartbeatContext(org.neo4j.cluster.protocol.heartbeat.HeartbeatContext) Test(org.junit.Test)

Example 14 with HeartbeatContext

use of org.neo4j.cluster.protocol.heartbeat.HeartbeatContext in project neo4j by neo4j.

the class ElectionContextTest method failedElectorRejoiningMustHaveItsVersionFromVoteRequestsSetTheElectorVersion.

@Test
public void failedElectorRejoiningMustHaveItsVersionFromVoteRequestsSetTheElectorVersion() throws Throwable {
    // Given
    final String role1 = "coordinator1";
    InstanceId me = new InstanceId(1);
    InstanceId failingInstance = new InstanceId(2);
    InstanceId forQuorum = 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(me);
    clusterMemberIds.add(forQuorum);
    when(clusterConfiguration.getMemberIds()).thenReturn(clusterMemberIds);
    MultiPaxosContext context = new MultiPaxosContext(me, Iterables.iterable(new ElectionRole(role1)), 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();
    ClusterContext clusterContext = context.getClusterContext();
    clusterContext.setLastElector(failingInstance);
    clusterContext.setLastElectorVersion(8);
    // When the elector fails
    heartbeatContext.suspicions(forQuorum, Collections.singleton(failingInstance));
    heartbeatContext.suspect(failingInstance);
    // Then the elector is reset to defaults
    assertEquals(clusterContext.getLastElector(), InstanceId.NONE);
    assertEquals(clusterContext.getLastElectorVersion(), ClusterContext.NO_ELECTOR_VERSION);
    // When the elector comes back with an election result
    clusterContext.elected(role1, forQuorum, failingInstance, 9);
    // Then the result is actually respected
    assertEquals(clusterContext.getLastElector(), failingInstance);
    assertEquals(clusterContext.getLastElectorVersion(), 9);
}
Also used : 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) 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 15 with HeartbeatContext

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

Aggregations

HeartbeatContext (org.neo4j.cluster.protocol.heartbeat.HeartbeatContext)26 InstanceId (org.neo4j.cluster.InstanceId)22 Test (org.junit.Test)21 Timeouts (org.neo4j.cluster.timeout.Timeouts)16 Config (org.neo4j.kernel.configuration.Config)15 Executor (java.util.concurrent.Executor)13 ObjectInputStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectInputStreamFactory)13 ObjectOutputStreamFactory (org.neo4j.cluster.protocol.atomicbroadcast.ObjectOutputStreamFactory)13 ClusterConfiguration (org.neo4j.cluster.protocol.cluster.ClusterConfiguration)11 MultiPaxosContext (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.context.MultiPaxosContext)10 ClusterContext (org.neo4j.cluster.protocol.cluster.ClusterContext)9 AcceptorInstanceStore (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.AcceptorInstanceStore)8 LinkedList (java.util.LinkedList)5 LearnerContext (org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.LearnerContext)5 HeartbeatListener (org.neo4j.cluster.protocol.heartbeat.HeartbeatListener)5 URI (java.net.URI)4 ArrayList (java.util.ArrayList)4 ElectionCredentialsProvider (org.neo4j.cluster.protocol.election.ElectionCredentialsProvider)4 ElectionRole (org.neo4j.cluster.protocol.election.ElectionRole)4 DelayedDirectExecutor (org.neo4j.cluster.DelayedDirectExecutor)3