From 95f50e3ed21220ec5d74e12bc44066cbd6ecb3e9 Mon Sep 17 00:00:00 2001 From: xiejihan Date: Mon, 8 Jun 2020 23:29:43 +0800 Subject: [PATCH 1/2] If attribute key already exists in request, don't create entry to avoid ErrorEntryFreeException --- .../adapter/spring/webmvc/AbstractSentinelInterceptor.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/sentinel-adapter/sentinel-spring-webmvc-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/AbstractSentinelInterceptor.java b/sentinel-adapter/sentinel-spring-webmvc-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/AbstractSentinelInterceptor.java index b065c54cb1..0be74cbae3 100644 --- a/sentinel-adapter/sentinel-spring-webmvc-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/AbstractSentinelInterceptor.java +++ b/sentinel-adapter/sentinel-spring-webmvc-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/AbstractSentinelInterceptor.java @@ -61,9 +61,7 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons String origin = parseOrigin(request); String contextName = getContextName(request); ContextUtil.enter(contextName, origin); - Entry entry = SphU.entry(resourceName, ResourceTypeConstants.COMMON_WEB, EntryType.IN); - - setEntryInRequest(request, baseWebMvcConfig.getRequestAttributeName(), entry); + setEntryInRequest(request, baseWebMvcConfig.getRequestAttributeName(), resourceName); } return true; } catch (BlockException e) { @@ -110,12 +108,13 @@ public void postHandle(HttpServletRequest request, HttpServletResponse response, ModelAndView modelAndView) throws Exception { } - protected void setEntryInRequest(HttpServletRequest request, String name, Entry entry) { + protected void setEntryInRequest(HttpServletRequest request, String name, String resourceName) throws BlockException { Object attrVal = request.getAttribute(name); if (attrVal != null) { RecordLog.warn("[{}] The attribute key '{}' already exists in request, please set `requestAttributeName`", getClass().getSimpleName(), name); } else { + Entry entry = SphU.entry(resourceName, ResourceTypeConstants.COMMON_WEB, EntryType.IN); request.setAttribute(name, entry); } } From 9bd0ccf49041950919df663254cb107400c39069 Mon Sep 17 00:00:00 2001 From: xiejihan Date: Mon, 3 Aug 2020 23:28:58 +0800 Subject: [PATCH 2/2] Add comments and related issues --- .../spring/webmvc/AbstractSentinelInterceptor.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/sentinel-adapter/sentinel-spring-webmvc-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/AbstractSentinelInterceptor.java b/sentinel-adapter/sentinel-spring-webmvc-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/AbstractSentinelInterceptor.java index 0be74cbae3..c4de7847af 100644 --- a/sentinel-adapter/sentinel-spring-webmvc-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/AbstractSentinelInterceptor.java +++ b/sentinel-adapter/sentinel-spring-webmvc-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/spring/webmvc/AbstractSentinelInterceptor.java @@ -108,6 +108,15 @@ public void postHandle(HttpServletRequest request, HttpServletResponse response, ModelAndView modelAndView) throws Exception { } + /** + * Note: + * If the attribute key already exists in request, don't create new {@link Entry}, + * to guarantee the order of {@link Entry} in pair and avoid {@link com.alibaba.csp.sentinel.ErrorEntryFreeException}. + * + * Refer to: + * https://github.com/alibaba/Sentinel/issues/1531 + * https://github.com/alibaba/Sentinel/issues/1482 + */ protected void setEntryInRequest(HttpServletRequest request, String name, String resourceName) throws BlockException { Object attrVal = request.getAttribute(name); if (attrVal != null) {