Skip to content

Commit

Permalink
[3.0] Opt ServiceInstance Memory Usage (#9331)
Browse files Browse the repository at this point in the history
* [3.0] Opt ServiceInstance Memory Usage

* fix ut

* fix port
  • Loading branch information
AlbumenJ authored Dec 1, 2021
1 parent 5349c13 commit 43d46d9
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.beans.Transient;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
Expand All @@ -31,7 +32,6 @@
import java.util.SortedMap;
import java.util.TreeMap;

import static org.apache.dubbo.common.constants.CommonConstants.SCOPE_MODEL;
import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.ENDPOINTS;
import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.EXPORTED_SERVICES_REVISION_PROPERTY_NAME;

Expand Down Expand Up @@ -71,7 +71,7 @@ public class DefaultServiceInstance implements ServiceInstance {
*/
private transient Map<String, String> extendParams;
private transient List<Endpoint> endpoints;
private transient Map<String, Object> attributes = new HashMap<>();
private transient ApplicationModel applicationModel;
private transient InstanceAddressURL instanceAddressURL = null;

public DefaultServiceInstance() {
Expand All @@ -87,7 +87,7 @@ public DefaultServiceInstance(DefaultServiceInstance other) {
this.registryCluster = other.registryCluster;
this.address = null;
this.metadata = new HashMap<>(other.metadata);
this.attributes = new HashMap<>(other.attributes);
this.applicationModel = other.applicationModel;
this.extendParams = other.extendParams != null ? new HashMap<>(other.extendParams) : other.extendParams;
this.endpoints = other.endpoints != null ? new ArrayList<>(other.endpoints) : other.endpoints;
}
Expand Down Expand Up @@ -192,11 +192,47 @@ public void setRegistryCluster(String registryCluster) {
@Override
public Map<String, String> getExtendParams() {
if (extendParams == null) {
extendParams = new HashMap<>();
return Collections.emptyMap();
}
return extendParams;
}

@Override
public String getExtendParam(String key) {
if (extendParams == null) {
return null;
}
return extendParams.get(key);
}

@Override
public String putExtendParam(String key, String value) {
if (extendParams == null) {
extendParams = new HashMap<>();
}
return extendParams.put(key, value);
}

@Override
public String putExtendParamIfAbsent(String key, String value) {
if (extendParams == null) {
extendParams = new HashMap<>();
}
return extendParams.putIfAbsent(key, value);
}

@Override
public String removeExtendParam(String key) {
if (extendParams == null) {
return null;
}
return extendParams.remove(key);
}

public void setEndpoints(List<Endpoint> endpoints) {
this.endpoints = endpoints;
}

public List<Endpoint> getEndpoints() {
if (endpoints == null) {
endpoints = new LinkedList<>(JSON.parseArray(metadata.get(ENDPOINTS), Endpoint.class));
Expand All @@ -222,21 +258,15 @@ public Map<String, String> getAllParams() {
}
}

@Override
@Transient
public Map<String, Object> getAttributes() {
return attributes;
}

@Override
public void setApplicationModel(ApplicationModel applicationModel) {
this.attributes.put(SCOPE_MODEL, applicationModel);
this.applicationModel = applicationModel;
}

@Override
@Transient
public ApplicationModel getApplicationModel() {
return (ApplicationModel) this.attributes.get(SCOPE_MODEL);
return applicationModel;
}

public void setMetadata(Map<String, String> metadata) {
Expand Down Expand Up @@ -311,22 +341,22 @@ public String toFullString() {
}

public static class Endpoint {
Integer port;
int port;
String protocol;

public Endpoint() {
}

public Endpoint(Integer port, String protocol) {
public Endpoint(int port, String protocol) {
this.port = port;
this.protocol = protocol;
}

public Integer getPort() {
public int getPort() {
return port;
}

public void setPort(Integer port) {
public void setPort(int port) {
this.port = port;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ public URL addParameter(String key, String value) {
return this;
}

getInstance().getExtendParams().put(key, value);
getInstance().putExtendParam(key, value);
return this;
}

Expand All @@ -386,7 +386,7 @@ public URL addParameterIfAbsent(String key, String value) {
return this;
}

getInstance().getExtendParams().putIfAbsent(key, value);
getInstance().putExtendParamIfAbsent(key, value);
return this;
}

Expand Down Expand Up @@ -486,7 +486,7 @@ private String getInstanceParameter(String key) {
if (StringUtils.isNotEmpty(value)) {
return value;
}
return this.instance.getExtendParams().get(key);
return this.instance.getExtendParam(key);
}

private Map<String, String> getInstanceMetadata() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,15 @@ default boolean isHealthy() {

Map<String, String> getExtendParams();

Map<String, String> getAllParams();
String getExtendParam(String key);

String putExtendParam(String key, String value);

String putExtendParamIfAbsent(String key, String value);

Map<String, Object> getAttributes();
String removeExtendParam(String key);

Map<String, String> getAllParams();

void setApplicationModel(ApplicationModel applicationModel);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ protected Object getServiceUrlsCache(Map<String, List<ServiceInstance>> revision
// different protocols may have ports specified in meta
if (ServiceInstanceMetadataUtils.hasEndpoints(i)) {
DefaultServiceInstance.Endpoint endpoint = ServiceInstanceMetadataUtils.getEndpoint(i, protocol);
if (endpoint != null && !endpoint.getPort().equals(i.getPort())) {
if (endpoint != null && endpoint.getPort() != i.getPort()) {
urls.add(((DefaultServiceInstance) i).copyFrom(endpoint).toURL());
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,18 +235,18 @@ public static void calInstanceRevision(ServiceDiscovery serviceDiscovery, Servic
if (!metadataInfo.calAndGetRevision().equals(existingInstanceRevision)) {
instance.getMetadata().put(EXPORTED_SERVICES_REVISION_PROPERTY_NAME, metadataInfo.calAndGetRevision());
if (existingInstanceRevision != null) {// skip the first registration.
instance.getExtendParams().put(INSTANCE_REVISION_UPDATED_KEY, "true");
instance.putExtendParam(INSTANCE_REVISION_UPDATED_KEY, "true");
}
}
}
}

public static boolean isInstanceUpdated(ServiceInstance instance) {
return "true".equals(instance.getExtendParams().get(INSTANCE_REVISION_UPDATED_KEY));
return "true".equals(instance.getExtendParam(INSTANCE_REVISION_UPDATED_KEY));
}

public static void resetInstanceUpdateKey(ServiceInstance instance) {
instance.getExtendParams().remove(INSTANCE_REVISION_UPDATED_KEY);
instance.removeExtendParam(INSTANCE_REVISION_UPDATED_KEY);
}

public static void registerMetadataAndInstance(ServiceInstance serviceInstance) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void testInstanceOperations() {
Map<String, String> allParams = instance.getAllParams();
assertEquals(instance.getMetadata().size(), allParams.size());
assertEquals("dubbo", allParams.get("site"));
instance.getExtendParams().put("key", "value");
instance.putExtendParam("key", "value");
Map<String, String> allParams2 = instance.getAllParams();
assertNotSame(allParams, allParams2);
assertEquals(instance.getMetadata().size() + instance.getExtendParams().size(), allParams2.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public void getRemoteCluster() {

@Test
public void testInstanceUpdateKey() {
serviceInstance.getExtendParams().put(INSTANCE_REVISION_UPDATED_KEY, "true");
serviceInstance.putExtendParam(INSTANCE_REVISION_UPDATED_KEY, "true");
Assertions.assertTrue(ServiceInstanceMetadataUtils.isInstanceUpdated(serviceInstance));

ServiceInstanceMetadataUtils.resetInstanceUpdateKey(serviceInstance);
Expand Down Expand Up @@ -180,12 +180,12 @@ public void testCalInstanceRevision() {

ServiceInstanceMetadataUtils.calInstanceRevision(serviceDiscovery, serviceInstance);
Assertions.assertEquals(metadataInfo.calAndGetRevision(), serviceInstance.getMetadata().get(EXPORTED_SERVICES_REVISION_PROPERTY_NAME));
Assertions.assertNull(serviceInstance.getExtendParams().get(INSTANCE_REVISION_UPDATED_KEY));
Assertions.assertNull(serviceInstance.getExtendParam(INSTANCE_REVISION_UPDATED_KEY));

writableMetadataService.getMetadataInfos().get(DEFAULT_KEY).addService(new MetadataInfo.ServiceInfo(url2));
ServiceInstanceMetadataUtils.calInstanceRevision(serviceDiscovery, serviceInstance);
Assertions.assertEquals(metadataInfo.calAndGetRevision(), serviceInstance.getMetadata().get(EXPORTED_SERVICES_REVISION_PROPERTY_NAME));
Assertions.assertEquals(serviceInstance.getExtendParams().get(INSTANCE_REVISION_UPDATED_KEY), "true");
Assertions.assertEquals(serviceInstance.getExtendParam(INSTANCE_REVISION_UPDATED_KEY), "true");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import org.apache.dubbo.rpc.model.ScopeModelUtil;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.internal.util.collections.Sets;

Expand Down Expand Up @@ -106,7 +106,7 @@ public void onEvent(ServiceInstancesChangedEvent event) {
Map<String, String> metadata = new HashMap<>();
metadata.put("message", "Hello,World");
serviceInstance.setMetadata(metadata);
serviceInstance.getExtendParams().put(INSTANCE_REVISION_UPDATED_KEY, "true");
serviceInstance.putExtendParam(INSTANCE_REVISION_UPDATED_KEY, "true");

discovery.update(serviceInstance);

Expand Down

0 comments on commit 43d46d9

Please sign in to comment.