Search in sources :

Example 16 with HeartbeatContext

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

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

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

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

the class ClusterMockTest method verifyConfigurations.

private void verifyConfigurations(StateMachines stateMachines, List<URI> members, Map<String, InstanceId> roles, Set<InstanceId> failed, List<AssertionError> errors) {
    ClusterContext context = (ClusterContext) stateMachines.getStateMachine(ClusterMessage.class).getContext();
    int myId = context.getMyId().toIntegerIndex();
    State<?, ?> clusterState = stateMachines.getStateMachine(ClusterMessage.class).getState();
    if (!clusterState.equals(ClusterState.entered)) {
        logger.getLogger().warning("Instance " + myId + " is not in the cluster (" + clusterState + ")");
        return;
    }
    HeartbeatContext heartbeatContext = (HeartbeatContext) stateMachines.getStateMachine(HeartbeatMessage.class).getContext();
    ClusterConfiguration clusterConfiguration = context.getConfiguration();
    try {
        assertEquals("Config for server" + myId + " is wrong", new HashSet<URI>(members), new HashSet<URI>(clusterConfiguration.getMemberURIs()));
    } catch (AssertionError e) {
        errors.add(e);
    }
    try {
        assertEquals("Roles for server" + myId + " is wrong", roles, clusterConfiguration.getRoles());
    } catch (AssertionError e) {
        errors.add(e);
    }
    try {
        assertEquals("Failed for server" + myId + " is wrong", failed, heartbeatContext.getFailed());
    } catch (AssertionError e) {
        errors.add(e);
    }
}
Also used : HeartbeatContext(org.neo4j.cluster.protocol.heartbeat.HeartbeatContext) URI(java.net.URI)

Example 20 with HeartbeatContext

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

the class ClusterMockTest method verifyConfigurations.

public void verifyConfigurations(String description) {
    logger.getLogger().fine("Verify configurations");
    List<URI> members = null;
    Map<String, InstanceId> roles = null;
    Set<InstanceId> failed = null;
    List<AssertionError> errors = new LinkedList<AssertionError>();
    List<TestProtocolServer> protocolServers = network.getServers();
    for (int j = 0; j < protocolServers.size(); j++) {
        StateMachines stateMachines = protocolServers.get(j).getServer().getStateMachines();
        State<?, ?> clusterState = stateMachines.getStateMachine(ClusterMessage.class).getState();
        if (!clusterState.equals(ClusterState.entered)) {
            logger.getLogger().fine("Instance " + (j + 1) + " is not in the cluster (" + clusterState + ")");
            continue;
        }
        ClusterContext context = (ClusterContext) stateMachines.getStateMachine(ClusterMessage.class).getContext();
        HeartbeatContext heartbeatContext = (HeartbeatContext) stateMachines.getStateMachine(HeartbeatMessage.class).getContext();
        ClusterConfiguration clusterConfiguration = context.getConfiguration();
        if (!clusterConfiguration.getMemberURIs().isEmpty()) {
            logger.getLogger().fine("   Server " + (j + 1) + ": Cluster:" + clusterConfiguration.getMemberURIs() + ", Roles:" + clusterConfiguration.getRoles() + ", Failed:" + heartbeatContext.getFailed());
            if (members == null) {
                members = clusterConfiguration.getMemberURIs();
                roles = clusterConfiguration.getRoles();
                failed = heartbeatContext.getFailed();
            } else {
                verifyConfigurations(stateMachines, members, roles, failed, errors);
            }
        }
    }
    assertEquals(description + ": In:" + in + ", Out:" + out, protocolServers.size(), Iterables.count(Iterables.<Cluster, List<Cluster>>flatten(in, out)));
    if (!errors.isEmpty()) {
        for (AssertionError error : errors) {
            logger.getLogger().severe(error.toString());
        }
        throw errors.get(0);
    }
}
Also used : InstanceId(org.neo4j.cluster.InstanceId) StateMachines(org.neo4j.cluster.StateMachines) URI(java.net.URI) LinkedList(java.util.LinkedList) TestProtocolServer(org.neo4j.cluster.TestProtocolServer) HeartbeatContext(org.neo4j.cluster.protocol.heartbeat.HeartbeatContext) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List)

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