Skip to content

Commit

Permalink
Merge pull request #5315 from Pandrex247/FISH-758
Browse files Browse the repository at this point in the history
FISH-758 Fault Tolerance Annotations Not Applied to Rest Client Interfaces
  • Loading branch information
Pandrex247 committed Jun 23, 2021
2 parents 3eb9296 + 26dae3a commit dbbc941
Show file tree
Hide file tree
Showing 16 changed files with 832 additions and 86 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2019-2020 Payara Foundation and/or its affiliates. All rights reserved.
* Copyright (c) 2019-2021 Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
Expand Down Expand Up @@ -39,33 +39,29 @@
*/
package fish.payara.microprofile.faulttolerance.cdi;

import fish.payara.microprofile.faulttolerance.FaultToleranceConfig;
import fish.payara.microprofile.faulttolerance.FaultToleranceService;
import fish.payara.microprofile.faulttolerance.policy.FaultTolerancePolicy;
import fish.payara.microprofile.faulttolerance.service.Stereotypes;
import org.eclipse.microprofile.faulttolerance.exceptions.FaultToleranceDefinitionException;
import org.glassfish.internal.api.Globals;

import javax.enterprise.context.Dependent;
import javax.enterprise.context.control.RequestContextController;
import javax.enterprise.inject.Instance;
import javax.enterprise.inject.spi.BeanManager;
import javax.inject.Inject;
import javax.interceptor.InvocationContext;
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.annotation.Priority;
import javax.enterprise.context.control.RequestContextController;
import javax.enterprise.inject.Instance;
import javax.enterprise.inject.spi.BeanManager;
import javax.inject.Inject;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;

import fish.payara.microprofile.faulttolerance.FaultToleranceConfig;
import fish.payara.microprofile.faulttolerance.FaultToleranceService;
import fish.payara.microprofile.faulttolerance.policy.FaultTolerancePolicy;
import fish.payara.microprofile.faulttolerance.service.Stereotypes;
import org.eclipse.microprofile.faulttolerance.exceptions.FaultToleranceDefinitionException;
import org.glassfish.internal.api.Globals;

@Interceptor
@FaultTolerance
@Priority(Interceptor.Priority.PLATFORM_AFTER + 15)
@Dependent
public class FaultToleranceInterceptor implements Stereotypes, Serializable {

private static final Logger logger = Logger.getLogger(FaultToleranceInterceptor.class.getName());
Expand All @@ -78,8 +74,14 @@ public class FaultToleranceInterceptor implements Stereotypes, Serializable {

private FaultToleranceService faultToleranceService;

@AroundInvoke
protected static final String PAYARA_FAULT_TOLERANCE_INTERCEPTOR_EXECUTED =
"fish.payara.microprofile.faulttolerance.executed";

public Object intercept(InvocationContext context) throws Exception {
if (!shouldIntercept(context)) {
return context.proceed();
}
context.getContextData().put(PAYARA_FAULT_TOLERANCE_INTERCEPTOR_EXECUTED, Boolean.TRUE);
try {
initialize();
AtomicReference<FaultToleranceConfig> lazyConfig = new AtomicReference<>();
Expand Down Expand Up @@ -118,5 +120,16 @@ public Set<Annotation> getStereotypeDefinition(Class<? extends Annotation> stere
return beanManager.getStereotypeDefinition(stereotype);
}

protected boolean shouldIntercept(InvocationContext invocationContext) throws Exception {
Map<String, Object> contextData = invocationContext.getContextData();

if (contextData.get(PAYARA_FAULT_TOLERANCE_INTERCEPTOR_EXECUTED) != null &&
(Boolean) contextData.get(PAYARA_FAULT_TOLERANCE_INTERCEPTOR_EXECUTED)) {
return false;
}

return true;
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2021 Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://github.com/payara/Payara/blob/master/LICENSE.txt
* See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* The Payara Foundation designates this particular file as subject to the "Classpath"
* exception as provided by the Payara Foundation in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/

package fish.payara.microprofile.faulttolerance.cdi;

import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
* Tests for the {@link FaultToleranceExtension#determineInterceptorIndex(List, int)} method.
*/
public class DetermineInterceptorIndexTest {

@Test
public void determineInterceptorIndex() {
List<Class<?>> interceptors = new ArrayList<>();
interceptors.add(DummyInterceptor1k.class);
interceptors.add(DummyInterceptor2k.class);
interceptors.add(DummyInterceptor3k.class);
interceptors.add(DummyInterceptor4k.class);
interceptors.add(DummyInterceptor5k.class);

int index = FaultToleranceExtension.determineInterceptorIndex(interceptors, 3700);
Assert.assertTrue(index == 3);

index = FaultToleranceExtension.determineInterceptorIndex(interceptors, 6000);
Assert.assertTrue(index == 4);

index = FaultToleranceExtension.determineInterceptorIndex(interceptors, 5000);
Assert.assertTrue(index == 4);

index = FaultToleranceExtension.determineInterceptorIndex(interceptors, 2000);
Assert.assertTrue(index == 1);

index = FaultToleranceExtension.determineInterceptorIndex(interceptors, 500);
Assert.assertTrue(index == 0);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package fish.payara.microprofile.faulttolerance.cdi;

import javax.annotation.Priority;

@Priority(1000)
public class DummyInterceptor1k {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package fish.payara.microprofile.faulttolerance.cdi;

import javax.annotation.Priority;

@Priority(2000)
public class DummyInterceptor2k {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package fish.payara.microprofile.faulttolerance.cdi;

import javax.annotation.Priority;

@Priority(3000)
public class DummyInterceptor3k {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package fish.payara.microprofile.faulttolerance.cdi;

import javax.annotation.Priority;

@Priority(4000)
public class DummyInterceptor4k {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package fish.payara.microprofile.faulttolerance.cdi;

import javax.annotation.Priority;

@Priority(5000)
public class DummyInterceptor5k {
}
36 changes: 36 additions & 0 deletions appserver/tests/payara-samples/samples/microprofile-rc-ft/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>payara-samples-profiled-tests</artifactId>
<groupId>fish.payara.samples</groupId>
<version>5.2021.5-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>microprofile-rc-ft</artifactId>

<name>Payara Samples - Payara - Microprofile Rest Client Fault Tolerance</name>
<packaging>war</packaging>

<dependencies>
<dependency>
<groupId>fish.payara.samples</groupId>
<artifactId>samples-test-utils</artifactId>
</dependency>
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-api</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.microprofile.rest.client</groupId>
<artifactId>microprofile-rest-client-api</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.microprofile.fault-tolerance</groupId>
<artifactId>microprofile-fault-tolerance-api</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2019 Payara Foundation and/or its affiliates. All rights reserved.
* Copyright (c) 2021 Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
Expand Down Expand Up @@ -37,35 +37,31 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
package fish.payara.microprofile.faulttolerance.cdi;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
package fish.payara.samples.microprofile.restclient.faulttolerance;

import javax.interceptor.InterceptorBinding;
import org.eclipse.microprofile.rest.client.inject.RestClient;

import org.eclipse.microprofile.faulttolerance.Fallback;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("client")
@RequestScoped
public class ClientResource {
@Inject
@RestClient
private HelloService helloService;


@GET
@Produces(MediaType.TEXT_PLAIN)
public String helloWorld() {
// this method relies on the retry of the MP REST Client
return helloService.hello("World");
}

/**
* Added to methods at runtime in case they are affected by one of the FT annotations.
* This means a FT annotation is either present directly on the method or on the class declaring the method.
*
* This indirection is needed for two reasons:
*
* 1) Allow to process all FT annotations with a single interceptor
*
* 2) Allow to process {@link Fallback} even though it cannot be annotated on type level what would be needed to bind it
* to an interceptor directly.
*
* @author Jan Bernitt
*/
@Inherited
@InterceptorBinding
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.TYPE })
public @interface FaultTolerance {
//marker
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2021 Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://github.com/payara/Payara/blob/master/LICENSE.txt
* See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* The Payara Foundation designates this particular file as subject to the "Classpath"
* exception as provided by the Payara Foundation in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package fish.payara.samples.microprofile.restclient.faulttolerance;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.logging.Logger;

@Path("/hello")
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.TEXT_PLAIN)
public class HelloRemoteResource {

private static int counter = 0;
private static final Logger LOGGER = Logger.getLogger(HelloRemoteResource.class.getName());

@Path("{name}")
@GET
public String hello(@PathParam("name") String name) {
// check if we're down
LOGGER.info(() -> String.format("Counter value: %s", counter));
if (isDown()) {
LOGGER.warning(() -> String.format("Service unavailable, name: %s", name));
throw new ServiceUnavailableException();
}
LOGGER.info(() -> String.format("Service available, name: %s", name));
return "Hello " + name + "!";
}

private boolean isDown() {

return counter++ % 2 == 1;
}
}

0 comments on commit dbbc941

Please sign in to comment.