Search in sources :

Example 1 with MetadataInfo

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;
}
Also used : MetadataInfo(org.apache.dubbo.metadata.MetadataInfo) ServiceInstance(org.apache.dubbo.registry.client.ServiceInstance) Provider(org.apache.dubbo.admin.model.domain.Provider)

Example 2 with MetadataInfo

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);
}
Also used : MetadataInfo(org.apache.dubbo.metadata.MetadataInfo)

Example 3 with MetadataInfo

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));
}
Also used : MetadataReport(org.apache.dubbo.metadata.report.MetadataReport) MetadataInfo(org.apache.dubbo.metadata.MetadataInfo) HashMap(java.util.HashMap) URL(org.apache.dubbo.common.URL) ServiceMetadataIdentifier(org.apache.dubbo.metadata.report.identifier.ServiceMetadataIdentifier) SubscriberMetadataIdentifier(org.apache.dubbo.metadata.report.identifier.SubscriberMetadataIdentifier) MetadataIdentifier(org.apache.dubbo.metadata.report.identifier.MetadataIdentifier) SubscriberMetadataIdentifier(org.apache.dubbo.metadata.report.identifier.SubscriberMetadataIdentifier) ServiceMetadataIdentifier(org.apache.dubbo.metadata.report.identifier.ServiceMetadataIdentifier) ServiceDefinition(org.apache.dubbo.metadata.definition.model.ServiceDefinition) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 4 with MetadataInfo

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();
}
Also used : MetadataInfo(org.apache.dubbo.metadata.MetadataInfo) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ServiceInstance(org.apache.dubbo.registry.client.ServiceInstance) DefaultServiceInstance(org.apache.dubbo.registry.client.DefaultServiceInstance) URL(org.apache.dubbo.common.URL) DefaultServiceInstance(org.apache.dubbo.registry.client.DefaultServiceInstance) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with MetadataInfo

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;
}
Also used : MetadataInfo(org.apache.dubbo.metadata.MetadataInfo) RemoteMetadataServiceImpl(org.apache.dubbo.registry.client.metadata.store.RemoteMetadataServiceImpl) MetadataService(org.apache.dubbo.metadata.MetadataService)

Aggregations

MetadataInfo (org.apache.dubbo.metadata.MetadataInfo)10 HashMap (java.util.HashMap)4 URL (org.apache.dubbo.common.URL)3 SubscriberMetadataIdentifier (org.apache.dubbo.metadata.report.identifier.SubscriberMetadataIdentifier)3 HashSet (java.util.HashSet)2 ServiceDefinition (org.apache.dubbo.metadata.definition.model.ServiceDefinition)2 MetadataReport (org.apache.dubbo.metadata.report.MetadataReport)2 MetadataIdentifier (org.apache.dubbo.metadata.report.identifier.MetadataIdentifier)2 ServiceMetadataIdentifier (org.apache.dubbo.metadata.report.identifier.ServiceMetadataIdentifier)2 ServiceInstance (org.apache.dubbo.registry.client.ServiceInstance)2 Test (org.junit.jupiter.api.Test)2 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 Provider (org.apache.dubbo.admin.model.domain.Provider)1 InfraAdapter (org.apache.dubbo.common.infra.InfraAdapter)1 ServiceInfo (org.apache.dubbo.metadata.MetadataInfo.ServiceInfo)1