use of io.github.resilience4j.core.ContextPropagator in project resilience4j by resilience4j.
the class HedgeImplCompletionStageBehaviorsTest method shouldUsePropagatorsCorrectly.
@Test
public void shouldUsePropagatorsCorrectly() {
HedgeBehaviorSpecification[] hedgeBehaviorSpecifications = { SLOW_PRIMARY_SUCCESS, FAST_HEDGE_SUCCESS };
ContextPropagator propagator = new TestContextPropagators.TestThreadLocalContextPropagator(threadLocal);
HedgeConfig config = HedgeConfig.custom().preconfiguredDuration(FIFTY_MILLIS).withContextPropagators(propagator).build();
threadLocal.set("LOCAL CONTEXT IS NOW SET");
// If the standard ScheduledThreadPoolExecutor is used to execute the code,
// the propagators don't have any context to propagate because it is already
// lost from the driver thread to the primary executor. Switch the executors in the next two lines.
// ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(10);
ScheduledThreadPoolExecutor executor = ContextAwareScheduledThreadPoolExecutor.newScheduledThreadPool().contextPropagators(propagator).corePoolSize(10).build();
Supplier<CompletableFuture<String>> futureSupplier = hedgingCompletionStage(hedgeBehaviorSpecifications, executor);
Hedge hedge = Hedge.of(config);
hedge.getEventPublisher().onSecondarySuccess(event -> logger.info(event.getEventType().toString()));
CompletableFuture<String> hedged = (CompletableFuture<String>) hedge.decorateCompletionStage(futureSupplier).get();
}
use of io.github.resilience4j.core.ContextPropagator in project resilience4j by resilience4j.
the class BulkHeadConfigurationTest method testCreateThreadPoolBulkHeadRegistryWithSharedConfigs.
@Test
public void testCreateThreadPoolBulkHeadRegistryWithSharedConfigs() {
// Given
ThreadPoolBulkheadConfigurationProperties.InstanceProperties defaultProperties = new ThreadPoolBulkheadConfigurationProperties.InstanceProperties();
defaultProperties.setCoreThreadPoolSize(1);
defaultProperties.setQueueCapacity(1);
defaultProperties.setKeepAliveDuration(Duration.ofNanos(5));
defaultProperties.setMaxThreadPoolSize(10);
ThreadPoolBulkheadConfigurationProperties.InstanceProperties sharedProperties = new ThreadPoolBulkheadConfigurationProperties.InstanceProperties();
sharedProperties.setCoreThreadPoolSize(2);
sharedProperties.setQueueCapacity(2);
sharedProperties.setContextPropagators(TestThreadLocalContextPropagator.class);
ThreadPoolBulkheadConfigurationProperties.InstanceProperties backendWithDefaultConfig = new ThreadPoolBulkheadConfigurationProperties.InstanceProperties();
backendWithDefaultConfig.setBaseConfig("default");
backendWithDefaultConfig.setCoreThreadPoolSize(3);
ThreadPoolBulkheadConfigurationProperties.InstanceProperties backendWithSharedConfig = new ThreadPoolBulkheadConfigurationProperties.InstanceProperties();
backendWithSharedConfig.setBaseConfig("sharedConfig");
backendWithSharedConfig.setCoreThreadPoolSize(4);
backendWithSharedConfig.setContextPropagators(TestThreadLocalContextPropagator.class);
ThreadPoolBulkheadConfigurationProperties bulkheadConfigurationProperties = new ThreadPoolBulkheadConfigurationProperties();
bulkheadConfigurationProperties.getConfigs().put("default", defaultProperties);
bulkheadConfigurationProperties.getConfigs().put("sharedConfig", sharedProperties);
bulkheadConfigurationProperties.getBackends().put("backendWithDefaultConfig", backendWithDefaultConfig);
bulkheadConfigurationProperties.getBackends().put("backendWithSharedConfig", backendWithSharedConfig);
ThreadPoolBulkheadConfiguration threadPoolBulkheadConfiguration = new ThreadPoolBulkheadConfiguration();
DefaultEventConsumerRegistry<BulkheadEvent> eventConsumerRegistry = new DefaultEventConsumerRegistry<>();
// When
try {
ThreadPoolBulkheadRegistry bulkheadRegistry = threadPoolBulkheadConfiguration.threadPoolBulkheadRegistry(bulkheadConfigurationProperties, eventConsumerRegistry, new CompositeRegistryEventConsumer<>(emptyList()), new CompositeCustomizer<>(Collections.emptyList()));
// Then
assertThat(bulkheadRegistry.getAllBulkheads().size()).isEqualTo(2);
// Should get default config and core number
ThreadPoolBulkhead bulkhead1 = bulkheadRegistry.bulkhead("backendWithDefaultConfig");
assertThat(bulkhead1).isNotNull();
assertThat(bulkhead1.getBulkheadConfig().getCoreThreadPoolSize()).isEqualTo(3);
assertThat(bulkhead1.getBulkheadConfig().getQueueCapacity()).isEqualTo(1);
assertThat(bulkhead1.getBulkheadConfig().getContextPropagator()).isNotNull();
assertThat(bulkhead1.getBulkheadConfig().getContextPropagator()).isEmpty();
// Should get shared config and overwrite core number
ThreadPoolBulkhead bulkhead2 = bulkheadRegistry.bulkhead("backendWithSharedConfig");
assertThat(bulkhead2).isNotNull();
assertThat(bulkhead2.getBulkheadConfig().getCoreThreadPoolSize()).isEqualTo(4);
assertThat(bulkhead2.getBulkheadConfig().getQueueCapacity()).isEqualTo(2);
assertThat(bulkhead2.getBulkheadConfig().getContextPropagator()).isNotNull();
assertThat(bulkhead2.getBulkheadConfig().getContextPropagator().size()).isEqualTo(2);
List<Class<? extends ContextPropagator>> ctxPropagators = bulkhead2.getBulkheadConfig().getContextPropagator().stream().map(ctx -> ctx.getClass()).collect(Collectors.toList());
assertThat(ctxPropagators).containsExactlyInAnyOrder(TestThreadLocalContextPropagator.class, TestThreadLocalContextPropagator.class);
// Unknown backend should get default config of Registry
ThreadPoolBulkhead bulkhead3 = bulkheadRegistry.bulkhead("unknownBackend");
assertThat(bulkhead3).isNotNull();
assertThat(bulkhead3.getBulkheadConfig().getCoreThreadPoolSize()).isEqualTo(1);
assertThat(bulkhead3.getBulkheadConfig().getContextPropagator()).isNotNull();
assertThat(bulkhead3.getBulkheadConfig().getContextPropagator()).isEmpty();
assertThat(eventConsumerRegistry.getAllEventConsumer()).hasSize(3);
} catch (Exception e) {
System.out.println("exception in testCreateThreadPoolBulkHeadRegistryWithSharedConfigs():" + e);
}
}
Aggregations