Java Code Examples for com.alibaba.csp.sentinel.context.Context#setCurEntry()

The following examples show how to use com.alibaba.csp.sentinel.context.Context#setCurEntry() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AsyncEntry.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
/**
 * Remove current entry from local context, but does not exit.
 */
void cleanCurrentEntryInLocal() {
    if (context instanceof NullContext) {
        return;
    }
    Context originalContext = context;
    if (originalContext != null) {
        Entry curEntry = originalContext.getCurEntry();
        if (curEntry == this) {
            Entry parent = this.parent;
            originalContext.setCurEntry(parent);
            if (parent != null) {
                ((CtEntry)parent).child = null;
            }
        } else {
            throw new IllegalStateException("Bad async context state");
        }
    }
}
 
Example 2
Source File: ClientFilterTest.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@AfterClass
public static void shutdown() {
    ctx.close();

    Context context = ContextUtil.getContext();
    if (context != null) {
        context.setCurEntry(null);
        ContextUtil.exit();
    }

    Constants.ROOT.removeChildList();

    ClusterBuilderSlot.getClusterNodeMap().clear();

    // Clear chainMap in CtSph
    try {
        Method resetChainMapMethod = CtSph.class.getDeclaredMethod("resetChainMap");
        resetChainMapMethod.setAccessible(true);
        resetChainMapMethod.invoke(null);
    } catch (Exception e) {
        // Empty
    }
}
 
Example 3
Source File: BaseTest.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
/**
 * Clean up resources.
 */
protected static void cleanUpAll() {
    Context context = ContextUtil.getContext();
    if (context != null) {
        context.setCurEntry(null);
        ContextUtil.exit();
    }

    Constants.ROOT.removeChildList();

    ClusterBuilderSlot.getClusterNodeMap().clear();

    // Clear chainMap in CtSph
    try {
        Method resetChainMapMethod = CtSph.class.getDeclaredMethod("resetChainMap");
        resetChainMapMethod.setAccessible(true);
        resetChainMapMethod.invoke(null);
    } catch (Exception e) {
        // Empty
    }
}
 
Example 4
Source File: AsyncEntry.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
/**
 * Remove current entry from local context, but does not exit.
 */
void cleanCurrentEntryInLocal() {
    if (context instanceof NullContext) {
        return;
    }
    Context originalContext = context;
    if (originalContext != null) {
        Entry curEntry = originalContext.getCurEntry();
        if (curEntry == this) {
            Entry parent = this.parent;
            originalContext.setCurEntry(parent);
            if (parent != null) {
                ((CtEntry)parent).child = null;
            }
        } else {
            String curEntryName = curEntry == null ? "none"
                : curEntry.resourceWrapper.getName() + "@" + curEntry.hashCode();
            String msg = String.format("Bad async context state, expected entry: %s, but actual: %s",
                getResourceWrapper().getName() + "@" + hashCode(), curEntryName);
            throw new IllegalStateException(msg);
        }
    }
}
 
Example 5
Source File: CtEntry.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
private void setUpEntryFor(Context context) {
    // The entry should not be associated to NullContext.
    if (context instanceof NullContext) {
        return;
    }
    this.parent = context.getCurEntry();
    if (parent != null) {
        ((CtEntry)parent).child = this;
    }
    context.setCurEntry(this);
}
 
Example 6
Source File: CtEntry.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
protected void exitForContext(Context context, int count, Object... args) throws ErrorEntryFreeException {
    if (context != null) {
        // Null context should exit without clean-up.
        if (context instanceof NullContext) {
            return;
        }
        if (context.getCurEntry() != this) {
            String curEntryNameInContext = context.getCurEntry() == null ? null : context.getCurEntry().getResourceWrapper().getName();
            // Clean previous call stack.
            CtEntry e = (CtEntry)context.getCurEntry();
            while (e != null) {
                e.exit(count, args);
                e = (CtEntry)e.parent;
            }
            String errorMessage = String.format("The order of entry exit can't be paired with the order of entry"
                + ", current entry in context: <%s>, but expected: <%s>", curEntryNameInContext, resourceWrapper.getName());
            throw new ErrorEntryFreeException(errorMessage);
        } else {
            if (chain != null) {
                chain.exit(context, resourceWrapper, count, args);
            }
            // Restore the call stack.
            context.setCurEntry(parent);
            if (parent != null) {
                ((CtEntry)parent).child = null;
            }
            if (parent == null) {
                // Default context (auto entered) will be exited automatically.
                if (ContextUtil.isDefaultContext(context)) {
                    ContextUtil.exit();
                }
            }
            // Clean the reference of context in current entry to avoid duplicate exit.
            clearEntryContext();
        }
    }
}
 
Example 7
Source File: CtEntry.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
private void setUpEntryFor(Context context) {
    // The entry should not be associated to NullContext.
    if (context instanceof NullContext) {
        return;
    }
    this.parent = context.getCurEntry();
    if (parent != null) {
        ((CtEntry)parent).child = this;
    }
    context.setCurEntry(this);
}
 
Example 8
Source File: CtEntry.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
protected void exitForContext(Context context, int count, Object... args) throws ErrorEntryFreeException {
    if (context != null) {
        // Null context should exit without clean-up.
        if (context instanceof NullContext) {
            return;
        }
        if (context.getCurEntry() != this) {
            String curEntryNameInContext = context.getCurEntry() == null ? null : context.getCurEntry().getResourceWrapper().getName();
            // Clean previous call stack.
            CtEntry e = (CtEntry)context.getCurEntry();
            while (e != null) {
                e.exit(count, args);
                e = (CtEntry)e.parent;
            }
            String errorMessage = String.format("The order of entry exit can't be paired with the order of entry"
                + ", current entry in context: <%s>, but expected: <%s>", curEntryNameInContext, resourceWrapper.getName());
            throw new ErrorEntryFreeException(errorMessage);
        } else {
            if (chain != null) {
                chain.exit(context, resourceWrapper, count, args);
            }
            // Restore the call stack.
            context.setCurEntry(parent);
            if (parent != null) {
                ((CtEntry)parent).child = null;
            }
            if (parent == null) {
                // Default context (auto entered) will be exited automatically.
                if (ContextUtil.isDefaultContext(context)) {
                    ContextUtil.exit();
                }
            }
            // Clean the reference of context in current entry to avoid duplicate exit.
            clearEntryContext();
        }
    }
}