Search in sources :

Example 1 with Configuration

use of com.tencent.polaris.api.config.Configuration in project polaris-java by polarismesh.

the class CircuitBreakerTest method testCircuitBreakByErrorRate.

@Test
public void testCircuitBreakByErrorRate() {
    Configuration configuration = TestUtils.configWithEnvAddress();
    try (ConsumerAPI consumerAPI = DiscoveryAPIFactory.createConsumerAPIByConfig(configuration)) {
        GetInstancesRequest getInstancesRequest = new GetInstancesRequest();
        getInstancesRequest.setNamespace(NAMESPACE_TEST);
        getInstancesRequest.setService(SERVICE_CIRCUIT_BREAKER);
        InstancesResponse instances = consumerAPI.getInstances(getInstancesRequest);
        Assert.assertEquals(MAX_COUNT, instances.getInstances().length);
        Instance instanceToLimit = instances.getInstances()[1];
        // report 60 fail in 500ms
        for (int i = 0; i < 60; ++i) {
            ServiceCallResult result = instanceToResult(instanceToLimit);
            result.setDelay(1000L);
            if (i % 2 == 0) {
                result.setRetCode(0);
                result.setRetStatus(RetStatus.RetSuccess);
                Utils.sleepUninterrupted(1);
            } else {
                result.setRetCode(-1);
                result.setRetStatus(RetStatus.RetFail);
            }
            consumerAPI.updateServiceCallResult(result);
            Utils.sleepUninterrupted(1);
        }
        Utils.sleepUninterrupted(1000);
        instances = consumerAPI.getInstances(getInstancesRequest);
        Assert.assertEquals(MAX_COUNT - 1, instances.getInstances().length);
        Instance[] instanceArray = instances.getInstances();
        boolean exists = false;
        for (int i = 0; i < instanceArray.length; ++i) {
            if (instanceArray[i].getId().equals(instanceToLimit.getId())) {
                exists = true;
            }
        }
        Assert.assertFalse(exists);
        Utils.sleepUninterrupted(10000);
        // default halfopen pass 3 success
        int requestCountAfterHalfOpen = configuration.getConsumer().getCircuitBreaker().getRequestCountAfterHalfOpen();
        for (int i = 0; i < requestCountAfterHalfOpen; i++) {
            ServiceCallResult result = instanceToResult(instanceToLimit);
            result.setRetCode(-1);
            result.setRetStatus(RetStatus.RetSuccess);
            consumerAPI.updateServiceCallResult(result);
            Utils.sleepUninterrupted(200);
            consumerAPI.updateServiceCallResult(result);
        }
        LOG.info("start to test half open to close");
        Utils.sleepUninterrupted(1000);
        instances = consumerAPI.getInstances(getInstancesRequest);
        Assert.assertEquals(MAX_COUNT, instances.getInstances().length);
    }
}
Also used : Configuration(com.tencent.polaris.api.config.Configuration) ServiceCallResult(com.tencent.polaris.api.rpc.ServiceCallResult) GetInstancesRequest(com.tencent.polaris.api.rpc.GetInstancesRequest) Instance(com.tencent.polaris.api.pojo.Instance) ConsumerAPI(com.tencent.polaris.api.core.ConsumerAPI) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse) Test(org.junit.Test)

Example 2 with Configuration

use of com.tencent.polaris.api.config.Configuration in project polaris-java by polarismesh.

the class CircuitBreakerTest method testUpdateServiceCallResult.

@Test
public void testUpdateServiceCallResult() {
    Configuration configuration = TestUtils.configWithEnvAddress();
    try (ConsumerAPI consumerAPI = DiscoveryAPIFactory.createConsumerAPIByConfig(configuration)) {
        int index = 1;
        GetInstancesRequest req = new GetInstancesRequest();
        req.setNamespace(NAMESPACE_TEST);
        req.setService(SERVICE_CIRCUIT_BREAKER);
        InstancesResponse instances = consumerAPI.getInstances(req);
        Assert.assertEquals(MAX_COUNT, instances.getInstances().length);
        Instance instanceToLimit = instances.getInstances()[index];
        ServiceCallResult result = instanceToResult(instanceToLimit);
        result.setRetCode(-1);
        result.setDelay(1000L);
        result.setRetStatus(RetStatus.RetFail);
        consumerAPI.updateServiceCallResult(result);
    }
}
Also used : Configuration(com.tencent.polaris.api.config.Configuration) ServiceCallResult(com.tencent.polaris.api.rpc.ServiceCallResult) GetInstancesRequest(com.tencent.polaris.api.rpc.GetInstancesRequest) Instance(com.tencent.polaris.api.pojo.Instance) ConsumerAPI(com.tencent.polaris.api.core.ConsumerAPI) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse) Test(org.junit.Test)

Example 3 with Configuration

use of com.tencent.polaris.api.config.Configuration in project polaris-java by polarismesh.

the class CircuitBreakerTest method testCircuitBreakByErrorCount.

@Test
public void testCircuitBreakByErrorCount() {
    Configuration configuration = TestUtils.configWithEnvAddress();
    try (ConsumerAPI consumerAPI = DiscoveryAPIFactory.createConsumerAPIByConfig(configuration)) {
        Utils.sleepUninterrupted(10000);
        Assert.assertNotNull(consumerAPI);
        GetInstancesRequest getInstancesRequest = new GetInstancesRequest();
        getInstancesRequest.setNamespace(NAMESPACE_TEST);
        getInstancesRequest.setService(SERVICE_CIRCUIT_BREAKER);
        InstancesResponse instances = consumerAPI.getInstances(getInstancesRequest);
        Assert.assertEquals(MAX_COUNT, instances.getInstances().length);
        Instance instanceToLimit = instances.getInstances()[1];
        // report 60 fail in 500ms
        for (int i = 0; i < 60; ++i) {
            ServiceCallResult result = instanceToResult(instanceToLimit);
            result.setRetCode(-1);
            result.setDelay(1000L);
            result.setRetStatus(RetStatus.RetFail);
            consumerAPI.updateServiceCallResult(result);
            if (i % 10 == 0) {
                Utils.sleepUninterrupted(1);
            }
        }
        Utils.sleepUninterrupted(1000);
        instances = consumerAPI.getInstances(getInstancesRequest);
        Assert.assertEquals(MAX_COUNT - 1, instances.getInstances().length);
        Instance[] instanceArray = instances.getInstances();
        boolean exists = false;
        for (int i = 0; i < instanceArray.length; ++i) {
            if (instanceArray[i].getId().equals(instanceToLimit.getId())) {
                exists = true;
            }
        }
        Assert.assertFalse(exists);
        LOG.info("start to test half open by error rate");
        Utils.sleepUninterrupted(10000);
        instances = consumerAPI.getInstances(getInstancesRequest);
        Assert.assertEquals(MAX_COUNT, instances.getInstances().length);
        for (Instance instance : instances.getInstances()) {
            CircuitBreakerStatus circuitBreakerStatus = instance.getCircuitBreakerStatus();
            if (null != circuitBreakerStatus && circuitBreakerStatus.getStatus() == CircuitBreakerStatus.Status.HALF_OPEN) {
                LOG.info("half open instance is {}", instance);
            }
        }
        // default halfopen pass 3 success
        int requestCountAfterHalfOpen = configuration.getConsumer().getCircuitBreaker().getRequestCountAfterHalfOpen();
        for (int i = 0; i < requestCountAfterHalfOpen; i++) {
            ServiceCallResult result = instanceToResult(instanceToLimit);
            result.setRetCode(-1);
            result.setRetStatus(RetStatus.RetSuccess);
            consumerAPI.updateServiceCallResult(result);
            Utils.sleepUninterrupted(200);
            consumerAPI.updateServiceCallResult(result);
        }
        LOG.info("start to test half open to close");
        Utils.sleepUninterrupted(1000);
        instances = consumerAPI.getInstances(getInstancesRequest);
        Assert.assertEquals(MAX_COUNT, instances.getInstances().length);
    }
}
Also used : Configuration(com.tencent.polaris.api.config.Configuration) ServiceCallResult(com.tencent.polaris.api.rpc.ServiceCallResult) GetInstancesRequest(com.tencent.polaris.api.rpc.GetInstancesRequest) Instance(com.tencent.polaris.api.pojo.Instance) ConsumerAPI(com.tencent.polaris.api.core.ConsumerAPI) CircuitBreakerStatus(com.tencent.polaris.api.pojo.CircuitBreakerStatus) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse) Test(org.junit.Test)

Example 4 with Configuration

use of com.tencent.polaris.api.config.Configuration in project polaris-java by polarismesh.

the class LocalTest method testSingleThreadLimit.

@Test
public void testSingleThreadLimit() {
    Configuration configuration = TestUtils.configWithEnvAddress();
    try (LimitAPI limitAPI = LimitAPIFactory.createLimitAPIByConfig(configuration)) {
        adjustTime();
        testQuotaAcquire(limitAPI, Consts.createSingleValueMap(new String[] { Consts.LABEL_METHOD }, new String[] { Consts.METHOD_PAY }), Consts.MAX_PAY_COUNT);
        testQuotaAcquire(limitAPI, Consts.createSingleValueMap(new String[] { Consts.LABEL_METHOD }, new String[] { Consts.METHOD_CASH }), Consts.MAX_CASH_COUNT);
        testQuotaAcquire(limitAPI, null, Consts.MAX_SERVICE_COUNT);
        System.out.println("start to wait expired");
        Utils.sleepUninterrupted(10 * 1000);
    }
}
Also used : Configuration(com.tencent.polaris.api.config.Configuration) LimitAPI(com.tencent.polaris.ratelimit.api.core.LimitAPI) MatchString(com.tencent.polaris.client.pb.ModelProto.MatchString) Test(org.junit.Test)

Example 5 with Configuration

use of com.tencent.polaris.api.config.Configuration in project polaris-java by polarismesh.

the class ServiceExpireTest method testMultiServices.

@Test
public void testMultiServices() {
    ConsumerAPI consumerAPI = null;
    try {
        Configuration configuration = createConfiguration();
        consumerAPI = DiscoveryAPIFactory.createConsumerAPIByConfig(configuration);
        CountDownLatch countDownLatch = new CountDownLatch(2);
        Thread firstThread = new Thread(new GetServiceTask(consumerAPI, countDownLatch, 0));
        Thread secondThread = new Thread(new GetServiceTask(consumerAPI, countDownLatch, 1));
        firstThread.start();
        secondThread.start();
        countDownLatch.await(2, TimeUnit.SECONDS);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        if (null != consumerAPI) {
            consumerAPI.destroy();
        }
    }
}
Also used : Configuration(com.tencent.polaris.api.config.Configuration) ConsumerAPI(com.tencent.polaris.api.core.ConsumerAPI) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) PolarisException(com.tencent.polaris.api.exception.PolarisException) Test(org.junit.Test)

Aggregations

Configuration (com.tencent.polaris.api.config.Configuration)25 ConsumerAPI (com.tencent.polaris.api.core.ConsumerAPI)18 Test (org.junit.Test)16 InstancesResponse (com.tencent.polaris.api.rpc.InstancesResponse)15 GetOneInstanceRequest (com.tencent.polaris.api.rpc.GetOneInstanceRequest)9 PolarisException (com.tencent.polaris.api.exception.PolarisException)7 Instance (com.tencent.polaris.api.pojo.Instance)6 HashMap (java.util.HashMap)6 GetInstancesRequest (com.tencent.polaris.api.rpc.GetInstancesRequest)5 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)3 ServiceCallResult (com.tencent.polaris.api.rpc.ServiceCallResult)3 ConfigurationImpl (com.tencent.polaris.factory.config.ConfigurationImpl)3 IOException (java.io.IOException)3 ProviderAPI (com.tencent.polaris.api.core.ProviderAPI)2 MatchString (com.tencent.polaris.client.pb.ModelProto.MatchString)2 HttpServer (com.sun.net.httpserver.HttpServer)1 StaticMetadataManager (com.tencent.cloud.common.metadata.StaticMetadataManager)1 DiscoveryPropertiesAutoConfiguration (com.tencent.cloud.polaris.DiscoveryPropertiesAutoConfiguration)1 PolarisDiscoveryProperties (com.tencent.cloud.polaris.PolarisDiscoveryProperties)1 APIConfig (com.tencent.polaris.api.config.global.APIConfig)1