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

Push RequestContext tolerance into ConcurrencyStrategy #951

Merged
merged 1 commit into from
Oct 21, 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
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package com.netflix.hystrix;

import java.lang.ref.Reference;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -51,7 +50,6 @@
import com.netflix.hystrix.exception.HystrixRuntimeException.FailureType;
import com.netflix.hystrix.strategy.HystrixPlugins;
import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy;
import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategyDefault;
import com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import com.netflix.hystrix.strategy.eventnotifier.HystrixEventNotifier;
Expand Down Expand Up @@ -262,21 +260,7 @@ protected AbstractCommand(HystrixCommandGroupKey group, HystrixCommandKey key, H

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 {
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 {
currentRequestLog = null;
}
}
currentRequestLog = HystrixRequestLog.getCurrentRequest(concurrencyStrategy);
} else {
currentRequestLog = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,7 @@ public <T> Callable<T> wrapCallable(Callable<T> callable) {
* @return {@code HystrixRequestVariable<T>}
*/
public <T> HystrixRequestVariable<T> getRequestVariable(final HystrixRequestVariableLifecycle<T> rv) {
return new HystrixRequestVariableDefault<T>() {
@Override
public T initialValue() {
return rv.initialValue();
}

public void shutdown(T value) {
rv.shutdown(value);
}
};
return new HystrixLifecycleForwardingRequestVariable<T>(rv);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* Copyright 2015 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.netflix.hystrix.strategy.concurrency;

/**
* Implementation of {@link HystrixRequestVariable} which forwards to the wrapped
* {@link HystrixRequestVariableLifecycle}.
* <p>
* This implementation also returns null when {@link #get()} is called while the {@link HystrixRequestContext} has not
* been initialized rather than throwing an exception, allowing for use in a {@link HystrixConcurrencyStrategy} which
* does not depend on an a HystrixRequestContext
*/
public class HystrixLifecycleForwardingRequestVariable<T> extends HystrixRequestVariableDefault<T> {
private final HystrixRequestVariableLifecycle<T> lifecycle;

/**
* Creates a HystrixRequestVariable which will return data as provided by the {@link HystrixRequestVariableLifecycle}
* @param lifecycle lifecycle used to provide values. Must have the same type parameter as the constructed instance.
*/
public HystrixLifecycleForwardingRequestVariable(HystrixRequestVariableLifecycle<T> lifecycle) {
this.lifecycle = lifecycle;
}

/**
* Delegates to the wrapped {@link HystrixRequestVariableLifecycle}
* @return T with initial value or null if none.
*/
@Override
public T initialValue() {
return lifecycle.initialValue();
}

/**
* Delegates to the wrapped {@link HystrixRequestVariableLifecycle}
* @param value
* of request variable to allow cleanup activity.
* <p>
* If nothing needs to be cleaned up then nothing needs to be done in this method.
*/
@Override
public void shutdown(T value) {
lifecycle.shutdown(value);
}

/**
* Return null if the {@link HystrixRequestContext} has not been initialized for the current thread.
* <p>
* If {@link HystrixRequestContext} has been initialized then call method in superclass:
* {@link HystrixRequestVariableDefault#get()}
*/
@Override
public T get() {
if (!HystrixRequestContext.isCurrentThreadInitialized()) {
return null;
}
return super.get();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* <p>
* This is used as a layer between the actual {@link HystrixRequestVariable} and calling code to allow injected implementations of {@link HystrixConcurrencyStrategy}.
* <p>
* Typically a {@link HystrixRequestVariable} would be statically references (similar to a ThreadLocal) but to allow dynamic injection we instead statically reference this class which can then
* Typically a {@link HystrixRequestVariable} would be statically referenced (similar to a ThreadLocal) but to allow dynamic injection we instead statically reference this class which can then
* dynamically fetch the correct implementation and statically retain an instance across threads within a context (such as {@link HystrixRequestContext}.
*
* @param <T>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2717,12 +2717,7 @@ public void testBasicExecutionWorksWithoutRequestVariable() {
fail("We received an exception => " + e.getMessage());
}

try {
HystrixRequestLog.getCurrentRequest();
fail("Should not have a RequestLog when RequestContext not initialized");
} catch (IllegalStateException ise) {
//expected
}
assertNull(HystrixRequestLog.getCurrentRequest());
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd been hoping to avoid changing any tests but the use of this same method call in command instantiation precludes that option without more logical changes.

}

/**
Expand All @@ -2749,13 +2744,6 @@ public void testCacheKeyExecutionRequiresRequestVariable() {
} catch (Exception e) {
e.printStackTrace();
}

try {
HystrixRequestLog.getCurrentRequest();
fail("Should not have a RequestLog when RequestContext not initialized");
} catch (IllegalStateException ise) {
//expected
}
}

/**
Expand Down