From b60c8a2b9ecd24c973fc1337df61b2c09578f161 Mon Sep 17 00:00:00 2001 From: Gabriel Roldan Date: Fri, 11 Oct 2024 17:25:39 -0300 Subject: [PATCH] Reduce datadir backend's eventual consistency overhead on non REST API requests --- .../EventuallyConsistentCatalogFacade.java | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/catalog/backends/datadir/src/main/java/org/geoserver/cloud/catalog/backend/datadir/EventuallyConsistentCatalogFacade.java b/src/catalog/backends/datadir/src/main/java/org/geoserver/cloud/catalog/backend/datadir/EventuallyConsistentCatalogFacade.java index fc929ec08..52bcb65e8 100644 --- a/src/catalog/backends/datadir/src/main/java/org/geoserver/cloud/catalog/backend/datadir/EventuallyConsistentCatalogFacade.java +++ b/src/catalog/backends/datadir/src/main/java/org/geoserver/cloud/catalog/backend/datadir/EventuallyConsistentCatalogFacade.java @@ -24,7 +24,9 @@ import org.geoserver.catalog.plugin.Patch; import org.geoserver.catalog.plugin.forwarding.ForwardingExtendedCatalogFacade; import org.geoserver.ows.util.OwsUtils; +import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; import java.util.Objects; import java.util.concurrent.TimeUnit; @@ -320,7 +322,25 @@ private T retryOnNull(Supplier supplier, Supplier op) { private T retry(Supplier supplier, Predicate predicate, Supplier op) { T ret = supplier.get(); if (predicate.test(ret)) return ret; - if (!isWebRequest()) return ret; + + // do retry if it's a REST API request or there're pending updates + if (isWebRequest() && (isRestRequest() || !enforcer.isConverged())) { + return doRetry(supplier, predicate, op, ret); + } + return ret; + } + + private boolean isRestRequest() { + RequestAttributes atts = RequestContextHolder.getRequestAttributes(); + if (atts instanceof ServletRequestAttributes webreq) { + String uri = webreq.getRequest().getRequestURI(); + return uri.contains("/rest/"); + } + return false; + } + + private T doRetry( + Supplier supplier, Predicate predicate, Supplier op, T ret) { // poor man's Retry implementation final int maxAttempts = retryAttemptMillis.length; final String opDesc = op.get();