From f8da6aa2dc25057555aa5251283c1bc152a639c1 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 6 Feb 2016 16:53:18 -0200 Subject: [PATCH 1/3] Fixed NPE on request cache when HystrixRequestContext is not initialized --- .../concurrency/HystrixRequestVariableHolder.java | 6 +++++- .../java/com/netflix/hystrix/HystrixRequestCacheTest.java | 8 ++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/hystrix-core/src/main/java/com/netflix/hystrix/strategy/concurrency/HystrixRequestVariableHolder.java b/hystrix-core/src/main/java/com/netflix/hystrix/strategy/concurrency/HystrixRequestVariableHolder.java index 84bd70d76..54a6dd56d 100644 --- a/hystrix-core/src/main/java/com/netflix/hystrix/strategy/concurrency/HystrixRequestVariableHolder.java +++ b/hystrix-core/src/main/java/com/netflix/hystrix/strategy/concurrency/HystrixRequestVariableHolder.java @@ -65,7 +65,11 @@ public T get(HystrixConcurrencyStrategy concurrencyStrategy) { } } - return (T) requestVariableInstance.get(key).get(); + T result = (T) requestVariableInstance.get(key).get(); + if (result == null) { + throw new IllegalStateException("Failed to get HystrixRequestVariable. Maybe you need to initialize the HystrixRequestContext?"); + } + return result; } private static class RVCacheKey { diff --git a/hystrix-core/src/test/java/com/netflix/hystrix/HystrixRequestCacheTest.java b/hystrix-core/src/test/java/com/netflix/hystrix/HystrixRequestCacheTest.java index dbc66c172..353ebc2cd 100644 --- a/hystrix-core/src/test/java/com/netflix/hystrix/HystrixRequestCacheTest.java +++ b/hystrix-core/src/test/java/com/netflix/hystrix/HystrixRequestCacheTest.java @@ -66,6 +66,14 @@ public void testCache() { } } + @Test(expected = IllegalStateException.class) + public void testCacheWithoutContext() { + HystrixRequestCache.getInstance( + HystrixCommandKey.Factory.asKey("command1"), + HystrixConcurrencyStrategyDefault.getInstance() + ).get("any"); + } + @Test public void testClearCache() { HystrixConcurrencyStrategy strategy = HystrixConcurrencyStrategyDefault.getInstance(); From 58460925e2f438007ed8a5259db920df63c233ac Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 6 Feb 2016 19:26:28 -0200 Subject: [PATCH 2/3] Improved implementation, fixed build --- .../main/java/com/netflix/hystrix/HystrixRequestCache.java | 4 ++++ .../strategy/concurrency/HystrixRequestVariableHolder.java | 6 +----- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/hystrix-core/src/main/java/com/netflix/hystrix/HystrixRequestCache.java b/hystrix-core/src/main/java/com/netflix/hystrix/HystrixRequestCache.java index 77815c148..97e366d08 100644 --- a/hystrix-core/src/main/java/com/netflix/hystrix/HystrixRequestCache.java +++ b/hystrix-core/src/main/java/com/netflix/hystrix/HystrixRequestCache.java @@ -23,6 +23,7 @@ import rx.Observable; import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy; +import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext; import com.netflix.hystrix.strategy.concurrency.HystrixRequestVariableDefault; import com.netflix.hystrix.strategy.concurrency.HystrixRequestVariableHolder; import com.netflix.hystrix.strategy.concurrency.HystrixRequestVariableLifecycle; @@ -100,6 +101,9 @@ private static HystrixRequestCache getInstance(RequestCacheKey rcKey, HystrixCon /* package */ Observable get(String cacheKey) { ValueCacheKey key = getRequestCacheKey(cacheKey); if (key != null) { + if (!HystrixRequestContext.isCurrentThreadInitialized()) { + throw new IllegalStateException("Failed to get HystrixRequestVariable. Maybe you need to initialize the HystrixRequestContext?"); + } /* look for the stored value */ return (Observable) requestVariableForCache.get(concurrencyStrategy).get(key); } diff --git a/hystrix-core/src/main/java/com/netflix/hystrix/strategy/concurrency/HystrixRequestVariableHolder.java b/hystrix-core/src/main/java/com/netflix/hystrix/strategy/concurrency/HystrixRequestVariableHolder.java index 54a6dd56d..84bd70d76 100644 --- a/hystrix-core/src/main/java/com/netflix/hystrix/strategy/concurrency/HystrixRequestVariableHolder.java +++ b/hystrix-core/src/main/java/com/netflix/hystrix/strategy/concurrency/HystrixRequestVariableHolder.java @@ -65,11 +65,7 @@ public T get(HystrixConcurrencyStrategy concurrencyStrategy) { } } - T result = (T) requestVariableInstance.get(key).get(); - if (result == null) { - throw new IllegalStateException("Failed to get HystrixRequestVariable. Maybe you need to initialize the HystrixRequestContext?"); - } - return result; + return (T) requestVariableInstance.get(key).get(); } private static class RVCacheKey { From 63630517c25ec6d1177a65efa47bde95a08a717e Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 6 Feb 2016 19:29:30 -0200 Subject: [PATCH 3/3] improving message --- .../src/main/java/com/netflix/hystrix/HystrixRequestCache.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hystrix-core/src/main/java/com/netflix/hystrix/HystrixRequestCache.java b/hystrix-core/src/main/java/com/netflix/hystrix/HystrixRequestCache.java index 97e366d08..d5240364a 100644 --- a/hystrix-core/src/main/java/com/netflix/hystrix/HystrixRequestCache.java +++ b/hystrix-core/src/main/java/com/netflix/hystrix/HystrixRequestCache.java @@ -102,7 +102,7 @@ private static HystrixRequestCache getInstance(RequestCacheKey rcKey, HystrixCon ValueCacheKey key = getRequestCacheKey(cacheKey); if (key != null) { if (!HystrixRequestContext.isCurrentThreadInitialized()) { - throw new IllegalStateException("Failed to get HystrixRequestVariable. Maybe you need to initialize the HystrixRequestContext?"); + throw new IllegalStateException("Request caching is not available. Maybe you need to initialize the HystrixRequestContext?"); } /* look for the stored value */ return (Observable) requestVariableForCache.get(concurrencyStrategy).get(key);