Search in sources :

Example 1 with ServiceInstances

use of com.tencent.polaris.api.pojo.ServiceInstances 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 ServiceInstances

use of com.tencent.polaris.api.pojo.ServiceInstances 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 ServiceInstances

use of com.tencent.polaris.api.pojo.ServiceInstances in project polaris-java by polarismesh.

the class BaseFlow method commonGetOneInstance.

/**
 * 通用获取单个服务实例的方法,用于SDK内部调用
 *
 * @param extensions 插件上下文
 * @param serviceKey 服务信息
 * @param coreRouterNames 核心路由插件链
 * @param lbPolicy 负载均衡策略
 * @param protocol 协议信息
 * @param hashKey 一致性hash的key
 * @return 过滤后的实例
 */
public static Instance commonGetOneInstance(Extensions extensions, ServiceKey serviceKey, List<String> coreRouterNames, String lbPolicy, String protocol, String hashKey) {
    ServiceEventKey svcEventKey = new ServiceEventKey(serviceKey, EventType.INSTANCE);
    LOG.info("[ConnectionManager]start to discover service {}", svcEventKey);
    DefaultServiceEventKeysProvider provider = new DefaultServiceEventKeysProvider();
    provider.setSvcEventKey(svcEventKey);
    // 为性能考虑,优先使用本地缓存
    provider.setUseCache(true);
    FlowControlParam flowControlParam = new DefaultFlowControlParam();
    APIConfig apiConfig = extensions.getConfiguration().getGlobal().getAPI();
    flowControlParam.setTimeoutMs(apiConfig.getTimeout());
    flowControlParam.setMaxRetry(apiConfig.getMaxRetryTimes());
    flowControlParam.setRetryIntervalMs(apiConfig.getRetryInterval());
    // 执行服务路由
    ServiceInfo dstSvcInfo = new ServiceInfo();
    Map<String, String> metadata = new HashMap<>();
    metadata.put("protocol", protocol);
    dstSvcInfo.setMetadata(metadata);
    RouteInfo routeInfo = new RouteInfo(null, null, dstSvcInfo, null, "");
    ResourcesResponse resourcesResponse = BaseFlow.syncGetResources(extensions, false, provider, flowControlParam);
    LOG.info("[ConnectionManager]success to discover service {}", svcEventKey);
    ServiceInstances serviceInstances = resourcesResponse.getServiceInstances(svcEventKey);
    RouterChainGroup sysRouterChainGroup = extensions.getSysRouterChainGroup();
    List<ServiceRouter> coreRouters = Extensions.loadServiceRouters(coreRouterNames, extensions.getPlugins(), false);
    RouterChainGroup routerChainGroup = new DefaultRouterChainGroup(sysRouterChainGroup.getBeforeRouters(), coreRouters, sysRouterChainGroup.getAfterRouters());
    ServiceInstances instancesAfterRoute = BaseFlow.processServiceRouters(routeInfo, serviceInstances, routerChainGroup);
    // 执行负载均衡
    LoadBalancer loadBalancer = (LoadBalancer) extensions.getPlugins().getPlugin(PluginTypes.LOAD_BALANCER.getBaseType(), lbPolicy);
    Criteria criteria = new Criteria();
    criteria.setHashKey(hashKey);
    return BaseFlow.processLoadBalance(loadBalancer, criteria, instancesAfterRoute);
}
Also used : ServiceRouter(com.tencent.polaris.api.plugin.route.ServiceRouter) DefaultServiceEventKeysProvider(com.tencent.polaris.api.pojo.DefaultServiceEventKeysProvider) HashMap(java.util.HashMap) LoadBalancer(com.tencent.polaris.api.plugin.loadbalance.LoadBalancer) Criteria(com.tencent.polaris.api.rpc.Criteria) ServiceInfo(com.tencent.polaris.api.pojo.ServiceInfo) ServiceInstances(com.tencent.polaris.api.pojo.ServiceInstances) APIConfig(com.tencent.polaris.api.config.global.APIConfig) DefaultRouterChainGroup(com.tencent.polaris.api.plugin.compose.DefaultRouterChainGroup) RouterChainGroup(com.tencent.polaris.api.plugin.compose.RouterChainGroup) ServiceEventKey(com.tencent.polaris.api.pojo.ServiceEventKey) RouteInfo(com.tencent.polaris.api.plugin.route.RouteInfo) DefaultRouterChainGroup(com.tencent.polaris.api.plugin.compose.DefaultRouterChainGroup)

Example 4 with ServiceInstances

use of com.tencent.polaris.api.pojo.ServiceInstances 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 5 with ServiceInstances

use of com.tencent.polaris.api.pojo.ServiceInstances 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)

Aggregations

ServiceInstances (com.tencent.polaris.api.pojo.ServiceInstances)25 Instance (com.tencent.polaris.api.pojo.Instance)12 Server (com.netflix.loadbalancer.Server)8 ArrayList (java.util.ArrayList)8 DefaultServiceInstances (com.tencent.polaris.api.pojo.DefaultServiceInstances)7 InstancesResponse (com.tencent.polaris.api.rpc.InstancesResponse)7 PolarisServer (com.tencent.cloud.common.pojo.PolarisServer)6 ProcessRoutersRequest (com.tencent.polaris.router.api.rpc.ProcessRoutersRequest)6 ResourceFilter (com.tencent.polaris.api.plugin.registry.ResourceFilter)5 DefaultInstance (com.tencent.polaris.api.pojo.DefaultInstance)4 ServiceEventKey (com.tencent.polaris.api.pojo.ServiceEventKey)4 ServiceInfo (com.tencent.polaris.api.pojo.ServiceInfo)4 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)4 ProcessRoutersResponse (com.tencent.polaris.router.api.rpc.ProcessRoutersResponse)4 ApplicationContextAwareUtils (com.tencent.cloud.common.util.ApplicationContextAwareUtils)3 ServiceRule (com.tencent.polaris.api.pojo.ServiceRule)3 Services (com.tencent.polaris.api.pojo.Services)3 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3