Search in sources :

Example 1 with AbstractQpsStrategy

use of org.apache.servicecomb.qps.strategy.AbstractQpsStrategy in project java-chassis by ServiceComb.

the class QpsControllerManager method chooseStrategy.

private AbstractQpsStrategy chooseStrategy(String configKey, Long limit, Long bucket, String strategyName) {
    if (StringUtils.isEmpty(strategyName)) {
        strategyName = "FixedWindow";
    }
    AbstractQpsStrategy strategy = null;
    List<IStrategyFactory> strategyFactories = SPIServiceUtils.getOrLoadSortedService(IStrategyFactory.class);
    for (IStrategyFactory strategyFactory : strategyFactories) {
        strategy = strategyFactory.createStrategy(strategyName);
        if (strategy != null) {
            break;
        }
    }
    if (strategy == null) {
        throw new ServiceCombException("the qps strategy name " + strategyName + " is not exist , please check.");
    }
    strategy.setKey(configKey);
    strategy.setQpsLimit(limit);
    strategy.setBucketLimit(bucket);
    return strategy;
}
Also used : AbstractQpsStrategy(org.apache.servicecomb.qps.strategy.AbstractQpsStrategy) IStrategyFactory(org.apache.servicecomb.qps.strategy.IStrategyFactory) ServiceCombException(org.apache.servicecomb.foundation.common.exceptions.ServiceCombException)

Example 2 with AbstractQpsStrategy

use of org.apache.servicecomb.qps.strategy.AbstractQpsStrategy in project java-chassis by ServiceComb.

the class QpsControllerManager method createQpsControllerIfNotExist.

private void createQpsControllerIfNotExist(String configKey) {
    if (configQpsControllerMap.containsKey(configKey)) {
        return;
    }
    LOGGER.info("Create qpsController, configKey = [{}]", configKey);
    DynamicProperty limitProperty = DynamicProperty.getInstance(limitKeyPrefix + configKey);
    DynamicProperty bucketProperty = DynamicProperty.getInstance(bucketKeyPrefix + configKey);
    DynamicProperty strategyProperty = DynamicProperty.getInstance(Config.STRATEGY_KEY);
    AbstractQpsStrategy qpsStrategy = chooseStrategy(configKey, limitProperty.getLong(), bucketProperty.getLong(), strategyProperty.getString());
    strategyProperty.addCallback(() -> {
        AbstractQpsStrategy innerQpsStrategy = chooseStrategy(configKey, limitProperty.getLong(), bucketProperty.getLong(), strategyProperty.getString());
        configQpsControllerMap.put(configKey, innerQpsStrategy);
        LOGGER.info("Global flow control strategy update, value = [{}]", strategyProperty.getString());
        updateObjMap(configKey);
    });
    limitProperty.addCallback(() -> {
        qpsStrategy.setQpsLimit(limitProperty.getLong());
        LOGGER.info("Qps limit updated, configKey = [{}], value = [{}]", configKey, limitProperty.getString());
        updateObjMap(configKey);
    });
    bucketProperty.addCallback(() -> {
        qpsStrategy.setBucketLimit(bucketProperty.getLong());
        LOGGER.info("bucket limit updated, configKey = [{}], value = [{}]", configKey, bucketProperty.getString());
        updateObjMap(configKey);
    });
    configQpsControllerMap.put(configKey, qpsStrategy);
}
Also used : DynamicProperty(com.netflix.config.DynamicProperty) AbstractQpsStrategy(org.apache.servicecomb.qps.strategy.AbstractQpsStrategy)

Example 3 with AbstractQpsStrategy

use of org.apache.servicecomb.qps.strategy.AbstractQpsStrategy in project java-chassis by ServiceComb.

the class QpsControllerManager method create.

/**
 * Create relevant qpsLimit dynamicProperty and watch the configuration change.
 * Search and return a valid qpsController.
 */
private AbstractQpsStrategy create(String qualifiedNameKey, String microserviceName, Invocation invocation) {
    createForService(qualifiedNameKey, microserviceName, invocation);
    String qualifiedAnyServiceName = Config.ANY_SERVICE + qualifiedNameKey.substring(microserviceName.length());
    createForService(qualifiedAnyServiceName, Config.ANY_SERVICE, invocation);
    AbstractQpsStrategy strategy = searchQpsController(qualifiedNameKey);
    if (strategy == null) {
        strategy = searchQpsController(qualifiedAnyServiceName);
    }
    if (strategy == null) {
        return globalQpsStrategy;
    }
    return strategy;
}
Also used : AbstractQpsStrategy(org.apache.servicecomb.qps.strategy.AbstractQpsStrategy)

Example 4 with AbstractQpsStrategy

use of org.apache.servicecomb.qps.strategy.AbstractQpsStrategy in project java-chassis by ServiceComb.

the class QpsControllerManagerTest method testGetOrCreateWithGlobalConfig.

@Test
public void testGetOrCreateWithGlobalConfig(@Mocked Invocation invocation, @Mocked OperationMeta operationMeta) {
    new Expectations() {

        {
            invocation.getOperationMeta();
            result = operationMeta;
            invocation.getSchemaId();
            result = "server";
            operationMeta.getSchemaQualifiedName();
            result = "server.test";
        }
    };
    QpsControllerManager testQpsControllerManager = new QpsControllerManager(true);
    // global
    setConfig(Config.PROVIDER_LIMIT_KEY_GLOBAL, 50);
    QpsStrategy qpsStrategy = testQpsControllerManager.getOrCreate("pojo", invocation);
    Assert.assertEquals(Config.PROVIDER_LIMIT_KEY_GLOBAL, ((AbstractQpsStrategy) qpsStrategy).getKey());
    Assert.assertTrue(50 == ((AbstractQpsStrategy) qpsStrategy).getQpsLimit());
    qpsStrategy = testQpsControllerManager.getOrCreate("pojo2", invocation);
    Assert.assertEquals(Config.PROVIDER_LIMIT_KEY_GLOBAL, ((AbstractQpsStrategy) qpsStrategy).getKey());
    Assert.assertTrue(50 == ((AbstractQpsStrategy) qpsStrategy).getQpsLimit());
    qpsStrategy = testQpsControllerManager.getOrCreate("poj", invocation);
    Assert.assertEquals(Config.PROVIDER_LIMIT_KEY_GLOBAL, ((AbstractQpsStrategy) qpsStrategy).getKey());
    Assert.assertTrue(50 == ((AbstractQpsStrategy) qpsStrategy).getQpsLimit());
    // pojo
    setConfigWithDefaultPrefix(true, "pojo", 100);
    qpsStrategy = testQpsControllerManager.getOrCreate("pojo", invocation);
    Assert.assertEquals("pojo", ((AbstractQpsStrategy) qpsStrategy).getKey());
    Assert.assertTrue(100 == ((AbstractQpsStrategy) qpsStrategy).getQpsLimit());
    qpsStrategy = testQpsControllerManager.getOrCreate("pojo2", invocation);
    Assert.assertEquals(Config.PROVIDER_LIMIT_KEY_GLOBAL, ((AbstractQpsStrategy) qpsStrategy).getKey());
    Assert.assertTrue(50 == ((AbstractQpsStrategy) qpsStrategy).getQpsLimit());
    qpsStrategy = testQpsControllerManager.getOrCreate("poj", invocation);
    Assert.assertEquals(Config.PROVIDER_LIMIT_KEY_GLOBAL, ((AbstractQpsStrategy) qpsStrategy).getKey());
    Assert.assertTrue(50 == ((AbstractQpsStrategy) qpsStrategy).getQpsLimit());
    testGetOrCreateCommon(true, testQpsControllerManager, invocation, operationMeta);
}
Also used : Expectations(mockit.Expectations) AbstractQpsStrategy(org.apache.servicecomb.qps.strategy.AbstractQpsStrategy) AbstractQpsStrategy(org.apache.servicecomb.qps.strategy.AbstractQpsStrategy) Test(org.junit.Test)

Example 5 with AbstractQpsStrategy

use of org.apache.servicecomb.qps.strategy.AbstractQpsStrategy in project java-chassis by ServiceComb.

the class QpsControllerManagerTest method testQualifiedNameKey.

@Test
public void testQualifiedNameKey(@Mocked Invocation invocation, @Mocked OperationMeta operationMeta) {
    new Expectations() {

        {
            invocation.getOperationMeta();
            result = operationMeta;
            invocation.getSchemaId();
            result = "schema";
            operationMeta.getSchemaQualifiedName();
            result = "schema.opr";
        }
    };
    QpsControllerManager qpsControllerManager = new QpsControllerManager(true);
    QpsStrategy qpsStrategy = qpsControllerManager.getOrCreate("service", invocation);
    Assert.assertEquals("servicecomb.flowcontrol.Provider.qps.global.limit", ((AbstractQpsStrategy) qpsStrategy).getKey());
    Assert.assertEquals(Integer.MAX_VALUE, ((AbstractQpsStrategy) qpsStrategy).getQpsLimit().intValue());
    new Expectations() {

        {
            invocation.getOperationMeta();
            result = operationMeta;
            invocation.getSchemaId();
            result = "test_schema";
            operationMeta.getSchemaQualifiedName();
            result = "test_schema.test_opr";
        }
    };
    qpsStrategy = qpsControllerManager.getOrCreate("test_service", invocation);
    Assert.assertEquals("servicecomb.flowcontrol.Provider.qps.global.limit", ((AbstractQpsStrategy) qpsStrategy).getKey());
    Assert.assertEquals(Integer.MAX_VALUE, ((AbstractQpsStrategy) qpsStrategy).getQpsLimit().intValue());
    new Expectations() {

        {
            invocation.getOperationMeta();
            result = operationMeta;
            invocation.getSchemaId();
            result = "test_schema";
            operationMeta.getSchemaQualifiedName();
            result = "test-schema.test-opr";
        }
    };
    qpsStrategy = qpsControllerManager.getOrCreate("test-service", invocation);
    Assert.assertEquals("servicecomb.flowcontrol.Provider.qps.global.limit", ((AbstractQpsStrategy) qpsStrategy).getKey());
    Assert.assertEquals(Integer.MAX_VALUE, ((AbstractQpsStrategy) qpsStrategy).getQpsLimit().intValue());
    new Expectations() {

        {
            invocation.getOperationMeta();
            result = operationMeta;
            invocation.getSchemaId();
            result = "schema";
            operationMeta.getSchemaQualifiedName();
            result = "schema.opr.tail";
        }
    };
    qpsStrategy = qpsControllerManager.getOrCreate("svc", invocation);
    Assert.assertEquals("servicecomb.flowcontrol.Provider.qps.global.limit", ((AbstractQpsStrategy) qpsStrategy).getKey());
    Assert.assertEquals(Integer.MAX_VALUE, ((AbstractQpsStrategy) qpsStrategy).getQpsLimit().intValue());
    new Expectations() {

        {
            invocation.getOperationMeta();
            result = operationMeta;
            invocation.getSchemaId();
            result = "schema.opr2";
            operationMeta.getSchemaQualifiedName();
            result = "schema.opr2.tail";
        }
    };
    qpsStrategy = qpsControllerManager.getOrCreate("svc", invocation);
    Assert.assertEquals("servicecomb.flowcontrol.Provider.qps.global.limit", ((AbstractQpsStrategy) qpsStrategy).getKey());
    Assert.assertEquals(Integer.MAX_VALUE, ((AbstractQpsStrategy) qpsStrategy).getQpsLimit().intValue());
}
Also used : Expectations(mockit.Expectations) AbstractQpsStrategy(org.apache.servicecomb.qps.strategy.AbstractQpsStrategy) AbstractQpsStrategy(org.apache.servicecomb.qps.strategy.AbstractQpsStrategy) Test(org.junit.Test)

Aggregations

AbstractQpsStrategy (org.apache.servicecomb.qps.strategy.AbstractQpsStrategy)17 Test (org.junit.Test)10 FixedWindowStrategy (org.apache.servicecomb.qps.strategy.FixedWindowStrategy)7 Expectations (mockit.Expectations)5 MockUp (mockit.MockUp)4 InvocationException (org.apache.servicecomb.swagger.invocation.exception.InvocationException)3 OperationMeta (org.apache.servicecomb.core.definition.OperationMeta)2 DynamicProperty (com.netflix.config.DynamicProperty)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 ServiceCombException (org.apache.servicecomb.foundation.common.exceptions.ServiceCombException)1 IStrategyFactory (org.apache.servicecomb.qps.strategy.IStrategyFactory)1 ExpectedException (org.junit.rules.ExpectedException)1