use of org.neo4j.cluster.protocol.election.ElectionRole in project neo4j by neo4j.
the class MultiPaxosServerFactory method newProtocolServer.
@Override
public ProtocolServer newProtocolServer(InstanceId me, TimeoutStrategy timeoutStrategy, MessageSource input, MessageSender output, AcceptorInstanceStore acceptorInstanceStore, ElectionCredentialsProvider electionCredentialsProvider, Executor stateMachineExecutor, ObjectInputStreamFactory objectInputStreamFactory, ObjectOutputStreamFactory objectOutputStreamFactory, Config config) {
DelayedDirectExecutor executor = new DelayedDirectExecutor(logging);
// Create state machines
Timeouts timeouts = new Timeouts(timeoutStrategy);
final MultiPaxosContext context = new MultiPaxosContext(me, Iterables.iterable(new ElectionRole(ClusterConfiguration.COORDINATOR)), new ClusterConfiguration(initialConfig.getName(), logging, initialConfig.getMemberURIs()), executor, logging, objectInputStreamFactory, objectOutputStreamFactory, acceptorInstanceStore, timeouts, electionCredentialsProvider, config);
SnapshotContext snapshotContext = new SnapshotContext(context.getClusterContext(), context.getLearnerContext());
return newProtocolServer(me, input, output, stateMachineExecutor, executor, timeouts, context, snapshotContext);
}
use of org.neo4j.cluster.protocol.election.ElectionRole in project neo4j by neo4j.
the class HeartbeatContextTest method setup.
@Before
public void setup() {
Map<InstanceId, URI> members = new HashMap<InstanceId, URI>();
for (int i = 0; i < instanceIds.length; i++) {
members.put(instanceIds[i], URI.create(initialHosts[i]));
}
ClusterConfiguration config = new ClusterConfiguration("clusterName", NullLogProvider.getInstance(), initialHosts);
config.setMembers(members);
context = mock(ClusterContext.class);
Config configuration = mock(Config.class);
when(configuration.get(ClusterSettings.max_acceptors)).thenReturn(10);
when(context.getConfiguration()).thenReturn(config);
when(context.getMyId()).thenReturn(instanceIds[0]);
MultiPaxosContext context = new MultiPaxosContext(instanceIds[0], Iterables.iterable(new ElectionRole("coordinator")), config, 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), configuration);
toTest = context.getHeartbeatContext();
}
use of org.neo4j.cluster.protocol.election.ElectionRole in project neo4j by neo4j.
the class HeartbeatStateTest method shouldLogFirstHeartbeatAfterTimeout.
@Test
public void shouldLogFirstHeartbeatAfterTimeout() throws Throwable {
// given
InstanceId instanceId = new InstanceId(1), otherInstance = new InstanceId(2);
ClusterConfiguration configuration = new ClusterConfiguration("whatever", NullLogProvider.getInstance(), "cluster://1", "cluster://2");
configuration.getMembers().put(otherInstance, URI.create("cluster://2"));
AssertableLogProvider internalLog = new AssertableLogProvider(true);
TimeoutStrategy timeoutStrategy = mock(TimeoutStrategy.class);
Timeouts timeouts = new Timeouts(timeoutStrategy);
Config config = mock(Config.class);
when(config.get(ClusterSettings.max_acceptors)).thenReturn(10);
MultiPaxosContext context = new MultiPaxosContext(instanceId, iterable(new ElectionRole("coordinator")), configuration, mock(Executor.class), internalLog, mock(ObjectInputStreamFactory.class), mock(ObjectOutputStreamFactory.class), mock(AcceptorInstanceStore.class), timeouts, mock(ElectionCredentialsProvider.class), config);
StateMachines stateMachines = new StateMachines(internalLog, mock(StateMachines.Monitor.class), mock(MessageSource.class), mock(MessageSender.class), timeouts, mock(DelayedDirectExecutor.class), command -> command.run(), instanceId);
stateMachines.addStateMachine(new StateMachine(context.getHeartbeatContext(), HeartbeatMessage.class, HeartbeatState.start, internalLog));
timeouts.tick(0);
when(timeoutStrategy.timeoutFor(any(Message.class))).thenReturn(5L);
// when
stateMachines.process(Message.internal(HeartbeatMessage.join));
stateMachines.process(Message.internal(HeartbeatMessage.i_am_alive, new HeartbeatMessage.IAmAliveState(otherInstance)).setHeader(Message.CREATED_BY, otherInstance.toString()));
for (int i = 1; i <= 15; i++) {
timeouts.tick(i);
}
// then
verify(timeoutStrategy, times(3)).timeoutTriggered(argThat(new MessageArgumentMatcher<>().onMessageType(HeartbeatMessage.timed_out)));
internalLog.assertExactly(inLog(HeartbeatState.class).debug("Received timed out for server 2"), inLog(HeartbeatContext.class).info("1(me) is now suspecting 2"), inLog(HeartbeatState.class).debug("Received timed out for server 2"), inLog(HeartbeatState.class).debug("Received timed out for server 2"));
internalLog.clear();
// when
stateMachines.process(Message.internal(HeartbeatMessage.i_am_alive, new HeartbeatMessage.IAmAliveState(otherInstance)).setHeader(Message.CREATED_BY, otherInstance.toString()));
// then
internalLog.assertExactly(inLog(HeartbeatState.class).debug("Received i_am_alive[2] after missing 3 (15ms)"));
}
use of org.neo4j.cluster.protocol.election.ElectionRole in project neo4j by neo4j.
the class HeartbeatStateTest method shouldIgnoreSuspicionsForOurselves.
@Test
public void shouldIgnoreSuspicionsForOurselves() 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"));
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(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);
HeartbeatContext heartbeatContext = context.getHeartbeatContext();
Message received = Message.internal(HeartbeatMessage.suspicions, new HeartbeatMessage.SuspicionsState(asSet(iterable(instanceId))));
received.setHeader(Message.FROM, "cluster://2").setHeader(Message.INSTANCE_ID, "2");
// When
heartbeat.handle(heartbeatContext, received, mock(MessageHolder.class));
// Then
assertThat(heartbeatContext.getSuspicionsOf(instanceId).size(), equalTo(0));
}
use of org.neo4j.cluster.protocol.election.ElectionRole in project neo4j by neo4j.
the class ClusterContextTest method testElectionVersionIsResetWhenElectorChangesFromMeToOther.
@Test
public void testElectionVersionIsResetWhenElectorChangesFromMeToOther() 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();
ElectionContext electionContext = multiPaxosContext.getElectionContext();
ClusterListener listener = mock(ClusterListener.class);
context.setLastElectorVersion(5);
context.setLastElector(me);
context.addClusterListener(listener);
long expectedVersion = electionContext.newConfigurationStateChange().getVersion();
context.elected(coordinatorRole, winner, me, expectedVersion);
verify(listener, times(1)).elected(coordinatorRole, winner, null);
context.elected(coordinatorRole, winner, elector, 2);
verify(listener, times(2)).elected(coordinatorRole, winner, null);
context.elected(coordinatorRole, winner, elector, 3);
verify(listener, times(3)).elected(coordinatorRole, winner, null);
context.elected(coordinatorRole, winner, elector, 2);
verifyNoMoreInteractions(listener);
}
Aggregations