Skip to content

Commit

Permalink
MNT-23889 Fix liveness/readiness probes (#874)
Browse files Browse the repository at this point in the history
Co-authored-by: Grzegorz Oleksy <[email protected]>
  • Loading branch information
pzhyland and goleksyalfresco authored Oct 19, 2023
1 parent 2658e3e commit 137ef8b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -115,6 +116,7 @@ public class TransformController
private boolean behindIngres;

TransformEngine transformEngine;
private final AtomicReference<ProbeTransform> probeTransform = new AtomicReference<>();

@PostConstruct
private void initTransformEngine()
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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;
}

Expand All @@ -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))
{
Expand Down

0 comments on commit 137ef8b

Please sign in to comment.