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

support deep copy of default service instance #9029

Merged
merged 5 commits into from
Oct 14, 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 @@ -16,11 +16,13 @@
*/
package org.apache.dubbo.registry.client;

import com.alibaba.fastjson.JSON;
import org.apache.dubbo.metadata.MetadataInfo;
import org.apache.dubbo.rpc.model.ApplicationModel;

import com.alibaba.fastjson.JSON;

import java.beans.Transient;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -79,13 +81,13 @@ public DefaultServiceInstance(DefaultServiceInstance other) {
this.port = other.port;
this.enabled = other.enabled;
this.healthy = other.healthy;
this.metadata = other.metadata;
this.serviceMetadata = other.serviceMetadata;
this.registryCluster = other.registryCluster;
this.extendParams = other.extendParams;
this.endpoints = other.endpoints;
this.address = null;
this.attributes = other.attributes;
this.metadata = new HashMap<>(other.metadata);
this.attributes = new HashMap<>(other.attributes);
this.extendParams = other.extendParams != null ? new HashMap<>(other.extendParams) : other.extendParams;
this.endpoints = other.endpoints != null ? new ArrayList<>(other.endpoints) : other.endpoints;
}

public DefaultServiceInstance(String serviceName, String host, Integer port, ApplicationModel applicationModel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package org.apache.dubbo.registry.client.metadata;

import com.google.gson.Gson;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.logger.Logger;
Expand All @@ -33,6 +32,8 @@
import org.apache.dubbo.registry.client.metadata.store.RemoteMetadataServiceImpl;
import org.apache.dubbo.registry.support.RegistryManager;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -253,6 +254,7 @@ public static void registerMetadataAndInstance(ServiceInstance serviceInstance)
RegistryManager registryManager = serviceInstance.getOrDefaultApplicationModel().getBeanFactory().getBean(RegistryManager.class);
registryManager.getServiceDiscoveries().forEach(serviceDiscovery ->
{
// copy instance for each registry to make sure instance in each registry can evolve independently
ServiceInstance serviceInstanceForRegistry = new DefaultServiceInstance((DefaultServiceInstance) serviceInstance);
calInstanceRevision(serviceDiscovery, serviceInstanceForRegistry);
if (LOGGER.isDebugEnabled()) {
Expand All @@ -273,6 +275,8 @@ public static void refreshMetadataAndInstance(ServiceInstance serviceInstance) {
LOGGER.warn("Refreshing of service instance started, but instance hasn't been registered yet.");
instance = serviceInstance;
}
// copy instance again, in case the same instance accidently shared among registries
instance = new DefaultServiceInstance((DefaultServiceInstance) instance);
calInstanceRevision(serviceDiscovery, instance);
customizeInstance(instance);
if (serviceInstance.getPort() > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
*/
package org.apache.dubbo.registry.client.metadata;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.metadata.MetadataInfo;
Expand All @@ -31,6 +28,10 @@
import org.apache.dubbo.registry.client.ServiceDiscoveryRegistry;
import org.apache.dubbo.registry.support.RegistryManager;
import org.apache.dubbo.rpc.model.ApplicationModel;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
Expand Down Expand Up @@ -202,8 +203,11 @@ public void refreshMetadataAndInstance() throws Exception {
Assertions.assertNull(inMemoryServiceDiscovery.getLocalInstance());

ServiceInstanceMetadataUtils.refreshMetadataAndInstance(serviceInstance);
Assertions.assertEquals(inMemoryServiceDiscovery.getLocalInstance(), serviceInstance);

Assertions.assertEquals(inMemoryServiceDiscovery.getLocalInstance().getServiceName(), serviceInstance.getServiceName());
Assertions.assertEquals(inMemoryServiceDiscovery.getLocalInstance().getHost(), serviceInstance.getHost());
Assertions.assertEquals(inMemoryServiceDiscovery.getLocalInstance().getPort(), serviceInstance.getPort());
Assertions.assertEquals(inMemoryServiceDiscovery.getLocalInstance().getApplicationModel(), serviceInstance.getApplicationModel());
}

private InMemoryServiceDiscovery prepare() throws NoSuchMethodException, InstantiationException, IllegalAccessException, java.lang.reflect.InvocationTargetException, NoSuchFieldException {
Expand Down