Skip to content

Commit 9165fe0

Browse files
authored
Support multiple tokens per request entry (#380)
- Refactor MetricBucket to support add multiple count - Refactor Node and Metric related classes - Refactor for StatisticSlot Signed-off-by: Eric Zhao <[email protected]>
1 parent 832d6e4 commit 9165fe0

File tree

10 files changed

+79
-76
lines changed

10 files changed

+79
-76
lines changed

sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/ClusterNode.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,11 @@ public synchronized Map<String, StatisticNode> getOriginCountMap() {
9393
* @param count count to add
9494
*/
9595
public void trace(Throwable throwable, int count) {
96+
if (count <= 0) {
97+
return;
98+
}
9699
if (!BlockException.isBlockException(throwable)) {
97-
for (int i = 0; i < count; i++) {
98-
this.increaseExceptionQps();
99-
}
100+
this.increaseExceptionQps(count);
100101
}
101102
}
102103
}

sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/DefaultNode.java

+12-12
Original file line numberDiff line numberDiff line change
@@ -107,21 +107,21 @@ public Set<Node> getChildList() {
107107
}
108108

109109
@Override
110-
public void increaseBlockQps() {
111-
super.increaseBlockQps();
112-
this.clusterNode.increaseBlockQps();
110+
public void increaseBlockQps(int count) {
111+
super.increaseBlockQps(count);
112+
this.clusterNode.increaseBlockQps(count);
113113
}
114114

115115
@Override
116-
public void increaseExceptionQps() {
117-
super.increaseExceptionQps();
118-
this.clusterNode.increaseExceptionQps();
116+
public void increaseExceptionQps(int count) {
117+
super.increaseExceptionQps(count);
118+
this.clusterNode.increaseExceptionQps(count);
119119
}
120120

121121
@Override
122-
public void rt(long rt) {
123-
super.rt(rt);
124-
this.clusterNode.rt(rt);
122+
public void addRtAndSuccess(long rt, int successCount) {
123+
super.addRtAndSuccess(rt, successCount);
124+
this.clusterNode.addRtAndSuccess(rt, successCount);
125125
}
126126

127127
@Override
@@ -137,9 +137,9 @@ public void decreaseThreadNum() {
137137
}
138138

139139
@Override
140-
public void addPassRequest() {
141-
super.addPassRequest();
142-
this.clusterNode.addPassRequest();
140+
public void addPassRequest(int count) {
141+
super.addPassRequest(count);
142+
this.clusterNode.addPassRequest(count);
143143
}
144144

145145
public void printDefaultNode() {

sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/Node.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
* @author qinan.qn
2727
* @author leyou
2828
* @author Eric Zhao
29-
* @author leitao
3029
*/
3130
public interface Node {
3231

@@ -122,25 +121,28 @@ public interface Node {
122121

123122
/**
124123
* Add pass count.
124+
*
125+
* @param count count to add pass
125126
*/
126-
void addPassRequest();
127+
void addPassRequest(int count);
127128

128129
/**
129130
* Add rt and success count.
130131
*
131132
* @param rt response time
133+
* @param success success count to add
132134
*/
133-
void rt(long rt);
135+
void addRtAndSuccess(long rt, int success);
134136

135137
/**
136138
* Increase the block count.
137139
*/
138-
void increaseBlockQps();
140+
void increaseBlockQps(int count);
139141

140142
/**
141143
* Increase the biz exception count.
142144
*/
143-
void increaseExceptionQps();
145+
void increaseExceptionQps(int count);
144146

145147
/**
146148
* Increase current thread count.

sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/StatisticNode.java

+12-12
Original file line numberDiff line numberDiff line change
@@ -227,30 +227,30 @@ public int curThreadNum() {
227227
}
228228

229229
@Override
230-
public void addPassRequest() {
231-
rollingCounterInSecond.addPass();
232-
rollingCounterInMinute.addPass();
230+
public void addPassRequest(int count) {
231+
rollingCounterInSecond.addPass(count);
232+
rollingCounterInMinute.addPass(count);
233233
}
234234

235235
@Override
236-
public void rt(long rt) {
237-
rollingCounterInSecond.addSuccess();
236+
public void addRtAndSuccess(long rt, int successCount) {
237+
rollingCounterInSecond.addSuccess(successCount);
238238
rollingCounterInSecond.addRT(rt);
239239

240-
rollingCounterInMinute.addSuccess();
240+
rollingCounterInMinute.addSuccess(successCount);
241241
rollingCounterInMinute.addRT(rt);
242242
}
243243

244244
@Override
245-
public void increaseBlockQps() {
246-
rollingCounterInSecond.addBlock();
247-
rollingCounterInMinute.addBlock();
245+
public void increaseBlockQps(int count) {
246+
rollingCounterInSecond.addBlock(count);
247+
rollingCounterInMinute.addBlock(count);
248248
}
249249

250250
@Override
251-
public void increaseExceptionQps() {
252-
rollingCounterInSecond.addException();
253-
rollingCounterInMinute.addException();
251+
public void increaseExceptionQps(int count) {
252+
rollingCounterInSecond.addException(count);
253+
rollingCounterInMinute.addException(count);
254254

255255
}
256256

sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/StatisticSlot.java

+12-12
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,18 @@ public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode
5656

5757
// Request passed, add thread count and pass count.
5858
node.increaseThreadNum();
59-
node.addPassRequest();
59+
node.addPassRequest(count);
6060

6161
if (context.getCurEntry().getOriginNode() != null) {
6262
// Add count for origin node.
6363
context.getCurEntry().getOriginNode().increaseThreadNum();
64-
context.getCurEntry().getOriginNode().addPassRequest();
64+
context.getCurEntry().getOriginNode().addPassRequest(count);
6565
}
6666

6767
if (resourceWrapper.getType() == EntryType.IN) {
6868
// Add count for global inbound entry node for global statistics.
6969
Constants.ENTRY_NODE.increaseThreadNum();
70-
Constants.ENTRY_NODE.addPassRequest();
70+
Constants.ENTRY_NODE.addPassRequest(count);
7171
}
7272

7373
// Handle pass event with registered entry callback handlers.
@@ -79,14 +79,14 @@ public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode
7979
context.getCurEntry().setError(e);
8080

8181
// Add block count.
82-
node.increaseBlockQps();
82+
node.increaseBlockQps(count);
8383
if (context.getCurEntry().getOriginNode() != null) {
84-
context.getCurEntry().getOriginNode().increaseBlockQps();
84+
context.getCurEntry().getOriginNode().increaseBlockQps(count);
8585
}
8686

8787
if (resourceWrapper.getType() == EntryType.IN) {
8888
// Add count for global inbound entry node for global statistics.
89-
Constants.ENTRY_NODE.increaseBlockQps();
89+
Constants.ENTRY_NODE.increaseBlockQps(count);
9090
}
9191

9292
// Handle block event with registered entry callback handlers.
@@ -100,13 +100,13 @@ public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode
100100
context.getCurEntry().setError(e);
101101

102102
// This should not happen.
103-
node.increaseExceptionQps();
103+
node.increaseExceptionQps(count);
104104
if (context.getCurEntry().getOriginNode() != null) {
105-
context.getCurEntry().getOriginNode().increaseExceptionQps();
105+
context.getCurEntry().getOriginNode().increaseExceptionQps(count);
106106
}
107107

108108
if (resourceWrapper.getType() == EntryType.IN) {
109-
Constants.ENTRY_NODE.increaseExceptionQps();
109+
Constants.ENTRY_NODE.increaseExceptionQps(count);
110110
}
111111
throw e;
112112
}
@@ -124,9 +124,9 @@ public void exit(Context context, ResourceWrapper resourceWrapper, int count, Ob
124124
}
125125

126126
// Record response time and success count.
127-
node.rt(rt);
127+
node.addRtAndSuccess(rt, count);
128128
if (context.getCurEntry().getOriginNode() != null) {
129-
context.getCurEntry().getOriginNode().rt(rt);
129+
context.getCurEntry().getOriginNode().addRtAndSuccess(rt, count);
130130
}
131131

132132
node.decreaseThreadNum();
@@ -136,7 +136,7 @@ public void exit(Context context, ResourceWrapper resourceWrapper, int count, Ob
136136
}
137137

138138
if (resourceWrapper.getType() == EntryType.IN) {
139-
Constants.ENTRY_NODE.rt(rt);
139+
Constants.ENTRY_NODE.addRtAndSuccess(rt, count);
140140
Constants.ENTRY_NODE.decreaseThreadNum();
141141
}
142142
} else {

sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/data/MetricBucket.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,20 @@ public long success() {
9090
return get(MetricEvent.SUCCESS);
9191
}
9292

93-
public void addPass() {
94-
add(MetricEvent.PASS, 1);
93+
public void addPass(int n) {
94+
add(MetricEvent.PASS, n);
9595
}
9696

97-
public void addException() {
98-
add(MetricEvent.EXCEPTION, 1);
97+
public void addException(int n) {
98+
add(MetricEvent.EXCEPTION, n);
9999
}
100100

101-
public void addBlock() {
102-
add(MetricEvent.BLOCK, 1);
101+
public void addBlock(int n) {
102+
add(MetricEvent.BLOCK, n);
103103
}
104104

105-
public void addSuccess() {
106-
add(MetricEvent.SUCCESS, 1);
105+
public void addSuccess(int n) {
106+
add(MetricEvent.SUCCESS, n);
107107
}
108108

109109
public void addRT(long rt) {

sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/metric/ArrayMetric.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -162,27 +162,27 @@ public MetricBucket[] windows() {
162162
}
163163

164164
@Override
165-
public void addException() {
165+
public void addException(int count) {
166166
WindowWrap<MetricBucket> wrap = data.currentWindow();
167-
wrap.value().addException();
167+
wrap.value().addException(count);
168168
}
169169

170170
@Override
171-
public void addBlock() {
171+
public void addBlock(int count) {
172172
WindowWrap<MetricBucket> wrap = data.currentWindow();
173-
wrap.value().addBlock();
173+
wrap.value().addBlock(count);
174174
}
175175

176176
@Override
177-
public void addSuccess() {
177+
public void addSuccess(int count) {
178178
WindowWrap<MetricBucket> wrap = data.currentWindow();
179-
wrap.value().addSuccess();
179+
wrap.value().addSuccess(count);
180180
}
181181

182182
@Override
183-
public void addPass() {
183+
public void addPass(int count) {
184184
WindowWrap<MetricBucket> wrap = data.currentWindow();
185-
wrap.value().addPass();
185+
wrap.value().addPass(count);
186186
}
187187

188188
@Override

sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/metric/Metric.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -94,22 +94,22 @@ public interface Metric {
9494
/**
9595
* Increment by one the current exception count.
9696
*/
97-
void addException();
97+
void addException(int n);
9898

9999
/**
100-
* Increment by one the current blovk count.
100+
* Increment by one the current block count.
101101
*/
102-
void addBlock();
102+
void addBlock(int n);
103103

104104
/**
105105
* Increment by one the current success count.
106106
*/
107-
void addSuccess();
107+
void addSuccess(int n);
108108

109109
/**
110110
* Increment by one the current pass count.
111111
*/
112-
void addPass();
112+
void addPass(int n);
113113

114114
/**
115115
* Add given RT to current total RT.

sentinel-core/src/test/java/com/alibaba/csp/sentinel/base/metric/ArrayMetricTest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,16 @@ public void testOperateArrayMetric() {
5454

5555
metric.addRT(expectedRt);
5656
for (int i = 0; i < expectedPass; i++) {
57-
metric.addPass();
57+
metric.addPass(1);
5858
}
5959
for (int i = 0; i < expectedBlock; i++) {
60-
metric.addBlock();
60+
metric.addBlock(1);
6161
}
6262
for (int i = 0; i < expectedSuccess; i++) {
63-
metric.addSuccess();
63+
metric.addSuccess(1);
6464
}
6565
for (int i = 0; i < expectedException; i++) {
66-
metric.addException();
66+
metric.addException(1);
6767
}
6868

6969
assertEquals(expectedPass, metric.pass());

sentinel-core/src/test/java/com/alibaba/csp/sentinel/base/metric/MetricsLeapArrayTest.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ public void testWindowAfterOneInterval() {
7979
MetricBucket currentWindow = window.value();
8080
assertNotNull(currentWindow);
8181

82-
currentWindow.addPass();
83-
currentWindow.addBlock();
82+
currentWindow.addPass(1);
83+
currentWindow.addBlock(1);
8484
assertEquals(1L, currentWindow.pass());
8585
assertEquals(1L, currentWindow.block());
8686

@@ -90,7 +90,7 @@ public void testWindowAfterOneInterval() {
9090
assertEquals(previousWindowStart, window.windowStart());
9191

9292
MetricBucket middleWindow = window.value();
93-
middleWindow.addPass();
93+
middleWindow.addPass(1);
9494
assertSame(currentWindow, middleWindow);
9595
assertEquals(2L, middleWindow.pass());
9696
assertEquals(1L, middleWindow.block());
@@ -114,7 +114,7 @@ public void testWindowDeprecatedRefresh() {
114114
List<WindowWrap<MetricBucket>> firstIterWindowList = new ArrayList<WindowWrap<MetricBucket>>(len);
115115
for (int i = 0; i < len; i++) {
116116
WindowWrap<MetricBucket> w = leapArray.currentWindow(firstTime + windowLengthInMs * i);
117-
w.value().addPass();
117+
w.value().addPass(1);
118118
firstIterWindowList.add(i, w);
119119
}
120120

@@ -133,7 +133,7 @@ public void testMultiThreadUpdateEmptyWindow() throws Exception {
133133
Runnable task = new Runnable() {
134134
@Override
135135
public void run() {
136-
leapArray.currentWindow(time).value().addPass();
136+
leapArray.currentWindow(time).value().addPass(1);
137137
latch.countDown();
138138
}
139139
};
@@ -183,7 +183,7 @@ public void testListWindowsResetOld() throws Exception {
183183
Thread.sleep(windowLengthInMs + intervalInMs);
184184

185185
// This will replace the deprecated bucket, so all deprecated buckets will be reset.
186-
leapArray.currentWindow(time + windowLengthInMs + intervalInMs).value().addPass();
186+
leapArray.currentWindow(time + windowLengthInMs + intervalInMs).value().addPass(1);
187187

188188
assertEquals(1, leapArray.list().size());
189189
}
@@ -212,7 +212,7 @@ public void testListWindowsNewBucket() throws Exception {
212212

213213
// This won't hit deprecated bucket, so no deprecated buckets will be reset.
214214
// But deprecated buckets can be filtered when collecting list.
215-
leapArray.currentWindow(TimeUtil.currentTimeMillis()).value().addPass();
215+
leapArray.currentWindow(TimeUtil.currentTimeMillis()).value().addPass(1);
216216

217217
assertEquals(1, leapArray.list().size());
218218
}

0 commit comments

Comments
 (0)