Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 3911E200D17 for ; Sun, 8 Oct 2017 18:38:22 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 379A91609E6; Sun, 8 Oct 2017 16:38:22 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 2D26C1609D3 for ; Sun, 8 Oct 2017 18:38:21 +0200 (CEST) Received: (qmail 66812 invoked by uid 500); 8 Oct 2017 16:38:20 -0000 Mailing-List: contact scm-help@geronimo.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: Reply-To: dev@geronimo.apache.org List-Id: Delivered-To: mailing list scm@geronimo.apache.org Received: (qmail 66803 invoked by uid 99); 8 Oct 2017 16:38:20 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 08 Oct 2017 16:38:20 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 91406F583C; Sun, 8 Oct 2017 16:38:19 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: johndament@apache.org To: scm@geronimo.apache.org Message-Id: <55cc2cf59e6a4719ba8d770aa243268d@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: geronimo-safeguard git commit: GERONIMO-6591 - Allow the fallback only execution mode via config. Date: Sun, 8 Oct 2017 16:38:19 +0000 (UTC) archived-at: Sun, 08 Oct 2017 16:38:22 -0000 Repository: geronimo-safeguard Updated Branches: refs/heads/master b70e077a2 -> a43eeec7d GERONIMO-6591 - Allow the fallback only execution mode via config. Made config an optional dependency, use Geronimo Config for testing. Project: http://git-wip-us.apache.org/repos/asf/geronimo-safeguard/repo Commit: http://git-wip-us.apache.org/repos/asf/geronimo-safeguard/commit/a43eeec7 Tree: http://git-wip-us.apache.org/repos/asf/geronimo-safeguard/tree/a43eeec7 Diff: http://git-wip-us.apache.org/repos/asf/geronimo-safeguard/diff/a43eeec7 Branch: refs/heads/master Commit: a43eeec7d6facc6267bda529250a4bbb66ba8c49 Parents: b70e077 Author: John D. Ament Authored: Sun Oct 8 12:38:14 2017 -0400 Committer: John D. Ament Committed: Sun Oct 8 12:38:14 2017 -0400 ---------------------------------------------------------------------- pom.xml | 12 +++++ safeguard-impl/pom.xml | 8 +++ .../impl/executionPlans/BasicExecutionPlan.java | 35 +++++++++++++ .../impl/executionPlans/ExecutionPlan.java | 2 +- .../executionPlans/ExecutionPlanFactory.java | 55 ++++++++++++++------ .../FallbackOnlyExecutionPlan.java | 43 +++++++++++++++ safeguard-tck-tests/pom.xml | 5 +- 7 files changed, 142 insertions(+), 18 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/geronimo-safeguard/blob/a43eeec7/pom.xml ---------------------------------------------------------------------- diff --git a/pom.xml b/pom.xml index 7829987..a3f1561 100644 --- a/pom.xml +++ b/pom.xml @@ -92,6 +92,18 @@ provided + org.eclipse.microprofile.config + microprofile-config-api + 1.1 + true + + + org.apache.geronimo.config + geronimo-config-impl + 1.0 + test + + org.eclipse.microprofile.fault-tolerance microprofile-fault-tolerance-api ${microprofile-fault-tolerance.version} http://git-wip-us.apache.org/repos/asf/geronimo-safeguard/blob/a43eeec7/safeguard-impl/pom.xml ---------------------------------------------------------------------- diff --git a/safeguard-impl/pom.xml b/safeguard-impl/pom.xml index ddeb37d..d55b2ef 100644 --- a/safeguard-impl/pom.xml +++ b/safeguard-impl/pom.xml @@ -60,6 +60,10 @@ geronimo-interceptor_1.2_spec + org.eclipse.microprofile.config + microprofile-config-api + + org.testng testng @@ -67,5 +71,9 @@ org.assertj assertj-core + + org.apache.geronimo.config + geronimo-config-impl + \ No newline at end of file http://git-wip-us.apache.org/repos/asf/geronimo-safeguard/blob/a43eeec7/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/BasicExecutionPlan.java ---------------------------------------------------------------------- diff --git a/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/BasicExecutionPlan.java b/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/BasicExecutionPlan.java new file mode 100644 index 0000000..74881d4 --- /dev/null +++ b/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/BasicExecutionPlan.java @@ -0,0 +1,35 @@ +/* + * 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 javax.interceptor.InvocationContext; +import java.util.concurrent.Callable; + +public class BasicExecutionPlan implements ExecutionPlan { + @Override + public T execute(Callable callable, InvocationContext invocationContext) { + try { + return callable.call(); + } + catch (Exception e) { + throw new RuntimeException(e); + } + } +} http://git-wip-us.apache.org/repos/asf/geronimo-safeguard/blob/a43eeec7/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/ExecutionPlan.java ---------------------------------------------------------------------- diff --git a/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/ExecutionPlan.java b/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/ExecutionPlan.java index f8092b8..590eaff 100644 --- a/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/ExecutionPlan.java +++ b/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/ExecutionPlan.java @@ -23,5 +23,5 @@ import javax.interceptor.InvocationContext; import java.util.concurrent.Callable; public interface ExecutionPlan { - T execute(Callable function, InvocationContext invocationContext); + T execute(Callable callable, InvocationContext invocationContext); } http://git-wip-us.apache.org/repos/asf/geronimo-safeguard/blob/a43eeec7/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 6c15bb0..3c11be5 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 @@ -28,6 +28,8 @@ import org.apache.safeguard.impl.retry.FailsafeRetryDefinition; import org.apache.safeguard.impl.retry.FailsafeRetryManager; import org.apache.safeguard.impl.util.AnnotationUtil; import org.apache.safeguard.impl.util.NamingUtil; +import org.eclipse.microprofile.config.Config; +import org.eclipse.microprofile.config.ConfigProvider; import org.eclipse.microprofile.faulttolerance.Asynchronous; import org.eclipse.microprofile.faulttolerance.CircuitBreaker; import org.eclipse.microprofile.faulttolerance.Fallback; @@ -38,8 +40,8 @@ import java.lang.reflect.Method; import java.time.Duration; import java.util.HashMap; import java.util.Map; -import java.util.concurrent.Callable; import java.util.concurrent.Executors; +import java.util.concurrent.atomic.AtomicBoolean; import static org.apache.safeguard.impl.executionPlans.MicroprofileAnnotationMapper.mapCircuitBreaker; import static org.apache.safeguard.impl.executionPlans.MicroprofileAnnotationMapper.mapRetry; @@ -48,10 +50,12 @@ public class ExecutionPlanFactory { private final FailsafeCircuitBreakerManager circuitBreakerManager; private final FailsafeRetryManager retryManager; private Map executionPlanMap = new HashMap<>(); + private boolean enableAllMicroProfileFeatures = false; public ExecutionPlanFactory(FailsafeCircuitBreakerManager circuitBreakerManager, FailsafeRetryManager retryManager) { this.circuitBreakerManager = circuitBreakerManager; this.retryManager = retryManager; + this.enableAllMicroProfileFeatures = this.enableNonFallbacksForMicroProfile(); } public ExecutionPlan locateExecutionPlan(String name, Duration timeout, boolean async) { @@ -80,28 +84,47 @@ public class ExecutionPlanFactory { boolean isAsync = isAsync(method); Duration timeout = readTimeout(method); FallbackRunner fallbackRunner = this.createFallback(method); - if(circuitBreaker == null && retryDefinition == null && isAsync) { - if(timeout == null) { - return new AsyncOnlyExecutionPlan(null); - } - else { + if(this.enableAllMicroProfileFeatures) { + if (circuitBreaker == null && retryDefinition == null && isAsync) { + if (timeout == null) { + return new AsyncOnlyExecutionPlan(null); + } else { + 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(circuitBreaker == null && retryDefinition == null && timeout != null) { - // then its just timeout - return new AsyncTimeoutExecutionPlan(timeout, Executors.newFixedThreadPool(5)); - } - else { - if (isAsync || timeout != null) { - return new AsyncFailsafeExecutionPlan(retryDefinition, circuitBreaker, fallbackRunner, Executors.newScheduledThreadPool(5), timeout); } else { - return new SyncFailsafeExecutionPlan(retryDefinition, circuitBreaker, fallbackRunner); + if (isAsync || timeout != null) { + return new AsyncFailsafeExecutionPlan(retryDefinition, circuitBreaker, fallbackRunner, Executors.newScheduledThreadPool(5), timeout); + } else { + return new SyncFailsafeExecutionPlan(retryDefinition, circuitBreaker, fallbackRunner); + } + } + }else { + if(fallbackRunner == null) { + return new BasicExecutionPlan(); + } + else { + return new FallbackOnlyExecutionPlan(fallbackRunner); } } }); } + private boolean enableNonFallbacksForMicroProfile() { + try { + Class.forName("org.eclipse.microprofile.config.Config"); + Config config = ConfigProvider.getConfig(); + AtomicBoolean disableExecutions = new AtomicBoolean(true); + config.getOptionalValue("MP_Fault_Tolerance_NonFallback_Enabled", Boolean.class) + .ifPresent(disableExecutions::set); + return disableExecutions.get(); + } catch (ClassNotFoundException e) { + return true; + } + } + private FailsafeRetryDefinition createDefinition(String name, Method method) { Retry retry = AnnotationUtil.getAnnotation(method, Retry.class); if (retry == null) { http://git-wip-us.apache.org/repos/asf/geronimo-safeguard/blob/a43eeec7/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/FallbackOnlyExecutionPlan.java ---------------------------------------------------------------------- diff --git a/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/FallbackOnlyExecutionPlan.java b/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/FallbackOnlyExecutionPlan.java new file mode 100644 index 0000000..57f56e7 --- /dev/null +++ b/safeguard-impl/src/main/java/org/apache/safeguard/impl/executionPlans/FallbackOnlyExecutionPlan.java @@ -0,0 +1,43 @@ +/* + * 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 org.apache.safeguard.impl.fallback.FallbackRunner; + +import javax.interceptor.InvocationContext; +import java.util.concurrent.Callable; + +public class FallbackOnlyExecutionPlan implements ExecutionPlan { + private final FallbackRunner fallbackRunner; + + public FallbackOnlyExecutionPlan(FallbackRunner fallbackRunner) { + this.fallbackRunner = fallbackRunner; + } + + @Override + public T execute(Callable callable, InvocationContext invocationContext) { + try { + return callable.call(); + } + catch (Exception e) { + return (T)fallbackRunner.executeFallback(invocationContext); + } + } +} http://git-wip-us.apache.org/repos/asf/geronimo-safeguard/blob/a43eeec7/safeguard-tck-tests/pom.xml ---------------------------------------------------------------------- diff --git a/safeguard-tck-tests/pom.xml b/safeguard-tck-tests/pom.xml index 5479be5..dd51f13 100644 --- a/safeguard-tck-tests/pom.xml +++ b/safeguard-tck-tests/pom.xml @@ -56,6 +56,10 @@ org.apache.geronimo.specs geronimo-interceptor_1.2_spec + + org.apache.geronimo.config + geronimo-config-impl + @@ -73,7 +77,6 @@ org.eclipse.microprofile.fault.tolerance.tck.bulkhead.BulkheadFutureTest org.eclipse.microprofile.fault.tolerance.tck.bulkhead.BulkheadSynchRetryTest org.eclipse.microprofile.fault.tolerance.tck.bulkhead.BulkheadSynchTest - org.eclipse.microprofile.fault.tolerance.tck.disableEnv.DisableTest org.eclipse.microprofile.fault.tolerance.tck.illegalConfig.IncompatibleFallbackMethodTest org.eclipse.microprofile.fault.tolerance.tck.illegalConfig.IncompatibleFallbackMethodWithArgsTest org.eclipse.microprofile.fault.tolerance.tck.illegalConfig.IncompatibleFallbackTest