Search in sources :

Example 21 with ServiceEventKey

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

the class Validator method validateGetResourcesRequest.

/**
 * 校验获取资源请求
 *
 * @param request 请求对象
 * @throws PolarisException 校验失败
 */
public static void validateGetResourcesRequest(GetResourcesRequest request) throws PolarisException {
    Set<ServiceEventKey> svcEventKeys = request.getSvcEventKeys();
    if (CollectionUtils.isEmpty(svcEventKeys)) {
        return;
    }
    for (ServiceEventKey svcEventKey : svcEventKeys) {
        ServiceKey serviceKey = svcEventKey.getServiceKey();
        ServiceEventKey.EventType eventType = svcEventKey.getEventType();
        if (null == serviceKey || null == eventType) {
            throw new PolarisException(ErrorCode.API_INVALID_ARGUMENT, "missing service key or event type");
        }
        CommonValidator.validateNamespaceService(serviceKey.getNamespace(), serviceKey.getService());
    }
}
Also used : PolarisException(com.tencent.polaris.api.exception.PolarisException) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) ServiceEventKey(com.tencent.polaris.api.pojo.ServiceEventKey)

Example 22 with ServiceEventKey

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

the class MessagePersistHandler method loadPersistedServices.

/**
 * 遍历缓存目录并加载之前缓存的服务信息
 *
 * @param message 消息对象
 * @return 服务标识-消息对象的集合
 */
public Map<ServiceEventKey, Message> loadPersistedServices(Message message) {
    Path curDir = Paths.get(persistDirPath);
    Map<ServiceEventKey, Message> result = new HashMap<>();
    try {
        Files.walkFileTree(curDir, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path filePath, BasicFileAttributes attrs) {
                Path fileNamePath = filePath.getFileName();
                if (null == fileNamePath) {
                    return FileVisitResult.CONTINUE;
                }
                String fileName = fileNamePath.toString();
                if (!REGEX_PATTERN_SERVICE.matcher(fileName).matches()) {
                    return FileVisitResult.CONTINUE;
                }
                ServiceEventKey svcEventKey = fileNameToServiceKey(fileName);
                int retryTimes = 0;
                Message readMessage = null;
                while (retryTimes <= maxReadRetry) {
                    retryTimes++;
                    Message.Builder builder = message.newBuilderForType();
                    readMessage = loadMessage(filePath.toFile(), builder);
                    if (null == readMessage) {
                        Utils.sleepUninterrupted(retryInterval);
                        continue;
                    }
                    break;
                }
                if (null == readMessage) {
                    LOG.debug("fail to read service from {} after retry {} times", fileName, retryTimes);
                    return FileVisitResult.CONTINUE;
                }
                result.put(svcEventKey, readMessage);
                return FileVisitResult.CONTINUE;
            }
        });
    } catch (IOException e) {
        LOG.error("fail to visit cache directory {}", persistDirPath);
    }
    return result;
}
Also used : Path(java.nio.file.Path) Message(com.google.protobuf.Message) HashMap(java.util.HashMap) ServiceEventKey(com.tencent.polaris.api.pojo.ServiceEventKey) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 23 with ServiceEventKey

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

the class DefaultRouterAPI method processRouters.

@Override
public ProcessRoutersResponse processRouters(ProcessRoutersRequest request) throws PolarisException {
    checkAvailable("RouterAPI");
    RouterValidator.validateProcessRouterRequest(request);
    RouterNamesGroup requestRouterGroup = request.getRouters();
    List<ServiceRouter> beforeRouters;
    if (null != requestRouterGroup && CollectionUtils.isNotEmpty(requestRouterGroup.getBeforeRouters())) {
        beforeRouters = Extensions.loadServiceRouters(requestRouterGroup.getBeforeRouters(), extensions.getPlugins(), true);
    } else {
        beforeRouters = extensions.getConfigRouterChainGroup().getBeforeRouters();
    }
    List<ServiceRouter> afterRouters;
    if (null != requestRouterGroup && CollectionUtils.isNotEmpty(requestRouterGroup.getAfterRouters())) {
        afterRouters = Extensions.loadServiceRouters(requestRouterGroup.getAfterRouters(), extensions.getPlugins(), true);
    } else {
        afterRouters = extensions.getConfigRouterChainGroup().getAfterRouters();
    }
    List<ServiceRouter> coreRouters;
    if (null != requestRouterGroup && CollectionUtils.isNotEmpty(requestRouterGroup.getCoreRouters())) {
        coreRouters = Extensions.loadServiceRouters(requestRouterGroup.getCoreRouters(), extensions.getPlugins(), true);
    } else {
        coreRouters = extensions.getConfigRouterChainGroup().getCoreRouters();
    }
    ServiceInstances dstInstances = request.getDstInstances();
    RouteInfo routeInfo = new RouteInfo(request.getSourceService(), dstInstances, request.getMethod());
    // 获取路由规则
    DefaultFlowControlParam engineFlowControlParam = new DefaultFlowControlParam();
    BaseFlow.buildFlowControlParam(request, config, engineFlowControlParam);
    Set<ServiceEventKey> routerKeys = new HashSet<>();
    ServiceEventKey dstSvcEventKey = new ServiceEventKey(new ServiceKey(dstInstances.getNamespace(), dstInstances.getService()), EventType.ROUTING);
    routerKeys.add(dstSvcEventKey);
    ServiceEventKey srcSvcEventKey = null;
    if (null != routeInfo.getSourceService() && StringUtils.isNotBlank(routeInfo.getSourceService().getNamespace()) && StringUtils.isNotBlank(routeInfo.getSourceService().getService())) {
        srcSvcEventKey = new ServiceEventKey(new ServiceKey(routeInfo.getSourceService().getNamespace(), routeInfo.getSourceService().getService()), EventType.ROUTING);
        routerKeys.add(srcSvcEventKey);
    }
    DefaultServiceEventKeysProvider svcKeysProvider = new DefaultServiceEventKeysProvider();
    svcKeysProvider.setSvcEventKeys(routerKeys);
    ResourcesResponse resourcesResponse = BaseFlow.syncGetResources(extensions, false, svcKeysProvider, engineFlowControlParam);
    routeInfo.setDestRouteRule(resourcesResponse.getServiceRule(dstSvcEventKey));
    if (null != srcSvcEventKey) {
        routeInfo.setSourceRouteRule(resourcesResponse.getServiceRule(srcSvcEventKey));
    }
    // 执行路由
    DefaultRouterChainGroup routerChainGroup = new DefaultRouterChainGroup(beforeRouters, coreRouters, afterRouters);
    ServiceInstances svcInstances = BaseFlow.processServiceRouters(routeInfo, dstInstances, routerChainGroup);
    return new ProcessRoutersResponse(svcInstances);
}
Also used : ServiceRouter(com.tencent.polaris.api.plugin.route.ServiceRouter) DefaultServiceEventKeysProvider(com.tencent.polaris.api.pojo.DefaultServiceEventKeysProvider) RouterNamesGroup(com.tencent.polaris.router.api.rpc.ProcessRoutersRequest.RouterNamesGroup) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) ResourcesResponse(com.tencent.polaris.client.flow.ResourcesResponse) ProcessRoutersResponse(com.tencent.polaris.router.api.rpc.ProcessRoutersResponse) DefaultFlowControlParam(com.tencent.polaris.client.flow.DefaultFlowControlParam) ServiceInstances(com.tencent.polaris.api.pojo.ServiceInstances) ServiceEventKey(com.tencent.polaris.api.pojo.ServiceEventKey) RouteInfo(com.tencent.polaris.api.plugin.route.RouteInfo) DefaultRouterChainGroup(com.tencent.polaris.api.plugin.compose.DefaultRouterChainGroup) HashSet(java.util.HashSet)

Aggregations

ServiceEventKey (com.tencent.polaris.api.pojo.ServiceEventKey)23 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)11 DefaultServiceEventKeysProvider (com.tencent.polaris.api.pojo.DefaultServiceEventKeysProvider)5 HashMap (java.util.HashMap)5 ServiceInstances (com.tencent.polaris.api.pojo.ServiceInstances)4 ResourcesResponse (com.tencent.polaris.client.flow.ResourcesResponse)4 ServiceInstancesByProto (com.tencent.polaris.client.pojo.ServiceInstancesByProto)4 PolarisException (com.tencent.polaris.api.exception.PolarisException)3 ResourceFilter (com.tencent.polaris.api.plugin.registry.ResourceFilter)3 RegistryCacheValue (com.tencent.polaris.api.pojo.RegistryCacheValue)3 Message (com.google.protobuf.Message)2 DefaultRouterChainGroup (com.tencent.polaris.api.plugin.compose.DefaultRouterChainGroup)2 CachedStatus (com.tencent.polaris.api.plugin.registry.CacheHandler.CachedStatus)2 RouteInfo (com.tencent.polaris.api.plugin.route.RouteInfo)2 ServiceRouter (com.tencent.polaris.api.plugin.route.ServiceRouter)2 Instance (com.tencent.polaris.api.pojo.Instance)2 EventType (com.tencent.polaris.api.pojo.ServiceEventKey.EventType)2 ServiceInfo (com.tencent.polaris.api.pojo.ServiceInfo)2 ServiceRule (com.tencent.polaris.api.pojo.ServiceRule)2 ServiceProto (com.tencent.polaris.client.pb.ServiceProto)2