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);
}
}
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;
}
}
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);
}
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;
}
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();
}
}
}
Aggregations