public void TestMetricsPublisherReset() { // precondition: HystrixMetricsPublisherFactory class is not loaded. Calling HystrixPlugins.reset() here should be good enough to run this with other tests. // set first custom publisher IHystrixCommandKey key = HystrixCommandKeyDefault.AsKey("key"); IHystrixMetricsPublisherCommand firstCommand = new HystrixMetricsPublisherCommandDefault(key, null, null, null, null); HystrixMetricsPublisher firstPublisher = new CustomPublisher(firstCommand); HystrixPlugins.RegisterMetricsPublisher(firstPublisher); // ensure that first custom publisher is used IHystrixMetricsPublisherCommand cmd = HystrixMetricsPublisherFactory.CreateOrRetrievePublisherForCommand(key, null, null, null, null); Assert.True(firstCommand == cmd); // reset, then change to second custom publisher HystrixPlugins.Reset(); IHystrixMetricsPublisherCommand secondCommand = new HystrixMetricsPublisherCommandDefault(key, null, null, null, null); HystrixMetricsPublisher secondPublisher = new CustomPublisher(secondCommand); HystrixPlugins.RegisterMetricsPublisher(secondPublisher); // ensure that second custom publisher is used cmd = HystrixMetricsPublisherFactory.CreateOrRetrievePublisherForCommand(key, null, null, null, null); Assert.True(firstCommand != cmd); Assert.True(secondCommand == cmd); }
public void EnsureThreadPoolInstanceIsTheOneRegisteredWithMetricsPublisherAndThreadPoolCache() { HystrixPlugins.RegisterMetricsPublisher(new MyHystrixMetricsPublisher()); IHystrixThreadPoolKey threadPoolKey = HystrixThreadPoolKeyDefault.AsKey("threadPoolFactoryConcurrencyTest"); IHystrixThreadPool poolOne = new HystrixThreadPoolDefault( threadPoolKey, HystrixThreadPoolOptionsTest.GetUnitTestPropertiesBuilder()); IHystrixThreadPool poolTwo = new HystrixThreadPoolDefault( threadPoolKey, HystrixThreadPoolOptionsTest.GetUnitTestPropertiesBuilder()); Assert.Equal(poolOne.GetScheduler(), poolTwo.GetScheduler()); // Now that we get the threadPool from the metrics object, this will always be equal HystrixMetricsPublisherThreadPoolContainer hystrixMetricsPublisherThreadPool = (HystrixMetricsPublisherThreadPoolContainer)HystrixMetricsPublisherFactory .CreateOrRetrievePublisherForThreadPool(threadPoolKey, null, null); IHystrixTaskScheduler threadPoolExecutor = hystrixMetricsPublisherThreadPool.HystrixThreadPoolMetrics.TaskScheduler; // assert that both HystrixThreadPools share the same ThreadPoolExecutor as the one in HystrixMetricsPublisherThreadPool Assert.True(threadPoolExecutor.Equals(poolOne.GetScheduler()) && threadPoolExecutor.Equals(poolTwo.GetScheduler())); Assert.False(threadPoolExecutor.IsShutdown); // Now the HystrixThreadPool ALWAYS has the same reference to the ThreadPoolExecutor so that it no longer matters which // wins to be inserted into the HystrixThreadPool.Factory.threadPools cache. poolOne.Dispose(); poolTwo.Dispose(); }
public void TestSingleInitializePerKey() { TestHystrixMetricsPublisher publisher = new TestHystrixMetricsPublisher(); HystrixPlugins.RegisterMetricsPublisher(publisher); HystrixMetricsPublisherFactory factory = new HystrixMetricsPublisherFactory(); List <Task> threads = new List <Task>(); for (int i = 0; i < 20; i++) { threads.Add(new Task(() => { factory.GetPublisherForCommand(TestCommandKey.TEST_A, null, null, null, null); factory.GetPublisherForCommand(TestCommandKey.TEST_B, null, null, null, null); factory.GetPublisherForThreadPool(TestThreadPoolKey.TEST_A, null, null); }, CancellationToken.None, TaskCreationOptions.LongRunning)); } // start them foreach (Task t in threads) { t.Start(); } // wait for them to finish Task.WaitAll(threads.ToArray()); Assert.Equal(2, factory.commandPublishers.Count); Assert.Equal(1, factory.threadPoolPublishers.Count); // we should see 2 commands and 1 threadPool publisher created Assert.Equal(2, publisher.commandCounter.Value); Assert.Equal(1, publisher.threadCounter.Value); }
public void MetricPublisherFactory_SingleInitializePerKey() { TestHystrixMetricsPublisher publisher = new TestHystrixMetricsPublisher(); HystrixMetricsPublisherFactory factory = new HystrixMetricsPublisherFactory(publisher); List <Thread> threads = new List <Thread>(); for (int i = 0; i < 20; i++) { threads.Add(new Thread(() => { factory.GetPublisherForCommand(CommandKeyForUnitTest.KeyOne, null, null, null, null); factory.GetPublisherForCommand(CommandKeyForUnitTest.KeyTwo, null, null, null, null); factory.GetPublisherForThreadPool(ThreadPoolKeyForUnitTest.ThreadPoolOne, null, null); })); } // start them foreach (Thread t in threads) { t.Start(); } // wait for them to finish foreach (Thread t in threads) { t.Join(); } // we should see 2 commands and 1 threadPool publisher created Assert.AreEqual(2, publisher.CommandCounter); Assert.AreEqual(1, publisher.ThreadCounter); }
public static void Reset() { notifier.Value = null; concurrencyStrategy.Value = null; metricsPublisher.Value = null; commandExecutionHook.Value = null; options.Value = null; HystrixMetricsPublisherFactory.Reset(); }
/// <summary> /// Initializes a new instance of the <see cref="HystrixThreadPoolDefault"/> class. /// </summary> /// <param name="threadPoolKey">The key of this thread pool.</param> /// <param name="setter">The default properties of this thread pool.</param> public HystrixThreadPoolDefault(HystrixThreadPoolKey threadPoolKey, HystrixThreadPoolPropertiesSetter setter) { this.properties = HystrixPropertiesFactory.GetThreadPoolProperties(threadPoolKey, setter); this.queue = HystrixPlugins.Instance.ConcurrencyStrategy.GetBlockingQueue(this.properties.MaxQueueSize.Get()); this.threadPool = HystrixPlugins.Instance.ConcurrencyStrategy.GetThreadPool(threadPoolKey, this.properties.CoreSize, this.properties.CoreSize, this.properties.KeepAliveTime, this.queue); this.metrics = HystrixThreadPoolMetrics.GetInstance(threadPoolKey, this.threadPool, this.properties); HystrixMetricsPublisherFactory.CreateOrRetrievePublisherForThreadPool(threadPoolKey, this.metrics, this.properties); }
public HystrixThreadPoolDefault(IHystrixThreadPoolKey threadPoolKey, IHystrixThreadPoolOptions propertiesDefaults) { _properties = HystrixOptionsFactory.GetThreadPoolOptions(threadPoolKey, propertiesDefaults); _properties = propertiesDefaults ?? new HystrixThreadPoolOptions(threadPoolKey); var concurrencyStrategy = HystrixPlugins.ConcurrencyStrategy; _queueSize = _properties.MaxQueueSize; _metrics = HystrixThreadPoolMetrics.GetInstance(threadPoolKey, concurrencyStrategy.GetTaskScheduler(_properties), _properties); _taskScheduler = _metrics.TaskScheduler; /* strategy: HystrixMetricsPublisherThreadPool */ HystrixMetricsPublisherFactory.CreateOrRetrievePublisherForThreadPool(threadPoolKey, _metrics, _properties); }
public HystrixThreadPoolDefault(IHystrixThreadPoolKey threadPoolKey, IHystrixThreadPoolOptions propertiesDefaults) { this.properties = HystrixOptionsFactory.GetThreadPoolOptions(threadPoolKey, propertiesDefaults); this.properties = propertiesDefaults ?? new HystrixThreadPoolOptions(threadPoolKey); HystrixConcurrencyStrategy concurrencyStrategy = HystrixPlugins.ConcurrencyStrategy; this.queueSize = properties.MaxQueueSize; this.metrics = HystrixThreadPoolMetrics.GetInstance(threadPoolKey, concurrencyStrategy.GetTaskScheduler(properties), properties); this.taskScheduler = this.metrics.TaskScheduler; /* strategy: HystrixMetricsPublisherThreadPool */ HystrixMetricsPublisherFactory.CreateOrRetrievePublisherForThreadPool(threadPoolKey, this.metrics, this.properties); }
protected HystrixCollapser(IHystrixCollapserKey collapserKey, RequestCollapserScope scope, ICollapserTimer timer, IHystrixCollapserOptions optionsDefault, HystrixCollapserMetrics metrics) { if (collapserKey == null || collapserKey.Name.Trim().Equals("")) { string defaultKeyName = GetDefaultNameFromClass(GetType()); collapserKey = HystrixCollapserKeyDefault.AsKey(defaultKeyName); } IHystrixCollapserOptions options = HystrixOptionsFactory.GetCollapserOptions(collapserKey, optionsDefault); this.collapserFactory = new RequestCollapserFactory(collapserKey, scope, timer, options); this.requestCache = HystrixRequestCache.GetInstance(collapserKey); if (metrics == null) { this.metrics = HystrixCollapserMetrics.GetInstance(collapserKey, options); } else { this.metrics = metrics; } HystrixMetricsPublisherFactory.CreateOrRetrievePublisherForCollapser(collapserKey, this.metrics, options); }
protected HystrixCollapser(IHystrixCollapserKey collapserKey, RequestCollapserScope scope, ICollapserTimer timer, IHystrixCollapserOptions optionsDefault, HystrixCollapserMetrics metrics) { if (collapserKey == null || string.IsNullOrWhiteSpace(collapserKey.Name)) { var defaultKeyName = GetDefaultNameFromClass(GetType()); collapserKey = HystrixCollapserKeyDefault.AsKey(defaultKeyName); } var options = HystrixOptionsFactory.GetCollapserOptions(collapserKey, optionsDefault); _collapserFactory = new RequestCollapserFactory(collapserKey, scope, timer, options); _requestCache = HystrixRequestCache.GetInstance(collapserKey); if (metrics == null) { _metrics = HystrixCollapserMetrics.GetInstance(collapserKey, options); } else { _metrics = metrics; } HystrixMetricsPublisherFactory.CreateOrRetrievePublisherForCollapser(collapserKey, _metrics, options); }