Search in sources :

Example 1 with RetriableException

use of com.tencent.polaris.api.exception.RetriableException in project polaris-java by polarismesh.

the class GrpcConnector method heartbeat.

@Override
public void heartbeat(CommonProviderRequest req) throws PolarisException {
    checkDestroyed();
    Connection connection = null;
    ServiceKey serviceKey = new ServiceKey(req.getNamespace(), req.getService());
    try {
        waitDiscoverReady();
        connection = connectionManager.getConnection(GrpcUtil.OP_KEY_INSTANCE_HEARTBEAT, ClusterType.HEALTH_CHECK_CLUSTER);
        req.setTargetServer(connectionToTargetNode(connection));
        PolarisGRPCGrpc.PolarisGRPCBlockingStub stub = PolarisGRPCGrpc.newBlockingStub(connection.getChannel());
        GrpcUtil.attachRequestHeader(stub, GrpcUtil.nextHeartbeatReqId());
        ResponseProto.Response heartbeatResponse = stub.heartbeat(buildHeartbeatRequest(req));
        GrpcUtil.checkResponse(heartbeatResponse);
        LOG.debug("received heartbeat response {}", heartbeatResponse);
    } catch (Throwable t) {
        if (t instanceof PolarisException) {
            // 服务端异常不进行重试
            throw t;
        }
        if (null != connection) {
            connection.reportFail();
        }
        throw new RetriableException(ErrorCode.NETWORK_ERROR, String.format("fail to heartbeat id %s, host %s:%d service %s", req.getInstanceID(), req.getHost(), req.getPort(), serviceKey), t);
    } finally {
        if (null != connection) {
            connection.release(GrpcUtil.OP_KEY_INSTANCE_HEARTBEAT);
        }
    }
}
Also used : PolarisException(com.tencent.polaris.api.exception.PolarisException) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) PolarisGRPCGrpc(com.tencent.polaris.client.pb.PolarisGRPCGrpc) ResponseProto(com.tencent.polaris.client.pb.ResponseProto) RetriableException(com.tencent.polaris.api.exception.RetriableException)

Example 2 with RetriableException

use of com.tencent.polaris.api.exception.RetriableException in project polaris-java by polarismesh.

the class PolarisConfigFileConnector method getConfigFile.

@Override
public ConfigFileResponse getConfigFile(ConfigFile configFile) {
    Connection connection = null;
    try {
        connection = connectionManager.getConnection(OP_KEY_GET_CONFIG_FILE, ClusterType.SERVICE_DISCOVER_CLUSTER);
        // grpc 调用
        PolarisConfigGRPCGrpc.PolarisConfigGRPCBlockingStub stub = PolarisConfigGRPCGrpc.newBlockingStub(connection.getChannel());
        // 附加通用 header
        GrpcUtil.attachRequestHeader(stub, GrpcUtil.nextInstanceRegisterReqId());
        // 执行调用
        ConfigFileProto.ConfigFileResponse response = stub.getConfigFile(transfer2DTO(configFile));
        return handleResponse(response);
    } catch (Throwable t) {
        // 网络访问异常
        if (connection != null) {
            connection.reportFail();
        }
        throw new RetriableException(ErrorCode.NETWORK_ERROR, String.format("failed to load config file. namespace = %s, group = %s, file = %s", configFile.getNamespace(), configFile.getFileGroup(), configFile.getFileName()), t);
    } finally {
        if (connection != null) {
            connection.release(OP_KEY_GET_CONFIG_FILE);
        }
    }
}
Also used : ConfigFileProto(com.tencent.polaris.client.pb.ConfigFileProto) Connection(com.tencent.polaris.plugins.connector.grpc.Connection) PolarisConfigGRPCGrpc(com.tencent.polaris.client.pb.PolarisConfigGRPCGrpc) RetriableException(com.tencent.polaris.api.exception.RetriableException)

Example 3 with RetriableException

use of com.tencent.polaris.api.exception.RetriableException in project polaris-java by polarismesh.

the class PolarisConfigFileConnector method watchConfigFiles.

@Override
public ConfigFileResponse watchConfigFiles(List<ConfigFile> configFiles) {
    Connection connection = null;
    try {
        connection = connectionManager.getConnection(OP_KEY_GET_CONFIG_FILE, ClusterType.SERVICE_DISCOVER_CLUSTER);
        // grpc 调用
        PolarisConfigGRPCGrpc.PolarisConfigGRPCBlockingStub stub = PolarisConfigGRPCGrpc.newBlockingStub(connection.getChannel());
        // 附加通用 header
        GrpcUtil.attachRequestHeader(stub, GrpcUtil.nextInstanceRegisterReqId());
        // 执行调用
        List<ConfigFileProto.ConfigFileDTO> dtos = Lists.newLinkedList();
        for (ConfigFile configFile : configFiles) {
            dtos.add(transfer2DTO(configFile));
        }
        ConfigFileProto.WatchConfigFileRequest request = ConfigFileProto.WatchConfigFileRequest.newBuilder().addAllWatchFiles(dtos).build();
        ConfigFileProto.ConfigFileResponse response = stub.watchConfigFiles(request);
        return handleResponse(response);
    } catch (Throwable t) {
        // 网络访问异常
        if (connection != null) {
            connection.reportFail();
        }
        throw new RetriableException(ErrorCode.NETWORK_ERROR, "[Config] failed to watch config file", t);
    } finally {
        if (connection != null) {
            connection.release(OP_KEY_GET_CONFIG_FILE);
        }
    }
}
Also used : ConfigFileProto(com.tencent.polaris.client.pb.ConfigFileProto) ConfigFile(com.tencent.polaris.api.plugin.configuration.ConfigFile) Connection(com.tencent.polaris.plugins.connector.grpc.Connection) PolarisConfigGRPCGrpc(com.tencent.polaris.client.pb.PolarisConfigGRPCGrpc) RetriableException(com.tencent.polaris.api.exception.RetriableException)

Example 4 with RetriableException

use of com.tencent.polaris.api.exception.RetriableException in project polaris-java by polarismesh.

the class DefaultProviderAPI method heartbeat.

@Override
public void heartbeat(InstanceHeartbeatRequest req) throws PolarisException {
    checkAvailable("ProviderAPI");
    Validator.validateHeartbeatRequest(req);
    long timeout = getTimeout(req);
    long retryInterval = sdkContext.getConfig().getGlobal().getAPI().getRetryInterval();
    while (timeout > 0) {
        long start = System.currentTimeMillis();
        ServiceCallResult serviceCallResult = new ServiceCallResult();
        CommonProviderRequest request = req.getRequest();
        try {
            serverConnector.heartbeat(request);
            serviceCallResult.setRetStatus(RetStatus.RetSuccess);
            serviceCallResult.setRetCode(ErrorCode.Success.getCode());
            return;
        } catch (PolarisException e) {
            serviceCallResult.setRetStatus(RetStatus.RetFail);
            serviceCallResult.setRetCode(exceptionToErrorCode(e).getCode());
            if (e instanceof RetriableException) {
                LOG.warn("heartbeat request error, retrying.", e);
                Utils.sleepUninterrupted(retryInterval);
                continue;
            }
            throw e;
        } finally {
            long delay = System.currentTimeMillis() - start;
            serviceCallResult.setDelay(delay);
            reportServerCall(serviceCallResult, request.getTargetServer(), "heartbeat");
            timeout -= delay;
        }
    }
    throw new PolarisException(ErrorCode.API_TIMEOUT, "heartbeat request timeout.");
}
Also used : CommonProviderRequest(com.tencent.polaris.api.plugin.server.CommonProviderRequest) PolarisException(com.tencent.polaris.api.exception.PolarisException) ServiceCallResult(com.tencent.polaris.api.rpc.ServiceCallResult) RetriableException(com.tencent.polaris.api.exception.RetriableException)

Example 5 with RetriableException

use of com.tencent.polaris.api.exception.RetriableException in project polaris-java by polarismesh.

the class ConsulAPIConnector method deregisterInstance.

@Override
public void deregisterInstance(CommonProviderRequest req) throws PolarisException {
    if (ieRegistered) {
        ServiceKey serviceKey = new ServiceKey(req.getNamespace(), req.getService());
        try {
            LOG.info("Unregistering service to Consul: " + consulContext.getInstanceId());
            this.consulClient.agentServiceDeregister(consulContext.getInstanceId());
            LOG.info("Unregistered service to Consul: " + consulContext.getInstanceId());
            ieRegistered = false;
        } catch (ConsulException e) {
            throw new RetriableException(ErrorCode.NETWORK_ERROR, String.format("fail to deregister host %s:%d service %s", req.getHost(), req.getPort(), serviceKey), e);
        }
    }
}
Also used : ConsulException(com.ecwid.consul.ConsulException) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) RetriableException(com.tencent.polaris.api.exception.RetriableException)

Aggregations

RetriableException (com.tencent.polaris.api.exception.RetriableException)13 PolarisException (com.tencent.polaris.api.exception.PolarisException)7 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)6 PolarisGRPCGrpc (com.tencent.polaris.client.pb.PolarisGRPCGrpc)4 ResponseProto (com.tencent.polaris.client.pb.ResponseProto)4 CommonProviderRequest (com.tencent.polaris.api.plugin.server.CommonProviderRequest)3 CommonProviderResponse (com.tencent.polaris.api.plugin.server.CommonProviderResponse)3 ServiceCallResult (com.tencent.polaris.api.rpc.ServiceCallResult)3 ConsulException (com.ecwid.consul.ConsulException)2 ConfigFileProto (com.tencent.polaris.client.pb.ConfigFileProto)2 PolarisConfigGRPCGrpc (com.tencent.polaris.client.pb.PolarisConfigGRPCGrpc)2 ConfigFileMetadata (com.tencent.polaris.configuration.api.core.ConfigFileMetadata)2 Connection (com.tencent.polaris.plugins.connector.grpc.Connection)2 Test (org.junit.Test)2 NewService (com.ecwid.consul.v1.agent.model.NewService)1 ConfigFile (com.tencent.polaris.api.plugin.configuration.ConfigFile)1 ReportClientResponse (com.tencent.polaris.api.plugin.server.ReportClientResponse)1 InstanceRegisterResponse (com.tencent.polaris.api.rpc.InstanceRegisterResponse)1 ClientProto (com.tencent.polaris.client.pb.ClientProto)1