-
Notifications
You must be signed in to change notification settings - Fork 833
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix metric disable when using the constructor injection of the Grpc Client #907
Open
SOOHYUN-LIM
wants to merge
11
commits into
grpc-ecosystem:master
Choose a base branch
from
SOOHYUN-LIM:fix-GrpcClientBeanPostProcessor
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+273
−12
Open
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b1b5f41
Change BeanPostProcessor to InstantiationAwareBeanPostProcessor in Gr…
Lim-Soohyun f7b2918
Merge branch 'master' into fix-GrpcClientBeanPostProcessor
ST-DDT 5e4b477
Missing Bean Registration for @GrpcClientBean
Lim-Soohyun 4c26935
Add LoadTimeWeaverAware and test
Lim-Soohyun eb84d2c
Code Formatting
Lim-Soohyun eac7be9
Merge branch 'master' into fix-GrpcClientBeanPostProcessor
SOOHYUN-LIM e797998
Code Formatting
Lim-Soohyun 020170f
Merge remote-tracking branch 'origin/master' into fix-GrpcClientBeanP…
SOOHYUN-LIM 3f3bd29
Revert formatting changes.
SOOHYUN-LIM 722604a
Merge branch 'master' into fix-GrpcClientBeanPostProcessor
ST-DDT ade0a79
Merge branch 'master' into fix-GrpcClientBeanPostProcessor
yidongnan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,33 +18,52 @@ | |
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.beans.PropertyDescriptor; | ||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.AccessibleObject; | ||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Member; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Modifier; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import org.springframework.beans.BeanInstantiationException; | ||
import org.springframework.beans.BeanUtils; | ||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.InvalidPropertyException; | ||
import org.springframework.beans.PropertyValues; | ||
import org.springframework.beans.factory.BeanCreationException; | ||
import org.springframework.beans.factory.BeanDefinitionStoreException; | ||
import org.springframework.beans.factory.NoSuchBeanDefinitionException; | ||
import org.springframework.beans.factory.annotation.InjectionMetadata; | ||
import org.springframework.beans.factory.config.BeanPostProcessor; | ||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; | ||
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor; | ||
import org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor; | ||
import org.springframework.beans.factory.support.RootBeanDefinition; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.BridgeMethodResolver; | ||
import org.springframework.core.annotation.AnnotationUtils; | ||
import org.springframework.core.annotation.MergedAnnotation; | ||
import org.springframework.core.annotation.MergedAnnotations; | ||
import org.springframework.lang.Nullable; | ||
import org.springframework.util.ClassUtils; | ||
import org.springframework.util.ReflectionUtils; | ||
import org.springframework.util.StringUtils; | ||
|
||
import com.google.common.collect.Lists; | ||
|
||
import io.grpc.Channel; | ||
import io.grpc.ClientInterceptor; | ||
import io.grpc.stub.AbstractStub; | ||
import jakarta.annotation.PostConstruct; | ||
import net.devh.boot.grpc.client.channelfactory.GrpcChannelFactory; | ||
import net.devh.boot.grpc.client.nameresolver.NameResolverRegistration; | ||
import net.devh.boot.grpc.client.stubfactory.FallbackStubFactory; | ||
|
@@ -57,7 +76,8 @@ | |
* @author Michael ([email protected]) | ||
* @author Daniel Theuke ([email protected]) | ||
*/ | ||
public class GrpcClientBeanPostProcessor implements BeanPostProcessor { | ||
public class GrpcClientBeanPostProcessor | ||
implements InstantiationAwareBeanPostProcessor, MergedBeanDefinitionPostProcessor { | ||
|
||
private final ApplicationContext applicationContext; | ||
|
||
|
@@ -70,25 +90,26 @@ public class GrpcClientBeanPostProcessor implements BeanPostProcessor { | |
// For bean registration via @GrpcClientBean | ||
private ConfigurableListableBeanFactory configurableBeanFactory; | ||
|
||
private final Set<Class<? extends Annotation>> grpcClientAnnotationTypes = new LinkedHashSet<>(4); | ||
|
||
private final Map<String, InjectionMetadata> injectionMetadataCache = new ConcurrentHashMap<>(256); | ||
|
||
/** | ||
* Creates a new GrpcClientBeanPostProcessor with the given ApplicationContext. | ||
* Creates a new GrpcClientBeanPostProcessor with the given ApplicationContext for GrpcClient standard | ||
* {@link GrpcClient @GrpcClient} annotation. | ||
* | ||
* @param applicationContext The application context that will be used to get lazy access to the | ||
* {@link GrpcChannelFactory} and {@link StubTransformer}s. | ||
*/ | ||
public GrpcClientBeanPostProcessor(final ApplicationContext applicationContext) { | ||
this.applicationContext = requireNonNull(applicationContext, "applicationContext"); | ||
} | ||
|
||
@PostConstruct | ||
public void init() { | ||
initGrpClientConstructorInjections(); | ||
this.grpcClientAnnotationTypes.add(GrpcClient.class); | ||
} | ||
|
||
/** | ||
* Triggers registering grpc client beans from GrpcClientConstructorInjection. | ||
*/ | ||
private void initGrpClientConstructorInjections() { | ||
public void initGrpClientConstructorInjections() { | ||
Iterable<GrpcClientConstructorInjection.Registry> registries; | ||
try { | ||
registries = getConfigurableBeanFactory().getBean(GrpcClientConstructorInjection.class).getRegistries(); | ||
|
@@ -120,9 +141,6 @@ private void initGrpClientConstructorInjections() { | |
public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { | ||
Class<?> clazz = bean.getClass(); | ||
do { | ||
processFields(clazz, bean); | ||
processMethods(clazz, bean); | ||
|
||
if (isAnnotatedWithConfiguration(clazz)) { | ||
processGrpcClientBeansAnnotations(clazz); | ||
} | ||
|
@@ -132,6 +150,19 @@ public Object postProcessBeforeInitialization(final Object bean, final String be | |
return bean; | ||
} | ||
|
||
@Override | ||
public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) { | ||
InjectionMetadata metadata = findGrpcClientMetadata(beanName, bean.getClass(), pvs); | ||
try { | ||
metadata.inject(bean, beanName, pvs); | ||
} catch (BeanCreationException ex) { | ||
throw ex; | ||
} catch (Throwable ex) { | ||
throw new BeanCreationException(beanName, "Injection of gRPC client stub failed", ex); | ||
} | ||
return pvs; | ||
} | ||
|
||
/** | ||
* Processes the bean's fields in the given class. | ||
* | ||
|
@@ -398,4 +429,110 @@ private boolean isAnnotatedWithConfiguration(final Class<?> clazz) { | |
return configurationAnnotation != null; | ||
} | ||
|
||
@Override | ||
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) { | ||
InjectionMetadata metadata = findGrpcClientMetadata(beanName, beanType, null); | ||
metadata.checkConfigMembers(beanDefinition); | ||
} | ||
|
||
private InjectionMetadata findGrpcClientMetadata(String beanName, Class<?> clazz, @Nullable PropertyValues pvs) { | ||
// Fall back to class name as cache key, for backwards compatibility with custom callers. | ||
String cacheKey = (StringUtils.hasLength(beanName) ? beanName : clazz.getName()); | ||
// Quick check on the concurrent map first, with minimal locking. | ||
InjectionMetadata metadata = this.injectionMetadataCache.get(cacheKey); | ||
if (InjectionMetadata.needsRefresh(metadata, clazz)) { | ||
synchronized (this.injectionMetadataCache) { | ||
metadata = this.injectionMetadataCache.get(cacheKey); | ||
if (InjectionMetadata.needsRefresh(metadata, clazz)) { | ||
if (metadata != null) { | ||
metadata.clear(pvs); | ||
} | ||
metadata = buildGrpcClientMetadata(clazz); | ||
this.injectionMetadataCache.put(cacheKey, metadata); | ||
} | ||
} | ||
} | ||
return metadata; | ||
} | ||
|
||
private InjectionMetadata buildGrpcClientMetadata(Class<?> clazz) { | ||
if (!AnnotationUtils.isCandidateClass(clazz, this.grpcClientAnnotationTypes)) { | ||
return InjectionMetadata.EMPTY; | ||
} | ||
|
||
List<InjectionMetadata.InjectedElement> elements = new ArrayList<>(); | ||
Class<?> targetClass = clazz; | ||
|
||
do { | ||
final List<InjectionMetadata.InjectedElement> currElements = new ArrayList<>(); | ||
|
||
ReflectionUtils.doWithLocalFields(targetClass, field -> { | ||
MergedAnnotation<?> ann = findGrpcClientAnnotation(field); | ||
if (ann != null) { | ||
if (Modifier.isStatic(field.getModifiers())) { | ||
throw new IllegalStateException( | ||
"GrpcClient annotation is not supported on static fields: " + field); | ||
} | ||
currElements.add(new GrpcClientMemberElement(field, null)); | ||
} | ||
}); | ||
|
||
ReflectionUtils.doWithLocalMethods(targetClass, method -> { | ||
Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method); | ||
if (!BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod)) { | ||
return; | ||
} | ||
MergedAnnotation<?> ann = findGrpcClientAnnotation(bridgedMethod); | ||
if (ann != null && method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) { | ||
if (Modifier.isStatic(method.getModifiers())) { | ||
throw new IllegalStateException( | ||
"GrpcClient annotation is not supported on static method: " + method); | ||
} | ||
if (method.getParameterCount() == 0) { | ||
throw new IllegalStateException( | ||
"GrpcClient annotation should only be used on methods with parameters: " + method); | ||
} | ||
PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz); | ||
currElements.add(new GrpcClientMemberElement(method, pd)); | ||
} | ||
}); | ||
|
||
elements.addAll(0, currElements); | ||
targetClass = targetClass.getSuperclass(); | ||
} while (targetClass != null && targetClass != Object.class); | ||
|
||
return InjectionMetadata.forElements(elements, clazz); | ||
} | ||
|
||
private MergedAnnotation<?> findGrpcClientAnnotation(AccessibleObject ao) { | ||
MergedAnnotations annotations = MergedAnnotations.from(ao); | ||
for (Class<? extends Annotation> type : this.grpcClientAnnotationTypes) { | ||
MergedAnnotation<?> annotation = annotations.get(type); | ||
if (annotation.isPresent()) { | ||
return annotation; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Class representing injection information about an annotated member. | ||
*/ | ||
private class GrpcClientMemberElement extends InjectionMetadata.InjectedElement { | ||
|
||
public GrpcClientMemberElement(Member member, @Nullable PropertyDescriptor pd) { | ||
super(member, pd); | ||
} | ||
|
||
@Override | ||
protected void inject(Object bean, @Nullable String beanName, @Nullable PropertyValues pvs) throws Throwable { | ||
Class<?> clazz = bean.getClass(); | ||
do { | ||
processFields(clazz, bean); | ||
processMethods(clazz, bean); | ||
|
||
clazz = clazz.getSuperclass(); | ||
} while (clazz != null); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please revert these unrelated formatting changes.