diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/StatisticNode.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/StatisticNode.java index fa559868..43cc0039 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/StatisticNode.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/StatisticNode.java @@ -44,10 +44,10 @@ import com.alibaba.csp.sentinel.slots.statistic.metric.Metric; * incoming request(QPS), block request(bq), etc. And the time-span is defined by sample count. *

*
- * 	0 	   100ms
+ * 	0      100ms
  *  +-------+--→ Sliding Windows
- * 		^
- * 	   	|
+ * 	    ^
+ * 	    |
  * 	  request
  * 
*

@@ -58,29 +58,29 @@ import com.alibaba.csp.sentinel.slots.statistic.metric.Metric; * *

case 2: continuous requests

*
- *  0 	 100ms 	  200ms	  300ms
+ *  0    100ms    200ms    300ms
  *  +-------+-------+-------+-----→ Sliding Windows
- * 						^
- * 						|
- * 					  request
+ *                      ^
+ *                      |
+ *                   request
  * 
* *

case 3: requests keeps coming, and previous buckets become invalid

*
- *  0 	 100ms 	  200ms	  800ms	   900ms  1000ms	1300ms
+ *  0    100ms    200ms	  800ms	   900ms  1000ms    1300ms
  *  +-------+-------+ ...... +-------+-------+ ...... +-------+-----→ Sliding Windows
- *  													^
- *  													|
- * 													  request
+ *                                                      ^
+ *                                                      |
+ *                                                    request
  * 
* *

The sliding window should become:

*
- * 300ms	  	800ms  900ms  1000ms	1300ms
+ * 300ms     800ms  900ms  1000ms  1300ms
  *  + ...... +-------+ ...... +-------+-----→ Sliding Windows
- *  													^
- *  													|
- * 													  request
+ *                                                      ^
+ *                                                      |
+ *                                                    request
  * 
* * @author qinan.qn diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/StatisticSlot.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/StatisticSlot.java index 67ddc4a9..8a6c3779 100755 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/StatisticSlot.java +++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/StatisticSlot.java @@ -35,10 +35,10 @@ import com.alibaba.csp.sentinel.slots.block.BlockException; * When entering this slot, we need to separately count the following * information: * *

* @@ -51,24 +51,31 @@ public class StatisticSlot extends AbstractLinkedProcessorSlot { public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count, Object... args) throws Throwable { try { + // Do some checking. fireEntry(context, resourceWrapper, node, count, args); + + // Request passed, add thread count and pass count. node.increaseThreadNum(); node.addPassRequest(); if (context.getCurEntry().getOriginNode() != null) { + // Add count for origin node. context.getCurEntry().getOriginNode().increaseThreadNum(); context.getCurEntry().getOriginNode().addPassRequest(); } if (resourceWrapper.getType() == EntryType.IN) { + // Add count for global inbound entry node for global statistics. Constants.ENTRY_NODE.increaseThreadNum(); Constants.ENTRY_NODE.addPassRequest(); } + // Handle pass event with registered entry callback handlers. for (ProcessorSlotEntryCallback handler : StatisticSlotCallbackRegistry.getEntryCallbacks()) { handler.onPass(context, resourceWrapper, node, count, args); } } catch (BlockException e) { + // Blocked, set block exception to current entry. context.getCurEntry().setError(e); // Add block count. @@ -78,18 +85,21 @@ public class StatisticSlot extends AbstractLinkedProcessorSlot { } if (resourceWrapper.getType() == EntryType.IN) { + // Add count for global inbound entry node for global statistics. Constants.ENTRY_NODE.increaseBlockQps(); } + // Handle block event with registered entry callback handlers. for (ProcessorSlotEntryCallback handler : StatisticSlotCallbackRegistry.getEntryCallbacks()) { handler.onBlocked(e, context, resourceWrapper, node, count, args); } throw e; } catch (Throwable e) { + // Unexpected error, set error to current entry. context.getCurEntry().setError(e); - // Should not happen + // This should not happen. node.increaseExceptionQps(); if (context.getCurEntry().getOriginNode() != null) { context.getCurEntry().getOriginNode().increaseExceptionQps(); @@ -107,11 +117,13 @@ public class StatisticSlot extends AbstractLinkedProcessorSlot { DefaultNode node = (DefaultNode)context.getCurNode(); if (context.getCurEntry().getError() == null) { + // Calculate response time (max RT is TIME_DROP_VALVE). long rt = TimeUtil.currentTimeMillis() - context.getCurEntry().getCreateTime(); if (rt > Constants.TIME_DROP_VALVE) { rt = Constants.TIME_DROP_VALVE; } + // Record response time and success count. node.rt(rt); if (context.getCurEntry().getOriginNode() != null) { context.getCurEntry().getOriginNode().rt(rt); @@ -131,6 +143,7 @@ public class StatisticSlot extends AbstractLinkedProcessorSlot { // Error may happen. } + // Handle exit event with registered exit callback handlers. Collection exitCallbacks = StatisticSlotCallbackRegistry.getExitCallbacks(); for (ProcessorSlotExitCallback handler : exitCallbacks) { handler.onExit(context, resourceWrapper, count, args);