From 9c361906541183d29c99da6bdc7871f17b4321bd Mon Sep 17 00:00:00 2001 From: zeroflag Date: Mon, 9 Oct 2023 12:21:20 +0200 Subject: [PATCH] KNOX-2962 - Knox readiness check gateway-status endpoint should return the list of topologies for which it is waiting for --- .../topology/impl/GatewayStatusService.java | 9 +++++++-- .../impl/GatewayStatusServiceTest.java | 18 ++++++++++++++++++ .../gateway/service/health/PingResource.java | 2 +- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/gateway-server/src/main/java/org/apache/knox/gateway/services/topology/impl/GatewayStatusService.java b/gateway-server/src/main/java/org/apache/knox/gateway/services/topology/impl/GatewayStatusService.java index 08ad866b29..1cc4472450 100644 --- a/gateway-server/src/main/java/org/apache/knox/gateway/services/topology/impl/GatewayStatusService.java +++ b/gateway-server/src/main/java/org/apache/knox/gateway/services/topology/impl/GatewayStatusService.java @@ -45,12 +45,17 @@ public synchronized boolean status() { LOG.noTopologiesToCheck(); return false; } - Set missing = new HashSet<>(topologyNamesToCheck); - missing.removeAll(deployedTopologies); + Set missing = pendingTopologies(); LOG.checkingGatewayStatus(deployedTopologies, missing); return missing.isEmpty(); } + public synchronized Set pendingTopologies() { + Set missing = new HashSet<>(topologyNamesToCheck); + missing.removeAll(deployedTopologies); + return missing; + } + /** * The list of topologies (which will be used to check the gateway status.) * are either coming from the config or collected automatically. diff --git a/gateway-server/src/test/java/org/apache/knox/gateway/services/topology/impl/GatewayStatusServiceTest.java b/gateway-server/src/test/java/org/apache/knox/gateway/services/topology/impl/GatewayStatusServiceTest.java index fe866a6606..8e121a1b92 100644 --- a/gateway-server/src/test/java/org/apache/knox/gateway/services/topology/impl/GatewayStatusServiceTest.java +++ b/gateway-server/src/test/java/org/apache/knox/gateway/services/topology/impl/GatewayStatusServiceTest.java @@ -17,10 +17,12 @@ */ package org.apache.knox.gateway.services.topology.impl; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import java.util.Arrays; +import java.util.Collections; import java.util.HashSet; import org.apache.knox.gateway.config.GatewayConfig; @@ -43,4 +45,20 @@ public void testReadyStatus() throws Exception { statusService.onTopologyReady("t2"); assertTrue(statusService.status()); } + + @Test + public void testPendingTopologies() throws Exception { + GatewayStatusService statusService = new GatewayStatusService(); + GatewayConfig config = EasyMock.createNiceMock(GatewayConfig.class); + statusService.init(config, null); + assertFalse(statusService.status()); + EasyMock.expect(config.getHealthCheckTopologies()).andReturn(new HashSet<>(Arrays.asList("t1", "t2"))).anyTimes(); + EasyMock.replay(config); + statusService.initTopologiesToCheck(); + assertEquals(new HashSet<>(Arrays.asList("t1", "t2")), statusService.pendingTopologies()); + statusService.onTopologyReady("t1"); + assertEquals(new HashSet<>(Arrays.asList("t2")), statusService.pendingTopologies()); + statusService.onTopologyReady("t2"); + assertEquals(Collections.emptySet(), statusService.pendingTopologies()); + } } \ No newline at end of file diff --git a/gateway-service-health/src/main/java/org/apache/knox/gateway/service/health/PingResource.java b/gateway-service-health/src/main/java/org/apache/knox/gateway/service/health/PingResource.java index 2d4f4e7203..0a58aeaa55 100644 --- a/gateway-service-health/src/main/java/org/apache/knox/gateway/service/health/PingResource.java +++ b/gateway-service-health/src/main/java/org/apache/knox/gateway/service/health/PingResource.java @@ -101,7 +101,7 @@ public Response status() { .getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE); GatewayStatusService statusService = services.getService(ServiceType.GATEWAY_STATUS_SERVICE); try (PrintWriter writer = response.getWriter()) { - writer.println(statusService.status() ? OK : PENDING); + writer.println(statusService.status() ? OK : PENDING + ": " + statusService.pendingTopologies()); } catch (IOException e) { log.logException("status", e); return Response.serverError().entity(String.format(Locale.ROOT, "Failed to reply correctly due to : %s ", e)).build();