Skip to content
Permalink
Browse files
GERONIMO-6582 - Adding more execution plans.
  • Loading branch information
johnament committed Sep 13, 2017
1 parent cf5a000 commit 7c93e298388dec63bd1835d71c703fb3507fa332
Showing 13 changed files with 471 additions and 54 deletions.
@@ -0,0 +1,33 @@
/*
* 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.api;

import org.apache.safeguard.api.circuitbreaker.CircuitBreakerManager;
import org.apache.safeguard.api.retry.RetryManager;

import java.util.concurrent.Callable;

public interface ExecutionManager {
<T> T execute(String name, Callable<T> callable);

CircuitBreakerManager getCircuitBreakerManager();

RetryManager getRetryManager();
}
@@ -0,0 +1,26 @@
/*
* 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.exception;

public class AsyncException extends RuntimeException {
public AsyncException(Exception e) {
super(e);
}
}
@@ -19,16 +19,19 @@

package org.apache.safeguard.impl;

import org.apache.safeguard.api.ExecutionManager;
import org.apache.safeguard.impl.circuitbreaker.FailsafeCircuitBreakerManager;
import org.apache.safeguard.impl.executionPlans.ExecutionPlanFactory;
import org.apache.safeguard.impl.retry.FailsafeRetryManager;

import javax.enterprise.inject.Vetoed;
import javax.interceptor.InvocationContext;
import java.lang.reflect.Method;
import java.time.Duration;
import java.util.concurrent.Callable;

@Vetoed
public class FailsafeExecutionManager {
public class FailsafeExecutionManager implements ExecutionManager {
private FailsafeCircuitBreakerManager circuitBreakerManager;
private FailsafeRetryManager retryManager;
private ExecutionPlanFactory executionPlanFactory;
@@ -42,47 +45,33 @@ public FailsafeExecutionManager() {
public Object execute(InvocationContext invocationContext) {
Method method = invocationContext.getMethod();
return executionPlanFactory.locateExecutionPlan(method).execute(invocationContext::proceed);
// String name = NamingUtil.createName(method);
// FailsafeRetryDefinition failsafeRetryDefinition = retryManager.getRetryDefinition(name);
// if (failsafeRetryDefinition == null) {
// failsafeRetryDefinition = createDefinition(name, method);
// }
// return Failsafe.with(failsafeRetryDefinition.getRetryPolicy()).get(invocationContext::proceed);
}

@Override
public <T> T execute(String name, Callable<T> callable) {
return executionPlanFactory.locateExecutionPlan(name).execute(callable);
// FailsafeRetryDefinition failsafeRetryDefinition = retryManager.getRetryDefinition(name);
// return Failsafe.with(failsafeRetryDefinition.getRetryPolicy()).get(callable);
return executionPlanFactory.locateExecutionPlan(name, null, false).execute(callable);
}

public <T> T executeAsync(String name, Callable<T> callable) {
return executionPlanFactory.locateExecutionPlan(name, null, true).execute(callable);
}

public <T> T executeAsync(String name, Callable<T> callable, Duration timeout) {
return executionPlanFactory.locateExecutionPlan(name, timeout, true).execute(callable);
}

public ExecutionPlanFactory getExecutionPlanFactory() {
return executionPlanFactory;
}

@Override
public FailsafeCircuitBreakerManager getCircuitBreakerManager() {
return circuitBreakerManager;
}

@Override
public FailsafeRetryManager getRetryManager() {
return retryManager;
}

// private FailsafeRetryDefinition createDefinition(String name, Method method) {
// Retry retry = AnnotationUtil.getAnnotation(method, Retry.class);
// if(retry == null) {
// return null;
// }
// FailsafeRetryBuilder retryBuilder = retryManager.newRetryDefinition(name);
// return mapRetry(retry, retryBuilder);
// }

// private FailsafeCircuitBreakerDefinition createCBDefinition(String name, Method method) {
// CircuitBreaker circuitBreaker = AnnotationUtil.getAnnotation(method, CircuitBreaker.class);
// if (circuitBreaker == null) {
// return null;
// }
// FailsafeCircuitBreakerBuilder circuitBreakerBuilder = this.circuitBreakerManager.newCircuitBreaker(name);
// return mapCircuitBreaker(circuitBreaker, circuitBreakerBuilder);
// }
}
@@ -0,0 +1,59 @@
/*
* 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 net.jodah.failsafe.AsyncFailsafe;
import org.apache.safeguard.impl.circuitbreaker.FailsafeCircuitBreaker;
import org.apache.safeguard.impl.retry.FailsafeRetryDefinition;

import java.time.Duration;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class AsyncFailsafeExecutionPlan extends SyncFailsafeExecutionPlan {
private final ScheduledExecutorService executorService;
private final Duration timeout;

public AsyncFailsafeExecutionPlan(FailsafeRetryDefinition retryDefinition,
FailsafeCircuitBreaker failsafeCircuitBreaker,
ScheduledExecutorService executorService,
Duration timeout) {
super(retryDefinition, failsafeCircuitBreaker);
this.executorService = executorService;
this.timeout = timeout;
}

@Override
public <T> T execute(Callable<T> callable) {
AsyncFailsafe<?> asyncFailsafe = getSyncFailsafe().with(executorService);
try {
if (this.timeout == null) {
return asyncFailsafe.get(callable).get();
} else {
return asyncFailsafe.get(callable).get(timeout.toMillis(), TimeUnit.MILLISECONDS);
}
} catch (TimeoutException | InterruptedException | ExecutionException e) {
throw new org.eclipse.microprofile.faulttolerance.exceptions.TimeoutException(e);
}
}
}
@@ -0,0 +1,45 @@
/*
* 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.exception.AsyncException;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

class AsyncOnlyExecutionPlan implements ExecutionPlan {
private final ExecutorService executorService;

AsyncOnlyExecutionPlan(ExecutorService executorService) {
this.executorService = executorService;
}

@Override
public <T> T execute(Callable<T> callable) {
Future<T> submitted = executorService.submit(callable);
try {
return submitted.get();
} catch (InterruptedException | ExecutionException e) {
throw new AsyncException(e);
}
}
}
@@ -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.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

class AsyncTimeoutExecutionPlan implements ExecutionPlan {
private final Duration timeout;
private final ExecutorService executorService;

AsyncTimeoutExecutionPlan(Duration timeout, ExecutorService executorService) {
this.timeout = timeout;
this.executorService = executorService;
}

@Override
public <T> T execute(Callable<T> callable) {
Future<T> future = executorService.submit(callable);
try {
return future.get(timeout.toMillis(), TimeUnit.MILLISECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
throw new org.eclipse.microprofile.faulttolerance.exceptions.TimeoutException(e);
}
}
}
@@ -0,0 +1,26 @@
/*
* 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.util.concurrent.Callable;

public interface ExecutionPlan {
<T> T execute(Callable<T> callable);
}

0 comments on commit 7c93e29

Please sign in to comment.