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

Fix the problem of multiple namespaces in MetadataReport #13971

Merged
merged 18 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Expand Up @@ -329,7 +329,7 @@ private void startMetadataCenter() {
}
}
metadataReportInstance.init(validMetadataReportConfigs);
if (!metadataReportInstance.inited()) {
if (!metadataReportInstance.isInitialized()) {
throw new IllegalStateException(String.format(
"%s MetadataConfigs found, but none of them is valid.", metadataReportConfigs.size()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_METADATA_STORAGE_TYPE;
import static org.apache.dubbo.common.constants.CommonConstants.PORT_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.REGISTRY_LOCAL_FILE_CACHE_ENABLED;
import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_CLUSTER_KEY;
import static org.apache.dubbo.common.utils.StringUtils.isEmpty;
import static org.apache.dubbo.metadata.MetadataConstants.NAMESPACE_KEY;
import static org.apache.dubbo.metadata.report.support.Constants.METADATA_REPORT_KEY;
Expand All @@ -54,7 +55,7 @@
*/
public class MetadataReportInstance implements Disposable {

private AtomicBoolean init = new AtomicBoolean(false);
private final AtomicBoolean initialized = new AtomicBoolean(false);
private String metadataType;

// mapping of registry id to metadata report instance, registry instances will use this mapping to find related
Expand All @@ -69,7 +70,7 @@ public MetadataReportInstance(ApplicationModel applicationModel) {
}

public void init(List<MetadataReportConfig> metadataReportConfigs) {
if (!init.compareAndSet(false, true)) {
if (!initialized.compareAndSet(false, true)) {
return;
}

Expand Down Expand Up @@ -114,9 +115,13 @@ private void init(MetadataReportConfig config, MetadataReportFactory metadataRep
}

private String getRelatedRegistryId(MetadataReportConfig config, URL url) {
String relatedRegistryId = isEmpty(config.getRegistry())
? (isEmpty(config.getId()) ? DEFAULT_KEY : config.getId())
: config.getRegistry();
String relatedRegistryId = config.getRegistry();
if (isEmpty(relatedRegistryId)) {
relatedRegistryId = config.getId();
}
if (isEmpty(relatedRegistryId)) {
relatedRegistryId = DEFAULT_KEY;
}
String namespace = url.getParameter(NAMESPACE_KEY);
if (!StringUtils.isEmpty(namespace)) {
relatedRegistryId += ":" + namespace;
Expand All @@ -128,6 +133,10 @@ public Map<String, MetadataReport> getMetadataReports(boolean checked) {
return metadataReports;
}

public MetadataReport getMetadataReport(URL registryURL) {
return getMetadataReport(getRegistryKey(registryURL));
}

public MetadataReport getMetadataReport(String registryKey) {
MetadataReport metadataReport = metadataReports.get(registryKey);
if (metadataReport == null && metadataReports.size() > 0) {
Expand All @@ -136,6 +145,15 @@ public MetadataReport getMetadataReport(String registryKey) {
return metadataReport;
}

public String getRegistryKey(URL registryURL) {
String registryKey = registryURL.getParameter(REGISTRY_CLUSTER_KEY);
String namespace = registryURL.getParameter(NAMESPACE_KEY);
if (!StringUtils.isEmpty(namespace)) {
registryKey += ":" + namespace;
}
return registryKey;
}

public MetadataReport getNopMetadataReport() {
return nopMetadataReport;
}
Expand All @@ -144,15 +162,13 @@ public String getMetadataType() {
return metadataType;
}

public boolean inited() {
return init.get();
public boolean isInitialized() {
return initialized.get();
}

@Override
public void destroy() {
metadataReports.forEach((_k, reporter) -> {
reporter.destroy();
});
metadataReports.forEach((k, reporter) -> reporter.destroy());
metadataReports.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.metadata.report.MetadataReport;
import org.apache.dubbo.metadata.report.MetadataReportFactory;

Expand All @@ -29,6 +30,7 @@
import static org.apache.dubbo.common.constants.CommonConstants.CHECK_KEY;
import static org.apache.dubbo.common.constants.LoggerCodeConstants.COMMON_UNEXPECTED_EXCEPTION;
import static org.apache.dubbo.common.constants.LoggerCodeConstants.PROXY_FAILED_EXPORT_SERVICE;
import static org.apache.dubbo.metadata.MetadataConstants.NAMESPACE_KEY;

public abstract class AbstractMetadataReportFactory implements MetadataReportFactory {

Expand Down Expand Up @@ -89,6 +91,12 @@ public MetadataReport getMetadataReport(URL url) {
}

protected String toMetadataReportKey(URL url) {
String namespace = url.getParameter(NAMESPACE_KEY);
if (!StringUtils.isEmpty(namespace)) {
return URL.valueOf(url.toServiceString())
.addParameter(NAMESPACE_KEY, namespace)
.toString();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you describe the difference between the new url and the old url?

Copy link
Contributor

Choose a reason for hiding this comment

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

How about making it a public util method to make calling it in other places easier? like here https://github.com/apache/dubbo/pull/13971/files#diff-17a0f234d88728ebc6f37959d84276704ca16846cd10cd0743616a8689949eabR33

Copy link
Contributor Author

@finefuture finefuture Apr 7, 2024

Choose a reason for hiding this comment

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

Add a new method for extra parameters.
image

return url.toServiceString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@
package org.apache.dubbo.metadata.store.nacos;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.metadata.report.MetadataReport;
import org.apache.dubbo.metadata.report.support.AbstractMetadataReportFactory;

import static org.apache.dubbo.metadata.MetadataConstants.NAMESPACE_KEY;

/**
* metadata report factory impl for nacos
*/
Expand All @@ -31,15 +28,4 @@ public class NacosMetadataReportFactory extends AbstractMetadataReportFactory {
protected MetadataReport createMetadataReport(URL url) {
return new NacosMetadataReport(url);
}

@Override
protected String toMetadataReportKey(URL url) {
String namespace = url.getParameter(NAMESPACE_KEY);
if (!StringUtils.isEmpty(namespace)) {
return URL.valueOf(url.toServiceString())
.addParameter(NAMESPACE_KEY, namespace)
.toString();
}
return super.toMetadataReportKey(url);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
import static org.apache.dubbo.common.constants.CommonConstants.REMOTE_METADATA_STORAGE_TYPE;
import static org.apache.dubbo.common.constants.LoggerCodeConstants.INTERNAL_ERROR;
import static org.apache.dubbo.common.constants.LoggerCodeConstants.REGISTRY_FAILED_LOAD_METADATA;
import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_CLUSTER_KEY;
import static org.apache.dubbo.metadata.RevisionResolver.EMPTY_REVISION;
import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.EXPORTED_SERVICES_REVISION_PROPERTY_NAME;
import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.getExportedServicesRevision;
Expand Down Expand Up @@ -84,8 +83,8 @@ public AbstractServiceDiscovery(ApplicationModel applicationModel, URL registryU
this(applicationModel, applicationModel.getApplicationName(), registryURL);
MetadataReportInstance metadataReportInstance =
applicationModel.getBeanFactory().getBean(MetadataReportInstance.class);
metadataType = metadataReportInstance.getMetadataType();
this.metadataReport = metadataReportInstance.getMetadataReport(registryURL.getParameter(REGISTRY_CLUSTER_KEY));
this.metadataType = metadataReportInstance.getMetadataType();
this.metadataReport = metadataReportInstance.getMetadataReport(registryURL);
}

public AbstractServiceDiscovery(String serviceName, URL registryURL) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,24 @@
package org.apache.dubbo.registry.client;

import org.apache.dubbo.common.URL;

import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_CLUSTER_KEY;
import org.apache.dubbo.metadata.report.MetadataReportInstance;
import org.apache.dubbo.rpc.model.ApplicationModel;

public class DefaultRegistryClusterIdentifier implements RegistryClusterIdentifier {

private final MetadataReportInstance metadataReportInstance;

public DefaultRegistryClusterIdentifier(ApplicationModel applicationModel) {
this.metadataReportInstance = applicationModel.getBeanFactory().getBean(MetadataReportInstance.class);
}

@Override
public String providerKey(URL url) {
return url.getParameter(REGISTRY_CLUSTER_KEY);
return metadataReportInstance.getRegistryKey(url);
}

@Override
public String consumerKey(URL url) {
return url.getParameter(REGISTRY_CLUSTER_KEY);
return metadataReportInstance.getRegistryKey(url);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import static org.apache.dubbo.common.constants.RegistryConstants.SERVICE_REGISTRY_TYPE;
import static org.apache.dubbo.common.function.ThrowableAction.execute;
import static org.apache.dubbo.common.utils.CollectionUtils.toTreeSet;
import static org.apache.dubbo.metadata.MetadataConstants.NAMESPACE_KEY;
import static org.apache.dubbo.metadata.ServiceNameMapping.toStringKeys;
import static org.apache.dubbo.registry.client.ServiceDiscoveryFactory.getExtension;

Expand Down Expand Up @@ -257,6 +258,10 @@ private URL addRegistryClusterKey(URL url) {
if (registryCluster != null && url.getParameter(REGISTRY_CLUSTER_KEY) == null) {
url = url.addParameter(REGISTRY_CLUSTER_KEY, registryCluster);
}
String namespace = serviceDiscovery.getUrl().getParameter(NAMESPACE_KEY);
if (namespace != null && url.getParameter(NAMESPACE_KEY) == null) {
url = url.addParameter(NAMESPACE_KEY, namespace);
}
return url;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ void testGet() {
set.add("app1");

MetadataReportInstance reportInstance = mock(MetadataReportInstance.class);
Mockito.when(reportInstance.getMetadataReport(any())).thenReturn(metadataReport);
Mockito.when(reportInstance.getMetadataReport((String) any())).thenReturn(metadataReport);
when(metadataReport.getServiceAppMapping(any(), any())).thenReturn(set);

mapping.metadataReportInstance = reportInstance;
Expand Down
Loading