Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if the request log is enabled before requiring the context is initialized. #724

Merged
merged 1 commit into from
Mar 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions hystrix-core/src/main/java/com/netflix/hystrix/AbstractCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -257,21 +257,25 @@ protected AbstractCommand(HystrixCommandGroupKey group, HystrixCommandKey key, H
/* setup the request cache for this instance */
this.requestCache = HystrixRequestCache.getInstance(this.commandKey, this.concurrencyStrategy);

/* store reference to request log regardless of which thread later hits it */
if (concurrencyStrategy instanceof HystrixConcurrencyStrategyDefault) {
// if we're using the default we support only optionally using a request context
if (HystrixRequestContext.isCurrentThreadInitialized()) {
if (properties.requestLogEnabled().get()) {
/* store reference to request log regardless of which thread later hits it */
if (concurrencyStrategy instanceof HystrixConcurrencyStrategyDefault) {
// if we're using the default we support only optionally using a request context
if (HystrixRequestContext.isCurrentThreadInitialized()) {
currentRequestLog = HystrixRequestLog.getCurrentRequest(concurrencyStrategy);
} else {
} else {
currentRequestLog = null;
}
} else {
// if it's a custom strategy it must ensure the context is initialized
if (HystrixRequestLog.getCurrentRequest(concurrencyStrategy) != null) {
currentRequestLog = HystrixRequestLog.getCurrentRequest(concurrencyStrategy);
}
} else {
// if it's a custom strategy it must ensure the context is initialized
if (HystrixRequestLog.getCurrentRequest(concurrencyStrategy) != null) {
currentRequestLog = HystrixRequestLog.getCurrentRequest(concurrencyStrategy);
} else {
currentRequestLog = null;
}
}
} else {
currentRequestLog = null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@
package com.netflix.hystrix.strategy.concurrency;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.lang.IllegalStateException;
import java.util.concurrent.atomic.AtomicBoolean;

import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -41,11 +45,7 @@ public void prepareForTest() {

@After
public void cleanup() {
// instead of storing the reference from initialize we'll just get the current state and shutdown
if (HystrixRequestContext.getContextForCurrentThread() != null) {
// it could have been set NULL by the test
HystrixRequestContext.getContextForCurrentThread().shutdown();
}
shutdownContextIfExists();

// force properties to be clean as well
ConfigurationManager.getConfigInstance().clear();
Expand Down Expand Up @@ -82,7 +82,9 @@ public SimpleCommand() {

@Override
protected String run() throws Exception {
System.out.println("Executing => Commands: " + HystrixRequestLog.getCurrentRequest().getAllExecutedCommands());
if (HystrixRequestContext.isCurrentThreadInitialized()) {
System.out.println("Executing => Commands: " + HystrixRequestLog.getCurrentRequest().getAllExecutedCommands());
}
return "Hello";
}

Expand All @@ -106,7 +108,26 @@ public void call(Throwable throwable) {
assertEquals(true, isInitialized.get());
}

public static class TimeoutCommand extends HystrixCommand<Void> {
@Test
public void testNoRequestContextOnSimpleConcurencyStrategyWithoutException() throws Exception {
shutdownContextIfExists();
ConfigurationManager.getConfigInstance().setProperty("hystrix.command.default.requestLog.enabled", "false");

new SimpleCommand().execute();

assertTrue("We are able to run the simple command without a context initialization error.", true);
}

private void shutdownContextIfExists() {
// instead of storing the reference from initialize we'll just get the current state and shutdown
if (HystrixRequestContext.getContextForCurrentThread() != null) {
// it could have been set NULL by the test
HystrixRequestContext.getContextForCurrentThread().shutdown();
}
}
private static class DummyHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy {}

public static class TimeoutCommand extends HystrixCommand<Void> {
static final HystrixCommand.Setter properties = HystrixCommand.Setter
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("TimeoutTest"))
.andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
Expand Down