Skip to content
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

[Dubbo-7367]fix too many instance bean created #7438

Merged
merged 21 commits into from
Apr 2, 2021
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* 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.dubbo.config.annotation;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ReferenceAnnotationUtils {
public static String generateArrayEntryString(Map.Entry<String, Object> entry) {
String value;
String[] entryValues = (String[]) entry.getValue();
if ("parameters".equals(entry.getKey())) {
// parameters spec is {key1,value1,key2,value2}
ArrayList<String> kvList = new ArrayList<>();
for (int i = 0; i < entryValues.length / 2 * 2; i = i + 2) {
kvList.add(entryValues[i] + "=" + entryValues[i + 1]);
}
value = kvList.stream().sorted().collect(Collectors.joining(",", "[", "]"));
} else {
//other spec is {string1,string2,string3}
value = Arrays.stream(entryValues).sorted().collect(Collectors.joining(",", "[", "]"));
}
return value;
}

public static String generateMethodsString(Method[] methods) {
if (methods.length == 0) {
return null;
}
List<String> methodList = new ArrayList<>();
for (Method method : methods) {
Map<String, Object> methodMap = new HashMap<>();
methodMap.put("name", method.name());
methodMap.put("timeout", method.timeout());
methodMap.put("retries", method.retries());
methodMap.put("loadbalance", method.loadbalance());
methodMap.put("async", method.async());
methodMap.put("actives", method.actives());
methodMap.put("executes", method.executes());
methodMap.put("deprecated", method.deprecated());
methodMap.put("sticky", method.sticky());
methodMap.put("isReturn", method.isReturn());
methodMap.put("oninvoke", method.oninvoke());
methodMap.put("onreturn", method.onreturn());
methodMap.put("onthrow", method.onthrow());
methodMap.put("cache", method.cache());
methodMap.put("validation", method.validation());
methodMap.put("merger", method.merger());
methodMap.put("arguments", generateArgumentsString(method.arguments()));
methodList.add(convertToString(methodMap, "@Method("));
}
return methodList.stream().sorted().collect(Collectors.joining(",", "[", "]"));
}

private static String generateArgumentsString(Argument[] arguments) {
if (arguments.length == 0) {
return null;
}
List<String> argumentList = new ArrayList<>();
for (Argument argument : arguments) {
Map<String, Object> argMap = new HashMap<>();
argMap.put("index", argument.index());
argMap.put("type", argument.type());
argMap.put("callback", argument.callback());
argumentList.add(convertToString(argMap, "@Argument("));
}
return argumentList.stream().sorted().collect(Collectors.joining(",", "[", "]"));
}

private static String convertToString(Map<String, Object> map, String prefix) {
return map.entrySet().stream()
.filter(e -> e.getValue() != null && String.valueOf(e.getValue()).length() > 0)
.map(e -> e.getKey() + "=" + e.getValue())
.sorted()
.collect(Collectors.joining(",", prefix, ")"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
*/
package org.apache.dubbo.config.spring.beans.factory.annotation;

import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.ReferenceAnnotationUtils;
import org.apache.dubbo.config.annotation.Service;
import org.apache.dubbo.config.spring.ReferenceBean;
import org.apache.dubbo.config.spring.ServiceBean;
Expand All @@ -31,15 +34,21 @@
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.annotation.AnnotationAttributes;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;

import static com.alibaba.spring.util.AnnotationUtils.getAttribute;
import static com.alibaba.spring.util.AnnotationUtils.getAttributes;
Expand All @@ -56,7 +65,9 @@
* @since 2.5.7
*/
public class ReferenceAnnotationBeanPostProcessor extends AbstractAnnotationBeanPostProcessor implements
ApplicationContextAware {
ApplicationContextAware, ApplicationListener {

private static final Logger logger = LoggerFactory.getLogger(ReferenceAnnotationBeanPostProcessor.class);

/**
* The bean name of {@link ReferenceAnnotationBeanPostProcessor}
Expand All @@ -79,6 +90,8 @@ public class ReferenceAnnotationBeanPostProcessor extends AbstractAnnotationBean

private ApplicationContext applicationContext;

private static Map<String, TreeSet<String>> referencedBeanNameIdx = new HashMap<>();

/**
* {@link com.alibaba.dubbo.config.annotation.Reference @com.alibaba.dubbo.config.annotation.Reference} has been supported since 2.7.3
* <p>
Expand Down Expand Up @@ -131,6 +144,8 @@ protected Object doGetInjectedBean(AnnotationAttributes attributes, Object bean,
*/
String referenceBeanName = getReferenceBeanName(attributes, injectedType);

referencedBeanNameIdx.computeIfAbsent(referencedBeanName, k -> new TreeSet<String>()).add(referenceBeanName);

ReferenceBean referenceBean = buildReferenceBeanIfAbsent(referenceBeanName, attributes, injectedType);

boolean localServiceBean = isLocalServiceBean(referencedBeanName, referenceBean, attributes);
Expand Down Expand Up @@ -212,9 +227,17 @@ private String generateReferenceBeanName(AnnotationAttributes attributes, Class<
if (!attributes.isEmpty()) {
beanNameBuilder.append('(');
for (Map.Entry<String, Object> entry : attributes.entrySet()) {
String value;
if (isArrayOf(entry.getValue(), String.class)) {
value = ReferenceAnnotationUtils.generateArrayEntryString(entry);
} else if (isArrayOf(entry.getValue(), org.apache.dubbo.config.annotation.Method.class)) {
value = ReferenceAnnotationUtils.generateMethodsString((org.apache.dubbo.config.annotation.Method[]) entry.getValue());
} else {
value = String.valueOf(entry.getValue());
}
beanNameBuilder.append(entry.getKey())
.append('=')
.append(entry.getValue())
.append(value)
.append(',');
}
// replace the latest "," to be ")"
Expand All @@ -226,6 +249,10 @@ private String generateReferenceBeanName(AnnotationAttributes attributes, Class<
return beanNameBuilder.toString();
}

private boolean isArrayOf(Object value, Class type) {
return value.getClass().isArray() && value.getClass().getComponentType() == type;
}

/**
* Is Local Service bean or not?
*
Expand Down Expand Up @@ -344,4 +371,15 @@ public void destroy() throws Exception {
this.injectedFieldReferenceBeanCache.clear();
this.injectedMethodReferenceBeanCache.clear();
}

@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ContextRefreshedEvent) {
referencedBeanNameIdx.entrySet().stream().filter(e -> e.getValue().size() > 1).forEach(e -> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

release referencedBeanNameIdx resource after app started.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

String logPrefix = e.getKey() + " has " + e.getValue().size() + " reference instances, there are: ";
logger.warn(e.getValue().stream().collect(Collectors.joining(", ", logPrefix, "")));
});
referencedBeanNameIdx.clear();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ public ReferenceAnnotationBeanPostProcessor referenceAnnotationBeanPostProcessor
@Reference
private HelloService helloService2;

@Reference(check = false, parameters = {"a", "1"}, filter = {"echo"})
private HelloService helloServiceWithArray1;

@Reference(check = false, parameters = {"a", "1"}, filter = {"echo"})
private HelloService helloServiceWithArray2;


@Test
public void test() throws Exception {

Expand Down Expand Up @@ -177,7 +184,7 @@ public void testGetReferenceBeans() {

Collection<ReferenceBean<?>> referenceBeans = beanPostProcessor.getReferenceBeans();

Assertions.assertEquals(4, referenceBeans.size());
Assertions.assertEquals(5, referenceBeans.size());

ReferenceBean<?> referenceBean = referenceBeans.iterator().next();

Expand All @@ -194,7 +201,7 @@ public void testGetInjectedFieldReferenceBeanMap() {
Map<InjectionMetadata.InjectedElement, ReferenceBean<?>> referenceBeanMap =
beanPostProcessor.getInjectedFieldReferenceBeanMap();

Assertions.assertEquals(3, referenceBeanMap.size());
Assertions.assertEquals(5, referenceBeanMap.size());

for (Map.Entry<InjectionMetadata.InjectedElement, ReferenceBean<?>> entry : referenceBeanMap.entrySet()) {

Expand Down Expand Up @@ -311,7 +318,7 @@ public void testReferenceBeansMethodAnnotation() {

Collection<ReferenceBean<?>> referenceBeans = beanPostProcessor.getReferenceBeans();

Assertions.assertEquals(4, referenceBeans.size());
Assertions.assertEquals(5, referenceBeans.size());

ReferenceBean<?> referenceBean = referenceBeans.iterator().next();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ protected void subscribeURLs(URL url, NotifyListener listener, Set<String> servi
serviceListener.addListener(protocolServiceKey, listener);
registerServiceInstancesChangedListener(url, serviceListener);


// FIXME: This will cause redundant duplicate notifications
serviceNames.forEach(serviceName -> {
List<ServiceInstance> serviceInstances = serviceDiscovery.getInstances(serviceName);
if (CollectionUtils.isNotEmpty(serviceInstances)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ public class ServiceInstancesChangedListener implements ConditionalEventListener

private final Set<String> serviceNames;
private final ServiceDiscovery serviceDiscovery;
private final String registryId;
private URL url;
private Map<String, NotifyListener> listeners;
private Map<String, List<NotifyListener>> listeners;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

List改成Set

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK~


private Map<String, List<ServiceInstance>> allInstances;

Expand All @@ -73,6 +74,7 @@ public class ServiceInstancesChangedListener implements ConditionalEventListener
public ServiceInstancesChangedListener(Set<String> serviceNames, ServiceDiscovery serviceDiscovery) {
this.serviceNames = serviceNames;
this.serviceDiscovery = serviceDiscovery;
this.registryId = serviceDiscovery.getUrl().getParameter("id");
this.listeners = new HashMap<>();
this.allInstances = new HashMap<>();
this.serviceUrls = new HashMap<>();
Expand Down Expand Up @@ -188,9 +190,10 @@ private MetadataInfo getMetadataInfo(ServiceInstance instance) {
}

private void notifyAddressChanged() {
listeners.forEach((key, notifyListener) -> {
//FIXME, group wildcard match
notifyListener.notify(toUrlsWithEmpty(serviceUrls.get(key)));
listeners.forEach((key, notifyListeners) -> {
notifyListeners.forEach(notifyListener -> {
notifyListener.notify(toUrlsWithEmpty(serviceUrls.get(key)));
});
});
}

Expand All @@ -202,7 +205,7 @@ private List<URL> toUrlsWithEmpty(List<URL> urls) {
}

public void addListener(String serviceKey, NotifyListener listener) {
this.listeners.put(serviceKey, listener);
this.listeners.computeIfAbsent(serviceKey, k -> new ArrayList<>()).add(listener);
}

public void removeListener(String serviceKey) {
Expand Down Expand Up @@ -241,6 +244,10 @@ public final boolean accept(ServiceInstancesChangedEvent event) {
return serviceNames.contains(event.getServiceName());
}

public String getRegistryId() {
return registryId;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand All @@ -250,11 +257,11 @@ public boolean equals(Object o) {
return false;
}
ServiceInstancesChangedListener that = (ServiceInstancesChangedListener) o;
return Objects.equals(getServiceNames(), that.getServiceNames());
return Objects.equals(getServiceNames(), that.getServiceNames()) && Objects.equals(getRegistryId(), that.getRegistryId());
}

@Override
public int hashCode() {
return Objects.hash(getClass(), getServiceNames());
return Objects.hash(getClass(), getServiceNames(), getRegistryId());
}
}