You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by jo...@apache.org on 2022/04/25 16:51:24 UTC

[sling-org-apache-sling-jcr-repoinit] branch SLING-11276 created (now 4df5ebb)

This is an automated email from the ASF dual-hosted git repository.

joerghoh pushed a change to branch SLING-11276
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-jcr-repoinit.git


      at 4df5ebb  SLING-11276 retry on RepoInitExceptions

This branch includes the following new commits:

     new 4df5ebb  SLING-11276 retry on RepoInitExceptions

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[sling-org-apache-sling-jcr-repoinit] 01/01: SLING-11276 retry on RepoInitExceptions

Posted by jo...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

joerghoh pushed a commit to branch SLING-11276
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-jcr-repoinit.git

commit 4df5ebb91185c8917a8e6cfca00e9123c3746634
Author: Joerg Hoh <jh...@adobe.com>
AuthorDate: Mon Apr 25 18:51:10 2022 +0200

    SLING-11276 retry on RepoInitExceptions
---
 .../impl/RepositoryInitializerFactory.java         | 34 +++++++++++++++-------
 .../impl/RepositoryInitializerFactoryTest.java     | 17 ++++++++++-
 2 files changed, 40 insertions(+), 11 deletions(-)

diff --git a/src/main/java/org/apache/sling/jcr/repoinit/impl/RepositoryInitializerFactory.java b/src/main/java/org/apache/sling/jcr/repoinit/impl/RepositoryInitializerFactory.java
index 8d30330..75a1942 100644
--- a/src/main/java/org/apache/sling/jcr/repoinit/impl/RepositoryInitializerFactory.java
+++ b/src/main/java/org/apache/sling/jcr/repoinit/impl/RepositoryInitializerFactory.java
@@ -27,6 +27,7 @@ import javax.jcr.Session;
 import org.apache.sling.jcr.api.SlingRepository;
 import org.apache.sling.jcr.api.SlingRepositoryInitializer;
 import org.apache.sling.jcr.repoinit.JcrRepoInitOpsProcessor;
+import org.apache.sling.jcr.repoinit.impl.RetryableOperation.RetryableOperationResult;
 import org.apache.sling.repoinit.parser.RepoInitParser;
 import org.apache.sling.repoinit.parser.operations.Operation;
 import org.osgi.framework.Constants;
@@ -146,21 +147,39 @@ public class RepositoryInitializerFactory implements SlingRepositoryInitializer
     protected void applyOperations(Session session, List<Operation> ops, String logMessage) throws RepositoryException {
 
         RetryableOperation retry = new RetryableOperation.Builder().withBackoffBaseMsec(1000).withMaxRetries(3).build();
-        RetryableOperation.RetryableOperationResult result = retry.apply(() -> {
+        RetryableOperation.RetryableOperationResult result = applyOperationInternal(session, ops, logMessage, retry);
+        if (!result.isSuccessful()) {
+            String msg = String.format("Applying repoinit operation failed despite retry; set loglevel to DEBUG to see all exceptions. "
+                    + "Last exception message was: %s", result.getFailureTrace().getMessage());
+            throw new RepositoryException(msg, result.getFailureTrace());
+        }
+    }
+
+    /**
+     * Perform the operations
+     * @param session the session to use
+     * @param ops the operations
+     * @param logMessage logmessage which should be printed
+     * @param retry the retry object
+     * @return
+     */
+    protected RetryableOperationResult applyOperationInternal(Session session, List<Operation> ops, String logMessage,
+            RetryableOperation retry) {
+        return retry.apply(() -> {
             try {
                 processor.apply(session, ops);
                 session.save();
                 return new RetryableOperation.RetryableOperationResult(true,false,null);
-            } catch (InvalidItemStateException ise) {
+            } catch (InvalidItemStateException|RepoInitException ex) {
                 // a retry makes sense, because this exception might be caused by an concurrent operation
-                log.debug("(temporarily) failed to apply repoinit operations",ise);
+                log.debug("(temporarily) failed to apply repoinit operations",ex);
                 try {
                     session.refresh(false); // discard all pending changes
                 } catch (RepositoryException e1) {
                     // ignore
                 }
-                return new RetryableOperation.RetryableOperationResult(false,true,ise);
-            } catch (RepositoryException|RepoInitException ex) {
+                return new RetryableOperation.RetryableOperationResult(false,true,ex);
+            } catch (RepositoryException ex) {
                 // a permanent error, retry is not useful
                 try {
                     session.refresh(false); // discard all pending changes
@@ -170,11 +189,6 @@ public class RepositoryInitializerFactory implements SlingRepositoryInitializer
                 return new RetryableOperation.RetryableOperationResult(false,false,ex);
             }
         }, logMessage);
-        if (!result.isSuccessful()) {
-            String msg = String.format("Applying repoinit operation failed despite retry; set loglevel to DEBUG to see all exceptions. "
-                    + "Last exception message was: %s", result.getFailureTrace().getMessage());
-            throw new RepositoryException(msg, result.getFailureTrace());
-        }
     }
 
 
diff --git a/src/test/java/org/apache/sling/jcr/repoinit/impl/RepositoryInitializerFactoryTest.java b/src/test/java/org/apache/sling/jcr/repoinit/impl/RepositoryInitializerFactoryTest.java
index ac69d7c..7505195 100644
--- a/src/test/java/org/apache/sling/jcr/repoinit/impl/RepositoryInitializerFactoryTest.java
+++ b/src/test/java/org/apache/sling/jcr/repoinit/impl/RepositoryInitializerFactoryTest.java
@@ -17,6 +17,7 @@
 package org.apache.sling.jcr.repoinit.impl;
 
 import org.apache.sling.jcr.repoinit.JcrRepoInitOpsProcessor;
+import org.apache.sling.jcr.repoinit.impl.RetryableOperation.RetryableOperationResult;
 import org.apache.sling.repoinit.parser.RepoInitParser;
 import org.apache.sling.testing.mock.sling.junit.SlingContext;
 import org.junit.Before;
@@ -28,6 +29,8 @@ import org.mockito.ArgumentMatchers;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.mock;
 
@@ -54,6 +57,18 @@ public class RepositoryInitializerFactoryTest {
     public void handleUncheckedErrorsInOperations() throws RepositoryException {
         doThrow(new RepoInitException("some op failed", new Exception("root cause")))
             .when(processor).apply(ArgumentMatchers.any(), ArgumentMatchers.any());
-        sut.applyOperations(mock(Session.class), null, null); 
+        sut.applyOperations(mock(Session.class), null, null);
     }
+
+    // https://issues.apache.org/jira/browse/SLING-11276
+    @Test
+    public void testRetriesWithExceptions() {
+        doThrow(new RepoInitException("some op failed", new Exception("root cause")))
+            .when(processor).apply(ArgumentMatchers.any(), ArgumentMatchers.any());
+        RetryableOperation retry = new RetryableOperation.Builder().withBackoffBaseMsec(1).withMaxRetries(3).build();
+        RetryableOperationResult result = sut.applyOperationInternal(mock(Session.class), null, null, retry);
+        assertEquals(3, retry.retryCount);
+        assertFalse(result.isSuccessful());
+    }
+
 }