use of org.apache.dubbo.metadata.MetadataInfo in project incubator-dubbo-ops by apache.
the class InstanceRegistryQueryHelper method urlsToProviderList.
private List<Provider> urlsToProviderList(List<InstanceAddressURL> urls) {
List<Provider> providers = Lists.newArrayList();
urls.stream().distinct().forEach(url -> {
ServiceInstance instance = url.getInstance();
MetadataInfo metadataInfo = url.getMetadataInfo();
metadataInfo.getServices().forEach((serviceKey, serviceInfo) -> {
Provider p = new Provider();
String service = serviceInfo.getServiceKey();
p.setService(service);
p.setAddress(url.getAddress());
p.setApplication(instance.getServiceName());
p.setUrl(url.toParameterString());
p.setDynamic(url.getParameter("dynamic", true));
p.setEnabled(url.getParameter(Constants.ENABLED_KEY, true));
p.setWeight(url.getParameter(Constants.WEIGHT_KEY, Constants.DEFAULT_WEIGHT));
p.setUsername(url.getParameter("owner"));
p.setRegistrySource(RegistrySource.INSTANCE);
providers.add(p);
});
});
return providers;
}
use of org.apache.dubbo.metadata.MetadataInfo in project dubbo by alibaba.
the class InMemoryWritableMetadataService method unexportURL.
@Override
public boolean unexportURL(URL url) {
String registryCluster = RegistryClusterIdentifier.getExtension(url).providerKey(url);
String[] clusters = registryCluster.split(",");
for (String cluster : clusters) {
MetadataInfo metadataInfo = metadataInfos.get(cluster);
metadataInfo.removeService(url.getProtocolServiceKey());
if (metadataInfo.getServices().isEmpty()) {
metadataInfos.remove(cluster);
}
}
metadataSemaphore.release();
return removeURL(exportedServiceURLs, url);
}
use of org.apache.dubbo.metadata.MetadataInfo in project dubbo by alibaba.
the class FailoverMetadataReportTest method testLocalDataCenterMetadataReport.
@Test
public void testLocalDataCenterMetadataReport() {
URL url = mockURL.addParameter("strategy", "local");
FailoverMetadataReport report = getFailoverReport(url);
Assertions.assertNotNull(report.getProxyReports(), "metadata reports should not be null.");
Assertions.assertEquals(2, report.getProxyReports().size(), "expect 2 metadata report, actual " + report.getProxyReports().size());
MetadataReport localReport = null, failoverReport = null;
for (FailoverMetadataReport.MetadataReportHolder holder : report.getProxyReports()) {
if (holder.url.getBackupAddress().contains(url.getAddress())) {
localReport = holder.report;
} else {
failoverReport = holder.report;
}
}
Assertions.assertNotNull(localReport);
Assertions.assertNotNull(failoverReport);
MetadataIdentifier identifier = new MetadataIdentifier("helloService", null, null, null, "test");
ServiceDefinition definition = new ServiceDefinition();
definition.setCanonicalName("helloService");
report.storeProviderMetadata(identifier, definition);
// assert local metadata report write already.
Assertions.assertNotNull(report.getServiceDefinition(identifier));
Assertions.assertNotNull(localReport.getServiceDefinition(identifier));
Assertions.assertNull(failoverReport.getServiceDefinition(identifier));
HashMap parameterMap = new HashMap();
report.storeConsumerMetadata(identifier, parameterMap);
// assert local metadata report write already.
Assertions.assertEquals(parameterMap, ((MockMetadataReport) localReport).consumerMetadata.get(identifier));
Assertions.assertNotEquals(parameterMap, ((MockMetadataReport) failoverReport).consumerMetadata.get(identifier));
SubscriberMetadataIdentifier subscribeIdentifier = new SubscriberMetadataIdentifier("test", "1.0");
MetadataInfo metadataInfo = new MetadataInfo(subscribeIdentifier.getApplication(), subscribeIdentifier.getRevision(), null);
report.publishAppMetadata(subscribeIdentifier, metadataInfo);
// assert all metadata report write already.
Assertions.assertEquals(metadataInfo, report.getAppMetadata(subscribeIdentifier, null));
Assertions.assertEquals(metadataInfo, localReport.getAppMetadata(subscribeIdentifier, null));
Assertions.assertNotEquals(metadataInfo, failoverReport.getAppMetadata(subscribeIdentifier, null));
report.registerServiceAppMapping("helloService", "test", null);
Set<String> appNames = report.getServiceAppMapping("helloService", null, null);
// assert local metadata report write already.
Assertions.assertEquals(appNames, report.getServiceAppMapping("helloService", null, null));
Assertions.assertEquals(appNames, localReport.getServiceAppMapping("helloService", null, null));
Assertions.assertNotEquals(appNames, failoverReport.getServiceAppMapping("helloService", null, null));
ServiceMetadataIdentifier serviceIdentifier = new ServiceMetadataIdentifier("helloService", null, null, null, "1.0", "dubbo");
report.saveServiceMetadata(serviceIdentifier, url);
// assert local metadata report write already.
Assertions.assertNotNull(report.getExportedURLs(serviceIdentifier));
Assertions.assertNotNull(localReport.getExportedURLs(serviceIdentifier));
Assertions.assertNull(failoverReport.getExportedURLs(serviceIdentifier));
Set<String> urls = new HashSet<>();
urls.add(url.toFullString());
report.saveSubscribedData(subscribeIdentifier, urls);
// assert local metadata report write already.
Assertions.assertEquals(new ArrayList<>(urls), report.getSubscribedURLs(subscribeIdentifier));
Assertions.assertEquals(new ArrayList<>(urls), localReport.getSubscribedURLs(subscribeIdentifier));
Assertions.assertNotEquals(new ArrayList<>(urls), failoverReport.getSubscribedURLs(subscribeIdentifier));
}
use of org.apache.dubbo.metadata.MetadataInfo in project dubbo by alibaba.
the class ServiceInstancesChangedListener method onEvent.
/**
* On {@link ServiceInstancesChangedEvent the service instances change event}
*
* @param event {@link ServiceInstancesChangedEvent}
*/
public synchronized void onEvent(ServiceInstancesChangedEvent event) {
logger.info("Received instance notification, serviceName: " + event.getServiceName() + ", instances: " + event.getServiceInstances().size());
String appName = event.getServiceName();
allInstances.put(appName, event.getServiceInstances());
if (logger.isDebugEnabled()) {
logger.debug(event.getServiceInstances().toString());
}
Map<String, List<ServiceInstance>> revisionToInstances = new HashMap<>();
Map<String, Set<String>> localServiceToRevisions = new HashMap<>();
Map<Set<String>, List<URL>> revisionsToUrls = new HashMap();
Map<String, List<URL>> tmpServiceUrls = new HashMap<>();
for (Map.Entry<String, List<ServiceInstance>> entry : allInstances.entrySet()) {
List<ServiceInstance> instances = entry.getValue();
for (ServiceInstance instance : instances) {
String revision = getExportedServicesRevision(instance);
if (DEFAULT_REVISION.equals(revision)) {
logger.info("Find instance without valid service metadata: " + instance.getAddress());
continue;
}
List<ServiceInstance> subInstances = revisionToInstances.computeIfAbsent(revision, r -> new LinkedList<>());
subInstances.add(instance);
MetadataInfo metadata = revisionToMetadata.get(revision);
if (metadata == null) {
metadata = getMetadataInfo(instance);
logger.info("MetadataInfo for instance " + instance.getAddress() + "?revision=" + revision + " is " + metadata);
if (metadata != null) {
revisionToMetadata.put(revision, metadata);
} else {
}
}
if (metadata != null) {
parseMetadata(revision, metadata, localServiceToRevisions);
((DefaultServiceInstance) instance).setServiceMetadata(metadata);
}
// else {
// logger.error("Failed to load service metadata for instance " + instance);
// Set<String> set = localServiceToRevisions.computeIfAbsent(url.getServiceKey(), k -> new TreeSet<>());
// set.add(revision);
// }
}
localServiceToRevisions.forEach((serviceKey, revisions) -> {
List<URL> urls = revisionsToUrls.get(revisions);
if (urls != null) {
tmpServiceUrls.put(serviceKey, urls);
} else {
urls = new ArrayList<>();
for (String r : revisions) {
for (ServiceInstance i : revisionToInstances.get(r)) {
urls.add(i.toURL());
}
}
revisionsToUrls.put(revisions, urls);
tmpServiceUrls.put(serviceKey, urls);
}
});
}
this.serviceUrls = tmpServiceUrls;
this.notifyAddressChanged();
}
use of org.apache.dubbo.metadata.MetadataInfo in project dubbo by alibaba.
the class ServiceInstancesChangedListener method getMetadataInfo.
private MetadataInfo getMetadataInfo(ServiceInstance instance) {
String metadataType = ServiceInstanceMetadataUtils.getMetadataStorageType(instance);
// FIXME, check "REGISTRY_CLUSTER_KEY" must be set by every registry implementation.
instance.getExtendParams().putIfAbsent(REGISTRY_CLUSTER_KEY, RegistryClusterIdentifier.getExtension(url).consumerKey(url));
MetadataInfo metadataInfo = null;
try {
if (logger.isDebugEnabled()) {
logger.debug("Instance " + instance.getAddress() + " is using metadata type " + metadataType);
}
if (REMOTE_METADATA_STORAGE_TYPE.equals(metadataType)) {
RemoteMetadataServiceImpl remoteMetadataService = MetadataUtils.getRemoteMetadataService();
metadataInfo = remoteMetadataService.getMetadata(instance);
} else {
MetadataService metadataServiceProxy = MetadataUtils.getMetadataServiceProxy(instance, serviceDiscovery);
metadataInfo = metadataServiceProxy.getMetadataInfo(ServiceInstanceMetadataUtils.getExportedServicesRevision(instance));
}
if (logger.isDebugEnabled()) {
logger.debug("Metadata " + metadataInfo.toString());
}
} catch (Exception e) {
logger.error("Failed to load service metadata, metadata type is " + metadataType, e);
// TODO, load metadata backup. Stop getting metadata after x times of failure for one revision?
}
return metadataInfo;
}
Aggregations