Browse Source

Fix NPE in Tracer when context size exceeds the limit (#1293)

master
cdfive GitHub 5 years ago
parent
commit
c1ff9135ee
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 13 deletions
  1. +4
    -12
      sentinel-core/src/main/java/com/alibaba/csp/sentinel/Tracer.java
  2. +1
    -1
      sentinel-core/src/main/java/com/alibaba/csp/sentinel/context/Context.java
  3. +34
    -0
      sentinel-core/src/test/java/com/alibaba/csp/sentinel/TracerTest.java

+ 4
- 12
sentinel-core/src/main/java/com/alibaba/csp/sentinel/Tracer.java View File

@@ -17,6 +17,7 @@ package com.alibaba.csp.sentinel;

import com.alibaba.csp.sentinel.context.Context;
import com.alibaba.csp.sentinel.context.ContextUtil;
import com.alibaba.csp.sentinel.context.NullContext;
import com.alibaba.csp.sentinel.metric.extension.MetricExtensionProvider;
import com.alibaba.csp.sentinel.node.ClusterNode;
import com.alibaba.csp.sentinel.node.DefaultNode;
@@ -53,17 +54,7 @@ public class Tracer {
* @param count exception count to add
*/
public static void trace(Throwable e, int count) {
if (!shouldTrace(e)) {
return;
}

Context context = ContextUtil.getContext();
if (context == null) {
return;
}

DefaultNode curNode = (DefaultNode)context.getCurNode();
traceExceptionToNode(e, count, context.getCurEntry(), curNode);
traceContext(e, count, ContextUtil.getContext());
}

/**
@@ -77,7 +68,8 @@ public class Tracer {
if (!shouldTrace(e)) {
return;
}
if (context == null) {

if (context == null || context instanceof NullContext) {
return;
}



+ 1
- 1
sentinel-core/src/main/java/com/alibaba/csp/sentinel/context/Context.java View File

@@ -109,7 +109,7 @@ public class Context {
}

public Node getCurNode() {
return curEntry.getCurNode();
return curEntry == null ? null : curEntry.getCurNode();
}

public Context setCurNode(Node node) {


+ 34
- 0
sentinel-core/src/test/java/com/alibaba/csp/sentinel/TracerTest.java View File

@@ -1,6 +1,10 @@
package com.alibaba.csp.sentinel;

import com.alibaba.csp.sentinel.context.ContextTestUtil;
import com.alibaba.csp.sentinel.context.ContextUtil;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
@@ -8,6 +12,36 @@ import org.junit.Test;
*/
public class TracerTest extends Tracer {

@Before
public void setUp() {
ContextTestUtil.cleanUpContext();
ContextTestUtil.resetContextMap();
}

@After
public void tearDown() {
ContextTestUtil.cleanUpContext();
ContextTestUtil.resetContextMap();
}

@Test
public void testTraceWhenContextSizeExceedsThreshold() {
int i = 0;
for (; i < Constants.MAX_CONTEXT_NAME_SIZE; i++) {
ContextUtil.enter("test-context-" + i);
ContextUtil.exit();
}

try {
ContextUtil.enter("test-context-" + i);
throw new RuntimeException("test");
} catch (Exception e) {
Tracer.trace(e);
} finally {
ContextUtil.exit();
}
}

@Test
public void setExceptionsToTrace() {
Tracer.ignoreClasses = null;


Loading…
Cancel
Save