From 137ef8bcf9335522b35aa57b5e70608b87ee18f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20=C5=BBurek?= Date: Thu, 19 Oct 2023 08:05:01 +0200 Subject: [PATCH] MNT-23889 Fix liveness/readiness probes (#874) Co-authored-by: Grzegorz Oleksy --- .../transform/base/TransformController.java | 14 +++++++++++++- .../transform/base/probes/ProbeTransform.java | 11 +++++------ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/engines/base/src/main/java/org/alfresco/transform/base/TransformController.java b/engines/base/src/main/java/org/alfresco/transform/base/TransformController.java index b32b2c48a..1bd86ad88 100644 --- a/engines/base/src/main/java/org/alfresco/transform/base/TransformController.java +++ b/engines/base/src/main/java/org/alfresco/transform/base/TransformController.java @@ -68,6 +68,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.atomic.AtomicReference; import static java.text.MessageFormat.format; import static org.alfresco.transform.base.html.OptionsHelper.getOptionNames; @@ -115,6 +116,7 @@ public class TransformController private boolean behindIngres; TransformEngine transformEngine; + private final AtomicReference probeTransform = new AtomicReference<>(); @PostConstruct private void initTransformEngine() @@ -247,7 +249,17 @@ public String live(HttpServletRequest request) public ProbeTransform getProbeTransform() { - return transformEngine.getProbeTransform(); + ProbeTransform probe = probeTransform.get(); + if (probe != null) + { + return probe; + } + probe = transformEngine.getProbeTransform(); + if (probeTransform.compareAndSet(null, probe)) + { + return probe; + } + return probeTransform.get(); } @GetMapping(value = ENDPOINT_TRANSFORM_CONFIG) 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 87be4eafc..254f4ba2c 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 @@ -176,11 +176,15 @@ public String doTransformOrNothing(boolean isLiveProbe, TransformHandler transfo { return doNothing(true); } - return (isLiveProbe && livenessTransformPeriod > 0 && + + String result = (isLiveProbe && livenessTransformPeriod > 0 && (transCount <= AVERAGE_OVER_TRANSFORMS || nextTransformTime < System.currentTimeMillis())) || !initialised.get() ? doTransform(isLiveProbe, transformHandler) : doNothing(isLiveProbe); + + checkMaxTransformTimeAndCount(isLiveProbe); + return result; } private String doNothing(boolean isLiveProbe) @@ -196,8 +200,6 @@ private String doNothing(boolean isLiveProbe) private String doTransform(boolean isLiveProbe, TransformHandler transformHandler) { - checkMaxTransformTimeAndCount(isLiveProbe); - long start = System.currentTimeMillis(); if (nextTransformTime != 0) @@ -230,8 +232,6 @@ private String doTransform(boolean isLiveProbe, TransformHandler transformHandle // We don't care if the ready or live probe works out if we are 'ready' to take requests. initialised.set(true); - checkMaxTransformTimeAndCount(isLiveProbe); - return getProbeMessage(isLiveProbe) + "Success - "+message; } @@ -254,7 +254,6 @@ private void checkMaxTransformTimeAndCount(boolean isLiveProbe) private File getSourceFile(boolean isLiveProbe) { - incrementTransformerCount(); File sourceFile = createTempFile("probe_source_", "_" + sourceFilename); try (InputStream inputStream = getClass().getResourceAsStream('/' + sourceFilename)) {