add logger to critical path (#6744)

This commit is contained in:
ken.lj 2020-09-15 18:50:22 +08:00 committed by GitHub
parent 966b46799a
commit 4204be7b28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 102 additions and 29 deletions

View File

@ -1173,11 +1173,18 @@ public class DubboBootstrap extends GenericEventListener {
private void doRegisterServiceInstance(ServiceInstance serviceInstance) {
//FIXME
if (logger.isInfoEnabled()) {
logger.info("Start publishing metadata to remote center, this only makes sense for applications enabled remote metadata center.");
}
publishMetadataToRemote(serviceInstance);
logger.info("Start registering instance address to registry.");
getServiceDiscoveries().forEach(serviceDiscovery ->
{
calInstanceRevision(serviceDiscovery, serviceInstance);
if (logger.isDebugEnabled()) {
logger.info("Start registering instance address to registry" + serviceDiscovery.getUrl() + ", instance " + serviceInstance);
}
// register metadata
serviceDiscovery.register(serviceInstance);
});

View File

@ -260,6 +260,7 @@ public class ServiceDiscoveryRegistry implements Registry {
writableMetadataService.subscribeURL(url);
Set<String> serviceNames = getServices(url, listener);
if (CollectionUtils.isEmpty(serviceNames)) {
throw new IllegalStateException("Should has at least one way to know which services this interface belongs to, subscription url: " + url);
}
@ -363,12 +364,16 @@ public class ServiceDiscoveryRegistry implements Registry {
String serviceNames = subscribedURL.getParameter(PROVIDED_BY);
if (StringUtils.isNotEmpty(serviceNames)) {
logger.info(subscribedURL.getServiceInterface() + " mapping to " + serviceNames + " instructed by provided-by set by user.");
subscribedServices.addAll(parseServices(serviceNames));
}
if (isEmpty(subscribedServices)) {
subscribedServices.addAll(findMappedServices(subscribedURL, new DefaultMappingListener(subscribedURL, subscribedServices, listener)));
Set<String> mappedServices = findMappedServices(subscribedURL, new DefaultMappingListener(subscribedURL, subscribedServices, listener));
logger.info(subscribedURL.getServiceInterface() + " mapping to " + serviceNames + " instructed by remote metadata center.");
subscribedServices.addAll(mappedServices);
if (isEmpty(subscribedServices)) {
logger.info(subscribedURL.getServiceInterface() + " mapping to " + serviceNames + " by default.");
subscribedServices.addAll(getSubscribedServices());
}
}

View File

@ -88,6 +88,9 @@ public class ServiceInstancesChangedListener implements ConditionalEventListener
logger.info("Received instance notification, serviceName: " + event.getServiceName() + ", instances: " + event.getServiceInstances().size());
String appName = event.getServiceName();
allInstances.put(appName, event.getServiceInstances());
if (logger.isDebugEnabled()) {
logger.debug(event.getServiceInstances().toString());
}
Map<String, List<ServiceInstance>> revisionToInstances = new HashMap<>();
Map<String, Set<String>> localServiceToRevisions = new HashMap<>();
@ -161,6 +164,9 @@ public class ServiceInstancesChangedListener implements ConditionalEventListener
instance.getExtendParams().putIfAbsent(REGISTRY_CLUSTER_KEY, RegistryClusterIdentifier.getExtension(url).consumerKey(url));
MetadataInfo metadataInfo;
try {
if (logger.isDebugEnabled()) {
logger.info("Instance " + instance.getAddress() + " is using metadata type " + metadataType);
}
if (REMOTE_METADATA_STORAGE_TYPE.equals(metadataType)) {
RemoteMetadataServiceImpl remoteMetadataService = MetadataUtils.getRemoteMetadataService();
metadataInfo = remoteMetadataService.getMetadata(instance);
@ -168,6 +174,9 @@ public class ServiceInstancesChangedListener implements ConditionalEventListener
MetadataService metadataServiceProxy = MetadataUtils.getMetadataServiceProxy(instance, serviceDiscovery);
metadataInfo = metadataServiceProxy.getMetadataInfo(ServiceInstanceMetadataUtils.getExportedServicesRevision(instance));
}
if (logger.isDebugEnabled()) {
logger.info("Metadata " + metadataInfo.toString());
}
} catch (Exception e) {
logger.error("Failed to load service metadata, metadta type is " + metadataType, e);
metadataInfo = null;

View File

@ -69,6 +69,10 @@ public class RemoteMetadataServiceImpl {
if (metadataReport == null) {
metadataReport = getMetadataReports().entrySet().iterator().next().getValue();
}
logger.info("Publishing metadata to " + metadataReport.getClass().getSimpleName());
if (logger.isDebugEnabled()) {
logger.debug(metadataInfo.toString());
}
metadataReport.publishAppMetadata(identifier, metadataInfo);
metadataInfo.markReported();
}

View File

@ -34,9 +34,11 @@ public class DefaultMigrationAddressComparator implements MigrationAddressCompar
@Override
public <T> boolean shouldMigrate(ClusterInvoker<T> serviceDiscoveryInvoker, ClusterInvoker<T> invoker) {
if (!serviceDiscoveryInvoker.isAvailable()) {
logger.info("No instance address available, will not migrate.");
return false;
}
if (!invoker.isAvailable()) {
logger.info("No interface address available, will migrate.");
return true;
}
@ -55,6 +57,8 @@ public class DefaultMigrationAddressComparator implements MigrationAddressCompar
threshold = DEFAULT_THREAD;
}
logger.info("Instance address size " + newAddressSize + ", interface address size " + oldAddressSize + ", threshold " + threshold);
if (newAddressSize != 0 && oldAddressSize == 0) {
return true;
}

View File

@ -18,6 +18,8 @@ package org.apache.dubbo.registry.client.migration;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.registry.Registry;
import org.apache.dubbo.registry.client.migration.model.MigrationStep;
@ -35,8 +37,10 @@ import java.util.Set;
import static org.apache.dubbo.rpc.cluster.Constants.REFER_KEY;
public class MigrationInvoker<T> implements MigrationClusterInvoker<T> {
private Logger logger = LoggerFactory.getLogger(MigrationInvoker.class);
private URL url;
private URL consumerUrl;
private Cluster cluster;
private Registry registry;
private Class<T> type;
@ -50,8 +54,9 @@ public class MigrationInvoker<T> implements MigrationClusterInvoker<T> {
Cluster cluster,
Registry registry,
Class<T> type,
URL url) {
this(null, null, registryProtocol, cluster, registry, type, url);
URL url,
URL consumerUrl) {
this(null, null, registryProtocol, cluster, registry, type, url, consumerUrl);
}
public MigrationInvoker(ClusterInvoker<T> invoker,
@ -60,7 +65,8 @@ public class MigrationInvoker<T> implements MigrationClusterInvoker<T> {
Cluster cluster,
Registry registry,
Class<T> type,
URL url) {
URL url,
URL consumerUrl) {
this.invoker = invoker;
this.serviceDiscoveryInvoker = serviceDiscoveryInvoker;
this.registryProtocol = registryProtocol;
@ -68,6 +74,7 @@ public class MigrationInvoker<T> implements MigrationClusterInvoker<T> {
this.registry = registry;
this.type = type;
this.url = url;
this.consumerUrl = consumerUrl;
}
public ClusterInvoker<T> getInvoker() {
@ -142,24 +149,18 @@ public class MigrationInvoker<T> implements MigrationClusterInvoker<T> {
@Override
public Result invoke(Invocation invocation) throws RpcException {
if (!checkInvokerAvailable(serviceDiscoveryInvoker)) {
logger.debug("Using interface addresses to handle invocation, interface " + type.getName() + ", total address size " + invoker.getDirectory().getAllInvokers().size());
return invoker.invoke(invocation);
}
if (!checkInvokerAvailable(invoker)) {
logger.debug("Using instance addresses to handle invocation, interface " + type.getName() + ", total address size " + serviceDiscoveryInvoker.getDirectory().getAllInvokers().size());
return serviceDiscoveryInvoker.invoke(invocation);
}
return currentAvailableInvoker.invoke(invocation);
}
@Override
public URL getUrl() {
if (invoker != null) {
return invoker.getUrl();
}
return serviceDiscoveryInvoker.getUrl();
}
@Override
public boolean isAvailable() {
return (invoker != null && invoker.isAvailable())
@ -176,20 +177,35 @@ public class MigrationInvoker<T> implements MigrationClusterInvoker<T> {
}
}
@Override
public URL getUrl() {
if (invoker != null) {
return invoker.getUrl();
} else if (serviceDiscoveryInvoker != null) {
return serviceDiscoveryInvoker.getUrl();
}
return consumerUrl;
}
@Override
public URL getRegistryUrl() {
if (invoker != null) {
return invoker.getRegistryUrl();
} else if (serviceDiscoveryInvoker != null) {
serviceDiscoveryInvoker.getRegistryUrl();
}
return serviceDiscoveryInvoker.getRegistryUrl();
return url;
}
@Override
public Directory<T> getDirectory() {
if (invoker != null) {
return invoker.getDirectory();
} else if (serviceDiscoveryInvoker != null) {
return serviceDiscoveryInvoker.getDirectory();
}
return serviceDiscoveryInvoker.getDirectory();
return null;
}
@Override
@ -217,6 +233,9 @@ public class MigrationInvoker<T> implements MigrationClusterInvoker<T> {
private synchronized void compareAddresses() {
this.invokersChanged = true;
if (logger.isDebugEnabled()) {
logger.info("" + invoker.getDirectory().getAllInvokers().size());
}
Set<MigrationAddressComparator> detectors = ExtensionLoader.getExtensionLoader(MigrationAddressComparator.class).getSupportedExtensionInstances();
if (detectors != null && detectors.stream().allMatch(migrationDetector -> migrationDetector.shouldMigrate(serviceDiscoveryInvoker, invoker))) {
@ -229,6 +248,9 @@ public class MigrationInvoker<T> implements MigrationClusterInvoker<T> {
protected synchronized void destroyServiceDiscoveryInvoker() {
this.currentAvailableInvoker = invoker;
if (serviceDiscoveryInvoker != null) {
if (logger.isDebugEnabled()) {
logger.debug("Destroying instance address invokers, will not listen for address changes until re-subscribed, " + type.getName());
}
serviceDiscoveryInvoker.destroy();
serviceDiscoveryInvoker = null;
}
@ -237,12 +259,18 @@ public class MigrationInvoker<T> implements MigrationClusterInvoker<T> {
protected synchronized void discardServiceDiscoveryInvokerAddress() {
this.currentAvailableInvoker = invoker;
if (serviceDiscoveryInvoker != null) {
if (logger.isDebugEnabled()) {
logger.debug("Discarding instance addresses, total size " + serviceDiscoveryInvoker.getDirectory().getAllInvokers().size());
}
serviceDiscoveryInvoker.getDirectory().discordAddresses();
}
}
protected synchronized void refreshServiceDiscoveryInvoker() {
if (needRefresh(serviceDiscoveryInvoker)) {
if (logger.isDebugEnabled()) {
logger.debug("Re-subscribing instance addresses, current interface " + type.getName());
}
serviceDiscoveryInvoker = registryProtocol.getServiceDiscoveryInvoker(cluster, registry, type, url);
}
}
@ -250,6 +278,9 @@ public class MigrationInvoker<T> implements MigrationClusterInvoker<T> {
protected synchronized void refreshInterfaceInvoker() {
if (needRefresh(invoker)) {
// FIXME invoker.destroy();
if (logger.isDebugEnabled()) {
logger.debug("Re-subscribing interface addresses for interface " + type.getName());
}
invoker = registryProtocol.getInvoker(cluster, registry, type, url);
}
}
@ -257,6 +288,9 @@ public class MigrationInvoker<T> implements MigrationClusterInvoker<T> {
protected synchronized void destroyInterfaceInvoker() {
this.currentAvailableInvoker = serviceDiscoveryInvoker;
if (invoker != null) {
if (logger.isDebugEnabled()) {
logger.debug("Destroying interface address invokers, will not listen for address changes until re-subscribed, " + type.getName());
}
invoker.destroy();
invoker = null;
}
@ -265,6 +299,9 @@ public class MigrationInvoker<T> implements MigrationClusterInvoker<T> {
protected synchronized void discardInterfaceInvokerAddress() {
this.currentAvailableInvoker = serviceDiscoveryInvoker;
if (invoker != null) {
if (logger.isDebugEnabled()) {
logger.debug("Discarding interface addresses, total address size " + invoker.getDirectory().getAllInvokers().size());
}
invoker.getDirectory().discordAddresses();
}
}

View File

@ -16,6 +16,7 @@
*/
package org.apache.dubbo.registry.client.migration;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.config.configcenter.ConfigChangedEvent;
import org.apache.dubbo.common.config.configcenter.ConfigurationListener;
import org.apache.dubbo.common.config.configcenter.DynamicConfiguration;
@ -49,6 +50,7 @@ public class MigrationRuleListener implements RegistryProtocolListener, Configur
public MigrationRuleListener() {
this.configuration = ApplicationModel.getEnvironment().getDynamicConfiguration().orElseGet(null);
logger.info("Listening for migration rules on dataId-" + RULE_KEY + " group-" + DUBBO_SERVICEDISCOVERY_MIGRATION);
configuration.addListener(RULE_KEY, DUBBO_SERVICEDISCOVERY_MIGRATION, this);
String rawRule = configuration.getConfig(RULE_KEY, DUBBO_SERVICEDISCOVERY_MIGRATION);
@ -66,6 +68,9 @@ public class MigrationRuleListener implements RegistryProtocolListener, Configur
return;
}
logger.info("Using the following migration rule to migrate:");
logger.info(rawRule);
if (CollectionUtils.isNotEmpty(listeners)) {
listeners.forEach(listener -> listener.doMigrate(rawRule));
}
@ -77,7 +82,7 @@ public class MigrationRuleListener implements RegistryProtocolListener, Configur
}
@Override
public synchronized void onRefer(RegistryProtocol registryProtocol, ClusterInvoker<?> invoker) {
public synchronized void onRefer(RegistryProtocol registryProtocol, ClusterInvoker<?> invoker, URL url) {
MigrationInvoker<?> migrationInvoker = (MigrationInvoker<?>) invoker;
MigrationRuleHandler<?> migrationListener = new MigrationRuleHandler<>(migrationInvoker);

View File

@ -27,8 +27,8 @@ import org.apache.dubbo.rpc.cluster.ClusterInvoker;
public class ServiceDiscoveryMigrationInvoker<T> extends MigrationInvoker<T> {
public ServiceDiscoveryMigrationInvoker(RegistryProtocol registryProtocol, Cluster cluster, Registry registry, Class<T> type, URL url) {
super(registryProtocol, cluster, registry, type, url);
public ServiceDiscoveryMigrationInvoker(RegistryProtocol registryProtocol, Cluster cluster, Registry registry, Class<T> type, URL url, URL consumerUrl) {
super(registryProtocol, cluster, registry, type, url, consumerUrl);
}
@Override

View File

@ -65,9 +65,9 @@ public class InterfaceCompatibleRegistryProtocol extends RegistryProtocol {
return doCreateInvoker(directory, cluster, registry, type);
}
protected <T> ClusterInvoker<T> getMigrationInvoker(RegistryProtocol registryProtocol, Cluster cluster, Registry registry, Class<T> type, URL url) {
protected <T> ClusterInvoker<T> getMigrationInvoker(RegistryProtocol registryProtocol, Cluster cluster, Registry registry, Class<T> type, URL url, URL consumerUrl) {
// ClusterInvoker<T> invoker = getInvoker(cluster, registry, type, url);
return new MigrationInvoker<T>(registryProtocol, cluster, registry, type, url);
return new MigrationInvoker<T>(registryProtocol, cluster, registry, type, url, consumerUrl);
}
}

View File

@ -443,31 +443,32 @@ public class RegistryProtocol implements Protocol {
String group = qs.get(GROUP_KEY);
if (group != null && group.length() > 0) {
if ((COMMA_SPLIT_PATTERN.split(group)).length > 1 || "*".equals(group)) {
return doRefer(Cluster.getCluster(MergeableCluster.NAME), registry, type, url);
return doRefer(Cluster.getCluster(MergeableCluster.NAME), registry, type, url, qs);
}
}
Cluster cluster = Cluster.getCluster(qs.get(CLUSTER_KEY));
return doRefer(cluster, registry, type, url);
return doRefer(cluster, registry, type, url, qs);
}
protected <T> Invoker<T> doRefer(Cluster cluster, Registry registry, Class<T> type, URL url) {
ClusterInvoker<T> migrationInvoker = getMigrationInvoker(this, cluster, registry, type, url);
return interceptInvoker(migrationInvoker, url);
protected <T> Invoker<T> doRefer(Cluster cluster, Registry registry, Class<T> type, URL url, Map<String, String> parameters) {
URL consumerUrl = new URL(CONSUMER_PROTOCOL, parameters.remove(REGISTER_IP_KEY), 0, type.getName(), parameters);
ClusterInvoker<T> migrationInvoker = getMigrationInvoker(this, cluster, registry, type, url, consumerUrl);
return interceptInvoker(migrationInvoker, url, consumerUrl);
}
protected <T> ClusterInvoker<T> getMigrationInvoker(RegistryProtocol registryProtocol, Cluster cluster, Registry registry, Class<T> type, URL url) {
return new ServiceDiscoveryMigrationInvoker<T>(registryProtocol, cluster, registry, type, url);
protected <T> ClusterInvoker<T> getMigrationInvoker(RegistryProtocol registryProtocol, Cluster cluster, Registry registry, Class<T> type, URL url, URL consumerUrl) {
return new ServiceDiscoveryMigrationInvoker<T>(registryProtocol, cluster, registry, type, url, consumerUrl);
}
protected <T> Invoker<T> interceptInvoker(ClusterInvoker<T> invoker, URL url) {
protected <T> Invoker<T> interceptInvoker(ClusterInvoker<T> invoker, URL url, URL consumerUrl) {
List<RegistryProtocolListener> listeners = findRegistryProtocolListeners(url);
if (CollectionUtils.isEmpty(listeners)) {
return invoker;
}
for (RegistryProtocolListener listener : listeners) {
listener.onRefer(this, invoker);
listener.onRefer(this, invoker, consumerUrl);
}
return invoker;
}

View File

@ -41,9 +41,10 @@ public interface RegistryProtocolListener {
*
* @param registryProtocol RegistryProtocol instance
* @param invoker invoker
* @param url
* @see RegistryProtocol#refer(Class, URL)
*/
void onRefer(RegistryProtocol registryProtocol, ClusterInvoker<?> invoker);
void onRefer(RegistryProtocol registryProtocol, ClusterInvoker<?> invoker, URL url);
/**
* Notify RegistryProtocol's listeners when the protocol is destroyed