Hystrix icon indicating copy to clipboard operation
Hystrix copied to clipboard

HystrixRequestCache always get empty cacheInstance

Open wangweiqing opened this issue 10 years ago • 10 comments

I try to use Hystrix with hystrix-javanica. add those dependency to my pom.

        <dependency>
            <groupId>com.netflix.hystrix</groupId>
            <artifactId>hystrix-javanica</artifactId>
            <version>1.5.3</version>
        </dependency>
        <dependency>
            <groupId>com.netflix.hystrix</groupId>
            <artifactId>hystrix-core</artifactId>
            <version>1.5.3</version>
        </dependency>
        <dependency>
            <groupId>com.netflix.hystrix</groupId>
            <artifactId>hystrix-request-servlet</artifactId>
            <version>1.5.3</version>
        </dependency>

and config filter in my web.xml

   <filter>
        <display-name>HystrixRequestContextServletFilter</display-name>
        <filter-name>HystrixRequestContextServletFilter</filter-name>
        <filter-class>com.netflix.hystrix.contrib.requestservlet.HystrixRequestContextServletFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HystrixRequestContextServletFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

add annotation on my method.

    @CacheResult
    @HystrixCommand(groupKey = "PoiService", commandKey = "GetTopPoiByDestinationId", fallbackMethod = "getTopPoiByDestinationIdFallback",
    commandProperties = {
            @HystrixProperty(name = "execution.isolation.strategy", value = "THREAD"),
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "500") },
            threadPoolProperties = {
                    @HystrixProperty(name = "coreSize", value = "30"),
                    @HystrixProperty(name = "maxQueueSize", value = "101"),
                    @HystrixProperty(name = "keepAliveTimeMinutes", value = "2"),
                    @HystrixProperty(name = "queueSizeRejectionThreshold", value = "15")
            }
    )
    public List<ImageModel> getTopPoiImageByDestinationId(@CacheKey Integer destinationId, @CacheKey CateEnum category) {

But the cache not work. I find in this method.

    /**
     * Retrieve a cached Future for this request scope if a matching command has already been executed/queued.
     * 
     * @return {@code Future<T>}
     */
    // suppressing warnings because we are using a raw Future since it's in a heterogeneous ConcurrentHashMap cache
    @SuppressWarnings({ "unchecked" })
    /* package */<T> HystrixCachedObservable<T> get(String cacheKey) {
        ValueCacheKey key = getRequestCacheKey(cacheKey);
        if (key != null) {
            ConcurrentHashMap<ValueCacheKey, HystrixCachedObservable<?>> cacheInstance = requestVariableForCache.get(concurrencyStrategy);
            if (cacheInstance == null) {
                throw new IllegalStateException("Request caching is not available. Maybe you need to initialize the HystrixRequestContext?");
            }
            /* look for the stored value */
            return (HystrixCachedObservable<T>) cacheInstance.get(key);
        }
        return null;
    }

cacheInstance is always empty.

wangweiqing avatar Jul 05 '16 03:07 wangweiqing

This is an example that wires together the RequestContext servlet filter with some Hystrix commands. Can you take a look at this and see where your project differs?

https://github.com/Netflix/Hystrix/tree/master/hystrix-examples-webapp

mattrjacobs avatar Jul 06 '16 22:07 mattrjacobs

I choose every branches in this project,there are no code in this project.

https://github.com/Netflix/Hystrix/tree/master/hystrix-examples-webapp

wangweiqing avatar Jul 07 '16 02:07 wangweiqing

If you checkout the master branch of this project, cd to hystrix-examples-webapp, then run ../gradlew jettyRun, you'll get a server that runs a series of Hystrix commands every time you hit the proper localhost endpoint. The code is all in that module: https://github.com/Netflix/Hystrix/tree/master/hystrix-examples-webapp/src/main/webapp

mattrjacobs avatar Jul 07 '16 02:07 mattrjacobs

I see. I was try to use hystrix with annotation. So I import hystrix-javanica to my project.I think is annotation problem. In those example you extands HystrixCommand.

wangweiqing avatar Jul 07 '16 12:07 wangweiqing

I'm not saying javanica is the problem. It's widely used and well-supported. But I don't know where your problem is with the level of detail you've provided. So I would start with the example in hystrix-examples-webapp, get that working, and then add javanica.

mattrjacobs avatar Jul 07 '16 14:07 mattrjacobs

Hi, I write a example to provided more detail. https://github.com/wangweiqing/hystrix-javanica-test If you have time, you can have a look.

wangweiqing avatar Jul 12 '16 06:07 wangweiqing

If you look at the for kI just made and run it, you can see that HystrixRequestContext is getting set (as expected). So it looks like everything works the way I would expect.

mattrjacobs avatar Jul 13 '16 22:07 mattrjacobs

Sorry I don't know what do you mean about "look at the for kl just made and run it". In my case, HystrixRequestContext is getting set. I think the cache would be work with the same cache key. I think if I run a HystrixCommand with the same param, It would not run HystrixCommand's run method. AKA If the method has @HystrixCommand and @CacheResult. It will not be invoked with the same param. Is that right?

wangweiqing avatar Jul 14 '16 06:07 wangweiqing

Sorry, was typing too quickly and forgot to paste the link I meant to. https://github.com/mattrjacobs/hystrix-javanica-test is a fork I made of your repo. I added a log that confirms the HystrixRequestContext is getting set.

Your understanding is correct - if two commands with the same cache key execute in the same request, only 1 actually executes the run() method.

mattrjacobs avatar Jul 14 '16 18:07 mattrjacobs

Closing due to inactivity. Please re-open if there's more to discuss.

mattrjacobs avatar Aug 02 '16 20:08 mattrjacobs