Search in sources :

Example 1 with EventType

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

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

the class SpecStreamClient method onNext.

/**
 * 正常回调
 *
 * @param response 应答说
 */
public void onNext(ResponseProto.DiscoverResponse response) {
    lastRecvTimeMs.set(System.currentTimeMillis());
    ValidResult validResult = validMessage(response);
    if (validResult.errorCode != ErrorCode.Success) {
        exceptionCallback(validResult);
        return;
    }
    ServiceProto.Service service = response.getService();
    ServiceKey serviceKey = new ServiceKey(service.getNamespace().getValue(), service.getName().getValue());
    EventType eventType = GrpcUtil.buildEventType(response.getType());
    ServiceEventKey serviceEventKey = new ServiceEventKey(serviceKey, eventType);
    ServiceUpdateTask updateTask;
    synchronized (clientLock) {
        updateTask = pendingTask.remove(serviceEventKey);
    }
    if (null == updateTask) {
        LOG.error("[ServerConnector]callback not found for:{}", TextFormat.shortDebugString(service));
        return;
    }
    if (updateTask.getTaskType() == Type.FIRST) {
        LOG.info("[ServerConnector]receive response for {}", serviceEventKey);
    } else {
        LOG.debug("[ServerConnector]receive response for {}", serviceEventKey);
    }
    PolarisException error;
    if (!response.hasCode() || response.getCode().getValue() == ServerCodes.EXECUTE_SUCCESS) {
        error = null;
    } else {
        int respCode = response.getCode().getValue();
        String info = response.getInfo().getValue();
        error = ServerErrorResponseException.build(respCode, String.format("[ServerConnector]code %d, fail to query service %s from server(%s): %s", respCode, serviceKey, connection.getConnID(), info));
    }
    boolean svcDeleted = updateTask.notifyServerEvent(new ServerEvent(serviceEventKey, response, error));
    if (!svcDeleted) {
        updateTask.addUpdateTaskSet();
    }
}
Also used : PolarisException(com.tencent.polaris.api.exception.PolarisException) ServerEvent(com.tencent.polaris.api.plugin.server.ServerEvent) EventType(com.tencent.polaris.api.pojo.ServiceEventKey.EventType) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) ServiceEventKey(com.tencent.polaris.api.pojo.ServiceEventKey) ServiceUpdateTask(com.tencent.polaris.plugins.connector.common.ServiceUpdateTask) ServiceProto(com.tencent.polaris.client.pb.ServiceProto)

Aggregations

ServiceEventKey (com.tencent.polaris.api.pojo.ServiceEventKey)2 EventType (com.tencent.polaris.api.pojo.ServiceEventKey.EventType)2 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)2 ServiceProto (com.tencent.polaris.client.pb.ServiceProto)2 ErrorCode (com.tencent.polaris.api.exception.ErrorCode)1 PolarisException (com.tencent.polaris.api.exception.PolarisException)1 ServerEvent (com.tencent.polaris.api.plugin.server.ServerEvent)1 ServiceUpdateTask (com.tencent.polaris.plugins.connector.common.ServiceUpdateTask)1