Search in sources :

Example 26 with PolarisException

use of com.tencent.polaris.api.exception.PolarisException 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 27 with PolarisException

use of com.tencent.polaris.api.exception.PolarisException 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)

Example 28 with PolarisException

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

the class ErrRateCircuitBreaker method init.

@Override
public void init(InitContext ctx) throws PolarisException {
    CircuitBreakerConfig circuitBreakerConfig = ctx.getConfig().getConsumer().getCircuitBreaker();
    OutlierDetectionConfig outlierDetection = ctx.getConfig().getConsumer().getOutlierDetection();
    metricWindowMs = circuitBreakerConfig.getCheckPeriod();
    HalfOpenConfig halfOpenConfig = new HalfOpenConfig(circuitBreakerConfig, outlierDetection);
    Config cfg = circuitBreakerConfig.getPluginConfig(getName(), Config.class);
    if (cfg == null) {
        throw new PolarisException(ErrorCode.INVALID_CONFIG, String.format("plugin %s config is missing", getName()));
    }
    ConfigSet<Config> configSet = new ConfigSet<>(StatusDimension.Level.SERVICE, false, halfOpenConfig, cfg);
    create = new Function<Integer, Object>() {

        @Override
        public Object apply(Integer integer) {
            return new ErrRateCounter(metricWindowName, configSet.getPlugConfig(), getBucketIntervalMs());
        }
    };
    configGroup = new ConfigGroup<>(configSet);
    stateMachine = new StateMachineImpl(configGroup, id, this, metricWindowMs);
    flowControlParam = new DefaultFlowControlParam(ctx.getConfig().getGlobal().getAPI());
}
Also used : OutlierDetectionConfig(com.tencent.polaris.api.config.consumer.OutlierDetectionConfig) HalfOpenConfig(com.tencent.polaris.plugins.circuitbreaker.common.HalfOpenConfig) OutlierDetectionConfig(com.tencent.polaris.api.config.consumer.OutlierDetectionConfig) RecoverConfig(com.tencent.polaris.client.pb.CircuitBreakerProto.RecoverConfig) CircuitBreakerConfig(com.tencent.polaris.api.config.consumer.CircuitBreakerConfig) ErrRateConfig(com.tencent.polaris.client.pb.CircuitBreakerProto.CbPolicy.ErrRateConfig) HalfOpenConfig(com.tencent.polaris.plugins.circuitbreaker.common.HalfOpenConfig) PolarisException(com.tencent.polaris.api.exception.PolarisException) DefaultFlowControlParam(com.tencent.polaris.client.flow.DefaultFlowControlParam) CircuitBreakerConfig(com.tencent.polaris.api.config.consumer.CircuitBreakerConfig) ConfigSet(com.tencent.polaris.plugins.circuitbreaker.common.ConfigSet)

Example 29 with PolarisException

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

the class SDKContext method initContextByConfig.

/**
 * 通过配置对象初始化SDK上下文
 *
 * @param config 配置对象
 * @return SDK上下文
 * @throws PolarisException 初始化过程的异常
 */
public static SDKContext initContextByConfig(Configuration config) throws PolarisException {
    try {
        ((ConfigurationImpl) config).setDefault();
        config.verify();
    } catch (IllegalArgumentException e) {
        throw new PolarisException(ErrorCode.INVALID_CONFIG, "fail to verify configuration", e);
    }
    ServiceLoader<TypeProvider> providers = ServiceLoader.load(TypeProvider.class);
    List<PluginType> types = new ArrayList<>();
    for (TypeProvider provider : providers) {
        types.addAll(provider.getTypes());
    }
    PluginManager manager = new PluginManager(types);
    ValueContext valueContext = new ValueContext();
    valueContext.setHost(parseHost(config));
    valueContext.setServerConnectorProtocol(parseServerConnectorProtocol(config));
    SDKContext initContext = new SDKContext(config, manager, valueContext);
    try {
        manager.initPlugins(initContext);
    } catch (Throwable e) {
        manager.destroyPlugins();
        if (e instanceof PolarisException) {
            throw e;
        }
        throw new PolarisException(ErrorCode.PLUGIN_ERROR, "plugin error", e);
    }
    return initContext;
}
Also used : PolarisException(com.tencent.polaris.api.exception.PolarisException) PluginManager(com.tencent.polaris.api.plugin.impl.PluginManager) ValueContext(com.tencent.polaris.api.plugin.common.ValueContext) ArrayList(java.util.ArrayList) TypeProvider(com.tencent.polaris.api.plugin.TypeProvider) ConfigurationImpl(com.tencent.polaris.factory.config.ConfigurationImpl) PluginType(com.tencent.polaris.api.plugin.PluginType)

Example 30 with PolarisException

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

the class PluginConfigImpl method mutableSetPluginConfig.

private Map<?, ?> mutableSetPluginConfig(String pluginName, Verifier config) throws PolarisException {
    Map<?, ?> configMap;
    try {
        configMap = ConfigUtils.objectToMap(config);
    } catch (Exception e) {
        throw new PolarisException(ErrorCode.INVALID_CONFIG, String.format("fail to marshal plugin config for %s", pluginName), e);
    }
    plugin.put(pluginName, configMap);
    return configMap;
}
Also used : PolarisException(com.tencent.polaris.api.exception.PolarisException) PolarisException(com.tencent.polaris.api.exception.PolarisException)

Aggregations

PolarisException (com.tencent.polaris.api.exception.PolarisException)39 HashMap (java.util.HashMap)8 Test (org.junit.Test)8 RetriableException (com.tencent.polaris.api.exception.RetriableException)7 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)7 ConsumerAPI (com.tencent.polaris.api.core.ConsumerAPI)6 InstancesResponse (com.tencent.polaris.api.rpc.InstancesResponse)6 Configuration (com.tencent.polaris.api.config.Configuration)5 GetOneInstanceRequest (com.tencent.polaris.api.rpc.GetOneInstanceRequest)5 ServiceCallResult (com.tencent.polaris.api.rpc.ServiceCallResult)5 CommonProviderRequest (com.tencent.polaris.api.plugin.server.CommonProviderRequest)3 Instance (com.tencent.polaris.api.pojo.Instance)3 ServiceEventKey (com.tencent.polaris.api.pojo.ServiceEventKey)3 PolarisGRPCGrpc (com.tencent.polaris.client.pb.PolarisGRPCGrpc)3 ResponseProto (com.tencent.polaris.client.pb.ResponseProto)3 Map (java.util.Map)3 CircuitBreakerConfig (com.tencent.polaris.api.config.consumer.CircuitBreakerConfig)2 OutlierDetectionConfig (com.tencent.polaris.api.config.consumer.OutlierDetectionConfig)2 Verifier (com.tencent.polaris.api.config.verify.Verifier)2 Plugin (com.tencent.polaris.api.plugin.Plugin)2