Search in sources :

Example 6 with ServiceEventKey

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

the class MessagePersistHandler method fileNameToServiceKey.

private static ServiceEventKey fileNameToServiceKey(String fileName) {
    fileName = fileName.substring(0, fileName.length() - CACHE_SUFFIX.length());
    String[] pieces = fileName.split("#");
    try {
        String namespace = URLDecoder.decode(pieces[1], "UTF-8");
        String service = URLDecoder.decode(pieces[2], "UTF-8");
        String eventTypeStr = URLDecoder.decode(pieces[3], "UTF-8");
        ServiceEventKey.EventType eventType = ServiceEventKey.EventType.valueOf(eventTypeStr.toUpperCase());
        return new ServiceEventKey(new ServiceKey(namespace, service), eventType);
    } catch (UnsupportedEncodingException e) {
        throw new AssertionError("UTF-8 is unknown");
    // or 'throw new AssertionError("Impossible things are happening today. " +
    // "Consider buying a lottery ticket!!");'
    }
}
Also used : ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ServiceEventKey(com.tencent.polaris.api.pojo.ServiceEventKey)

Example 7 with ServiceEventKey

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

the class SpecStreamClient method sendRequest.

/**
 * 发送请求
 *
 * @param serviceUpdateTask 服务更新任务
 */
public void sendRequest(ServiceUpdateTask serviceUpdateTask) {
    ServiceEventKey serviceEventKey = serviceUpdateTask.getServiceEventKey();
    ServiceProto.Service.Builder builder = ServiceProto.Service.newBuilder();
    builder.setName(StringValue.newBuilder().setValue(serviceEventKey.getServiceKey().getService()).build());
    builder.setNamespace(StringValue.newBuilder().setValue(serviceEventKey.getServiceKey().getNamespace()).build());
    RequestProto.DiscoverRequest.Builder req = RequestProto.DiscoverRequest.newBuilder();
    // switch
    req.setType(GrpcUtil.buildDiscoverRequestType(serviceEventKey.getEventType()));
    req.setService(builder);
    if (serviceUpdateTask.getTaskType() == Type.FIRST) {
        LOG.info("[ServerConnector]send request(id={}) to {} for service {}", reqId, connection.getConnID(), serviceEventKey);
    } else {
        LOG.debug("[ServerConnector]send request(id={}) to {} for service {}", reqId, connection.getConnID(), serviceEventKey);
    }
    discoverClient.onNext(req.build());
}
Also used : ServiceEventKey(com.tencent.polaris.api.pojo.ServiceEventKey)

Example 8 with ServiceEventKey

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

the class SpecStreamClient method validMessage.

/**
 * 检查该应答是否合法,不合法则走异常流程
 *
 * @param response 服务端应答
 * @return 合法返回true,否则返回false
 */
private ValidResult validMessage(ResponseProto.DiscoverResponse response) {
    ErrorCode errorCode = ErrorCode.Success;
    if (response.hasCode()) {
        errorCode = ServerCodes.convertServerErrorToRpcError(response.getCode().getValue());
    }
    ServiceProto.Service service;
    if (CollectionUtils.isNotEmpty(response.getServicesList())) {
        service = response.getServicesList().get(0);
    } else {
        service = response.getService();
    }
    if (null == service || StringUtils.isEmpty(service.getNamespace().getValue()) || StringUtils.isEmpty(service.getName().getValue())) {
        return new ValidResult(null, ErrorCode.INVALID_SERVER_RESPONSE, "service is empty, response text is " + response.toString());
    }
    EventType eventType = GrpcUtil.buildEventType(response.getType());
    if (eventType == EventType.UNKNOWN) {
        return new ValidResult(null, ErrorCode.INVALID_SERVER_RESPONSE, "invalid event type " + response.getType());
    }
    ServiceEventKey serviceEventKey = new ServiceEventKey(new ServiceKey(service.getNamespace().getValue(), service.getName().getValue()), eventType);
    if (errorCode == ErrorCode.SERVER_ERROR) {
        // 返回了500错误
        return new ValidResult(serviceEventKey, errorCode, "invalid event type " + response.getType());
    }
    return new ValidResult(serviceEventKey, ErrorCode.Success, "");
}
Also used : EventType(com.tencent.polaris.api.pojo.ServiceEventKey.EventType) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) ServiceEventKey(com.tencent.polaris.api.pojo.ServiceEventKey) ErrorCode(com.tencent.polaris.api.exception.ErrorCode) ServiceProto(com.tencent.polaris.client.pb.ServiceProto)

Example 9 with ServiceEventKey

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

the class SpecStreamClient method exceptionCallback.

/**
 * 异常回调
 *
 * @param validResult 异常
 */
public void exceptionCallback(ValidResult validResult) {
    this.closeStream(false);
    LOG.error("[ServerConnector]exceptionCallback: errCode {}, info {}, serviceEventKey {}", validResult.getErrorCode(), validResult.getMessage(), validResult.getServiceEventKey());
    // report down
    connection.reportFail();
    List<ServiceUpdateTask> notifyTasks = new ArrayList<>();
    synchronized (clientLock) {
        ServiceEventKey serviceEventKey = validResult.getServiceEventKey();
        if (null == serviceEventKey) {
            if (CollectionUtils.isNotEmpty(pendingTask.values())) {
                notifyTasks.addAll(pendingTask.values());
                pendingTask.clear();
            }
        } else {
            ServiceUpdateTask task = pendingTask.remove(serviceEventKey);
            if (null != task) {
                notifyTasks.add(task);
            }
        }
    }
    for (ServiceUpdateTask notifyTask : notifyTasks) {
        notifyTask.retry();
    }
}
Also used : ArrayList(java.util.ArrayList) ServiceUpdateTask(com.tencent.polaris.plugins.connector.common.ServiceUpdateTask) ServiceEventKey(com.tencent.polaris.api.pojo.ServiceEventKey)

Example 10 with ServiceEventKey

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

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