diff --git a/tmail-backend/tmail-third-party/openpaas/src/main/java/com/linagora/tmail/dav/DavClient.java b/tmail-backend/tmail-third-party/openpaas/src/main/java/com/linagora/tmail/dav/DavClient.java index 4459cac7ce..065c7a74f7 100644 --- a/tmail-backend/tmail-third-party/openpaas/src/main/java/com/linagora/tmail/dav/DavClient.java +++ b/tmail-backend/tmail-third-party/openpaas/src/main/java/com/linagora/tmail/dav/DavClient.java @@ -58,7 +58,6 @@ public class DavClient { public static final int MAX_CALENDAR_OBJECT_UPDATE_RETRIES = 5; - public static final Duration MIN_CALENDAR_OBJECT_UPDATE_RETRY_BACKOFF = Duration.ofMillis(100); private static final Logger LOGGER = LoggerFactory.getLogger(DavClient.class); private static final Duration DEFAULT_RESPONSE_TIMEOUT = Duration.ofSeconds(10); @@ -67,10 +66,14 @@ public class DavClient { private static final String ACCEPT_VCARD_JSON = "application/vcard+json"; private static final String ACCEPT_XML = "application/xml"; private static final String CONTENT_TYPE_VCARD = "application/vcard"; - private static final boolean SHOULD_RETRY_CALENDAR_OBJECT_UPDATE = true; private final HttpClient client; private final DavConfiguration config; + public final Duration calendarObjectUpdateRetryBackoff = + Optional.ofNullable(System.getProperty("MIN_CALENDAR_OBJECT_UPDATE_RETRY_BACKOFF_IN_MILLS")) + .map(Long::parseLong) + .map(Duration::ofMillis) + .orElse(Duration.ofMillis(100)); public DavClient(DavConfiguration config) { this.config = config; @@ -157,8 +160,8 @@ public Mono updateCalendarObject(String username, URI calendarObjectUri, F .map(calendarObjectUpdater) .flatMap(updatedCalendarObject -> doUpdateCalendarObject(username, updatedCalendarObject)) .retryWhen( - Retry.backoff(MAX_CALENDAR_OBJECT_UPDATE_RETRIES, MIN_CALENDAR_OBJECT_UPDATE_RETRY_BACKOFF) - .filter(e -> e instanceof DavClientException && ((DavClientException) e).shouldRetry()) + Retry.backoff(MAX_CALENDAR_OBJECT_UPDATE_RETRIES, calendarObjectUpdateRetryBackoff) + .filter(RetriableDavClientException.class::isInstance) .onRetryExhaustedThrow((retryBackoffSpec, retrySignal) -> new DavClientException("Max retries exceeded for calendar update", retrySignal.failure()))); } @@ -184,10 +187,10 @@ private static Mono handleCalendarObjectUpdateResponse(DavCalendarObject u () -> LOGGER.info("Calendar object '{}' updated successfully.", updatedCalendarObject.uri())); } else if (response.status() == HttpResponseStatus.PRECONDITION_FAILED) { - return Mono.error(new DavClientException( + return Mono.error(new RetriableDavClientException( String.format( "Precondition failed (ETag mismatch) when updating calendar object '%s'. Retry may be needed.", - updatedCalendarObject.uri()), SHOULD_RETRY_CALENDAR_OBJECT_UPDATE)); + updatedCalendarObject.uri()))); } else { return Mono.error(new DavClientException( String.format( diff --git a/tmail-backend/tmail-third-party/openpaas/src/main/java/com/linagora/tmail/dav/DavClientException.java b/tmail-backend/tmail-third-party/openpaas/src/main/java/com/linagora/tmail/dav/DavClientException.java index 7313c46002..1107ff641e 100644 --- a/tmail-backend/tmail-third-party/openpaas/src/main/java/com/linagora/tmail/dav/DavClientException.java +++ b/tmail-backend/tmail-third-party/openpaas/src/main/java/com/linagora/tmail/dav/DavClientException.java @@ -19,27 +19,12 @@ package com.linagora.tmail.dav; public class DavClientException extends RuntimeException { - private final boolean shouldRetry; public DavClientException(String message) { - this(message, false); - } - - public DavClientException(String message, boolean shouldRetry) { super(message); - this.shouldRetry = shouldRetry; } public DavClientException(String message, Throwable cause) { - this(message, cause, false); - } - - public DavClientException(String message, Throwable cause, boolean shouldRetry) { super(message, cause); - this.shouldRetry = shouldRetry; - } - - public boolean shouldRetry() { - return shouldRetry; } } diff --git a/tmail-backend/tmail-third-party/openpaas/src/main/java/com/linagora/tmail/dav/RetriableDavClientException.java b/tmail-backend/tmail-third-party/openpaas/src/main/java/com/linagora/tmail/dav/RetriableDavClientException.java new file mode 100644 index 0000000000..ddef279952 --- /dev/null +++ b/tmail-backend/tmail-third-party/openpaas/src/main/java/com/linagora/tmail/dav/RetriableDavClientException.java @@ -0,0 +1,30 @@ +/******************************************************************** + * As a subpart of Twake Mail, this file is edited by Linagora. * + * * + * https://twake-mail.com/ * + * https://linagora.com * + * * + * This file is subject to The Affero Gnu Public License * + * version 3. * + * * + * https://www.gnu.org/licenses/agpl-3.0.en.html * + * * + * This program is distributed in the hope that it will be * + * useful, but WITHOUT ANY WARRANTY; without even the implied * + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR * + * PURPOSE. See the GNU Affero General Public License for * + * more details. * + ********************************************************************/ + +package com.linagora.tmail.dav; + +public class RetriableDavClientException extends DavClientException { + + public RetriableDavClientException(String message) { + super(message); + } + + public RetriableDavClientException(String message, Throwable cause) { + super(message, cause); + } +} \ No newline at end of file diff --git a/tmail-backend/tmail-third-party/openpaas/src/test/java/com/linagora/tmail/dav/DavClientTest.java b/tmail-backend/tmail-third-party/openpaas/src/test/java/com/linagora/tmail/dav/DavClientTest.java index 94f9a6d406..4c28a1f26e 100644 --- a/tmail-backend/tmail-third-party/openpaas/src/test/java/com/linagora/tmail/dav/DavClientTest.java +++ b/tmail-backend/tmail-third-party/openpaas/src/test/java/com/linagora/tmail/dav/DavClientTest.java @@ -52,6 +52,7 @@ import org.apache.http.HttpStatus; import org.apache.james.core.MailAddress; import org.apache.james.util.ClassLoaderUtils; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; @@ -79,9 +80,15 @@ public class DavClientTest { @BeforeEach void setup() { + System.setProperty("MIN_CALENDAR_OBJECT_UPDATE_RETRY_BACKOFF_IN_MILLS", "10"); client = new DavClient(davServerExtension.getDavConfiguration()); } + @AfterEach + void tearDown() { + System.clearProperty("MIN_CALENDAR_OBJECT_UPDATE_RETRY_BACKOFF_IN_MILLS"); + } + @Test void existsCollectedContactShouldReturnTrueWhenHTTPResponseIs200() { String collectedContactUid = UUID.randomUUID().toString();