geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From johndam...@apache.org
Subject geronimo-safeguard git commit: GERONIMO-6585 - Adding more behavior to timeout support.
Date Mon, 18 Sep 2017 03:25:10 GMT
Repository: geronimo-safeguard
Updated Branches:
  refs/heads/master 315543e9d -> adee0a94c


GERONIMO-6585 - Adding more behavior to timeout support.

Upgrading to the final released version of FT.


Project: http://git-wip-us.apache.org/repos/asf/geronimo-safeguard/repo
Commit: http://git-wip-us.apache.org/repos/asf/geronimo-safeguard/commit/adee0a94
Tree: http://git-wip-us.apache.org/repos/asf/geronimo-safeguard/tree/adee0a94
Diff: http://git-wip-us.apache.org/repos/asf/geronimo-safeguard/diff/adee0a94

Branch: refs/heads/master
Commit: adee0a94cbc715f504fd5ae7a3d2a16e5eaeb9d5
Parents: 315543e
Author: John D. Ament <johndament@apache.org>
Authored: Sun Sep 17 23:25:05 2017 -0400
Committer: John D. Ament <johndament@apache.org>
Committed: Sun Sep 17 23:25:05 2017 -0400

----------------------------------------------------------------------
 pom.xml                                         | 19 +-------
 .../safeguard/impl/cdi/SafeguardExtension.java  |  3 +-
 .../impl/cdi/SafeguardInterceptor.java          |  4 +-
 .../AsyncFailsafeExecutionPlan.java             | 23 ++++++++--
 .../AsyncTimeoutExecutionPlan.java              |  9 +++-
 .../executionPlans/ExecutionPlanFactory.java    | 11 +++--
 .../MicroprofileAnnotationMapper.java           |  3 ++
 .../SyncFailsafeExecutionPlan.java              | 10 +++-
 .../executionPlans/TimeoutWrappedCallable.java  | 48 ++++++++++++++++++++
 safeguard-tck-tests/pom.xml                     |  8 +++-
 10 files changed, 108 insertions(+), 30 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/geronimo-safeguard/blob/adee0a94/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index 2f1468d..7829987 100644
--- a/pom.xml
+++ b/pom.xml
@@ -53,7 +53,7 @@
     <properties>
         <maven.compiler.source>1.8</maven.compiler.source>
         <maven.compiler.target>1.8</maven.compiler.target>
-        <microprofile-fault-tolerance.version>1.0-SNAPSHOT</microprofile-fault-tolerance.version>
+        <microprofile-fault-tolerance.version>1.0</microprofile-fault-tolerance.version>
         <owb.version>2.0.1</owb.version>
         <arquillian.version>1.1.13.Final</arquillian.version>
         <arquillian-weld-embedded.version>2.0.0.Beta5</arquillian-weld-embedded.version>
@@ -167,23 +167,6 @@
 
     <profiles>
         <profile>
-            <id>eclipse-repo</id>
-            <repositories>
-                <repository>
-                    <id>microprofile-snapshots</id>
-                    <releases><enabled>false</enabled></releases>
-                    <snapshots><enabled>true</enabled></snapshots>
-                    <url>https://repo.eclipse.org/content/groups/microprofile/</url>
-                </repository>
-                <repository>
-                    <id>microprofile-releases</id>
-                    <releases><enabled>true</enabled></releases>
-                    <snapshots><enabled>false</enabled></snapshots>
-                    <url>https://repo.eclipse.org/content/repositories/microprofile-releases</url>
-                </repository>
-            </repositories>
-        </profile>
-        <profile>
             <id>OWB2</id>
             <activation>
                 <activeByDefault>true</activeByDefault>

http://git-wip-us.apache.org/repos/asf/geronimo-safeguard/blob/adee0a94/safeguard-impl/src/main/java/org/apache/safeguard/impl/cdi/SafeguardExtension.java
----------------------------------------------------------------------
diff --git a/safeguard-impl/src/main/java/org/apache/safeguard/impl/cdi/SafeguardExtension.java
b/safeguard-impl/src/main/java/org/apache/safeguard/impl/cdi/SafeguardExtension.java
index e868c68..c826255 100644
--- a/safeguard-impl/src/main/java/org/apache/safeguard/impl/cdi/SafeguardExtension.java
+++ b/safeguard-impl/src/main/java/org/apache/safeguard/impl/cdi/SafeguardExtension.java
@@ -22,6 +22,7 @@ package org.apache.safeguard.impl.cdi;
 import org.apache.safeguard.api.SafeguardEnabled;
 import org.eclipse.microprofile.faulttolerance.CircuitBreaker;
 import org.eclipse.microprofile.faulttolerance.Retry;
+import org.eclipse.microprofile.faulttolerance.Timeout;
 
 import javax.enterprise.event.Observes;
 import javax.enterprise.inject.spi.AnnotatedConstructor;
@@ -40,7 +41,7 @@ import java.util.Set;
 import static org.apache.safeguard.api.SafeguardEnabled.INSTANCE;
 
 public class SafeguardExtension implements Extension {
-    public void findFaultTolerantBeans(@Observes @WithAnnotations({Retry.class, CircuitBreaker.class})
+    public void findFaultTolerantBeans(@Observes @WithAnnotations({Retry.class, CircuitBreaker.class,
Timeout.class})
                                                ProcessAnnotatedType<?> pat) {
         if (!pat.getAnnotatedType().isAnnotationPresent(SafeguardEnabled.class)) {
             pat.setAnnotatedType(new SafeguardAnnotatedTypeWrapper(pat.getAnnotatedType()));

http://git-wip-us.apache.org/repos/asf/geronimo-safeguard/blob/adee0a94/safeguard-impl/src/main/java/org/apache/safeguard/impl/cdi/SafeguardInterceptor.java
----------------------------------------------------------------------
diff --git a/safeguard-impl/src/main/java/org/apache/safeguard/impl/cdi/SafeguardInterceptor.java
b/safeguard-impl/src/main/java/org/apache/safeguard/impl/cdi/SafeguardInterceptor.java
index 00d98bf..d3bd604 100644
--- a/safeguard-impl/src/main/java/org/apache/safeguard/impl/cdi/SafeguardInterceptor.java
+++ b/safeguard-impl/src/main/java/org/apache/safeguard/impl/cdi/SafeguardInterceptor.java
@@ -24,6 +24,7 @@ import org.apache.safeguard.impl.FailsafeExecutionManager;
 import org.apache.safeguard.impl.util.AnnotationUtil;
 import org.eclipse.microprofile.faulttolerance.CircuitBreaker;
 import org.eclipse.microprofile.faulttolerance.Retry;
+import org.eclipse.microprofile.faulttolerance.Timeout;
 
 import javax.annotation.Priority;
 import javax.enterprise.context.Dependent;
@@ -53,6 +54,7 @@ public class SafeguardInterceptor {
 
     private boolean isMethodSafeguarded(Method method) {
         return AnnotationUtil.getAnnotation(method, Retry.class) != null ||
-                AnnotationUtil.getAnnotation(method, CircuitBreaker.class) != null;
+                AnnotationUtil.getAnnotation(method, CircuitBreaker.class) != null ||
+                AnnotationUtil.getAnnotation(method, Timeout.class) != null;
     }
 }

http://git-wip-us.apache.org/repos/asf/geronimo-safeguard/blob/adee0a94/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/AsyncFailsafeExecutionPlan.java
----------------------------------------------------------------------
diff --git a/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/AsyncFailsafeExecutionPlan.java
b/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/AsyncFailsafeExecutionPlan.java
index 589883a..a93531e 100644
--- a/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/AsyncFailsafeExecutionPlan.java
+++ b/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/AsyncFailsafeExecutionPlan.java
@@ -20,6 +20,7 @@
 package org.apache.safeguard.impl.executionPlans;
 
 import net.jodah.failsafe.AsyncFailsafe;
+import net.jodah.failsafe.CircuitBreakerOpenException;
 import org.apache.safeguard.impl.circuitbreaker.FailsafeCircuitBreaker;
 import org.apache.safeguard.impl.retry.FailsafeRetryDefinition;
 
@@ -50,10 +51,26 @@ public class AsyncFailsafeExecutionPlan extends SyncFailsafeExecutionPlan
{
             if (this.timeout == null) {
                 return asyncFailsafe.get(callable).get();
             } else {
-                return asyncFailsafe.get(callable).get(timeout.toMillis(), TimeUnit.MILLISECONDS);
+                return asyncFailsafe
+                        .get(new TimeoutWrappedCallable<>(callable, executorService,
timeout))
+                        .get();
+            }
+        } catch (CircuitBreakerOpenException e) {
+            throw new org.eclipse.microprofile.faulttolerance.exceptions.CircuitBreakerOpenException(e);
+        } catch (InterruptedException | ExecutionException e) {
+            Throwable cause = e.getCause();
+            if(cause == null) {
+                throw new RuntimeException(e);
+            }
+            if (cause instanceof CircuitBreakerOpenException) {
+                throw new org.eclipse.microprofile.faulttolerance.exceptions.CircuitBreakerOpenException(cause);
+            }
+            else if(cause instanceof RuntimeException) {
+                throw (RuntimeException) cause;
+            }
+            else {
+                throw new RuntimeException(e);
             }
-        } catch (TimeoutException | InterruptedException | ExecutionException e) {
-            throw new org.eclipse.microprofile.faulttolerance.exceptions.TimeoutException(e);
         }
     }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/geronimo-safeguard/blob/adee0a94/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/AsyncTimeoutExecutionPlan.java
----------------------------------------------------------------------
diff --git a/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/AsyncTimeoutExecutionPlan.java
b/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/AsyncTimeoutExecutionPlan.java
index 849f7c4..5a351ac 100644
--- a/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/AsyncTimeoutExecutionPlan.java
+++ b/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/AsyncTimeoutExecutionPlan.java
@@ -41,7 +41,14 @@ class AsyncTimeoutExecutionPlan implements ExecutionPlan {
         Future<T> future = executorService.submit(callable);
         try {
             return future.get(timeout.toMillis(), TimeUnit.MILLISECONDS);
-        } catch (InterruptedException | ExecutionException | TimeoutException e) {
+        } catch (ExecutionException e) {
+            if(e.getCause() != null && e.getCause() instanceof RuntimeException)
{
+                throw (RuntimeException)e.getCause();
+            }
+            else {
+                throw new RuntimeException(e);
+            }
+        } catch (InterruptedException | TimeoutException e) {
             throw new org.eclipse.microprofile.faulttolerance.exceptions.TimeoutException(e);
         }
     }

http://git-wip-us.apache.org/repos/asf/geronimo-safeguard/blob/adee0a94/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/ExecutionPlanFactory.java
----------------------------------------------------------------------
diff --git a/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/ExecutionPlanFactory.java
b/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/ExecutionPlanFactory.java
index 4a5086e..e3c0642 100644
--- a/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/ExecutionPlanFactory.java
+++ b/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/ExecutionPlanFactory.java
@@ -36,6 +36,7 @@ import java.lang.reflect.Method;
 import java.time.Duration;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.concurrent.Executors;
 
 import static org.apache.safeguard.impl.executionPlans.MicroprofileAnnotationMapper.mapCircuitBreaker;
 import static org.apache.safeguard.impl.executionPlans.MicroprofileAnnotationMapper.mapRetry;
@@ -80,12 +81,16 @@ public class ExecutionPlanFactory {
                     return new AsyncOnlyExecutionPlan(null);
                 }
                 else {
-                    return new AsyncTimeoutExecutionPlan(timeout, null);
+                    return new AsyncTimeoutExecutionPlan(timeout, Executors.newFixedThreadPool(5));
                 }
             }
+            else if(circuitBreaker == null && retryDefinition == null &&
timeout != null) {
+                // then its just timeout
+                return new AsyncTimeoutExecutionPlan(timeout, Executors.newFixedThreadPool(5));
+            }
             else {
-                if (isAsync) {
-                    return new AsyncFailsafeExecutionPlan(retryDefinition, circuitBreaker,
null, timeout);
+                if (isAsync || timeout != null) {
+                    return new AsyncFailsafeExecutionPlan(retryDefinition, circuitBreaker,
Executors.newScheduledThreadPool(5), timeout);
                 } else {
                     return new SyncFailsafeExecutionPlan(retryDefinition, circuitBreaker);
                 }

http://git-wip-us.apache.org/repos/asf/geronimo-safeguard/blob/adee0a94/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/MicroprofileAnnotationMapper.java
----------------------------------------------------------------------
diff --git a/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/MicroprofileAnnotationMapper.java
b/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/MicroprofileAnnotationMapper.java
index befd5e5..ee2b3e6 100644
--- a/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/MicroprofileAnnotationMapper.java
+++ b/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/MicroprofileAnnotationMapper.java
@@ -27,11 +27,13 @@ import org.eclipse.microprofile.faulttolerance.CircuitBreaker;
 import org.eclipse.microprofile.faulttolerance.Retry;
 
 import java.time.Duration;
+import java.util.concurrent.TimeoutException;
 
 class MicroprofileAnnotationMapper {
     static FailsafeRetryDefinition mapRetry(Retry retry, FailsafeRetryBuilder retryBuilder)
{
         retryBuilder.withMaxRetries(retry.maxRetries())
                 .withRetryOn(retry.retryOn())
+                .withRetryOn(TimeoutException.class)
                 .withAbortOn(retry.abortOn());
         if (retry.delay() > 0L) {
             retryBuilder.withDelay(Duration.of(retry.delay(), retry.delayUnit()));
@@ -50,6 +52,7 @@ class MicroprofileAnnotationMapper {
         int failureCount = (int) (circuitBreaker.failureRatio() * circuitBreaker.requestVolumeThreshold());
         FailsafeCircuitBreakerBuilder failsafeCircuitBreakerBuilder = builder
                 .withFailOn(circuitBreaker.failOn())
+                .withFailOn(TimeoutException.class)
                 .withDelay(Duration.of(circuitBreaker.delay(), circuitBreaker.delayUnit()))
                 .withSuccessCount(circuitBreaker.successThreshold());
         if (failureCount > 0) {

http://git-wip-us.apache.org/repos/asf/geronimo-safeguard/blob/adee0a94/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/SyncFailsafeExecutionPlan.java
----------------------------------------------------------------------
diff --git a/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/SyncFailsafeExecutionPlan.java
b/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/SyncFailsafeExecutionPlan.java
index 613ceab..7a53dea 100644
--- a/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/SyncFailsafeExecutionPlan.java
+++ b/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/SyncFailsafeExecutionPlan.java
@@ -19,6 +19,7 @@
 
 package org.apache.safeguard.impl.executionPlans;
 
+import net.jodah.failsafe.CircuitBreakerOpenException;
 import net.jodah.failsafe.Failsafe;
 import net.jodah.failsafe.SyncFailsafe;
 import org.apache.safeguard.impl.circuitbreaker.FailsafeCircuitBreaker;
@@ -45,7 +46,11 @@ public class SyncFailsafeExecutionPlan implements ExecutionPlan {
     @Override
     public <T> T execute(Callable<T> callable) {
         SyncFailsafe<?> syncFailsafe = getSyncFailsafe();
-        return syncFailsafe.get(callable);
+        try {
+            return syncFailsafe.get(callable);
+        } catch (CircuitBreakerOpenException e) {
+            throw new org.eclipse.microprofile.faulttolerance.exceptions.CircuitBreakerOpenException(e);
+        }
     }
 
     SyncFailsafe<?> getSyncFailsafe() {
@@ -58,7 +63,8 @@ public class SyncFailsafeExecutionPlan implements ExecutionPlan {
                 syncFailsafe = Failsafe.with(retryDefinition.getRetryPolicy());
             }
             else {
-                syncFailsafe = Failsafe.with(retryDefinition.getRetryPolicy()).with(failsafeCircuitBreaker.getDefinition().getCircuitBreaker());
+                syncFailsafe = Failsafe.with(retryDefinition.getRetryPolicy())
+                        .with(failsafeCircuitBreaker.getDefinition().getCircuitBreaker());
             }
         }
         return syncFailsafe;

http://git-wip-us.apache.org/repos/asf/geronimo-safeguard/blob/adee0a94/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/TimeoutWrappedCallable.java
----------------------------------------------------------------------
diff --git a/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/TimeoutWrappedCallable.java
b/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/TimeoutWrappedCallable.java
new file mode 100644
index 0000000..8013a42
--- /dev/null
+++ b/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/TimeoutWrappedCallable.java
@@ -0,0 +1,48 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+
+package org.apache.safeguard.impl.executionPlans;
+
+import java.time.Duration;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+class TimeoutWrappedCallable<T> implements Callable<T> {
+    private final Callable<T> delegate;
+    private final ExecutorService executorService;
+    private final Duration timeout;
+
+    TimeoutWrappedCallable(Callable<T> delegate, ExecutorService executorService, Duration
timeout) {
+        this.delegate = delegate;
+        this.executorService = executorService;
+        this.timeout = timeout;
+    }
+
+    @Override
+    public T call() throws Exception {
+        try {
+            return executorService.submit(delegate).get(timeout.toMillis(), TimeUnit.MILLISECONDS);
+        }
+        catch (TimeoutException e) {
+            throw new org.eclipse.microprofile.faulttolerance.exceptions.TimeoutException(e);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/geronimo-safeguard/blob/adee0a94/safeguard-tck-tests/pom.xml
----------------------------------------------------------------------
diff --git a/safeguard-tck-tests/pom.xml b/safeguard-tck-tests/pom.xml
index d859603..631ebc3 100644
--- a/safeguard-tck-tests/pom.xml
+++ b/safeguard-tck-tests/pom.xml
@@ -67,8 +67,14 @@
                         <dependency>org.eclipse.microprofile.fault-tolerance:microprofile-fault-tolerance-tck</dependency>
                     </dependenciesToScan>
                     <includes>
-                        <include>org.eclipse.microprofile.fault.tolerance.tck.RetryTest</include>
+                        <include>org.eclipse.microprofile.fault.tolerance.tck.CircuitBreakerTest</include>
+                        <!--<include>org.eclipse.microprofile.fault.tolerance.tck.CircuitBreakerRetryTest</include>-->
+                        <!--<include>org.eclipse.microprofile.fault.tolerance.tck.ConfigTest</include>-->
+                        <!--<include>org.eclipse.microprofile.fault.tolerance.tck.FallbackTest</include>-->
                         <include>org.eclipse.microprofile.fault.tolerance.tck.RetryConditionTest</include>
+                        <include>org.eclipse.microprofile.fault.tolerance.tck.RetryTest</include>
+                        <include>org.eclipse.microprofile.fault.tolerance.tck.RetryTimeoutTest</include>
+                        <include>org.eclipse.microprofile.fault.tolerance.tck.TimeoutTest</include>
                     </includes>
                 </configuration>
             </plugin>


Mime
View raw message