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;
}
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);
}
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;
}
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);
}
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());
}
Aggregations