Skip to content

Commit

Permalink
Switch to KeyAndBuilderValuesHystrixPropertiesStrategy
Browse files Browse the repository at this point in the history
  • Loading branch information
lloydmeta committed Oct 3, 2014
1 parent 5153fef commit cf0507a
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 25 deletions.
12 changes: 6 additions & 6 deletions app/com/m3/octoparts/Global.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import _root_.controllers.ControllersModule
import com.kenshoo.play.metrics.MetricsFilter
import com.m3.octoparts.cache.CacheModule
import com.m3.octoparts.http.HttpModule
import com.m3.octoparts.hystrix.{ CachelessHystrixPropertiesStrategy, HystrixMetricsLogger, HystrixModule }
import com.m3.octoparts.hystrix.{ KeyAndBuilderValuesHystrixPropertiesStrategy, HystrixMetricsLogger, HystrixModule }
import com.m3.octoparts.logging.PartRequestLogger
import com.beachape.logging.LTSVLogger
import com.m3.octoparts.repository.RepositoriesModule
Expand Down Expand Up @@ -101,24 +101,24 @@ object Global extends WithFilters(MetricsFilter) with ScaldiSupport {
}

/**
* Tries to set the Hystrix properties strategy to CachelessHystrixPropertiesStrategy
* Tries to set the Hystrix properties strategy to [[KeyAndBuilderValuesHystrixPropertiesStrategy]]
*
* Resist the temptation to do a HystrixPlugins.getInstance().getPropertiesStrategy first to do
* checking, as that actually also sets the strategy if it isn't already set.
*/
def setHystrixPropertiesStrategy(app: Application): Unit = {
// If it's defined, we don't need to set anything
if (sys.props.get("hystrix.plugin.HystrixPropertiesStrategy.implementation").isEmpty) {
LTSVLogger.warn("-Dhystrix.plugin.HystrixPropertiesStrategy.implementation is not set. It should be" -> "com.m3.octoparts.hystrix.CachelessHystrixPropertiesStrategy")
LTSVLogger.warn("-Dhystrix.plugin.HystrixPropertiesStrategy.implementation is not set. It should be" -> "com.m3.octoparts.hystrix.KeyAndBuilderValuesHystrixPropertiesStrategy")
try {
HystrixPlugins.getInstance().registerPropertiesStrategy(new CachelessHystrixPropertiesStrategy)
HystrixPlugins.getInstance().registerPropertiesStrategy(new KeyAndBuilderValuesHystrixPropertiesStrategy)
} catch {
case NonFatal(e) => {
val currentStrategy = HystrixPlugins.getInstance().getPropertiesStrategy.getClass
if (currentStrategy != classOf[CachelessHystrixPropertiesStrategy]) {
if (currentStrategy != classOf[KeyAndBuilderValuesHystrixPropertiesStrategy]) {
LTSVLogger.error(e, "Current Hystrix Properties Strategy:" -> currentStrategy)
if (Play.mode(app) != Mode.Test)
sys.error("Tried but failed to set CachelessHystrixPropertiesStrategy as HystrixPropertiesStrategy")
sys.error("Tried but failed to set KeyAndBuilderValuesHystrixPropertiesStrategy as HystrixPropertiesStrategy")
}
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.m3.octoparts.hystrix

import com.fasterxml.jackson.databind.ObjectMapper
import com.netflix.hystrix.{ HystrixCommandKey, HystrixCommandProperties }
import com.netflix.hystrix.strategy.properties.HystrixPropertiesStrategy

/**
* Custom [[HystrixPropertiesStrategy]] implementation
*/
class KeyAndBuilderValuesHystrixPropertiesStrategy extends HystrixPropertiesStrategy {
private val mapper = new ObjectMapper()

/**
* Overriden to return a [[String]] that is a combination of the commandKey name and a JSON string
* of the builder values
*/
override def getCommandPropertiesCacheKey(commandKey: HystrixCommandKey, builder: HystrixCommandProperties.Setter): String =
s"${commandKey.name()}-${mapper.writeValueAsString(builder)}"

}
2 changes: 1 addition & 1 deletion project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ object OctopartsBuild extends Build {
sonatypeSettings ++
coverallsSettings ++
Seq(
javaOptions += "-Dhystrix.plugin.HystrixPropertiesStrategy.implementation=com.m3.octoparts.hystrix.CachelessHystrixPropertiesStrategy",
javaOptions += "-Dhystrix.plugin.HystrixPropertiesStrategy.implementation=com.m3.octoparts.hystrix.KeyAndBuilderValuesHystrixPropertiesStrategy",
publishArtifact := false,
libraryDependencies ++= Seq(
// Logging
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import com.netflix.hystrix.strategy.properties.HystrixPropertiesFactory
import com.netflix.hystrix.{ HystrixCommandProperties, HystrixCommandKey }
import org.scalatest.{ Matchers, FunSpec }

class CachelessHystrixPropertiesStrategySpec extends FunSpec with Matchers {
class KeyAndBuilderValuesHystrixPropertiesStrategySpec extends FunSpec with Matchers {

val subject = new CachelessHystrixPropertiesStrategy
val subject = new KeyAndBuilderValuesHystrixPropertiesStrategy
val commandKey = HystrixCommandKey.Factory.asKey("hello")
val commandProps = HystrixCommandProperties.Setter()
describe("getCommandPropertiesCacheKey") {
it("should return null") {
it("should return a combination of the commandKey name and commandProps JSON value") {
val r = subject.getCommandPropertiesCacheKey(commandKey, commandProps)
r should be(null)
r should be("""hello-{"circuitBreakerEnabled":null,"circuitBreakerErrorThresholdPercentage":null,"circuitBreakerForceClosed":null,"circuitBreakerForceOpen":null,"circuitBreakerRequestVolumeThreshold":null,"circuitBreakerSleepWindowInMilliseconds":null,"executionIsolationSemaphoreMaxConcurrentRequests":null,"executionIsolationStrategy":null,"executionIsolationThreadInterruptOnTimeout":null,"executionIsolationThreadTimeoutInMilliseconds":null,"fallbackIsolationSemaphoreMaxConcurrentRequests":null,"fallbackEnabled":null,"metricsHealthSnapshotIntervalInMilliseconds":null,"metricsRollingPercentileBucketSize":null,"metricsRollingPercentileEnabled":null,"metricsRollingPercentileWindowInMilliseconds":null,"metricsRollingPercentileWindowBuckets":null,"metricsRollingStatisticalWindowInMilliseconds":null,"metricsRollingStatisticalWindowBuckets":null,"requestCacheEnabled":null,"requestLogEnabled":null}""")
}
}

Expand All @@ -22,7 +22,7 @@ class CachelessHystrixPropertiesStrategySpec extends FunSpec with Matchers {

it("should allow HystrixPropertiesFactory.getCommandProperties to instantiate different HystrixCommandProperties for the same command key") {
if (HystrixPlugins.getInstance().getPropertiesStrategy.getClass != subject.getClass) {
fail("HystrixPlugins.getPropertiesStrategy did not return CachelessHystrixPropertiesStrategy")
fail("HystrixPlugins.getPropertiesStrategy did not return KeyAndBuilderValuesHystrixPropertiesStrategy")
}
val properties1 = HystrixPropertiesFactory.getCommandProperties(
commandKey,
Expand Down

0 comments on commit cf0507a

Please sign in to comment.