Skip to content

Commit

Permalink
fix review issue and refactor metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
cvictory committed Aug 14, 2019
1 parent 4baa0aa commit 2a30b22
Show file tree
Hide file tree
Showing 17 changed files with 136 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
import static org.apache.dubbo.common.constants.CommonConstants.METADATA_REMOTE;
import static org.apache.dubbo.common.utils.StringUtils.isNotEmpty;
import static org.apache.dubbo.config.context.ConfigManager.getInstance;
import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.MEATADATA_STORED_TYPE_KEY;
import static org.apache.dubbo.remoting.Constants.CLIENT_KEY;

/**
Expand Down Expand Up @@ -792,7 +793,7 @@ private void unregisterServiceInstance() {

private ServiceInstance initServiceInstance(String serviceName, String host, int port, String metadataType) {
this.serviceInstance = new DefaultServiceInstance(serviceName, host, port);
this.serviceInstance.getMetadata().put(METADATA_KEY, metadataType);
this.serviceInstance.getMetadata().put(MEATADATA_STORED_TYPE_KEY, metadataType);
return this.serviceInstance;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@
package org.apache.dubbo.bootstrap;

import org.apache.dubbo.bootstrap.rest.UserService;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.MetadataReportConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.context.ConfigManager;

import java.util.HashMap;
import java.util.Map;

import static org.apache.dubbo.common.constants.CommonConstants.METADATA_KEY;

/**
* Dubbo Provider Bootstrap
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@

import org.apache.dubbo.bootstrap.rest.UserService;
import org.apache.dubbo.bootstrap.rest.UserServiceImpl;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.MetadataReportConfig;
import org.apache.dubbo.config.ProtocolConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.ServiceConfig;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import static org.apache.dubbo.common.constants.CommonConstants.METADATA_KEY;

/**
* Dubbo Provider Bootstrap
Expand Down Expand Up @@ -61,9 +66,10 @@ private static void multipleRegistries() {
userService.setProtocol(restProtocol);
// userService.setRegistries(Arrays.asList(interfaceRegistry, serviceRegistry));


ApplicationConfig applicationConfig = new ApplicationConfig("dubbo-provider-demo");
applicationConfig.setMetadata("remote");
new DubboBootstrap()
.application("dubbo-provider-demo")
.application(applicationConfig)
// Zookeeper in service registry type
// .registry("zookeeper", builder -> builder.address("zookeeper://127.0.0.1:2181?registry.type=service"))
// Nacos
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,18 @@ public interface Prioritized extends Comparable<Prioritized> {
* The minimum priority
*/
int MIN_PRIORITY = Integer.MAX_VALUE;
/**
* Normal Priority
*/
int NORMAL_PRIORITY = 0;

/**
* Get the priority
*
* @return the default is {@link #MIN_PRIORITY minimum one}
*/
default int getPriority() {
return MIN_PRIORITY;
return NORMAL_PRIORITY;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ public class ServiceMetadataIdentifier extends BaseServiceMetadataIdentifier {
public ServiceMetadataIdentifier() {
}

public ServiceMetadataIdentifier(String serviceInterface, String version, String group, String side, String revision) {
public ServiceMetadataIdentifier(String serviceInterface, String version, String group, String side, String revision, String protocol) {
this.serviceInterface = serviceInterface;
this.version = version;
this.group = group;
this.side = side;
this.revision = revision;
this.protocol = protocol;
}


Expand All @@ -36,7 +37,6 @@ public ServiceMetadataIdentifier(URL url) {
this.version = url.getParameter(VERSION_KEY);
this.group = url.getParameter(GROUP_KEY);
this.side = url.getParameter(SIDE_KEY);
this.revision = (url.getParameter(REVISION_KEY));
this.protocol = url.getProtocol();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ public void saveServiceMetadata(ServiceMetadataIdentifier metadataIdentifier, UR
if (syncReport) {
doSaveMetadata(metadataIdentifier, url);
} else {
reportCacheExecutor.execute(() -> doSaveMetadata(new ServiceMetadataIdentifier(url), url));
reportCacheExecutor.execute(() -> doSaveMetadata(metadataIdentifier, url));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class InMemoryWritableMetadataService extends BaseWritableMetadataService

@Override
public SortedSet<String> getSubscribedURLs() {
return getAllUnmodifiableServiceURLs(subscribedServiceURLs);
return super.getSubscribedURLs();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ public boolean exportURL(URL url) {
public boolean unexportURL(URL url) {
ServiceMetadataIdentifier metadataIdentifier = new ServiceMetadataIdentifier(url);
metadataIdentifier.setRevision(exportedRevision);
metadataIdentifier.setProtocol(url.getProtocol());
return throwableAction(getMetadataReport()::removeServiceMetadata, metadataIdentifier);
}

Expand Down Expand Up @@ -174,6 +175,7 @@ private boolean saveServiceMetadata() {
// refresh revision in urls
ServiceMetadataIdentifier metadataIdentifier = new ServiceMetadataIdentifier(url);
metadataIdentifier.setRevision(exportedRevision);
metadataIdentifier.setProtocol(url.getProtocol());

boolean tmpResult = throwableAction(getMetadataReport()::saveServiceMetadata, metadataIdentifier, url);
if (!tmpResult) result = tmpResult;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package org.apache.dubbo.metadata.store;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.metadata.WritableMetadataService;

import java.util.ArrayList;
import java.util.List;
import java.util.SortedSet;
import java.util.function.BiFunction;

/**
* 2019-08-14
*
* @since 2.7.5
*/
public class RemoteWritableMetadataServiceDelegate implements WritableMetadataService {
List<WritableMetadataService> metadataServiceList = new ArrayList<>(2);

public RemoteWritableMetadataServiceDelegate() {
metadataServiceList.add(WritableMetadataService.getDefaultExtension());
metadataServiceList.add(new RemoteWritableMetadataService());
}

private WritableMetadataService getInMemoryWritableMetadataService() {
for (WritableMetadataService writableMetadataService : metadataServiceList) {
if (writableMetadataService instanceof InMemoryWritableMetadataService) {
return writableMetadataService;
}
}
return metadataServiceList.get(0);
}

@Override
public boolean exportURL(URL url) {
return doFunction(WritableMetadataService::exportURL, url);
}

@Override
public boolean unexportURL(URL url) {
return doFunction(WritableMetadataService::unexportURL, url);
}

@Override
public boolean subscribeURL(URL url) {
return doFunction(WritableMetadataService::subscribeURL, url);
}

@Override
public boolean unsubscribeURL(URL url) {
return doFunction(WritableMetadataService::unsubscribeURL, url);
}

@Override
public boolean refreshMetadata(String exportedRevision, String subscribedRevision) {
boolean result = true;
for (WritableMetadataService writableMetadataService : metadataServiceList) {
result &= writableMetadataService.refreshMetadata(exportedRevision, subscribedRevision);
}
return result;
}

@Override
public void publishServiceDefinition(URL providerUrl) {
for (WritableMetadataService writableMetadataService : metadataServiceList) {
writableMetadataService.publishServiceDefinition(providerUrl);
}
}

@Override
public SortedSet<String> getExportedURLs(String serviceInterface, String group, String version, String protocol) {
return getInMemoryWritableMetadataService().getExportedURLs(serviceInterface, group, version, protocol);
}
@Override
public SortedSet<String> getSubscribedURLs() {
return getInMemoryWritableMetadataService().getSubscribedURLs();
}

@Override
public String getServiceDefinition(String interfaceName, String version, String group) {
return getInMemoryWritableMetadataService().getServiceDefinition(interfaceName, version, group);
}

@Override
public String getServiceDefinition(String serviceKey) {
return getInMemoryWritableMetadataService().getServiceDefinition(serviceKey);
}

private boolean doFunction(BiFunction<WritableMetadataService, URL, Boolean> func, URL url) {
boolean result = true;
for (WritableMetadataService writableMetadataService : metadataServiceList) {
result &= func.apply(writableMetadataService, url);
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
default=org.apache.dubbo.metadata.store.InMemoryWritableMetadataService
remote=org.apache.dubbo.metadata.store.RemoteWritableMetadataService
remote=org.apache.dubbo.metadata.store.RemoteWritableMetadataServiceDelegate
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@

import static java.lang.String.valueOf;
import static java.util.Objects.hash;
import static org.apache.dubbo.common.constants.CommonConstants.METADATA_KEY;
import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.EXPORTED_SERVICES_REVISION_KEY;
import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.MEATADATA_STORED_TYPE_KEY;

/**
* The customizer to a add the metadata that the reversion of Dubbo exported services calculates.
Expand All @@ -49,7 +49,7 @@ protected String buildMetadataKey(ServiceInstance serviceInstance) {
@Override
protected String buildMetadataValue(ServiceInstance serviceInstance) {
WritableMetadataService writableMetadataService = WritableMetadataService.getExtension(
serviceInstance.getMetadata().get(METADATA_KEY)
serviceInstance.getMetadata().get(MEATADATA_STORED_TYPE_KEY)
);
SortedSet<String> exportedURLs = writableMetadataService.getExportedURLs();
Object[] data = exportedURLs.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

import java.util.SortedSet;

import static org.apache.dubbo.common.constants.CommonConstants.METADATA_KEY;
import static org.apache.dubbo.metadata.MetadataService.toURLs;
import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.MEATADATA_STORED_TYPE_KEY;
import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.METADATA_SERVICE_URL_PARAMS_KEY;
import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.getMetadataServiceParameter;

Expand All @@ -47,7 +47,7 @@ public String buildMetadataKey(ServiceInstance serviceInstance) {
public String buildMetadataValue(ServiceInstance serviceInstance) {

WritableMetadataService writableMetadataService = WritableMetadataService.getExtension(
serviceInstance.getMetadata().get(METADATA_KEY)
serviceInstance.getMetadata().get(MEATADATA_STORED_TYPE_KEY)
);

String serviceInterface = MetadataService.class.getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
public class RefreshServiceMetadataCustomizer implements ServiceInstanceCustomizer {

public int getPriority() {
return MAX_PRIORITY;
return MIN_PRIORITY;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@

import static java.lang.String.valueOf;
import static java.util.Objects.hash;
import static org.apache.dubbo.common.constants.CommonConstants.METADATA_KEY;
import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.EXPORTED_SERVICES_REVISION_KEY;
import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.MEATADATA_STORED_TYPE_KEY;
import static org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.SUBSCRIBER_SERVICES_REVISION_KEY;

/**
Expand All @@ -50,7 +49,7 @@ protected String buildMetadataKey(ServiceInstance serviceInstance) {
@Override
protected String buildMetadataValue(ServiceInstance serviceInstance) {
WritableMetadataService writableMetadataService = WritableMetadataService.getExtension(
serviceInstance.getMetadata().get(METADATA_KEY)
serviceInstance.getMetadata().get(MEATADATA_STORED_TYPE_KEY)
);
SortedSet<String> subscribedURLs = writableMetadataService.getSubscribedURLs();
Object[] data = subscribedURLs.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ abstract class BaseMetadataServiceProxyFactory implements MetadataServiceProxyFa

public final MetadataService getProxy(ServiceInstance serviceInstance) {
return proxies.computeIfAbsent(serviceInstance.getServiceName() + "##" +
serviceInstance.getMetadata().getOrDefault(ServiceInstanceMetadataUtils.EXPORTED_SERVICES_REVISION_KEY, ""), id -> createProxy(serviceInstance));
ServiceInstanceMetadataUtils.getExportedServicesRevision(serviceInstance), id -> createProxy(serviceInstance));
}

protected abstract MetadataService createProxy(ServiceInstance serviceInstance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public class RemoteMetadataServiceProxy implements MetadataService {

public RemoteMetadataServiceProxy(ServiceInstance serviceInstance) {
this.serviceName = serviceInstance.getServiceName();
this.revision = serviceInstance.getMetadata().getOrDefault(ServiceInstanceMetadataUtils.EXPORTED_SERVICES_REVISION_KEY, "");
this.revision = serviceInstance.getMetadata()
.getOrDefault(ServiceInstanceMetadataUtils.EXPORTED_SERVICES_REVISION_KEY, "");
}

@Override
Expand All @@ -45,7 +46,8 @@ public String serviceName() {
// TODO, protocol should be used
@Override
public SortedSet<String> getExportedURLs(String serviceInterface, String group, String version, String protocol) {
return toSortedStrings(getMetadataReport().getExportedURLs(new ServiceMetadataIdentifier(serviceInterface, group, version, PROVIDER_SIDE, revision)));
return toSortedStrings(getMetadataReport().getExportedURLs(
new ServiceMetadataIdentifier(serviceInterface, group, version, PROVIDER_SIDE, revision, protocol)));
}

private static SortedSet<String> toSortedStrings(Collection<String> values) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ protected void createEphemeral(String path, String data) {
@Override
public void delete(String path) {
try {
client.delete().forPath(path);
client.delete().deletingChildrenIfNeeded().forPath(path);
} catch (NoNodeException e) {
} catch (Exception e) {
throw new IllegalStateException(e.getMessage(), e);
Expand Down

0 comments on commit 2a30b22

Please sign in to comment.