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

add test case for service discovery process #8607

Merged
merged 7 commits into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -22,15 +22,9 @@
import java.util.Map;
import java.util.Objects;

import static org.apache.dubbo.common.constants.CommonConstants.DUBBO_VERSION_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.METHODS_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.RELEASE_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.SIDE_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.TAG_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.TIMESTAMP_KEY;

public class DubboServiceAddressURL extends ServiceAddressURL {
public static final String[] PROVIDER_FIRST_KEYS = new String[]{RELEASE_KEY, DUBBO_VERSION_KEY, METHODS_KEY, TIMESTAMP_KEY, TAG_KEY};

public static DubboServiceAddressURL valueOf(String rawURL, URL consumerURL) {
return valueOf(rawURL, consumerURL, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,14 @@ protected void exported() {
exportedURLs.forEach(url -> {
if (url.getParameters().containsKey(SERVICE_NAME_MAPPING_KEY)) {
ServiceNameMapping serviceNameMapping = ServiceNameMapping.getDefaultExtension();
serviceNameMapping.map(url);
try {
boolean succeeded = serviceNameMapping.map(url);
if (succeeded) {
logger.info("Successfully registered interface application mapping for service " + url.getServiceKey());
}
} catch (Exception e) {
logger.error("Failed register interface application mapping for service " + url.getServiceKey(), e);
}
}
});
onExported();
Expand Down
6 changes: 6 additions & 0 deletions dubbo-distribution/dubbo-all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,12 @@
META-INF/dubbo/internal/org.apache.dubbo.rpc.cluster.router.mesh.route.MeshEnvListenerFactory
</resource>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>
META-INF/dubbo/internal/org.apache.dubbo.registry.ProviderFirstParams
</resource>
</transformer>
</transformers>
<filters>
<filter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ public MetadataInfo(String app) {
public MetadataInfo(String app, String revision, Map<String, ServiceInfo> services) {
this.app = app;
this.revision = revision;
this.services = services == null ? new HashMap<>() : services;
this.extendParams = new HashMap<>();
this.services = services == null ? new ConcurrentHashMap<>() : services;
this.extendParams = new ConcurrentHashMap<>();
}

public void addService(ServiceInfo serviceInfo) {
Expand All @@ -98,6 +98,9 @@ public void removeService(String key) {
markChanged();
}

/**
* Reported status and metadata modification must be synchronized if used in multiple threads.
*/
public String calAndGetRevision() {
if (revision != null && hasReported()) {
return revision;
Expand All @@ -120,14 +123,23 @@ public void setRevision(String revision) {
this.revision = revision;
}

/**
* Reported status and metadata modification must be synchronized if used in multiple threads.
*/
public boolean hasReported() {
return reported.get();
}

/**
* Reported status and metadata modification must be synchronized if used in multiple threads.
*/
public void markReported() {
reported.compareAndSet(false, true);
}

/**
* Reported status and metadata modification must be synchronized if used in multiple threads.
*/
public void markChanged() {
reported.compareAndSet(true, false);
}
Expand All @@ -148,8 +160,8 @@ public void setServices(Map<String, ServiceInfo> services) {
this.services = services;
}

public ServiceInfo getServiceInfo(String serviceKey) {
return services.get(serviceKey);
public ServiceInfo getServiceInfo(String protocolServiceKey) {
return services.get(protocolServiceKey);
}

public Map<String, String> getExtendParams() {
Expand Down Expand Up @@ -184,6 +196,28 @@ public String getServiceString(String protocolServiceKey) {
return serviceInfo.toString();
}

@Override
public int hashCode() {
return Objects.hash(app, services);
}

@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}

if (!(obj instanceof MetadataInfo)) {
return false;
}

MetadataInfo other = (MetadataInfo)obj;

return Objects.equals(app, other.getApp())
&& ((services == null && other.getServices() == null)
|| (services != null && services.equals(other.getServices())));
}

@Override
public String toString() {
return "metadata{" +
Expand Down Expand Up @@ -220,8 +254,7 @@ public static class ServiceInfo implements Serializable {
private final static String[] KEYS_TO_REMOVE = {MONITOR_KEY, BIND_IP_KEY, BIND_PORT_KEY, QOS_ENABLE,
QOS_HOST, QOS_PORT, ACCEPT_FOREIGN_IP, VALIDATION_KEY, INTERFACES, PID_KEY, TIMESTAMP_KEY};

public ServiceInfo() {
}
public ServiceInfo() {}

public ServiceInfo(URL url) {
this(url.getServiceInterface(), url.getGroup(), url.getVersion(), url.getProtocol(), url.getPath(), null);
Expand Down Expand Up @@ -264,7 +297,7 @@ public ServiceInfo(String name, String group, String version, String protocol, S
this.version = version;
this.protocol = protocol;
this.path = path;
this.params = params == null ? new HashMap<>() : params;
this.params = params == null ? new ConcurrentHashMap<>() : params;

this.serviceKey = URL.buildKey(name, group, version);
this.matchKey = buildMatchKey();
Expand Down Expand Up @@ -423,7 +456,7 @@ public void addParameterIfAbsent(String key, String value) {
public void addConsumerParams(Map<String, String> params) {
// copy once for one service subscription
if (consumerParams == null) {
consumerParams = new HashMap<>(params);
consumerParams = new ConcurrentHashMap<>(params);
}
}

Expand Down Expand Up @@ -456,20 +489,20 @@ public boolean equals(Object obj) {
}

ServiceInfo serviceInfo = (ServiceInfo) obj;
// return this.getMatchKey().equals(serviceInfo.getMatchKey()) && this.getParams().equals(serviceInfo.getParams());
// Please check ServiceInstancesChangedListener.localServiceToRevisions before changing this behaviour.
// equals to Objects.equals(this.getMatchKey(), serviceInfo.getMatchKey()), but match key will not get initialized
// on json deserialization.
/**
* Equals to Objects.equals(this.getMatchKey(), serviceInfo.getMatchKey()), but match key will not get initialized
* on json deserialization.
*/
return Objects.equals(this.getVersion(), serviceInfo.getVersion())
&& Objects.equals(this.getGroup(), serviceInfo.getGroup())
&& Objects.equals(this.getName(), serviceInfo.getName())
&& Objects.equals(this.getProtocol(), serviceInfo.getProtocol());
&& Objects.equals(this.getProtocol(), serviceInfo.getProtocol())
&& this.getParams().equals(serviceInfo.getParams());
}

@Override
public int hashCode() {
// return Objects.hash(getMatchKey(), getParams());
return Objects.hash(getVersion(), getGroup(), getName(), getProtocol());
return Objects.hash(getVersion(), getGroup(), getName(), getProtocol(), getParams());

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public interface ServiceNameMapping {
/**
* Map the specified Dubbo service interface, group, version and protocol to current Dubbo service name
*/
void map(URL url);
boolean map(URL url);

/**
* Get the default extension of {@link ServiceNameMapping}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,34 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.apache.dubbo.common.constants.CommonConstants.APPLICATION_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.INTERFACE_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.TIMEOUT_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY;
import static org.apache.dubbo.metadata.RevisionResolver.EMPTY_REVISION;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Some construction and filter cases are covered in InMemoryMetadataServiceTest
*/
public class MetadataInfoTest {
private static URL url = URL.valueOf("dubbo://30.225.21.30:20880/org.apache.dubbo.registry.service.DemoService2?" +
"REGISTRY_CLUSTER=registry1&anyhost=true&application=demo-provider2&delay=5000&deprecated=false&dubbo=2.0.2" +
"&dynamic=true&generic=false&group=greeting&interface=org.apache.dubbo.registry.service.DemoService2" +
"&metadata-type=remote&methods=sayHello&sayHello.timeout=7000&pid=36621&release=&revision=1.0.0&service-name-mapping=true" +
"&side=provider&timeout=5000&timestamp=1629970068002&version=1.0.0&params-filter=customized,-excluded");

private static URL url2 = URL.valueOf("dubbo://30.225.21.30:20880/org.apache.dubbo.registry.service.DemoService?" +
"REGISTRY_CLUSTER=registry1&anyhost=true&application=demo-provider2&delay=5000&deprecated=false&dubbo=2.0.2" +
"&dynamic=true&generic=false&group=greeting&interface=org.apache.dubbo.registry.service.DemoService" +
"&metadata-type=remote&methods=sayHello&pid=36621&release=&revision=1.0.0&service-name-mapping=true" +
"&side=provider&timeout=5000&timestamp=1629970068002&version=1.0.0&params-filter=customized,-excluded");

@Test
public void testEmptyRevision() {
Expand All @@ -33,40 +58,66 @@ public void testEmptyRevision() {
Assertions.assertEquals(EMPTY_REVISION, metadataInfo.calAndGetRevision());
}

@Test
public void testRevisionNotChange() {
MetadataInfo metadataInfo = new MetadataInfo("demo");
metadataInfo.setApp("demo");

URL url = URL.valueOf("dubbo://10.230.11.211:20880/org.apache.dubbo.metadata.DemoService?timeout=1000&testKey=aaa");
MetadataInfo.ServiceInfo serviceInfo = new MetadataInfo.ServiceInfo(url);
metadataInfo.addService(serviceInfo);

System.out.println(serviceInfo.toDescString());
System.out.println(metadataInfo.calAndGetRevision());
}

@Test
public void testParamsFiltered() {
MetadataInfo metadataInfo = new MetadataInfo("demo");
metadataInfo.setApp("demo");


// export normal url again
metadataInfo.addService(new MetadataInfo.ServiceInfo(url));
MetadataInfo.ServiceInfo serviceInfo2 = metadataInfo.getServiceInfo(url.getProtocolServiceKey());
assertNotNull(serviceInfo2);
assertEquals(5, serviceInfo2.getParams().size());
assertNull(serviceInfo2.getParams().get(INTERFACE_KEY));
assertNull(serviceInfo2.getParams().get("delay"));
assertNotNull(serviceInfo2.getParams().get(APPLICATION_KEY));
assertNotNull(serviceInfo2.getParams().get(VERSION_KEY));
assertNotNull(serviceInfo2.getParams().get(GROUP_KEY));
assertNotNull(serviceInfo2.getParams().get(TIMEOUT_KEY));
assertEquals("7000", serviceInfo2.getMethodParameter("sayHello", TIMEOUT_KEY, "1000"));
}

@Test
public void testParamsStatusChangeAsExpected() {
public void testEqualsAndRevision() {
// same metadata
MetadataInfo metadataInfo = new MetadataInfo("demo");
metadataInfo.setApp("demo");


metadataInfo.addService(new MetadataInfo.ServiceInfo(url));
MetadataInfo sameMetadataInfo = new MetadataInfo("demo");
sameMetadataInfo.addService(new MetadataInfo.ServiceInfo(url));
assertEquals(metadataInfo, sameMetadataInfo);
assertEquals(metadataInfo.calAndGetRevision(), sameMetadataInfo.calAndGetRevision());

// url with different params that are not counted in ServiceInfo
MetadataInfo metadataInfoWithDifferentParam1 = new MetadataInfo("demo");
metadataInfoWithDifferentParam1.addService(new MetadataInfo.ServiceInfo(url.addParameter("delay", 6000)));
assertEquals(metadataInfo, metadataInfoWithDifferentParam1);
assertEquals(metadataInfo.calAndGetRevision(), metadataInfoWithDifferentParam1.calAndGetRevision());
// url with different params that are counted in ServiceInfo
MetadataInfo metadataInfoWithDifferentParam2 = new MetadataInfo("demo");
metadataInfoWithDifferentParam2.addService(new MetadataInfo.ServiceInfo(url.addParameter(TIMEOUT_KEY, 6000)));
assertNotEquals(metadataInfo, metadataInfoWithDifferentParam2);
assertNotEquals(metadataInfo.calAndGetRevision(), metadataInfoWithDifferentParam2.calAndGetRevision());

MetadataInfo metadataInfoWithDifferentGroup = new MetadataInfo("demo");
metadataInfoWithDifferentGroup.addService(new MetadataInfo.ServiceInfo(url.addParameter(GROUP_KEY, "newGroup")));
assertNotEquals(metadataInfo, metadataInfoWithDifferentGroup);
assertNotEquals(metadataInfo.calAndGetRevision(), metadataInfoWithDifferentGroup.calAndGetRevision());

MetadataInfo metadataInfoWithDifferentServices = new MetadataInfo("demo");
metadataInfoWithDifferentServices.addService(new MetadataInfo.ServiceInfo(url));
metadataInfoWithDifferentServices.addService(new MetadataInfo.ServiceInfo(url2));
assertNotEquals(metadataInfo, metadataInfoWithDifferentServices);
assertNotEquals(metadataInfo.calAndGetRevision(), metadataInfoWithDifferentServices.calAndGetRevision());
}

@Test
public void testEquals() {
public void testChanged() {
MetadataInfo metadataInfo = new MetadataInfo("demo");
metadataInfo.setApp("demo");


metadataInfo.addService(new MetadataInfo.ServiceInfo(url));
metadataInfo.addService(new MetadataInfo.ServiceInfo(url2));
assertFalse(metadataInfo.hasReported());
metadataInfo.markReported();
assertTrue(metadataInfo.hasReported());
metadataInfo.removeService(new MetadataInfo.ServiceInfo(url2));
assertFalse(metadataInfo.hasReported());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.metadata.filter;

import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.metadata.MetadataParamsFilter;

import static org.apache.dubbo.common.constants.CommonConstants.APPLICATION_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.TIMEOUT_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY;

@Activate
public class CustomizedParamsFilter implements MetadataParamsFilter {

@Override
public String[] serviceParamsIncluded() {
return new String[]{APPLICATION_KEY, TIMEOUT_KEY, GROUP_KEY, VERSION_KEY};
}

/**
* Not included in this test
*/
@Override
public String[] instanceParamsIncluded() {
return new String[0];
}
}
Loading