Search in sources :

Example 1 with ResourceFilter

use of com.tencent.polaris.api.plugin.registry.ResourceFilter in project polaris-java by polarismesh.

the class InstancesDetectTask method doInstanceDetectForService.

private void doInstanceDetectForService(ServiceKey serviceKey) throws PolarisException {
    ServiceEventKey svcEventKey = new ServiceEventKey(serviceKey, EventType.INSTANCE);
    ServiceInstances instances = extensions.getLocalRegistry().getInstances(new ResourceFilter(svcEventKey, true, true));
    if (!instances.isInitialized() || instances.getInstances().size() == 0) {
        return;
    }
    Map<Instance, DetectResult> aliveResults = new HashMap<>();
    for (Instance instance : instances.getInstances()) {
        if (destroy.get()) {
            // 如果要停止定时任务,则剩下的实例探测也没必要进行下去了
            break;
        }
        DetectResult result = detectInstance(instance);
        if (result == null || result.getRetStatus() != RetStatus.RetSuccess) {
            continue;
        }
        aliveResults.put(instance, result);
    }
    if (MapUtils.isNotEmpty(aliveResults)) {
        ServiceUpdateRequest updateRequest = buildInstanceUpdateResult(serviceKey, aliveResults);
        LOG.info("update cache for outlier detect, value is {}", updateRequest);
        extensions.getLocalRegistry().updateInstances(updateRequest);
    }
}
Also used : ResourceFilter(com.tencent.polaris.api.plugin.registry.ResourceFilter) ServiceInstances(com.tencent.polaris.api.pojo.ServiceInstances) Instance(com.tencent.polaris.api.pojo.Instance) HashMap(java.util.HashMap) ServiceUpdateRequest(com.tencent.polaris.api.plugin.registry.ServiceUpdateRequest) ServiceEventKey(com.tencent.polaris.api.pojo.ServiceEventKey) DetectResult(com.tencent.polaris.api.pojo.DetectResult)

Example 2 with ResourceFilter

use of com.tencent.polaris.api.plugin.registry.ResourceFilter in project polaris-java by polarismesh.

the class BaseFlow method loadLocalResources.

private static boolean loadLocalResources(ServiceEventKey svcEventKey, ResourcesResponse resourcesResponse, LocalRegistry localRegistry) {
    ResourceFilter filter = new ResourceFilter(svcEventKey, false, true);
    if (svcEventKey.getEventType() == EventType.INSTANCE) {
        ServiceInstances instances = localRegistry.getInstances(filter);
        if (instances.isInitialized()) {
            resourcesResponse.addServiceInstances(svcEventKey, instances);
            return false;
        } else {
            return true;
        }
    }
    if (svcEventKey.getEventType() == EventType.SERVICE) {
        Services services = localRegistry.getServices(filter);
        if (services.isInitialized()) {
            resourcesResponse.addServices(svcEventKey, services);
            return false;
        } else {
            return true;
        }
    }
    ServiceRule serviceRule = localRegistry.getServiceRule(filter);
    if (serviceRule.isInitialized()) {
        resourcesResponse.addServiceRule(svcEventKey, serviceRule);
        return false;
    } else {
        return true;
    }
}
Also used : Services(com.tencent.polaris.api.pojo.Services) ResourceFilter(com.tencent.polaris.api.plugin.registry.ResourceFilter) ServiceInstances(com.tencent.polaris.api.pojo.ServiceInstances) ServiceRule(com.tencent.polaris.api.pojo.ServiceRule)

Example 3 with ResourceFilter

use of com.tencent.polaris.api.plugin.registry.ResourceFilter in project polaris-java by polarismesh.

the class GetResourcesInvoker method processSvcEventKey.

private int processSvcEventKey(LocalRegistry localRegistry, int callbacks, ServiceEventKey svcEventKey) {
    ResourceFilter filter = new ResourceFilter(svcEventKey, internalRequest, useCache);
    switch(svcEventKey.getEventType()) {
        case INSTANCE:
            ServiceInstances instances = localRegistry.getInstances(filter);
            if (instances.isInitialized()) {
                resourcesResponse.addServiceInstances(svcEventKey, instances);
            } else {
                localRegistry.loadInstances(svcEventKey, this);
                callbacks++;
            }
            break;
        case SERVICE:
            Services services = localRegistry.getServices(filter);
            if (services.isInitialized()) {
                resourcesResponse.addServices(svcEventKey, services);
            } else {
                localRegistry.loadServices(svcEventKey, this);
                callbacks++;
            }
            break;
        default:
            ServiceRule serviceRule = localRegistry.getServiceRule(filter);
            if (serviceRule.isInitialized()) {
                resourcesResponse.addServiceRule(svcEventKey, serviceRule);
            } else {
                localRegistry.loadServiceRule(svcEventKey, this);
                callbacks++;
            }
            break;
    }
    return callbacks;
}
Also used : Services(com.tencent.polaris.api.pojo.Services) ResourceFilter(com.tencent.polaris.api.plugin.registry.ResourceFilter) ServiceInstances(com.tencent.polaris.api.pojo.ServiceInstances) ServiceRule(com.tencent.polaris.api.pojo.ServiceRule)

Example 4 with ResourceFilter

use of com.tencent.polaris.api.plugin.registry.ResourceFilter in project polaris-java by polarismesh.

the class GetResourcesInvoker method complete.

@Override
public void complete(ServiceEventKey svcEventKey) {
    LocalRegistry localRegistry = extensions.getLocalRegistry();
    ResourceFilter filter = new ResourceFilter(svcEventKey, internalRequest, useCache);
    if (svcEventKey.getEventType() == ServiceEventKey.EventType.INSTANCE) {
        ServiceInstances instances = localRegistry.getInstances(filter);
        resourcesResponse.addServiceInstances(svcEventKey, instances);
    } else if (svcEventKey.getEventType() == EventType.SERVICE) {
        Services services = localRegistry.getServices(filter);
        resourcesResponse.addServices(svcEventKey, services);
    } else {
        ServiceRule serviceRule = localRegistry.getServiceRule(filter);
        resourcesResponse.addServiceRule(svcEventKey, serviceRule);
    }
    synchronized (notifier) {
        int curTotal = responseIncrement.addAndGet(1);
        if (totalCallback == curTotal) {
            notifier.notifyAll();
        }
    }
}
Also used : Services(com.tencent.polaris.api.pojo.Services) ResourceFilter(com.tencent.polaris.api.plugin.registry.ResourceFilter) ServiceInstances(com.tencent.polaris.api.pojo.ServiceInstances) ServiceRule(com.tencent.polaris.api.pojo.ServiceRule) LocalRegistry(com.tencent.polaris.api.plugin.registry.LocalRegistry)

Example 5 with ResourceFilter

use of com.tencent.polaris.api.plugin.registry.ResourceFilter in project polaris-java by polarismesh.

the class ChangeStateUtils method getInstance.

/**
 * 获取实例ID
 *
 * @param instanceGauge 实例统计数据
 * @param localRegistry 本地缓存插件
 * @return ID
 */
public static InstanceByProto getInstance(InstanceGauge instanceGauge, LocalRegistry localRegistry) {
    ServiceEventKey serviceEventKey = new ServiceEventKey(new ServiceKey(instanceGauge.getNamespace(), instanceGauge.getService()), EventType.INSTANCE);
    ResourceFilter resourceFilter = new ResourceFilter(serviceEventKey, true, true);
    ServiceInstances instances = localRegistry.getInstances(resourceFilter);
    if (!instances.isInitialized()) {
        return null;
    }
    ServiceInstancesByProto serviceInstancesByProto = (ServiceInstancesByProto) instances;
    Instance instance = instanceGauge.getInstance();
    if (instance instanceof InstanceByProto) {
        return (InstanceByProto) instance;
    }
    InstanceByProto instanceByProto;
    String instanceId = instanceGauge.getInstanceId();
    if (StringUtils.isNotBlank(instanceId)) {
        instanceByProto = serviceInstancesByProto.getInstance(instanceId);
    } else {
        Node node = new Node(instanceGauge.getHost(), instanceGauge.getPort());
        instanceByProto = serviceInstancesByProto.getInstanceByNode(node);
    }
    if (null != instanceByProto) {
        instanceGauge.setInstance(instanceByProto);
    }
    return instanceByProto;
}
Also used : ResourceFilter(com.tencent.polaris.api.plugin.registry.ResourceFilter) ServiceInstances(com.tencent.polaris.api.pojo.ServiceInstances) InstanceByProto(com.tencent.polaris.client.pojo.InstanceByProto) Instance(com.tencent.polaris.api.pojo.Instance) Node(com.tencent.polaris.client.pojo.Node) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) ServiceEventKey(com.tencent.polaris.api.pojo.ServiceEventKey) ServiceInstancesByProto(com.tencent.polaris.client.pojo.ServiceInstancesByProto)

Aggregations

ResourceFilter (com.tencent.polaris.api.plugin.registry.ResourceFilter)6 ServiceInstances (com.tencent.polaris.api.pojo.ServiceInstances)5 ServiceEventKey (com.tencent.polaris.api.pojo.ServiceEventKey)3 ServiceRule (com.tencent.polaris.api.pojo.ServiceRule)3 Services (com.tencent.polaris.api.pojo.Services)3 Instance (com.tencent.polaris.api.pojo.Instance)2 ServiceInstancesByProto (com.tencent.polaris.client.pojo.ServiceInstancesByProto)2 LocalRegistry (com.tencent.polaris.api.plugin.registry.LocalRegistry)1 ServiceUpdateRequest (com.tencent.polaris.api.plugin.registry.ServiceUpdateRequest)1 DetectResult (com.tencent.polaris.api.pojo.DetectResult)1 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)1 InstanceByProto (com.tencent.polaris.client.pojo.InstanceByProto)1 Node (com.tencent.polaris.client.pojo.Node)1 HashMap (java.util.HashMap)1