use of org.neo4j.cluster.protocol.heartbeat.HeartbeatListener in project neo4j by neo4j.
the class ObservedClusterMembersTest method failedThenAliveMemberShowsAsAlive.
@Test
public void failedThenAliveMemberShowsAsAlive() throws Exception {
// given
Cluster cluster = mock(Cluster.class);
Heartbeat heartbeat = mock(Heartbeat.class);
ClusterMemberEvents memberEvents = mock(ClusterMemberEvents.class);
ObservedClusterMembers members = new ObservedClusterMembers(logProvider, cluster, heartbeat, memberEvents, null);
ArgumentCaptor<ClusterListener> listener = ArgumentCaptor.forClass(ClusterListener.class);
verify(cluster).addClusterListener(listener.capture());
listener.getValue().enteredCluster(clusterConfiguration(clusterUri1, clusterUri2, clusterUri3));
ArgumentCaptor<HeartbeatListener> heartBeatListener = ArgumentCaptor.forClass(HeartbeatListener.class);
verify(heartbeat).addHeartbeatListener(heartBeatListener.capture());
// when
heartBeatListener.getValue().failed(clusterId1);
heartBeatListener.getValue().alive(clusterId1);
// then
assertThat(members.getMembers(), hasItem(sameMemberAs(new ClusterMember(clusterId1))));
}
use of org.neo4j.cluster.protocol.heartbeat.HeartbeatListener in project neo4j by neo4j.
the class ObservedClusterMembersTest method failedMemberShowsAsSuch.
@Test
public void failedMemberShowsAsSuch() throws Exception {
// given
Cluster cluster = mock(Cluster.class);
Heartbeat heartbeat = mock(Heartbeat.class);
ClusterMemberEvents memberEvents = mock(ClusterMemberEvents.class);
ObservedClusterMembers members = new ObservedClusterMembers(logProvider, cluster, heartbeat, memberEvents, null);
ArgumentCaptor<ClusterListener> listener = ArgumentCaptor.forClass(ClusterListener.class);
verify(cluster).addClusterListener(listener.capture());
listener.getValue().enteredCluster(clusterConfiguration(clusterUri1, clusterUri2, clusterUri3));
ArgumentCaptor<HeartbeatListener> heartBeatListener = ArgumentCaptor.forClass(HeartbeatListener.class);
verify(heartbeat).addHeartbeatListener(heartBeatListener.capture());
// when
heartBeatListener.getValue().failed(clusterId1);
// then
assertThat(members.getMembers(), hasItem(sameMemberAs(new ClusterMember(clusterId1).failed())));
}
use of org.neo4j.cluster.protocol.heartbeat.HeartbeatListener in project neo4j by neo4j.
the class ClusterMockTest method testCluster.
protected void testCluster(int[] serverIds, VerifyInstanceConfiguration[] finalConfig, NetworkMock mock, ClusterTestScript script) throws ExecutionException, InterruptedException, URISyntaxException, TimeoutException {
this.script = script;
network = mock;
servers.clear();
out.clear();
in.clear();
for (int i = 0; i < serverIds.length; i++) {
final URI uri = new URI("server" + (i + 1));
members.put(serverIds[i], uri);
TestProtocolServer server = network.addServer(serverIds[i], uri);
final Cluster cluster = server.newClient(Cluster.class);
clusterStateListener(uri, cluster);
server.newClient(Heartbeat.class).addHeartbeatListener(new HeartbeatListener() {
@Override
public void failed(InstanceId server) {
logger.getLogger().warning(uri + ": Failed:" + server);
}
@Override
public void alive(InstanceId server) {
logger.getLogger().fine(uri + ": Alive:" + server);
}
});
server.newClient(AtomicBroadcast.class).addAtomicBroadcastListener(new AtomicBroadcastListener() {
AtomicBroadcastSerializer serializer = new AtomicBroadcastSerializer(new ObjectStreamFactory(), new ObjectStreamFactory());
@Override
public void receive(Payload value) {
try {
logger.getLogger().fine(uri + " received: " + serializer.receive(value));
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
});
servers.add(server);
out.add(cluster);
}
// Run test
for (int i = 0; i < script.rounds(); i++) {
logger.getLogger().fine("Round " + i + ", time:" + network.getTime());
script.tick(network.getTime());
network.tick();
}
// Let messages settle
network.tick(100);
if (finalConfig == null) {
verifyConfigurations("final config");
} else {
verifyConfigurations(finalConfig);
}
logger.getLogger().fine("All nodes leave");
// All leave
for (Cluster cluster : new ArrayList<Cluster>(in)) {
logger.getLogger().fine("Leaving:" + cluster);
cluster.leave();
in.remove(cluster);
network.tick(400);
}
if (finalConfig != null) {
verifyConfigurations(finalConfig);
} else {
verifyConfigurations("test over");
}
}
use of org.neo4j.cluster.protocol.heartbeat.HeartbeatListener in project neo4j by neo4j.
the class ClusterContextImplTest method electorFailingMustCauseElectorVersionToBeReset.
/*
* This test ensures that an instance that is marked as failed has its elector version reset. This means that
* the instance, once it comes back, will still be able to do elections even if it lost state
*/
@Test
public void electorFailingMustCauseElectorVersionToBeReset() throws Exception {
// Given
InstanceId me = new InstanceId(1);
InstanceId elector = new InstanceId(2);
CommonContextState commonContextState = mock(CommonContextState.class, RETURNS_MOCKS);
Timeouts timeouts = mock(Timeouts.class);
Executor executor = mock(Executor.class);
HeartbeatContext heartbeatContext = mock(HeartbeatContext.class);
ArgumentCaptor<HeartbeatListener> listenerCaptor = ArgumentCaptor.forClass(HeartbeatListener.class);
ClusterContext context = new ClusterContextImpl(me, commonContextState, NullLogProvider.getInstance(), timeouts, executor, mock(ObjectOutputStreamFactory.class), mock(ObjectInputStreamFactory.class), mock(LearnerContext.class), heartbeatContext, mock(Config.class));
verify(heartbeatContext).addHeartbeatListener(listenerCaptor.capture());
HeartbeatListener theListener = listenerCaptor.getValue();
// This means instance 2 was the elector at version 8
context.setLastElector(elector);
context.setLastElectorVersion(8);
// When
theListener.failed(elector);
// Then
assertEquals(context.getLastElector(), InstanceId.NONE);
assertEquals(context.getLastElectorVersion(), ClusterContextImpl.NO_ELECTOR_VERSION);
}
use of org.neo4j.cluster.protocol.heartbeat.HeartbeatListener 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));
}
Aggregations