init
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>sentinel-demo</artifactId>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<version>1.8.3</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>sentinel-demo-parameter-flow-control</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-parameter-flow-control</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba.csp</groupId>
|
||||
<artifactId>sentinel-transport-simple-http</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.csp.sentinel.demo.flow.param;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowItem;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule;
|
||||
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRuleManager;
|
||||
|
||||
/**
|
||||
* This demo demonstrates flow control by frequent ("hot spot") parameters.
|
||||
*
|
||||
* @author Eric Zhao
|
||||
* @since 0.2.0
|
||||
*/
|
||||
public class ParamFlowQpsDemo {
|
||||
|
||||
private static final int PARAM_A = 1;
|
||||
private static final int PARAM_B = 2;
|
||||
private static final int PARAM_C = 3;
|
||||
private static final int PARAM_D = 4;
|
||||
|
||||
/**
|
||||
* Here we prepare different parameters to validate flow control by parameters.
|
||||
*/
|
||||
private static final Integer[] PARAMS = new Integer[] {PARAM_A, PARAM_B, PARAM_C, PARAM_D};
|
||||
|
||||
private static final String RESOURCE_KEY = "resA";
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
initParamFlowRules();
|
||||
|
||||
final int threadCount = 20;
|
||||
ParamFlowQpsRunner<Integer> runner = new ParamFlowQpsRunner<>(PARAMS, RESOURCE_KEY, threadCount, 120);
|
||||
runner.tick();
|
||||
|
||||
Thread.sleep(1000);
|
||||
runner.simulateTraffic();
|
||||
}
|
||||
|
||||
private static void initParamFlowRules() {
|
||||
// QPS mode, threshold is 5 for every frequent "hot spot" parameter in index 0 (the first arg).
|
||||
ParamFlowRule rule = new ParamFlowRule(RESOURCE_KEY)
|
||||
.setParamIdx(0)
|
||||
.setGrade(RuleConstant.FLOW_GRADE_QPS)
|
||||
//.setDurationInSec(3)
|
||||
//.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER)
|
||||
//.setMaxQueueingTimeMs(600)
|
||||
.setCount(5);
|
||||
|
||||
// We can set threshold count for specific parameter value individually.
|
||||
// Here we add an exception item. That means: QPS threshold of entries with parameter `PARAM_B` (type: int)
|
||||
// in index 0 will be 10, rather than the global threshold (5).
|
||||
ParamFlowItem item = new ParamFlowItem().setObject(String.valueOf(PARAM_B))
|
||||
.setClassType(int.class.getName())
|
||||
.setCount(10);
|
||||
rule.setParamFlowItemList(Collections.singletonList(item));
|
||||
ParamFlowRuleManager.loadRules(Collections.singletonList(rule));
|
||||
}
|
||||
}
|
@@ -0,0 +1,179 @@
|
||||
/*
|
||||
* Copyright 1999-2018 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.csp.sentinel.demo.flow.param;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import com.alibaba.csp.sentinel.Entry;
|
||||
import com.alibaba.csp.sentinel.EntryType;
|
||||
import com.alibaba.csp.sentinel.SphU;
|
||||
import com.alibaba.csp.sentinel.slots.block.BlockException;
|
||||
import com.alibaba.csp.sentinel.util.StringUtil;
|
||||
import com.alibaba.csp.sentinel.util.TimeUtil;
|
||||
|
||||
/**
|
||||
* A traffic runner to simulate flow for different parameters.
|
||||
*
|
||||
* @author Eric Zhao
|
||||
* @since 0.2.0
|
||||
*/
|
||||
class ParamFlowQpsRunner<T> {
|
||||
|
||||
private final T[] params;
|
||||
private final String resourceName;
|
||||
private int seconds;
|
||||
private final int threadCount;
|
||||
|
||||
private final Map<T, AtomicLong> passCountMap = new ConcurrentHashMap<>();
|
||||
private final Map<T, AtomicLong> blockCountMap = new ConcurrentHashMap<>();
|
||||
|
||||
private volatile boolean stop = false;
|
||||
|
||||
public ParamFlowQpsRunner(T[] params, String resourceName, int threadCount, int seconds) {
|
||||
assertTrue(params != null && params.length > 0, "Parameter array should not be empty");
|
||||
assertTrue(StringUtil.isNotBlank(resourceName), "Resource name cannot be empty");
|
||||
assertTrue(seconds > 0, "Time period should be positive");
|
||||
assertTrue(threadCount > 0 && threadCount <= 1000, "Invalid thread count");
|
||||
this.params = params;
|
||||
this.resourceName = resourceName;
|
||||
this.seconds = seconds;
|
||||
this.threadCount = threadCount;
|
||||
|
||||
for (T param : params) {
|
||||
assertTrue(param != null, "Parameters should not be null");
|
||||
passCountMap.putIfAbsent(param, new AtomicLong());
|
||||
blockCountMap.putIfAbsent(param, new AtomicLong());
|
||||
}
|
||||
}
|
||||
|
||||
private void assertTrue(boolean b, String message) {
|
||||
if (!b) {
|
||||
throw new IllegalArgumentException(message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Pick one of provided parameters randomly.
|
||||
*
|
||||
* @return picked parameter
|
||||
*/
|
||||
private T generateParam() {
|
||||
int i = ThreadLocalRandom.current().nextInt(0, params.length);
|
||||
return params[i];
|
||||
}
|
||||
|
||||
void simulateTraffic() {
|
||||
for (int i = 0; i < threadCount; i++) {
|
||||
Thread t = new Thread(new RunTask());
|
||||
t.setName("sentinel-simulate-traffic-task-" + i);
|
||||
t.start();
|
||||
}
|
||||
}
|
||||
|
||||
void tick() {
|
||||
Thread timer = new Thread(new TimerTask());
|
||||
timer.setName("sentinel-timer-task");
|
||||
timer.start();
|
||||
}
|
||||
|
||||
private void passFor(T param) {
|
||||
passCountMap.get(param).incrementAndGet();
|
||||
// System.out.println(String.format("Parameter <%s> passed at: %d", param, TimeUtil.currentTimeMillis()));
|
||||
}
|
||||
|
||||
private void blockFor(T param) {
|
||||
blockCountMap.get(param).incrementAndGet();
|
||||
}
|
||||
|
||||
final class RunTask implements Runnable {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
while (!stop) {
|
||||
Entry entry = null;
|
||||
T param = generateParam();
|
||||
try {
|
||||
entry = SphU.entry(resourceName, EntryType.IN, 1, param);
|
||||
// Add pass for parameter.
|
||||
passFor(param);
|
||||
} catch (BlockException e) {
|
||||
// block.incrementAndGet();
|
||||
blockFor(param);
|
||||
} catch (Exception ex) {
|
||||
// biz exception
|
||||
ex.printStackTrace();
|
||||
} finally {
|
||||
// total.incrementAndGet();
|
||||
if (entry != null) {
|
||||
entry.exit(1, param);
|
||||
}
|
||||
}
|
||||
|
||||
sleep(ThreadLocalRandom.current().nextInt(0, 10));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void sleep(int timeMs) {
|
||||
try {
|
||||
TimeUnit.MILLISECONDS.sleep(timeMs);
|
||||
} catch (InterruptedException e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
final class TimerTask implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
long start = System.currentTimeMillis();
|
||||
System.out.println("Begin to run! Go go go!");
|
||||
System.out.println("See corresponding metrics.log for accurate statistic data");
|
||||
|
||||
Map<T, Long> map = new HashMap<>(params.length);
|
||||
for (T param : params) {
|
||||
map.putIfAbsent(param, 0L);
|
||||
}
|
||||
while (!stop) {
|
||||
sleep(1000);
|
||||
|
||||
// There may be a mismatch for time window of internal sliding window.
|
||||
// See corresponding `metrics.log` for accurate statistic log.
|
||||
for (T param : params) {
|
||||
|
||||
System.out.println(String.format(
|
||||
"[%d][%d] Parameter flow metrics for resource %s: "
|
||||
+ "pass count for param <%s> is %d, block count: %d",
|
||||
seconds, TimeUtil.currentTimeMillis(), resourceName, param,
|
||||
passCountMap.get(param).getAndSet(0), blockCountMap.get(param).getAndSet(0)));
|
||||
}
|
||||
System.out.println("=============================");
|
||||
if (seconds-- <= 0) {
|
||||
stop = true;
|
||||
}
|
||||
}
|
||||
|
||||
long cost = System.currentTimeMillis() - start;
|
||||
System.out.println("Time cost: " + cost + " ms");
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user