use of org.neo4j.cluster.protocol.cluster.ClusterConfiguration 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.cluster.ClusterConfiguration in project neo4j by neo4j.
the class AtomicBroadcastContextImplTest method shouldHasQuorumWhenTwoMachinesAliveInAClusterWithThreeMachines.
@Test
public void shouldHasQuorumWhenTwoMachinesAliveInAClusterWithThreeMachines() {
//Given
HeartbeatContext heartbeatContext = mock(HeartbeatContext.class);
CommonContextState commonState = mock(CommonContextState.class);
ClusterConfiguration configuration = mock(ClusterConfiguration.class);
when(heartbeatContext.getAlive()).thenReturn(ids(2));
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
assertTrue(hasQuorum);
}
use of org.neo4j.cluster.protocol.cluster.ClusterConfiguration in project neo4j by neo4j.
the class ClusterContextImplTest method electorLeavingTheClusterMustBeRemovedAsElector.
/*
* This test ensures that an instance that cleanly leaves the cluster is no longer assumed to be an elector. This
* has the effect that when it rejoins its elector version will be reset and its results will go through
*/
@Test
public void electorLeavingTheClusterMustBeRemovedAsElector() throws Throwable {
// Given
InstanceId me = new InstanceId(1);
InstanceId elector = new InstanceId(2);
ClusterConfiguration clusterConfiguration = mock(ClusterConfiguration.class);
when(clusterConfiguration.getUriForId(elector)).thenReturn(URI.create("cluster://instance2"));
CommonContextState commonContextState = mock(CommonContextState.class);
when(commonContextState.configuration()).thenReturn(clusterConfiguration);
ClusterContext context = new ClusterContextImpl(me, commonContextState, NullLogProvider.getInstance(), mock(Timeouts.class), mock(Executor.class), mock(ObjectOutputStreamFactory.class), mock(ObjectInputStreamFactory.class), mock(LearnerContext.class), mock(HeartbeatContext.class), mock(Config.class));
// This means instance 2 was the elector at version 8
context.setLastElector(elector);
context.setLastElectorVersion(8);
// When
context.left(elector);
// Then
assertEquals(context.getLastElector(), InstanceId.NONE);
assertEquals(context.getLastElectorVersion(), -1);
}
use of org.neo4j.cluster.protocol.cluster.ClusterConfiguration in project neo4j by neo4j.
the class HeartbeatContextImplTest method majorityOfNonSuspectedInstancesShouldBeEnoughToMarkAnInstanceAsFailed.
@Test
public void majorityOfNonSuspectedInstancesShouldBeEnoughToMarkAnInstanceAsFailed() throws Exception {
// Given
InstanceId me = new InstanceId(1);
InstanceId member2 = new InstanceId(2);
InstanceId member3 = new InstanceId(3);
InstanceId member4 = new InstanceId(4);
InstanceId member5 = new InstanceId(5);
Timeouts timeouts = mock(Timeouts.class);
CommonContextState commonState = mock(CommonContextState.class);
ClusterConfiguration configuration = mock(ClusterConfiguration.class);
when(commonState.configuration()).thenReturn(configuration);
when(configuration.getMembers()).thenReturn(members(5));
when(configuration.getMemberIds()).thenReturn(ids(5));
DelayedDirectExecutor executor = new DelayedDirectExecutor(NullLogProvider.getInstance());
HeartbeatContext context = new HeartbeatContextImpl(me, commonState, NullLogProvider.getInstance(), timeouts, executor);
final List<InstanceId> failed = new ArrayList<>(4);
HeartbeatListener listener = new HeartbeatListener() {
@Override
public void failed(InstanceId server) {
failed.add(server);
}
@Override
public void alive(InstanceId server) {
failed.remove(server);
}
};
context.addHeartbeatListener(listener);
// when
// just two suspicions come, no extra failing action should be taken since this is not majority
context.suspect(member2);
context.suspect(member3);
executor.drain();
// then
assertEquals(0, failed.size());
// when
// the another instance suspects them, therefore have a majority of non suspected, then 2 and 3 must fail
Set<InstanceId> suspicionsFrom5 = new HashSet<>();
suspicionsFrom5.add(member2);
suspicionsFrom5.add(member3);
context.suspicions(member5, suspicionsFrom5);
executor.drain();
// then
assertEquals(2, failed.size());
assertTrue(failed.contains(member2));
assertTrue(failed.contains(member3));
// when
// an instance sends a heartbeat, it should be set as alive
context.alive(member2);
executor.drain();
// then
assertEquals(1, failed.size());
assertTrue(failed.contains(member3));
}
use of org.neo4j.cluster.protocol.cluster.ClusterConfiguration in project neo4j by neo4j.
the class HeartbeatContextImplTest method shouldFailAllInstancesIfAllOtherInstancesAreSuspected.
@Test
public void shouldFailAllInstancesIfAllOtherInstancesAreSuspected() throws Exception {
// Given
InstanceId me = new InstanceId(1);
InstanceId member2 = new InstanceId(2);
InstanceId member3 = new InstanceId(3);
Timeouts timeouts = mock(Timeouts.class);
CommonContextState commonState = mock(CommonContextState.class);
ClusterConfiguration configuration = mock(ClusterConfiguration.class);
when(commonState.configuration()).thenReturn(configuration);
when(configuration.getMembers()).thenReturn(members(3));
when(configuration.getMemberIds()).thenReturn(ids(3));
DelayedDirectExecutor executor = new DelayedDirectExecutor(NullLogProvider.getInstance());
HeartbeatContext context = new HeartbeatContextImpl(me, commonState, NullLogProvider.getInstance(), timeouts, executor);
List<InstanceId> failed = new ArrayList<>(2);
HeartbeatListener listener = new HeartbeatListener() {
@Override
public void failed(InstanceId server) {
failed.add(server);
}
@Override
public void alive(InstanceId server) {
failed.remove(server);
}
};
context.addHeartbeatListener(listener);
// when
// just one suspicion comes, no extra failing action should be taken
context.suspect(member2);
executor.drain();
// then
assertEquals(0, failed.size());
// when
// the other instance is suspected, all instances must be marked as failed
context.suspect(member3);
executor.drain();
// then
assertEquals(2, failed.size());
assertTrue(failed.contains(member2));
assertTrue(failed.contains(member3));
// when
// one of them comes alive again, only that instance should be marked as alive
context.alive(member2);
executor.drain();
// then
assertEquals(1, failed.size());
assertTrue(failed.contains(member3));
}
Aggregations