LeaderPingHealthCheck { public HealthCheckDigest getStatus() { Set<Client> namespacesToCheck = namespaceTracker.trackedNamespaces(); PaxosResponses<PaxosContainer<Map<Client, HealthCheckResponse>>> responses = PaxosQuorumChecker.collectAsManyResponsesAsPossible( ImmutableList.copyOf(pingers), pinger -> PaxosContainer.of(pinger.apply(namespacesToCheck)), executorService, HEALTH_CHECK_TIME_LIMIT, PaxosConstants.CANCEL_REMAINING_CALLS); Map<Client, PaxosResponses<HealthCheckResponse>> responsesByClient = responses.stream() .map(PaxosContainer::get) .map(Map::entrySet) .flatMap(Collection::stream) .collect(Collectors.groupingBy( Map.Entry::getKey, Collectors.mapping(Map.Entry::getValue, Collectors.collectingAndThen(Collectors.toList(), r -> PaxosResponses.of(getQuorumSize(), r))))); SetMultimap<TimeLockStatus, Client> statusesToClient = KeyedStream.stream(responsesByClient) .map(this::convertQuorumResponsesToStatus) .mapEntries((client, status) -> Maps.immutableEntry(status, client)) .collectToSetMultimap(); return ImmutableHealthCheckDigest.of(statusesToClient); } LeaderPingHealthCheck(
            NamespaceTracker namespaceTracker,
            List<HealthCheckPinger> pingers); HealthCheckDigest getStatus();  }
@Test public void shouldBeUnhealthyIfQuorumNodesAreNotUpAndNoNodePingedSuccessfully() { HealthCheckPinger leader1 = getMockOfPingableLeaderWherePingReturns(ImmutableSet.of()); HealthCheckPinger leader2 = getMockOfPingableLeaderWherePingThrows(); HealthCheckPinger leader3 = getMockOfPingableLeaderWherePingThrows(); List<HealthCheckPinger> leaders = ImmutableList.of(leader1, leader2, leader3); SetMultimap<TimeLockStatus, Client> expected = ImmutableSetMultimap.<TimeLockStatus, Client>builder() .putAll(TimeLockStatus.NO_QUORUM, ALL_CLIENTS) .build(); assertThat(new LeaderPingHealthCheck(TRACKER, leaders).getStatus().statusesToClient()) .isEqualTo(expected); }
@Test public void canHandleIndividualClientsSeperately() { List<HealthCheckPinger> leaders = getHealthCheckPingers( ImmutableSet.of(CLIENT1), ImmutableSet.of(CLIENT3), ImmutableSet.of(CLIENT3)); SetMultimap<TimeLockStatus, Client> expected = ImmutableSetMultimap.<TimeLockStatus, Client>builder() .put(TimeLockStatus.ONE_LEADER, CLIENT1) .put(TimeLockStatus.NO_LEADER, CLIENT2) .put(TimeLockStatus.MULTIPLE_LEADERS, CLIENT3) .build(); assertThat(new LeaderPingHealthCheck(TRACKER, leaders).getStatus().statusesToClient()) .isEqualTo(expected); }
@Test public void shouldBeUnhealthyIfAllNodesPingedSuccessfully() { List<HealthCheckPinger> leaders = getHealthCheckPingers(ALL_CLIENTS, ALL_CLIENTS, ALL_CLIENTS); SetMultimap<TimeLockStatus, Client> expected = ImmutableSetMultimap.<TimeLockStatus, Client>builder() .putAll(TimeLockStatus.MULTIPLE_LEADERS, ALL_CLIENTS) .build(); assertThat(new LeaderPingHealthCheck(TRACKER, leaders).getStatus().statusesToClient()) .isEqualTo(expected); }
@Test public void shouldBeUnhealthyIfMultipleNodesPingedSuccessfully() { List<HealthCheckPinger> leaders = getHealthCheckPingers(ALL_CLIENTS, ALL_CLIENTS, ImmutableSet.of()); SetMultimap<TimeLockStatus, Client> expected = ImmutableSetMultimap.<TimeLockStatus, Client>builder() .putAll(TimeLockStatus.MULTIPLE_LEADERS, ALL_CLIENTS) .build(); assertThat(new LeaderPingHealthCheck(TRACKER, leaders).getStatus().statusesToClient()) .isEqualTo(expected); }
@Test public void shouldBeHealthyIfExactlyOneNodePingedSuccessfully() { List<HealthCheckPinger> leaders = getHealthCheckPingers(ALL_CLIENTS, ImmutableSet.of(), ImmutableSet.of()); SetMultimap<TimeLockStatus, Client> expected = ImmutableSetMultimap.<TimeLockStatus, Client>builder() .putAll(TimeLockStatus.ONE_LEADER, ALL_CLIENTS) .build(); assertThat(new LeaderPingHealthCheck(TRACKER, leaders).getStatus().statusesToClient()) .isEqualTo(expected); }
@Test public void shouldBeUnhealthyIfNoNodesPingedSuccessfully() { List<HealthCheckPinger> leaders = getHealthCheckPingers(ImmutableSet.of(), ImmutableSet.of(), ImmutableSet.of()); SetMultimap<TimeLockStatus, Client> expected = ImmutableSetMultimap.<TimeLockStatus, Client>builder() .putAll(TimeLockStatus.NO_LEADER, ALL_CLIENTS) .build(); assertThat(new LeaderPingHealthCheck(TRACKER, leaders).getStatus().statusesToClient()) .isEqualTo(expected); }
@Test public void shouldBeHealthyIfQuorumNodesUpAndOnePingedSuccessfully() { HealthCheckPinger leader1 = getMockOfPingableLeaderWherePingReturns(ALL_CLIENTS); HealthCheckPinger leader2 = getMockOfPingableLeaderWherePingReturns(ImmutableSet.of()); HealthCheckPinger leader3 = getMockOfPingableLeaderWherePingThrows(); List<HealthCheckPinger> leaders = ImmutableList.of(leader1, leader2, leader3); SetMultimap<TimeLockStatus, Client> expected = ImmutableSetMultimap.<TimeLockStatus, Client>builder() .putAll(TimeLockStatus.ONE_LEADER, ALL_CLIENTS) .build(); assertThat(new LeaderPingHealthCheck(TRACKER, leaders).getStatus().statusesToClient()) .isEqualTo(expected); }
@Test public void shouldBeUnhealthyIfQuorumNodesAreUpAndNoNodePingedSuccessfully() { HealthCheckPinger leader1 = getMockOfPingableLeaderWherePingReturns(ImmutableSet.of()); HealthCheckPinger leader2 = getMockOfPingableLeaderWherePingReturns(ImmutableSet.of()); HealthCheckPinger leader3 = getMockOfPingableLeaderWherePingThrows(); List<HealthCheckPinger> leaders = ImmutableList.of(leader1, leader2, leader3); SetMultimap<TimeLockStatus, Client> expected = ImmutableSetMultimap.<TimeLockStatus, Client>builder() .putAll(TimeLockStatus.NO_LEADER, ALL_CLIENTS) .build(); assertThat(new LeaderPingHealthCheck(TRACKER, leaders).getStatus().statusesToClient()) .isEqualTo(expected); }
@Test public void shouldBeUnhealthyIfQuorumNodesAreNotUpAndOnePingedSuccessfully() { HealthCheckPinger leader1 = getMockOfPingableLeaderWherePingReturns(ALL_CLIENTS); HealthCheckPinger leader2 = getMockOfPingableLeaderWherePingThrows(); HealthCheckPinger leader3 = getMockOfPingableLeaderWherePingThrows(); List<HealthCheckPinger> leaders = ImmutableList.of(leader1, leader2, leader3); SetMultimap<TimeLockStatus, Client> expected = ImmutableSetMultimap.<TimeLockStatus, Client>builder() .putAll(TimeLockStatus.NO_QUORUM, ALL_CLIENTS) .build(); assertThat(new LeaderPingHealthCheck(TRACKER, leaders).getStatus().statusesToClient()) .isEqualTo(expected); }