Search in sources :

Example 31 with PolarisException

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

the class PluginConfigImpl method getPluginConfig.

@Override
public <T extends Verifier> T getPluginConfig(String pluginName, Class<T> clazz) throws PolarisException {
    synchronized (lock) {
        Map<?, ?> properties = plugin.get(pluginName);
        if (null == properties) {
            Verifier config = getConfigByName(clazz);
            properties = mutableSetPluginConfig(pluginName, config);
        }
        T result;
        try {
            result = mapper.convertValue(properties, clazz);
        } catch (IllegalArgumentException e) {
            throw new PolarisException(ErrorCode.INVALID_CONFIG, String.format("fail to deserialize properties %s to clazz %s for plugin %s", properties, clazz.getCanonicalName(), pluginName), e);
        }
        return result;
    }
}
Also used : PolarisException(com.tencent.polaris.api.exception.PolarisException) Verifier(com.tencent.polaris.api.config.verify.Verifier)

Example 32 with PolarisException

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

the class MetadataRouterTest method testFailoverNotKeyScene2.

@Test
public void testFailoverNotKeyScene2() {
    // 传入Env-set:1-1 ,应该返回第2个实例,因为第一个都包含Env-set这个key
    Configuration configuration = TestUtils.configWithEnvAddress();
    try (ConsumerAPI consumer = DiscoveryAPIFactory.createConsumerAPIByConfig(configuration)) {
        // 降级策略采用默认返回所有非metadata健康实例,故只返回不满足metadata的那个实例
        for (int i = 0; i < 10; i++) {
            GetOneInstanceRequest getInstances2 = new GetOneInstanceRequest();
            getInstances2.setNamespace(NAMESPACE_PRODUCTION);
            getInstances2.setService(METADATA_SERVICE_1);
            Map<String, String> map = new HashMap<>();
            map.put("Env-set", "1-1");
            getInstances2.setMetadata(map);
            getInstances2.setMetadataFailoverType(MetadataFailoverType.METADATAFAILOVERNOTKEY);
            InstancesResponse ins = null;
            try {
                ins = consumer.getOneInstance(getInstances2);
            } catch (PolarisException e) {
                e.printStackTrace();
            }
            Assert.assertEquals(81, ins.getInstances()[0].getPort());
        }
    }
}
Also used : PolarisException(com.tencent.polaris.api.exception.PolarisException) Configuration(com.tencent.polaris.api.config.Configuration) HashMap(java.util.HashMap) GetOneInstanceRequest(com.tencent.polaris.api.rpc.GetOneInstanceRequest) ConsumerAPI(com.tencent.polaris.api.core.ConsumerAPI) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse) Test(org.junit.Test)

Example 33 with PolarisException

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

the class MetadataRouterTest method testNormalScene.

@Test
public void testNormalScene() {
    Configuration configuration = TestUtils.configWithEnvAddress();
    try (ConsumerAPI consumer = DiscoveryAPIFactory.createConsumerAPIByConfig(configuration)) {
        // 该服务下有四个实例,其中有两个满足metadata路由(分别为port70,100),80 90端口不满足
        for (int i = 0; i < 10; i++) {
            GetOneInstanceRequest getInstances2 = new GetOneInstanceRequest();
            getInstances2.setNamespace(NAMESPACE_PRODUCTION);
            getInstances2.setService(METADATA_SERVICE);
            Map<String, String> map = new HashMap<>();
            map.put("Env-set", "1-0");
            getInstances2.setMetadata(map);
            InstancesResponse ins = null;
            try {
                ins = consumer.getOneInstance(getInstances2);
            } catch (PolarisException e) {
                e.printStackTrace();
            }
            int port = ins.getInstances()[0].getPort();
            Assert.assertNotEquals(90, port);
            Assert.assertNotEquals(80, port);
        }
    }
}
Also used : PolarisException(com.tencent.polaris.api.exception.PolarisException) Configuration(com.tencent.polaris.api.config.Configuration) HashMap(java.util.HashMap) GetOneInstanceRequest(com.tencent.polaris.api.rpc.GetOneInstanceRequest) ConsumerAPI(com.tencent.polaris.api.core.ConsumerAPI) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse) Test(org.junit.Test)

Example 34 with PolarisException

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

the class DefaultProviderAPI method deRegister.

@Override
public void deRegister(InstanceDeregisterRequest req) throws PolarisException {
    checkAvailable("ProviderAPI");
    Validator.validateInstanceDeregisterRequest(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 {
            serverConnector.deregisterInstance(request);
            serviceCallResult.setRetStatus(RetStatus.RetSuccess);
            serviceCallResult.setRetCode(ErrorCode.Success.getCode());
            LOG.info("deregister instance {} succ", req);
            return;
        } catch (PolarisException e) {
            serviceCallResult.setRetStatus(RetStatus.RetFail);
            serviceCallResult.setRetCode(exceptionToErrorCode(e).getCode());
            if (e instanceof RetriableException) {
                LOG.warn("instance deregister request error, retrying.", e);
                Utils.sleepUninterrupted(retryInterval);
                continue;
            }
            throw e;
        } finally {
            long delay = System.currentTimeMillis() - start;
            serviceCallResult.setDelay(delay);
            reportServerCall(serviceCallResult, request.getTargetServer(), "deRegister");
            timeout -= delay;
        }
    }
    throw new PolarisException(ErrorCode.API_TIMEOUT, "instance deregister 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 35 with PolarisException

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

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