Search in sources :

Example 6 with ThreadPoolBulkhead

use of io.github.resilience4j.bulkhead.ThreadPoolBulkhead in project resilience4j by resilience4j.

the class TaggedThreadPoolBulkheadMetricsPublisherTest method shouldAddMetricsForANewlyCreatedRetry.

@Test
public void shouldAddMetricsForANewlyCreatedRetry() {
    ThreadPoolBulkhead newBulkhead = bulkheadRegistry.bulkhead("backendB");
    assertThat(taggedBulkheadMetricsPublisher.meterIdMap).containsKeys("backendA", "backendB");
    assertThat(taggedBulkheadMetricsPublisher.meterIdMap.get("backendA")).hasSize(EXPECTED_METER_COUNT);
    assertThat(taggedBulkheadMetricsPublisher.meterIdMap.get("backendB")).hasSize(EXPECTED_METER_COUNT);
    List<Meter> meters = meterRegistry.getMeters();
    assertThat(meters).hasSize(EXPECTED_METER_COUNT * 2);
    Collection<Gauge> gauges = meterRegistry.get(DEFAULT_MAX_THREAD_POOL_SIZE_METRIC_NAME).gauges();
    Optional<Gauge> successful = findMeterByNamesTag(gauges, newBulkhead.getName());
    assertThat(successful).isPresent();
    assertThat(successful.get().value()).isEqualTo(newBulkhead.getMetrics().getMaximumThreadPoolSize());
}
Also used : ThreadPoolBulkhead(io.github.resilience4j.bulkhead.ThreadPoolBulkhead) Meter(io.micrometer.core.instrument.Meter) Gauge(io.micrometer.core.instrument.Gauge) Test(org.junit.Test)

Example 7 with ThreadPoolBulkhead

use of io.github.resilience4j.bulkhead.ThreadPoolBulkhead in project resilience4j by resilience4j.

the class TaggedThreadPoolBulkheadMetrics method bindTo.

@Override
public void bindTo(MeterRegistry registry) {
    for (ThreadPoolBulkhead bulkhead : bulkheadRegistry.getAllBulkheads()) {
        addMetrics(registry, bulkhead);
    }
    bulkheadRegistry.getEventPublisher().onEntryAdded(event -> addMetrics(registry, event.getAddedEntry()));
    bulkheadRegistry.getEventPublisher().onEntryRemoved(event -> removeMetrics(registry, event.getRemovedEntry().getName()));
    bulkheadRegistry.getEventPublisher().onEntryReplaced(event -> {
        removeMetrics(registry, event.getOldEntry().getName());
        addMetrics(registry, event.getNewEntry());
    });
}
Also used : ThreadPoolBulkhead(io.github.resilience4j.bulkhead.ThreadPoolBulkhead)

Example 8 with ThreadPoolBulkhead

use of io.github.resilience4j.bulkhead.ThreadPoolBulkhead in project resilience4j by resilience4j.

the class ThreadPoolBulkheadMetricsCollector method collect.

@Override
public List<MetricFamilySamples> collect() {
    GaugeMetricFamily availableCallsFamily = new GaugeMetricFamily(names.getCurrentThreadPoolSizeName(), "The number of currently used bulkhead threads", LabelNames.NAME);
    GaugeMetricFamily maxAllowedCallsFamily = new GaugeMetricFamily(names.getAvailableQueueCapacityName(), "The number of available bulkhead queue slots", LabelNames.NAME);
    for (ThreadPoolBulkhead bulkhead : bulkheadRegistry.getAllBulkheads()) {
        List<String> labelValues = singletonList(bulkhead.getName());
        availableCallsFamily.addMetric(labelValues, bulkhead.getMetrics().getThreadPoolSize());
        maxAllowedCallsFamily.addMetric(labelValues, bulkhead.getMetrics().getRemainingQueueCapacity());
    }
    return asList(availableCallsFamily, maxAllowedCallsFamily);
}
Also used : ThreadPoolBulkhead(io.github.resilience4j.bulkhead.ThreadPoolBulkhead) GaugeMetricFamily(io.prometheus.client.GaugeMetricFamily)

Example 9 with ThreadPoolBulkhead

use of io.github.resilience4j.bulkhead.ThreadPoolBulkhead in project resilience4j by resilience4j.

the class DecoratorsTest method testDecorateSupplierWithBulkheadFullExceptionFallback.

@Test
public void testDecorateSupplierWithBulkheadFullExceptionFallback() throws ExecutionException, InterruptedException {
    ThreadPoolBulkhead bulkhead = ThreadPoolBulkhead.ofDefaults("helloBackend");
    ThreadPoolBulkhead bulkheadMock = spy(bulkhead);
    given(bulkheadMock.submit(any(Callable.class))).willThrow(BulkheadFullException.createBulkheadFullException(bulkhead));
    CompletionStage<String> completionStage = Decorators.ofSupplier(() -> helloWorldService.returnHelloWorld()).withThreadPoolBulkhead(bulkheadMock).withFallback(BulkheadFullException.class, (e) -> "Fallback").get();
    String result = completionStage.toCompletableFuture().get();
    assertThat(result).isEqualTo("Fallback");
}
Also used : ThreadPoolBulkhead(io.github.resilience4j.bulkhead.ThreadPoolBulkhead) CheckedFunction(io.github.resilience4j.core.functions.CheckedFunction) BulkheadFullException(io.github.resilience4j.bulkhead.BulkheadFullException) RateLimiter(io.github.resilience4j.ratelimiter.RateLimiter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Bulkhead(io.github.resilience4j.bulkhead.Bulkhead) TimeLimiterConfig(io.github.resilience4j.timelimiter.TimeLimiterConfig) RequestNotPermitted(io.github.resilience4j.ratelimiter.RequestNotPermitted) TestThreadLocalContextHolder(io.github.resilience4j.test.TestContextPropagators.TestThreadLocalContextPropagatorWithHolder.TestThreadLocalContextHolder) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) BDDMockito.given(org.mockito.BDDMockito.given) Arrays.asList(java.util.Arrays.asList) Cache(io.github.resilience4j.cache.Cache) Duration(java.time.Duration) Map(java.util.Map) HelloWorldService(io.github.resilience4j.test.HelloWorldService) RateLimiterConfig(io.github.resilience4j.ratelimiter.RateLimiterConfig) HelloWorldException(io.github.resilience4j.test.HelloWorldException) Before(org.junit.Before) ThreadPoolBulkhead(io.github.resilience4j.bulkhead.ThreadPoolBulkhead) CheckedRunnable(io.github.resilience4j.core.functions.CheckedRunnable) CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) CheckedSupplier(io.github.resilience4j.core.functions.CheckedSupplier) Retry(io.github.resilience4j.retry.Retry) Awaitility.matches(com.jayway.awaitility.Awaitility.matches) java.util.concurrent(java.util.concurrent) ContextAwareScheduledThreadPoolExecutor(io.github.resilience4j.core.ContextAwareScheduledThreadPoolExecutor) BDDMockito.then(org.mockito.BDDMockito.then) Test(org.junit.Test) IOException(java.io.IOException) AsyncHelloWorldService(io.github.resilience4j.test.AsyncHelloWorldService) TestThreadLocalContextPropagatorWithHolder(io.github.resilience4j.test.TestContextPropagators.TestThreadLocalContextPropagatorWithHolder) Awaitility.waitAtMost(com.jayway.awaitility.Awaitility.waitAtMost) Mockito(org.mockito.Mockito) Try(io.vavr.control.Try) TimeLimiter(io.github.resilience4j.timelimiter.TimeLimiter) MDC(org.slf4j.MDC) CallNotPermittedException(io.github.resilience4j.circuitbreaker.CallNotPermittedException) BulkheadFullException(io.github.resilience4j.bulkhead.BulkheadFullException) Test(org.junit.Test)

Example 10 with ThreadPoolBulkhead

use of io.github.resilience4j.bulkhead.ThreadPoolBulkhead in project resilience4j by resilience4j.

the class DecoratorsTest method shouldThrowTimeoutExceptionAndPropagateContext.

@Test
public void shouldThrowTimeoutExceptionAndPropagateContext() {
    TimeLimiter timeLimiter = TimeLimiter.of("helloBackend", TimeLimiterConfig.custom().timeoutDuration(Duration.ofMillis(100)).build());
    CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("helloBackend");
    ThreadPoolBulkhead bulkhead = ThreadPoolBulkhead.ofDefaults("helloBackend");
    TestThreadLocalContextPropagatorWithHolder propagator = new TestThreadLocalContextPropagatorWithHolder();
    TestThreadLocalContextHolder.put("ValueShouldCrossThreadBoundary");
    ContextAwareScheduledThreadPoolExecutor scheduledThreadPool = ContextAwareScheduledThreadPoolExecutor.newScheduledThreadPool().corePoolSize(1).contextPropagators(propagator).build();
    CompletionStage<String> completionStage = Decorators.ofCallable(() -> {
        assertThat(Thread.currentThread().getName()).isEqualTo("bulkhead-helloBackend-1");
        Thread.sleep(1000);
        return "Bla";
    }).withThreadPoolBulkhead(bulkhead).withTimeLimiter(timeLimiter, scheduledThreadPool).withCircuitBreaker(circuitBreaker).get();
    final CompletableFuture<String> completableFuture = completionStage.toCompletableFuture().exceptionally(throwable -> {
        if (throwable != null) {
            assertThat(Thread.currentThread().getName()).isEqualTo("ContextAwareScheduledThreadPool-1");
            assertThat(TestThreadLocalContextHolder.get().get()).isEqualTo("ValueShouldCrossThreadBoundary");
            return (String) TestThreadLocalContextHolder.get().orElse(null);
        }
        return null;
    });
    waitAtMost(2, TimeUnit.SECONDS).until(matches(() -> assertThat(completableFuture).isCompletedWithValue("ValueShouldCrossThreadBoundary")));
    CircuitBreaker.Metrics metrics = circuitBreaker.getMetrics();
    assertThat(metrics.getNumberOfBufferedCalls()).isEqualTo(1);
    assertThat(metrics.getNumberOfFailedCalls()).isEqualTo(1);
}
Also used : TimeLimiter(io.github.resilience4j.timelimiter.TimeLimiter) ThreadPoolBulkhead(io.github.resilience4j.bulkhead.ThreadPoolBulkhead) CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) ContextAwareScheduledThreadPoolExecutor(io.github.resilience4j.core.ContextAwareScheduledThreadPoolExecutor) TestThreadLocalContextPropagatorWithHolder(io.github.resilience4j.test.TestContextPropagators.TestThreadLocalContextPropagatorWithHolder) Test(org.junit.Test)

Aggregations

ThreadPoolBulkhead (io.github.resilience4j.bulkhead.ThreadPoolBulkhead)17 Test (org.junit.Test)15 CircuitBreaker (io.github.resilience4j.circuitbreaker.CircuitBreaker)8 TimeLimiter (io.github.resilience4j.timelimiter.TimeLimiter)8 ContextAwareScheduledThreadPoolExecutor (io.github.resilience4j.core.ContextAwareScheduledThreadPoolExecutor)7 TestThreadLocalContextPropagatorWithHolder (io.github.resilience4j.test.TestContextPropagators.TestThreadLocalContextPropagatorWithHolder)6 Awaitility.matches (com.jayway.awaitility.Awaitility.matches)5 Awaitility.waitAtMost (com.jayway.awaitility.Awaitility.waitAtMost)5 Bulkhead (io.github.resilience4j.bulkhead.Bulkhead)5 BulkheadFullException (io.github.resilience4j.bulkhead.BulkheadFullException)5 Cache (io.github.resilience4j.cache.Cache)5 CallNotPermittedException (io.github.resilience4j.circuitbreaker.CallNotPermittedException)5 CheckedFunction (io.github.resilience4j.core.functions.CheckedFunction)5 CheckedRunnable (io.github.resilience4j.core.functions.CheckedRunnable)5 CheckedSupplier (io.github.resilience4j.core.functions.CheckedSupplier)5 RateLimiter (io.github.resilience4j.ratelimiter.RateLimiter)5 RateLimiterConfig (io.github.resilience4j.ratelimiter.RateLimiterConfig)5 RequestNotPermitted (io.github.resilience4j.ratelimiter.RequestNotPermitted)5 Retry (io.github.resilience4j.retry.Retry)5 AsyncHelloWorldService (io.github.resilience4j.test.AsyncHelloWorldService)5