Search in sources :

Example 1 with HystrixCollapserKey

use of com.netflix.hystrix.HystrixCollapserKey in project Hystrix by Netflix.

the class HystrixConfigurationJsonStream method convertToString.

public static String convertToString(HystrixConfiguration config) throws IOException {
    StringWriter jsonString = new StringWriter();
    JsonGenerator json = jsonFactory.createGenerator(jsonString);
    json.writeStartObject();
    json.writeStringField("type", "HystrixConfig");
    json.writeObjectFieldStart("commands");
    for (Map.Entry<HystrixCommandKey, HystrixCommandConfiguration> entry : config.getCommandConfig().entrySet()) {
        final HystrixCommandKey key = entry.getKey();
        final HystrixCommandConfiguration commandConfig = entry.getValue();
        writeCommandConfigJson(json, key, commandConfig);
    }
    json.writeEndObject();
    json.writeObjectFieldStart("threadpools");
    for (Map.Entry<HystrixThreadPoolKey, HystrixThreadPoolConfiguration> entry : config.getThreadPoolConfig().entrySet()) {
        final HystrixThreadPoolKey threadPoolKey = entry.getKey();
        final HystrixThreadPoolConfiguration threadPoolConfig = entry.getValue();
        writeThreadPoolConfigJson(json, threadPoolKey, threadPoolConfig);
    }
    json.writeEndObject();
    json.writeObjectFieldStart("collapsers");
    for (Map.Entry<HystrixCollapserKey, HystrixCollapserConfiguration> entry : config.getCollapserConfig().entrySet()) {
        final HystrixCollapserKey collapserKey = entry.getKey();
        final HystrixCollapserConfiguration collapserConfig = entry.getValue();
        writeCollapserConfigJson(json, collapserKey, collapserConfig);
    }
    json.writeEndObject();
    json.writeEndObject();
    json.close();
    return jsonString.getBuffer().toString();
}
Also used : HystrixCommandKey(com.netflix.hystrix.HystrixCommandKey) StringWriter(java.io.StringWriter) HystrixCommandConfiguration(com.netflix.hystrix.config.HystrixCommandConfiguration) HystrixCollapserKey(com.netflix.hystrix.HystrixCollapserKey) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) HystrixThreadPoolKey(com.netflix.hystrix.HystrixThreadPoolKey) Map(java.util.Map) HystrixCollapserConfiguration(com.netflix.hystrix.config.HystrixCollapserConfiguration) HystrixThreadPoolConfiguration(com.netflix.hystrix.config.HystrixThreadPoolConfiguration)

Example 2 with HystrixCollapserKey

use of com.netflix.hystrix.HystrixCollapserKey in project Hystrix by Netflix.

the class RollingCollapserBatchSizeDistributionStreamTest method testEmptyStreamProducesEmptyDistributions.

@Test
public void testEmptyStreamProducesEmptyDistributions() {
    HystrixCollapserKey key = HystrixCollapserKey.Factory.asKey("Collapser-Batch-Size-A");
    stream = RollingCollapserBatchSizeDistributionStream.getInstance(key, 10, 100);
    stream.startCachingStreamValuesIfUnstarted();
    final CountDownLatch latch = new CountDownLatch(1);
    stream.observe().skip(10).take(10).subscribe(new Subscriber<CachedValuesHistogram>() {

        @Override
        public void onCompleted() {
            latch.countDown();
        }

        @Override
        public void onError(Throwable e) {
            fail(e.getMessage());
        }

        @Override
        public void onNext(CachedValuesHistogram distribution) {
            System.out.println("OnNext @ " + System.currentTimeMillis());
            assertEquals(0, distribution.getTotalCount());
        }
    });
    try {
        assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    } catch (InterruptedException ex) {
        fail("Interrupted ex");
    }
    assertEquals(0, stream.getLatest().getTotalCount());
}
Also used : CachedValuesHistogram(com.netflix.hystrix.metric.CachedValuesHistogram) HystrixCollapserKey(com.netflix.hystrix.HystrixCollapserKey) CountDownLatch(java.util.concurrent.CountDownLatch) CommandStreamTest(com.netflix.hystrix.metric.CommandStreamTest) Test(org.junit.Test)

Example 3 with HystrixCollapserKey

use of com.netflix.hystrix.HystrixCollapserKey in project Hystrix by Netflix.

the class RollingCollapserEventCounterStreamTest method testEmptyStreamProducesZeros.

@Test
public void testEmptyStreamProducesZeros() {
    HystrixCollapserKey key = HystrixCollapserKey.Factory.asKey("RollingCollapser-A");
    stream = RollingCollapserEventCounterStream.getInstance(key, 10, 100);
    stream.startCachingStreamValuesIfUnstarted();
    final CountDownLatch latch = new CountDownLatch(1);
    stream.observe().take(10).subscribe(getSubscriber(latch));
    try {
        assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    } catch (InterruptedException ex) {
        fail("Interrupted ex");
    }
    System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
    assertEquals(HystrixEventType.Collapser.values().length, stream.getLatest().length);
    assertEquals(0, stream.getLatest(HystrixEventType.Collapser.ADDED_TO_BATCH));
    assertEquals(0, stream.getLatest(HystrixEventType.Collapser.BATCH_EXECUTED));
    assertEquals(0, stream.getLatest(HystrixEventType.Collapser.RESPONSE_FROM_CACHE));
}
Also used : HystrixCollapserKey(com.netflix.hystrix.HystrixCollapserKey) CountDownLatch(java.util.concurrent.CountDownLatch) CommandStreamTest(com.netflix.hystrix.metric.CommandStreamTest) Test(org.junit.Test)

Example 4 with HystrixCollapserKey

use of com.netflix.hystrix.HystrixCollapserKey in project Hystrix by Netflix.

the class RollingCollapserEventCounterStreamTest method testCollapsedAndResponseFromCacheAgeOutOfRollingWindow.

//by doing a take(30), we expect all values to return to 0 as they age out of rolling window
@Test
public void testCollapsedAndResponseFromCacheAgeOutOfRollingWindow() {
    HystrixCollapserKey key = HystrixCollapserKey.Factory.asKey("RollingCollapser-D");
    stream = RollingCollapserEventCounterStream.getInstance(key, 10, 100);
    stream.startCachingStreamValuesIfUnstarted();
    final CountDownLatch latch = new CountDownLatch(1);
    stream.observe().take(30).subscribe(getSubscriber(latch));
    for (int i = 0; i < 3; i++) {
        CommandStreamTest.Collapser.from(key, i).observe();
        //same arg - should get a response from cache
        CommandStreamTest.Collapser.from(key, i).observe();
        //same arg - should get a response from cache
        CommandStreamTest.Collapser.from(key, i).observe();
    }
    try {
        assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    } catch (InterruptedException ex) {
        fail("Interrupted ex");
    }
    assertEquals(HystrixEventType.Collapser.values().length, stream.getLatest().length);
    long[] expected = new long[HystrixEventType.Collapser.values().length];
    expected[HystrixEventType.Collapser.BATCH_EXECUTED.ordinal()] = 0;
    expected[HystrixEventType.Collapser.ADDED_TO_BATCH.ordinal()] = 0;
    expected[HystrixEventType.Collapser.RESPONSE_FROM_CACHE.ordinal()] = 0;
    System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
    assertArrayEquals(expected, stream.getLatest());
}
Also used : HystrixCollapserKey(com.netflix.hystrix.HystrixCollapserKey) CountDownLatch(java.util.concurrent.CountDownLatch) CommandStreamTest(com.netflix.hystrix.metric.CommandStreamTest) Test(org.junit.Test)

Example 5 with HystrixCollapserKey

use of com.netflix.hystrix.HystrixCollapserKey in project Hystrix by Netflix.

the class CumulativeCollapserEventCounterStreamTest method testCollapsedAndResponseFromCache.

@Test
public void testCollapsedAndResponseFromCache() {
    HystrixCollapserKey key = HystrixCollapserKey.Factory.asKey("CumulativeCollapser-C");
    stream = CumulativeCollapserEventCounterStream.getInstance(key, 10, 100);
    stream.startCachingStreamValuesIfUnstarted();
    final CountDownLatch latch = new CountDownLatch(1);
    stream.observe().take(10).subscribe(getSubscriber(latch));
    for (int i = 0; i < 3; i++) {
        CommandStreamTest.Collapser.from(key, i).observe();
        //same arg - should get a response from cache
        CommandStreamTest.Collapser.from(key, i).observe();
        //same arg - should get a response from cache
        CommandStreamTest.Collapser.from(key, i).observe();
    }
    try {
        assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    } catch (InterruptedException ex) {
        fail("Interrupted ex");
    }
    assertEquals(HystrixEventType.Collapser.values().length, stream.getLatest().length);
    long[] expected = new long[HystrixEventType.Collapser.values().length];
    expected[HystrixEventType.Collapser.BATCH_EXECUTED.ordinal()] = 1;
    expected[HystrixEventType.Collapser.ADDED_TO_BATCH.ordinal()] = 3;
    expected[HystrixEventType.Collapser.RESPONSE_FROM_CACHE.ordinal()] = 6;
    System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
    assertArrayEquals(expected, stream.getLatest());
}
Also used : HystrixCollapserKey(com.netflix.hystrix.HystrixCollapserKey) CountDownLatch(java.util.concurrent.CountDownLatch) CommandStreamTest(com.netflix.hystrix.metric.CommandStreamTest) Test(org.junit.Test)

Aggregations

HystrixCollapserKey (com.netflix.hystrix.HystrixCollapserKey)15 CommandStreamTest (com.netflix.hystrix.metric.CommandStreamTest)11 CountDownLatch (java.util.concurrent.CountDownLatch)11 Test (org.junit.Test)11 CachedValuesHistogram (com.netflix.hystrix.metric.CachedValuesHistogram)3 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)2 HystrixCommandKey (com.netflix.hystrix.HystrixCommandKey)2 HystrixThreadPoolKey (com.netflix.hystrix.HystrixThreadPoolKey)2 HystrixCollapserConfiguration (com.netflix.hystrix.config.HystrixCollapserConfiguration)2 HystrixCommandConfiguration (com.netflix.hystrix.config.HystrixCommandConfiguration)2 HystrixThreadPoolConfiguration (com.netflix.hystrix.config.HystrixThreadPoolConfiguration)2 StringWriter (java.io.StringWriter)2 Map (java.util.Map)2 IOException (java.io.IOException)1