-
Notifications
You must be signed in to change notification settings - Fork 8k
/
SphU.java
executable file
·368 lines (343 loc) · 18.5 KB
/
SphU.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
* 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;
import java.lang.reflect.Method;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.Rule;
import com.alibaba.csp.sentinel.slots.system.SystemRule;
/**
* <p>The fundamental Sentinel API for recording statistics and performing rule checking for resources.</p>
* <p>
* Conceptually, physical or logical resource that need protection should be
* surrounded by an entry. The requests to this resource will be blocked if any
* criteria is met, eg. when any {@link Rule}'s threshold is exceeded. Once blocked,
* a {@link BlockException} will be thrown.
* </p>
* <p>
* To configure the criteria, we can use <code>XxxRuleManager.loadRules()</code> to load rules.
* </p>
*
* <p>
* Following code is an example, {@code "abc"} represent a unique name for the
* protected resource:
* </p>
*
* <pre>
* public void foo() {
* Entry entry = null;
* try {
* entry = SphU.entry("abc");
* // resource that need protection
* } catch (BlockException blockException) {
* // when goes there, it is blocked
* // add blocked handle logic here
* } catch (Throwable bizException) {
* // business exception
* Tracer.trace(bizException);
* } finally {
* // ensure finally be executed
* if (entry != null){
* entry.exit();
* }
* }
* }
* </pre>
*
* <p>
* Make sure {@code SphU.entry()} and {@link Entry#exit()} be paired in the same thread,
* otherwise {@link ErrorEntryFreeException} will be thrown.
* </p>
*
* @author jialiang.linjl
* @author Eric Zhao
* @see SphO
*/
public class SphU {
private static final Object[] OBJECTS0 = new Object[0];
private SphU() {}
/**
* Record statistics and perform rule checking for the given resource.
*
* @param name the unique name of the protected resource
* @return the {@link Entry} of this invocation (used for mark the invocation complete and get context data)
* @throws BlockException if the block criteria is met (e.g. metric exceeded the threshold of any rules)
*/
public static Entry entry(String name) throws BlockException {
return Env.sph.entry(name, EntryType.OUT, 1, OBJECTS0);
}
/**
* Checking all {@link Rule}s about the protected method.
*
* @param method the protected method
* @return the {@link Entry} of this invocation (used for mark the invocation complete and get context data)
* @throws BlockException if the block criteria is met (e.g. metric exceeded the threshold of any rules)
*/
public static Entry entry(Method method) throws BlockException {
return Env.sph.entry(method, EntryType.OUT, 1, OBJECTS0);
}
/**
* Checking all {@link Rule}s about the protected method.
*
* @param method the protected method
* @param batchCount the amount of calls within the invocation (e.g. batchCount=2 means request for 2 tokens)
* @return the {@link Entry} of this invocation (used for mark the invocation complete and get context data)
* @throws BlockException if the block criteria is met (e.g. metric exceeded the threshold of any rules)
*/
public static Entry entry(Method method, int batchCount) throws BlockException {
return Env.sph.entry(method, EntryType.OUT, batchCount, OBJECTS0);
}
/**
* Record statistics and perform rule checking for the given resource.
*
* @param name the unique string for the resource
* @param batchCount the amount of calls within the invocation (e.g. batchCount=2 means request for 2 tokens)
* @return the {@link Entry} of this invocation (used for mark the invocation complete and get context data)
* @throws BlockException if the block criteria is met (e.g. metric exceeded the threshold of any rules)
*/
public static Entry entry(String name, int batchCount) throws BlockException {
return Env.sph.entry(name, EntryType.OUT, batchCount, OBJECTS0);
}
/**
* Checking all {@link Rule}s about the protected method.
*
* @param method the protected method
* @param trafficType the traffic type (inbound, outbound or internal). This is used
* to mark whether it can be blocked when the system is unstable,
* only inbound traffic could be blocked by {@link SystemRule}
* @throws BlockException if the block criteria is met (e.g. metric exceeded the threshold of any rules)
*/
public static Entry entry(Method method, EntryType trafficType) throws BlockException {
return Env.sph.entry(method, trafficType, 1, OBJECTS0);
}
/**
* Record statistics and perform rule checking for the given resource.
*
* @param name the unique name for the protected resource
* @param trafficType the traffic type (inbound, outbound or internal). This is used
* to mark whether it can be blocked when the system is unstable,
* only inbound traffic could be blocked by {@link SystemRule}
* @throws BlockException if the block criteria is met (e.g. metric exceeded the threshold of any rules)
*/
public static Entry entry(String name, EntryType trafficType) throws BlockException {
return Env.sph.entry(name, trafficType, 1, OBJECTS0);
}
/**
* Checking all {@link Rule}s about the protected method.
*
* @param method the protected method
* @param trafficType the traffic type (inbound, outbound or internal). This is used
* to mark whether it can be blocked when the system is unstable,
* only inbound traffic could be blocked by {@link SystemRule}
* @param batchCount the amount of calls within the invocation (e.g. batchCount=2 means request for 2 tokens)
* @throws BlockException if the block criteria is met (e.g. metric exceeded the threshold of any rules)
*/
public static Entry entry(Method method, EntryType trafficType, int batchCount) throws BlockException {
return Env.sph.entry(method, trafficType, batchCount, OBJECTS0);
}
/**
* Record statistics and perform rule checking for the given resource.
*
* @param name the unique name for the protected resource
* @param trafficType the traffic type (inbound, outbound or internal). This is used
* to mark whether it can be blocked when the system is unstable,
* only inbound traffic could be blocked by {@link SystemRule}
* @param batchCount the amount of calls within the invocation (e.g. batchCount=2 means request for 2 tokens)
* @return the {@link Entry} of this invocation (used for mark the invocation complete and get context data)
* @throws BlockException if the block criteria is met (e.g. metric exceeded the threshold of any rules)
*/
public static Entry entry(String name, EntryType trafficType, int batchCount) throws BlockException {
return Env.sph.entry(name, trafficType, batchCount, OBJECTS0);
}
/**
* Checking all {@link Rule}s about the protected method.
*
* @param method the protected method
* @param trafficType the traffic type (inbound, outbound or internal). This is used
* to mark whether it can be blocked when the system is unstable,
* only inbound traffic could be blocked by {@link SystemRule}
* @param batchCount the amount of calls within the invocation (e.g. batchCount=2 means request for 2 tokens)
* @param args args for parameter flow control or customized slots
* @return the {@link Entry} of this invocation (used for mark the invocation complete and get context data)
* @throws BlockException if the block criteria is met (e.g. metric exceeded the threshold of any rules)
*/
public static Entry entry(Method method, EntryType trafficType, int batchCount, Object... args)
throws BlockException {
return Env.sph.entry(method, trafficType, batchCount, args);
}
/**
* Record statistics and perform rule checking for the given resource.
*
* @param name the unique name for the protected resource
* @param trafficType the traffic type (inbound, outbound or internal). This is used
* to mark whether it can be blocked when the system is unstable,
* only inbound traffic could be blocked by {@link SystemRule}
* @param batchCount the amount of calls within the invocation (e.g. batchCount=2 means request for 2 tokens)
* @param args args for parameter flow control
* @throws BlockException if the block criteria is met (e.g. metric exceeded the threshold of any rules)
*/
public static Entry entry(String name, EntryType trafficType, int batchCount, Object... args)
throws BlockException {
return Env.sph.entry(name, trafficType, batchCount, args);
}
/**
* Record statistics and check all rules of the resource that indicates an async invocation.
*
* @param name the unique name of the protected resource
* @throws BlockException if the block criteria is met (e.g. metric exceeded the threshold of any rules)
* @since 0.2.0
*/
public static AsyncEntry asyncEntry(String name) throws BlockException {
return Env.sph.asyncEntry(name, EntryType.OUT, 1, OBJECTS0);
}
/**
* Record statistics and check all rules of the resource that indicates an async invocation.
*
* @param name the unique name for the protected resource
* @param trafficType the traffic type (inbound, outbound or internal). This is used
* to mark whether it can be blocked when the system is unstable,
* only inbound traffic could be blocked by {@link SystemRule}
* @return the {@link Entry} of this invocation (used for mark the invocation complete and get context data)
* @throws BlockException if the block criteria is met (e.g. metric exceeded the threshold of any rules)
* @since 0.2.0
*/
public static AsyncEntry asyncEntry(String name, EntryType trafficType) throws BlockException {
return Env.sph.asyncEntry(name, trafficType, 1, OBJECTS0);
}
/**
* Record statistics and check all rules of the resource that indicates an async invocation.
*
* @param name the unique name for the protected resource
* @param trafficType the traffic type (inbound, outbound or internal). This is used
* to mark whether it can be blocked when the system is unstable,
* only inbound traffic could be blocked by {@link SystemRule}
* @param batchCount the amount of calls within the invocation (e.g. batchCount=2 means request for 2 tokens)
* @param args args for parameter flow control
* @return the {@link Entry} of this invocation (used for mark the invocation complete and get context data)
* @throws BlockException if the block criteria is met (e.g. metric exceeded the threshold of any rules)
* @since 0.2.0
*/
public static AsyncEntry asyncEntry(String name, EntryType trafficType, int batchCount, Object... args)
throws BlockException {
return Env.sph.asyncEntry(name, trafficType, batchCount, args);
}
/**
* Record statistics and perform rule checking for the given resource. The entry is prioritized.
*
* @param name the unique name for the protected resource
* @throws BlockException if the block criteria is met (e.g. metric exceeded the threshold of any rules)
* @since 1.4.0
*/
public static Entry entryWithPriority(String name) throws BlockException {
return Env.sph.entryWithPriority(name, EntryType.OUT, 1, true);
}
/**
* Record statistics and perform rule checking for the given resource. The entry is prioritized.
*
* @param name the unique name for the protected resource
* @param trafficType the traffic type (inbound, outbound or internal). This is used
* to mark whether it can be blocked when the system is unstable,
* only inbound traffic could be blocked by {@link SystemRule}
* @return the {@link Entry} of this invocation (used for mark the invocation complete and get context data)
* @throws BlockException if the block criteria is met (e.g. metric exceeded the threshold of any rules)
* @since 1.4.0
*/
public static Entry entryWithPriority(String name, EntryType trafficType) throws BlockException {
return Env.sph.entryWithPriority(name, trafficType, 1, true);
}
/**
* Record statistics and perform rule checking for the given resource.
*
* @param name the unique name for the protected resource
* @param resourceType classification of the resource (e.g. Web or RPC)
* @param trafficType the traffic type (inbound, outbound or internal). This is used
* to mark whether it can be blocked when the system is unstable,
* only inbound traffic could be blocked by {@link SystemRule}
* @return the {@link Entry} of this invocation (used for mark the invocation complete and get context data)
* @throws BlockException if the block criteria is met (e.g. metric exceeded the threshold of any rules)
* @since 1.7.0
*/
public static Entry entry(String name, int resourceType, EntryType trafficType) throws BlockException {
return Env.sph.entryWithType(name, resourceType, trafficType, 1, OBJECTS0);
}
/**
* Record statistics and perform rule checking for the given resource.
*
* @param name the unique name for the protected resource
* @param trafficType the traffic type (inbound, outbound or internal). This is used
* to mark whether it can be blocked when the system is unstable,
* only inbound traffic could be blocked by {@link SystemRule}
* @param resourceType classification of the resource (e.g. Web or RPC)
* @param args args for parameter flow control or customized slots
* @return the {@link Entry} of this invocation (used for mark the invocation complete and get context data)
* @throws BlockException if the block criteria is met (e.g. metric exceeded the threshold of any rules)
* @since 1.7.0
*/
public static Entry entry(String name, int resourceType, EntryType trafficType, Object[] args)
throws BlockException {
return Env.sph.entryWithType(name, resourceType, trafficType, 1, args);
}
/**
* Record statistics and perform rule checking for the given resource that indicates an async invocation.
*
* @param name the unique name for the protected resource
* @param trafficType the traffic type (inbound, outbound or internal). This is used
* to mark whether it can be blocked when the system is unstable,
* only inbound traffic could be blocked by {@link SystemRule}
* @param resourceType classification of the resource (e.g. Web or RPC)
* @return the {@link Entry} of this invocation (used for mark the invocation complete and get context data)
* @throws BlockException if the block criteria is met (e.g. metric exceeded the threshold of any rules)
* @since 1.7.0
*/
public static AsyncEntry asyncEntry(String name, int resourceType, EntryType trafficType)
throws BlockException {
return Env.sph.asyncEntryWithType(name, resourceType, trafficType, 1, false, OBJECTS0);
}
/**
* Record statistics and perform rule checking for the given resource that indicates an async invocation.
*
* @param name the unique name for the protected resource
* @param trafficType the traffic type (inbound, outbound or internal). This is used
* to mark whether it can be blocked when the system is unstable,
* only inbound traffic could be blocked by {@link SystemRule}
* @param resourceType classification of the resource (e.g. Web or RPC)
* @param args args for parameter flow control or customized slots
* @return the {@link Entry} of this invocation (used for mark the invocation complete and get context data)
* @throws BlockException if the block criteria is met (e.g. metric exceeded the threshold of any rules)
* @since 1.7.0
*/
public static AsyncEntry asyncEntry(String name, int resourceType, EntryType trafficType, Object[] args)
throws BlockException {
return Env.sph.asyncEntryWithType(name, resourceType, trafficType, 1, false, args);
}
/**
* Record statistics and perform rule checking for the given resource that indicates an async invocation.
*
* @param name the unique name for the protected resource
* @param trafficType the traffic type (inbound, outbound or internal). This is used
* to mark whether it can be blocked when the system is unstable,
* only inbound traffic could be blocked by {@link SystemRule}
* @param resourceType classification of the resource (e.g. Web or RPC)
* @param batchCount the amount of calls within the invocation (e.g. batchCount=2 means request for 2 tokens)
* @param args args for parameter flow control or customized slots
* @return the {@link Entry} of this invocation (used for mark the invocation complete and get context data)
* @throws BlockException if the block criteria is met (e.g. metric exceeded the threshold of any rules)
* @since 1.7.0
*/
public static AsyncEntry asyncEntry(String name, int resourceType, EntryType trafficType, int batchCount,
Object[] args) throws BlockException {
return Env.sph.asyncEntryWithType(name, resourceType, trafficType, batchCount, false, args);
}
}