Search in sources :

Example 16 with PolarisException

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

the class MetadataRouterTest method testFailoverAllScene.

@Test
public void testFailoverAllScene() {
    Configuration configuration = TestUtils.configWithEnvAddress();
    try (ConsumerAPI consumer = DiscoveryAPIFactory.createConsumerAPIByConfig(configuration)) {
        // 但是不健康,降级策略采用默认返回所有健康实例,其中80端口实例为不健康,故80端口的实例不会返回
        for (int i = 0; i < 10; i++) {
            GetOneInstanceRequest getInstances2 = new GetOneInstanceRequest();
            getInstances2.setNamespace("Production");
            getInstances2.setService(METADATA_SERVICE);
            Map<String, String> map = new HashMap<>();
            map.put("Env-set", "1-1");
            getInstances2.setMetadata(map);
            getInstances2.setMetadataFailoverType(MetadataFailoverType.METADATAFAILOVERALL);
            InstancesResponse ins = null;
            try {
                ins = consumer.getOneInstance(getInstances2);
            } catch (PolarisException e) {
                e.printStackTrace();
            }
            Assert.assertNotEquals(80, 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 17 with PolarisException

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

the class MetadataRouterTest method testFailoverNotKeyScene.

@Test
public void testFailoverNotKeyScene() {
    // 传入Env-set:1-1 ,应该返回第4个实例,因为前三个都包含Env-set这个key
    Configuration configuration = TestUtils.configWithEnvAddress();
    try (ConsumerAPI consumer = DiscoveryAPIFactory.createConsumerAPIByConfig(configuration)) {
        for (int i = 0; i < 10; i++) {
            GetOneInstanceRequest getInstances2 = new GetOneInstanceRequest();
            getInstances2.setNamespace("Production");
            getInstances2.setService(METADATA_SERVICE);
            Map<String, String> map = new HashMap<>();
            map.put("Env-set", "1-1");
            getInstances2.setMetadata(map);
            // TODO: 通过配置文件来设置该配置
            getInstances2.setMetadataFailoverType(MetadataFailoverType.METADATAFAILOVERNOTKEY);
            InstancesResponse ins = null;
            try {
                ins = consumer.getOneInstance(getInstances2);
            } catch (PolarisException e) {
                Assert.fail(e.getMessage());
            }
            Assert.assertEquals(90, ins.getInstances()[0].getPort());
            Assert.assertEquals("127.0.0.1", ins.getInstances()[0].getHost());
        }
    }
}
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 18 with PolarisException

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

the class APIFactoryTest method testInitContextByConfig.

@Test
public void testInitContextByConfig() {
    SDKContext sdkContext = null;
    try {
        sdkContext = APIFactory.initContextByConfig(TestUtils.createSimpleConfiguration(8888));
        Supplier plugins = sdkContext.getPlugins();
        Plugin plugin = plugins.getPlugin(PluginTypes.LOAD_BALANCER.getBaseType(), LoadBalanceConfig.LOAD_BALANCE_WEIGHTED_RANDOM);
        Assert.assertNotNull(plugin);
    } catch (PolarisException e) {
        Assert.fail(e.getMessage());
    } finally {
        if (null != sdkContext) {
            sdkContext.destroy();
        }
    }
}
Also used : SDKContext(com.tencent.polaris.client.api.SDKContext) PolarisException(com.tencent.polaris.api.exception.PolarisException) Supplier(com.tencent.polaris.api.plugin.Supplier) Plugin(com.tencent.polaris.api.plugin.Plugin) Test(org.junit.Test)

Example 19 with PolarisException

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

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

the class PluginManager method initPlugins.

@Override
public void initPlugins(InitContext context) throws PolarisException {
    // 先实例化所有插件
    int baseId = 0;
    for (PluginType pluginType : pluginTypes) {
        Map<String, Plugin> plugins = new HashMap<>();
        typedPlugins.put(pluginType, plugins);
        ServiceLoader<? extends Plugin> loader = ServiceLoader.load(pluginType.getClazz());
        for (Plugin plugin : loader) {
            baseId++;
            String name = plugin.getName();
            if (StringUtils.isBlank(name) || plugins.containsKey(name)) {
                throw new PolarisException(ErrorCode.PLUGIN_ERROR, String.format("duplicated name for plugin(name=%s, type=%s)", name, pluginType));
            }
            if (plugin instanceof IdAwarePlugin) {
                ((IdAwarePlugin) plugin).setId(baseId);
            }
            plugins.put(name, plugin);
        }
    }
    // 再进行初始化
    for (PluginType pluginType : pluginTypes) {
        Map<String, Plugin> plugins = typedPlugins.get(pluginType);
        for (Map.Entry<String, Plugin> pluginEntry : plugins.entrySet()) {
            pluginEntry.getValue().init(context);
        }
    }
}
Also used : PolarisException(com.tencent.polaris.api.exception.PolarisException) HashMap(java.util.HashMap) IdAwarePlugin(com.tencent.polaris.api.plugin.IdAwarePlugin) PluginType(com.tencent.polaris.api.plugin.PluginType) HashMap(java.util.HashMap) Map(java.util.Map) IdAwarePlugin(com.tencent.polaris.api.plugin.IdAwarePlugin) Plugin(com.tencent.polaris.api.plugin.Plugin)

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