Browse Source

Code and javadoc refinement

Signed-off-by: Eric Zhao <sczyh16@gmail.com>
master
Eric Zhao 6 years ago
parent
commit
f2a401cdb3
2 changed files with 33 additions and 20 deletions
  1. +15
    -15
      sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/StatisticNode.java
  2. +18
    -5
      sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/StatisticSlot.java

+ 15
- 15
sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/StatisticNode.java View File

@@ -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. * incoming request(QPS), block request(bq), etc. And the time-span is defined by sample count.
* </p> * </p>
* <pre> * <pre>
* 0 100ms
* 0 100ms
* +-------+--→ Sliding Windows * +-------+--→ Sliding Windows
* ^
* |
* ^
* |
* request * request
* </pre> * </pre>
* <p> * <p>
@@ -58,29 +58,29 @@ import com.alibaba.csp.sentinel.slots.statistic.metric.Metric;
* *
* <p>case 2: continuous requests</p> * <p>case 2: continuous requests</p>
* <pre> * <pre>
* 0 100ms 200ms 300ms
* 0 100ms 200ms 300ms
* +-------+-------+-------+-----→ Sliding Windows * +-------+-------+-------+-----→ Sliding Windows
* ^
* |
* request
* ^
* |
* request
* </pre> * </pre>
* *
* <p>case 3: requests keeps coming, and previous buckets become invalid</p> * <p>case 3: requests keeps coming, and previous buckets become invalid</p>
* <pre> * <pre>
* 0 100ms 200ms 800ms 900ms 1000ms 1300ms
* 0 100ms 200ms 800ms 900ms 1000ms 1300ms
* +-------+-------+ ...... +-------+-------+ ...... +-------+-----→ Sliding Windows * +-------+-------+ ...... +-------+-------+ ...... +-------+-----→ Sliding Windows
* ^
* |
* request
* ^
* |
* request
* </pre> * </pre>
* *
* <p>The sliding window should become:</p> * <p>The sliding window should become:</p>
* <pre> * <pre>
* 300ms 800ms 900ms 1000ms 1300ms
* 300ms 800ms 900ms 1000ms 1300ms
* + ...... +-------+ ...... +-------+-----→ Sliding Windows * + ...... +-------+ ...... +-------+-----→ Sliding Windows
* ^
* |
* request
* ^
* |
* request
* </pre> * </pre>
* *
* @author qinan.qn * @author qinan.qn


+ 18
- 5
sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/statistic/StatisticSlot.java View File

@@ -35,10 +35,10 @@ import com.alibaba.csp.sentinel.slots.block.BlockException;
* When entering this slot, we need to separately count the following * When entering this slot, we need to separately count the following
* information: * information:
* <ul> * <ul>
* <li>{@link ClusterNode}: total statistics of a cluster node of the resource id  </li>
* <li> origin node: statistics of a cluster node from different callers/origins.</li>
* <li> {@link DefaultNode}: statistics for specific resource name in the specific context.
* <li> Finally, the sum statistics of all entrances.</li>
* <li>{@link ClusterNode}: total statistics of a cluster node of the resource ID.</li>
* <li>Origin node: statistics of a cluster node from different callers/origins.</li>
* <li>{@link DefaultNode}: statistics for specific resource name in the specific context.</li>
* <li>Finally, the sum statistics of all entrances.</li>
* </ul> * </ul>
* </p> * </p>
* *
@@ -51,24 +51,31 @@ public class StatisticSlot extends AbstractLinkedProcessorSlot<DefaultNode> {
public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count, Object... args) public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count, Object... args)
throws Throwable { throws Throwable {
try { try {
// Do some checking.
fireEntry(context, resourceWrapper, node, count, args); fireEntry(context, resourceWrapper, node, count, args);

// Request passed, add thread count and pass count.
node.increaseThreadNum(); node.increaseThreadNum();
node.addPassRequest(); node.addPassRequest();


if (context.getCurEntry().getOriginNode() != null) { if (context.getCurEntry().getOriginNode() != null) {
// Add count for origin node.
context.getCurEntry().getOriginNode().increaseThreadNum(); context.getCurEntry().getOriginNode().increaseThreadNum();
context.getCurEntry().getOriginNode().addPassRequest(); context.getCurEntry().getOriginNode().addPassRequest();
} }


if (resourceWrapper.getType() == EntryType.IN) { if (resourceWrapper.getType() == EntryType.IN) {
// Add count for global inbound entry node for global statistics.
Constants.ENTRY_NODE.increaseThreadNum(); Constants.ENTRY_NODE.increaseThreadNum();
Constants.ENTRY_NODE.addPassRequest(); Constants.ENTRY_NODE.addPassRequest();
} }


// Handle pass event with registered entry callback handlers.
for (ProcessorSlotEntryCallback<DefaultNode> handler : StatisticSlotCallbackRegistry.getEntryCallbacks()) { for (ProcessorSlotEntryCallback<DefaultNode> handler : StatisticSlotCallbackRegistry.getEntryCallbacks()) {
handler.onPass(context, resourceWrapper, node, count, args); handler.onPass(context, resourceWrapper, node, count, args);
} }
} catch (BlockException e) { } catch (BlockException e) {
// Blocked, set block exception to current entry.
context.getCurEntry().setError(e); context.getCurEntry().setError(e);


// Add block count. // Add block count.
@@ -78,18 +85,21 @@ public class StatisticSlot extends AbstractLinkedProcessorSlot<DefaultNode> {
} }


if (resourceWrapper.getType() == EntryType.IN) { if (resourceWrapper.getType() == EntryType.IN) {
// Add count for global inbound entry node for global statistics.
Constants.ENTRY_NODE.increaseBlockQps(); Constants.ENTRY_NODE.increaseBlockQps();
} }


// Handle block event with registered entry callback handlers.
for (ProcessorSlotEntryCallback<DefaultNode> handler : StatisticSlotCallbackRegistry.getEntryCallbacks()) { for (ProcessorSlotEntryCallback<DefaultNode> handler : StatisticSlotCallbackRegistry.getEntryCallbacks()) {
handler.onBlocked(e, context, resourceWrapper, node, count, args); handler.onBlocked(e, context, resourceWrapper, node, count, args);
} }


throw e; throw e;
} catch (Throwable e) { } catch (Throwable e) {
// Unexpected error, set error to current entry.
context.getCurEntry().setError(e); context.getCurEntry().setError(e);


// Should not happen
// This should not happen.
node.increaseExceptionQps(); node.increaseExceptionQps();
if (context.getCurEntry().getOriginNode() != null) { if (context.getCurEntry().getOriginNode() != null) {
context.getCurEntry().getOriginNode().increaseExceptionQps(); context.getCurEntry().getOriginNode().increaseExceptionQps();
@@ -107,11 +117,13 @@ public class StatisticSlot extends AbstractLinkedProcessorSlot<DefaultNode> {
DefaultNode node = (DefaultNode)context.getCurNode(); DefaultNode node = (DefaultNode)context.getCurNode();


if (context.getCurEntry().getError() == null) { if (context.getCurEntry().getError() == null) {
// Calculate response time (max RT is TIME_DROP_VALVE).
long rt = TimeUtil.currentTimeMillis() - context.getCurEntry().getCreateTime(); long rt = TimeUtil.currentTimeMillis() - context.getCurEntry().getCreateTime();
if (rt > Constants.TIME_DROP_VALVE) { if (rt > Constants.TIME_DROP_VALVE) {
rt = Constants.TIME_DROP_VALVE; rt = Constants.TIME_DROP_VALVE;
} }


// Record response time and success count.
node.rt(rt); node.rt(rt);
if (context.getCurEntry().getOriginNode() != null) { if (context.getCurEntry().getOriginNode() != null) {
context.getCurEntry().getOriginNode().rt(rt); context.getCurEntry().getOriginNode().rt(rt);
@@ -131,6 +143,7 @@ public class StatisticSlot extends AbstractLinkedProcessorSlot<DefaultNode> {
// Error may happen. // Error may happen.
} }


// Handle exit event with registered exit callback handlers.
Collection<ProcessorSlotExitCallback> exitCallbacks = StatisticSlotCallbackRegistry.getExitCallbacks(); Collection<ProcessorSlotExitCallback> exitCallbacks = StatisticSlotCallbackRegistry.getExitCallbacks();
for (ProcessorSlotExitCallback handler : exitCallbacks) { for (ProcessorSlotExitCallback handler : exitCallbacks) {
handler.onExit(context, resourceWrapper, count, args); handler.onExit(context, resourceWrapper, count, args);


Loading…
Cancel
Save