StatusFeClient { public SystemStatus getSystemStatus() throws InternalException { logger.info("Getting system status"); try { Operation result = xenonClient.get(ServiceUriPaths.DEFAULT_NODE_GROUP); Collection<NodeState> nodes = result.getBody(NodeGroupService.NodeGroupState.class).nodes.values(); List<InetSocketAddress> servers = nodes.stream() .map(item -> new InetSocketAddress(item.groupReference.getHost(), item.groupReference.getPort())) .collect(Collectors.toList()); logger.info("Get all nodes in default node group: {}", servers); StaticServerSet serverSet = new StaticServerSet(servers.toArray(new InetSocketAddress[servers.size()])); statusProviderFactories.get(Component.PHOTON_CONTROLLER).setServerSet(serverSet); } catch (DocumentNotFoundException ex) { logger.error("Couldn't find nodegroup document.", ex); throw new InternalException(ex); } SystemStatus systemStatus = new SystemStatus(); List<Callable<Status>> componentStatuses = new ArrayList<>(); for (Component component : components) { ComponentStatus componentStatus = new ComponentStatusBuilder().component(component).build(); Set<InetSocketAddress> servers = statusProviderFactories.get(component).getServerSet().getServers(); if (servers.isEmpty()) { componentStatus.setStatus(StatusType.UNREACHABLE); componentStatus.setMessage("Empty ServerSet"); } else { for (InetSocketAddress server : servers) { StatusProvider client = statusProviderFactories.get(component).create(server); ComponentInstance instance = new ComponentInstanceBuilder() .status(StatusType.UNREACHABLE).address(server.getHostString()).build(); componentStatus.addInstance(instance); Callable<Status> callable = () -> { try { Status status = client.getStatus(); instance.setStats(status.getStats()); instance.setStatus(status.getType()); instance.setMessage(status.getMessage()); instance.setBuildInfo(status.getBuild_info()); return status; } catch (Exception e) { logger.error("client.getStatus() call failed with Exception: %s", e); throw e; } }; componentStatuses.add(callable); } } systemStatus.getComponents().add(componentStatus); } try { executor.invokeAll(componentStatuses, GET_STATUS_TIMEOUT_SECONDS, TimeUnit.SECONDS); } catch (InterruptedException ex) { logger.error("InterruptedException when calling get_status in parallel", ex); throw new InternalException(ex); } computeSingleComponentStatus(systemStatus); StatusType overall = systemStatus.getComponents().stream() .map(c -> c.getStatus()) .max(Comparator.comparing(statusType -> StatusFeClientUtils.STATUS_MAP.get(statusType))) .get(); if (overall == StatusType.UNREACHABLE) { overall = StatusType.ERROR; } systemStatus.setStatus(overall); logger.info("Returning system status {}", systemStatus); return systemStatus; } @Inject  StatusFeClient(
      @BackendTaskExecutor ExecutorService executor,
      @ScheduledTaskExecutor ScheduledExecutorService scheduledExecutorService,
      StatusConfig statusConfig,
      ServiceHost serviceHost,
      ApiFeXenonRestClient xenonClient); SystemStatus getSystemStatus();  }
@Test public void testAllComponents() throws Throwable { Status readyStatus = new Status(StatusType.READY); setMessageAndStats(readyStatus); mockAllClientsToReturnSameStatus(readyStatus); SystemStatus systemStatus = client.getSystemStatus(); assertThat(systemStatus.getComponents().size(), is(statusConfig.getComponents().size())); assertThat(systemStatus.getStatus(), is(StatusType.READY)); }
@Test public void testDefaultComponents() throws Throwable { statusConfig.setComponents(ImmutableList.<String>of()); Status readyStatus = new Status(StatusType.READY); setMessageAndStats(readyStatus); mockAllClientsToReturnSameStatus(readyStatus); SystemStatus systemStatus = client.getSystemStatus(); assertThat(systemStatus.getStatus(), is(StatusType.READY)); assertThat(systemStatus.getComponents().size(), is(statusConfig.getComponents().size())); }
@Test public void testPartialErrorScenarioInSystemStatus() throws Throwable { Status readyStatus = new Status(StatusType.READY); setMessageAndStats(readyStatus); Status initializingStatus = new Status(StatusType.INITIALIZING); setMessageAndStats(initializingStatus); Status errorStatus = new Status(StatusType.ERROR); setMessageAndStats(errorStatus); mockAllClientsToReturnSameStatus(readyStatus); when(photonControllerClients.get(0).getStatus()).thenReturn(readyStatus); when(photonControllerClients.get(1).getStatus()).thenReturn(initializingStatus); when(photonControllerClients.get(2).getStatus()).thenReturn(errorStatus); SystemStatus systemStatus = client.getSystemStatus(); assertThat(systemStatus.getComponents().size(), is(statusConfig.getComponents().size())); assertThat(systemStatus.getStatus(), is(StatusType.PARTIAL_ERROR)); List<ComponentStatus> compStatuses = systemStatus.getComponents(); for (ComponentStatus status : compStatuses) { if (status.getComponent().equals(Component.PHOTON_CONTROLLER)) { assertThat(status.getStatus(), is(StatusType.PARTIAL_ERROR)); assertThat(status.getStats().get(StatusType.READY.toString()), is("1")); assertThat(status.getStats().get(StatusType.INITIALIZING.toString()), is("1")); assertThat(status.getStats().get(StatusType.ERROR.toString()), is("1")); } else { assertThat(status.getStatus(), is(StatusType.READY)); assertThat(status.getStats().get(StatusType.READY.toString()), is("3")); } } }
@Test public void testAllInstancesInErrorState() throws Throwable { Status readyStatus = new Status(StatusType.READY); setMessageAndStats(readyStatus); Status initializingStatus = new Status(StatusType.INITIALIZING); setMessageAndStats(initializingStatus); Status errorStatus = new Status(StatusType.ERROR); setMessageAndStats(errorStatus); mockAllClientsToReturnSameStatus(errorStatus); SystemStatus systemStatus = client.getSystemStatus(); assertThat(systemStatus.getComponents().size(), is(statusConfig.getComponents().size())); assertThat(systemStatus.getStatus(), is(StatusType.ERROR)); List<ComponentStatus> compStatuses = systemStatus.getComponents(); for (ComponentStatus status : compStatuses) { assertThat(status.getStatus(), is(StatusType.ERROR)); assertThat(status.getStats().get(StatusType.ERROR.toString()), is("3")); } }