Search in sources :

Example 1 with CommonProviderResponse

use of com.tencent.polaris.api.plugin.server.CommonProviderResponse in project polaris-java by polarismesh.

the class CompositeConnector method registerInstance.

@Override
public CommonProviderResponse registerInstance(CommonProviderRequest req) throws PolarisException {
    checkDestroyed();
    CommonProviderResponse response = null;
    for (DestroyableServerConnector sc : serverConnectors) {
        CommonProviderResponse temp = sc.registerInstance(req);
        if (DefaultPlugins.SERVER_CONNECTOR_GRPC.equals(sc.getName())) {
            response = temp;
        }
    }
    return response;
}
Also used : CommonProviderResponse(com.tencent.polaris.api.plugin.server.CommonProviderResponse) DestroyableServerConnector(com.tencent.polaris.plugins.connector.common.DestroyableServerConnector)

Example 2 with CommonProviderResponse

use of com.tencent.polaris.api.plugin.server.CommonProviderResponse in project polaris-java by polarismesh.

the class ConsulAPIConnector method registerInstance.

@Override
public CommonProviderResponse registerInstance(CommonProviderRequest req) throws PolarisException {
    if (!ieRegistered) {
        ServiceKey serviceKey = new ServiceKey(req.getNamespace(), req.getService());
        try {
            LOG.info("Registering service to Consul");
            NewService service = buildRegisterInstanceRequest(req);
            this.consulClient.agentServiceRegister(service);
            CommonProviderResponse resp = new CommonProviderResponse();
            consulContext.setInstanceId(service.getId());
            resp.setInstanceID(service.getId());
            resp.setExists(true);
            LOG.info("Registered service to Consul: " + service);
            ieRegistered = true;
            return resp;
        } catch (ConsulException e) {
            throw new RetriableException(ErrorCode.NETWORK_ERROR, String.format("fail to register host %s:%d service %s", req.getHost(), req.getPort(), serviceKey), e);
        }
    }
    return null;
}
Also used : ConsulException(com.ecwid.consul.ConsulException) CommonProviderResponse(com.tencent.polaris.api.plugin.server.CommonProviderResponse) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) NewService(com.ecwid.consul.v1.agent.model.NewService) RetriableException(com.tencent.polaris.api.exception.RetriableException)

Example 3 with CommonProviderResponse

use of com.tencent.polaris.api.plugin.server.CommonProviderResponse in project polaris-java by polarismesh.

the class GrpcConnector method registerInstance.

@Override
public CommonProviderResponse registerInstance(CommonProviderRequest req) throws PolarisException {
    checkDestroyed();
    Connection connection = null;
    ServiceKey serviceKey = new ServiceKey(req.getNamespace(), req.getService());
    try {
        waitDiscoverReady();
        connection = connectionManager.getConnection(GrpcUtil.OP_KEY_REGISTER_INSTANCE, ClusterType.SERVICE_DISCOVER_CLUSTER);
        req.setTargetServer(connectionToTargetNode(connection));
        PolarisGRPCGrpc.PolarisGRPCBlockingStub stub = PolarisGRPCGrpc.newBlockingStub(connection.getChannel());
        GrpcUtil.attachRequestHeader(stub, GrpcUtil.nextInstanceRegisterReqId());
        ResponseProto.Response registerInstanceResponse = stub.registerInstance(buildRegisterInstanceRequest(req));
        GrpcUtil.checkResponse(registerInstanceResponse);
        if (!registerInstanceResponse.hasInstance()) {
            throw new PolarisException(ErrorCode.SERVER_USER_ERROR, "invalid register response: missing instance");
        }
        CommonProviderResponse resp = new CommonProviderResponse();
        resp.setInstanceID(registerInstanceResponse.getInstance().getId().getValue());
        resp.setExists(registerInstanceResponse.getCode().getValue() == ServerCodes.EXISTED_RESOURCE);
        return resp;
    } catch (Throwable t) {
        if (t instanceof PolarisException) {
            throw t;
        }
        if (null != connection) {
            connection.reportFail();
        }
        throw new RetriableException(ErrorCode.NETWORK_ERROR, String.format("fail to register host %s:%d service %s", req.getHost(), req.getPort(), serviceKey), t);
    } finally {
        if (null != connection) {
            connection.release(GrpcUtil.OP_KEY_REGISTER_INSTANCE);
        }
    }
}
Also used : PolarisException(com.tencent.polaris.api.exception.PolarisException) CommonProviderResponse(com.tencent.polaris.api.plugin.server.CommonProviderResponse) 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 4 with CommonProviderResponse

use of com.tencent.polaris.api.plugin.server.CommonProviderResponse in project polaris-java by polarismesh.

the class DefaultProviderAPI method register.

@Override
public InstanceRegisterResponse register(InstanceRegisterRequest req) throws PolarisException {
    checkAvailable("ProviderAPI");
    Validator.validateInstanceRegisterRequest(req);
    long retryInterval = sdkContext.getConfig().getGlobal().getAPI().getRetryInterval();
    long timeout = getTimeout(req);
    while (timeout > 0) {
        long start = System.currentTimeMillis();
        ServiceCallResult serviceCallResult = new ServiceCallResult();
        CommonProviderRequest request = req.getRequest();
        try {
            CommonProviderResponse response = serverConnector.registerInstance(request);
            LOG.info("register {}/{} instance {} succ", req.getNamespace(), req.getService(), response.getInstanceID());
            serviceCallResult.setRetStatus(RetStatus.RetSuccess);
            serviceCallResult.setRetCode(ErrorCode.Success.getCode());
            return new InstanceRegisterResponse(response.getInstanceID(), response.isExists());
        } catch (PolarisException e) {
            serviceCallResult.setRetStatus(RetStatus.RetFail);
            serviceCallResult.setRetCode(exceptionToErrorCode(e).getCode());
            if (e instanceof RetriableException) {
                LOG.warn("instance register request error, retrying.", e);
                Utils.sleepUninterrupted(retryInterval);
                continue;
            }
            throw e;
        } finally {
            long delay = System.currentTimeMillis() - start;
            serviceCallResult.setDelay(delay);
            reportServerCall(serviceCallResult, request.getTargetServer(), "register");
            timeout -= delay;
        }
    }
    throw new PolarisException(ErrorCode.API_TIMEOUT, "instance register 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) CommonProviderResponse(com.tencent.polaris.api.plugin.server.CommonProviderResponse) InstanceRegisterResponse(com.tencent.polaris.api.rpc.InstanceRegisterResponse) RetriableException(com.tencent.polaris.api.exception.RetriableException)

Aggregations

CommonProviderResponse (com.tencent.polaris.api.plugin.server.CommonProviderResponse)4 RetriableException (com.tencent.polaris.api.exception.RetriableException)3 PolarisException (com.tencent.polaris.api.exception.PolarisException)2 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)2 ConsulException (com.ecwid.consul.ConsulException)1 NewService (com.ecwid.consul.v1.agent.model.NewService)1 CommonProviderRequest (com.tencent.polaris.api.plugin.server.CommonProviderRequest)1 InstanceRegisterResponse (com.tencent.polaris.api.rpc.InstanceRegisterResponse)1 ServiceCallResult (com.tencent.polaris.api.rpc.ServiceCallResult)1 PolarisGRPCGrpc (com.tencent.polaris.client.pb.PolarisGRPCGrpc)1 ResponseProto (com.tencent.polaris.client.pb.ResponseProto)1 DestroyableServerConnector (com.tencent.polaris.plugins.connector.common.DestroyableServerConnector)1