From ad747ee01a19155afd4a2900abc7bbd18ede5c18 Mon Sep 17 00:00:00 2001 From: Tom Page Date: Fri, 2 Feb 2024 10:02:07 +0000 Subject: [PATCH] ACS-6578 Liveness probe should fail if transformer requests to die. --- .../transform/base/probes/ProbeTransform.java | 6 +- .../base/TransformControllerTest.java | 122 ++++++++++-------- 2 files changed, 74 insertions(+), 54 deletions(-) diff --git a/engines/base/src/main/java/org/alfresco/transform/base/probes/ProbeTransform.java b/engines/base/src/main/java/org/alfresco/transform/base/probes/ProbeTransform.java index 254f4ba2c..28ca153a3 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/probes/ProbeTransform.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/probes/ProbeTransform.java @@ -169,6 +169,9 @@ private long getPositiveLongEnv(String name, long defaultValue) // We don't want to be doing test transforms every few seconds, but do want frequent live probes. public String doTransformOrNothing(boolean isLiveProbe, TransformHandler transformHandler) { + // If we already decided to die then don't bother with any other checks. + checkMaxTransformTimeAndCount(isLiveProbe); + // If not initialised OR it is a live probe and we are scheduled to to do a test transform. probeCount++; // TODO: update/fix/refactor liveness probes as part of ATS-138 @@ -182,7 +185,8 @@ public String doTransformOrNothing(boolean isLiveProbe, TransformHandler transfo || !initialised.get() ? doTransform(isLiveProbe, transformHandler) : doNothing(isLiveProbe); - + + // Check if the test transformation was too slow. checkMaxTransformTimeAndCount(isLiveProbe); return result; } diff --git a/engines/base/src/test/java/org/alfresco/transform/base/TransformControllerTest.java b/engines/base/src/test/java/org/alfresco/transform/base/TransformControllerTest.java index bde11f48b..b1ae9e434 100644 --- a/engines/base/src/test/java/org/alfresco/transform/base/TransformControllerTest.java +++ b/engines/base/src/test/java/org/alfresco/transform/base/TransformControllerTest.java @@ -26,6 +26,51 @@ */ package org.alfresco.transform.base; +import static org.hamcrest.Matchers.containsString; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.springframework.http.HttpHeaders.ACCEPT; +import static org.springframework.http.HttpHeaders.CONTENT_DISPOSITION; +import static org.springframework.http.HttpHeaders.CONTENT_TYPE; +import static org.springframework.http.HttpStatus.CREATED; +import static org.springframework.http.HttpStatus.TOO_MANY_REQUESTS; +import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import static org.alfresco.transform.base.AbstractBaseTest.getTestFile; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_BMP; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_PDF; +import static org.alfresco.transform.common.Mimetype.MIMETYPE_TEXT_PLAIN; +import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_ERROR; +import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_LIVE; +import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_LOG; +import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_READY; +import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_ROOT; +import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TEST; +import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM; +import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM_CONFIG; +import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM_CONFIG_LATEST; +import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_VERSION; +import static org.alfresco.transform.common.RequestParamMap.PAGE_REQUEST_PARAM; +import static org.alfresco.transform.common.RequestParamMap.SOURCE_ENCODING; +import static org.alfresco.transform.common.RequestParamMap.SOURCE_MIMETYPE; +import static org.alfresco.transform.common.RequestParamMap.TARGET_MIMETYPE; + +import java.io.File; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.Map; +import java.util.StringJoiner; +import java.util.UUID; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + import ch.qos.logback.classic.Level; import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.LoggerContext; @@ -33,16 +78,6 @@ import ch.qos.logback.core.AppenderBase; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.collect.ImmutableMap; -import org.alfresco.transform.base.fakes.FakeTransformEngineWithTwoCustomTransformers; -import org.alfresco.transform.base.fakes.FakeTransformerPdf2Png; -import org.alfresco.transform.base.fakes.FakeTransformerTxT2Pdf; -import org.alfresco.transform.base.model.FileRefEntity; -import org.alfresco.transform.base.model.FileRefResponse; -import org.alfresco.transform.base.sfs.SharedFileStoreClient; -import org.alfresco.transform.base.transform.TransformHandler; -import org.alfresco.transform.client.model.TransformReply; -import org.alfresco.transform.client.model.TransformRequest; -import org.alfresco.transform.config.TransformConfig; import org.codehaus.plexus.util.FileUtils; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; @@ -61,47 +96,17 @@ import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; -import java.io.File; -import java.nio.charset.StandardCharsets; -import java.util.HashMap; -import java.util.Map; -import java.util.StringJoiner; -import java.util.UUID; -import java.util.stream.Collectors; - -import static org.alfresco.transform.base.AbstractBaseTest.getTestFile; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_IMAGE_BMP; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_PDF; -import static org.alfresco.transform.common.Mimetype.MIMETYPE_TEXT_PLAIN; -import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_ERROR; -import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_LIVE; -import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_LOG; -import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_READY; -import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_ROOT; -import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TEST; -import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM; -import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM_CONFIG; -import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_TRANSFORM_CONFIG_LATEST; -import static org.alfresco.transform.common.RequestParamMap.ENDPOINT_VERSION; -import static org.alfresco.transform.common.RequestParamMap.PAGE_REQUEST_PARAM; -import static org.alfresco.transform.common.RequestParamMap.SOURCE_ENCODING; -import static org.alfresco.transform.common.RequestParamMap.SOURCE_MIMETYPE; -import static org.alfresco.transform.common.RequestParamMap.TARGET_MIMETYPE; -import static org.hamcrest.Matchers.containsString; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -import static org.springframework.http.HttpHeaders.ACCEPT; -import static org.springframework.http.HttpHeaders.CONTENT_DISPOSITION; -import static org.springframework.http.HttpHeaders.CONTENT_TYPE; -import static org.springframework.http.HttpStatus.CREATED; -import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import org.alfresco.transform.base.fakes.FakeTransformEngineWithTwoCustomTransformers; +import org.alfresco.transform.base.fakes.FakeTransformerPdf2Png; +import org.alfresco.transform.base.fakes.FakeTransformerTxT2Pdf; +import org.alfresco.transform.base.model.FileRefEntity; +import org.alfresco.transform.base.model.FileRefResponse; +import org.alfresco.transform.base.probes.ProbeTransform; +import org.alfresco.transform.base.sfs.SharedFileStoreClient; +import org.alfresco.transform.base.transform.TransformHandler; +import org.alfresco.transform.client.model.TransformReply; +import org.alfresco.transform.client.model.TransformRequest; +import org.alfresco.transform.config.TransformConfig; /** * Tests the endpoints of the TransformController. @@ -294,8 +299,19 @@ public void testLiveEndpointReturnsSuccessful() throws Exception { resetProbeForTesting(transformController); mockMvc.perform(MockMvcRequestBuilders.get(ENDPOINT_LIVE)) - .andExpect(status().isOk()) - .andExpect(content().string(containsString("Success - "))); + .andExpect(status().isOk()) + .andExpect(content().string(containsString("Success - "))); + } + + @Test + public void testLiveEndpointReturnsErrorAfterTooManyTransforms() throws Exception + { + resetProbeForTesting(transformController); + ProbeTransform probeTransform = transformController.getProbeTransform(); + IntStream.range(0, 1024 + 1).forEach(i -> probeTransform.incrementTransformerCount()); + mockMvc.perform(MockMvcRequestBuilders.get(ENDPOINT_LIVE)) + .andExpect(status().is(TOO_MANY_REQUESTS.value())) + .andExpect(content().string(containsString("Transformer requested to die. It has performed more than 1024 transformations"))); } @Test