@Override public ClusterHealth checkCluster() { checkState(!webServer.isStandalone(), "Clustering is not enabled"); checkState(sharedHealthState != null, "HealthState instance can't be null when clustering is enabled"); Set<NodeHealth> nodeHealths = sharedHealthState.readAll(); Health health = clusterHealthChecks.stream() .map(clusterHealthCheck -> clusterHealthCheck.check(nodeHealths)) .reduce(Health.GREEN, HealthReducer.INSTANCE); return new ClusterHealth(health, nodeHealths); }
public void stop() { sharedHealthState.clearMine(); } }
private void refresh() { try { NodeHealth nodeHealth = nodeHealthProvider.get(); sharedHealthState.writeMine(nodeHealth); } catch (HazelcastInstanceNotActiveException | RetryableHazelcastException e) { LOG.debug("Hazelcast is no more active", e); } catch (Throwable t) { LOG.error("An error occurred while attempting to refresh HealthState of the current node in the shared state:", t); } }
@Test public void checkCluster_returns_NodeHealths_returned_by_HealthState() { when(webServer.isStandalone()).thenReturn(false); Set<NodeHealth> nodeHealths = IntStream.range(0, 1 + random.nextInt(4)).mapToObj(i -> randomNodeHealth()).collect(Collectors.toSet()); when(sharedHealthState.readAll()).thenReturn(nodeHealths); HealthCheckerImpl underTest = new HealthCheckerImpl(webServer, new NodeHealthCheck[0], new ClusterHealthCheck[0], sharedHealthState); ClusterHealth clusterHealth = underTest.checkCluster(); assertThat(clusterHealth.getNodes()).isEqualTo(nodeHealths); }
@Test public void do_not_log_errors_when_hazelcast_is_not_active() { logging.setLevel(DEBUG); doThrow(new HazelcastInstanceNotActiveException()).when(sharedHealthState).writeMine(any()); ArgumentCaptor<Runnable> runnableCaptor = ArgumentCaptor.forClass(Runnable.class); underTest.start(); verify(executorService).scheduleWithFixedDelay(runnableCaptor.capture(), eq(1L), eq(10L), eq(TimeUnit.SECONDS)); Runnable runnable = runnableCaptor.getValue(); runnable.run(); assertThat(logging.getLogs(ERROR)).isEmpty(); assertThat(logging.hasLog(DEBUG, "Hazelcast is no more active")).isTrue(); } }
@Test public void stop_has_no_effect() { underTest.stop(); verify(sharedHealthState).clearMine(); verifyZeroInteractions(executorService, nodeHealthProvider); }
@Test public void checkCluster_passes_set_of_NodeHealth_returns_by_HealthState_to_all_ClusterHealthChecks() { when(webServer.isStandalone()).thenReturn(false); ClusterHealthCheck[] mockedClusterHealthChecks = IntStream.range(0, 1 + random.nextInt(3)) .mapToObj(i -> mock(ClusterHealthCheck.class)) .toArray(ClusterHealthCheck[]::new); Set<NodeHealth> nodeHealths = IntStream.range(0, 1 + random.nextInt(4)).mapToObj(i -> randomNodeHealth()).collect(Collectors.toSet()); when(sharedHealthState.readAll()).thenReturn(nodeHealths); for (ClusterHealthCheck mockedClusterHealthCheck : mockedClusterHealthChecks) { when(mockedClusterHealthCheck.check(same(nodeHealths))).thenReturn(Health.GREEN); } HealthCheckerImpl underTest = new HealthCheckerImpl(webServer, new NodeHealthCheck[0], mockedClusterHealthChecks, sharedHealthState); underTest.checkCluster(); for (ClusterHealthCheck mockedClusterHealthCheck : mockedClusterHealthChecks) { verify(mockedClusterHealthCheck).check(same(nodeHealths)); } }
@Test public void start_adds_runnable_with_10_second_delay_and_initial_delay_putting_NodeHealth_from_provider_into_SharedHealthState() { ArgumentCaptor<Runnable> runnableCaptor = ArgumentCaptor.forClass(Runnable.class); NodeHealth[] nodeHealths = { testSupport.randomNodeHealth(), testSupport.randomNodeHealth(), testSupport.randomNodeHealth() }; Error expected = new Error("Simulating exception raised by NodeHealthProvider"); when(nodeHealthProvider.get()) .thenReturn(nodeHealths[0]) .thenReturn(nodeHealths[1]) .thenReturn(nodeHealths[2]) .thenThrow(expected); underTest.start(); verify(executorService).scheduleWithFixedDelay(runnableCaptor.capture(), eq(1L), eq(10L), eq(TimeUnit.SECONDS)); Runnable runnable = runnableCaptor.getValue(); runnable.run(); runnable.run(); runnable.run(); verify(sharedHealthState).writeMine(nodeHealths[0]); verify(sharedHealthState).writeMine(nodeHealths[1]); verify(sharedHealthState).writeMine(nodeHealths[2]); try { runnable.run(); } catch (IllegalStateException e) { fail("Runnable should catch any Throwable"); } }
@Override public ClusterHealth checkCluster() { checkState(!webServer.isStandalone(), "Clustering is not enabled"); checkState(sharedHealthState != null, "HealthState instance can't be null when clustering is enabled"); Set<NodeHealth> nodeHealths = sharedHealthState.readAll(); Health health = clusterHealthChecks.stream() .map(clusterHealthCheck -> clusterHealthCheck.check(nodeHealths)) .reduce(Health.GREEN, HealthReducer.INSTANCE); return new ClusterHealth(health, nodeHealths); }