Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creates FT executor using our ThreadPoolSupplier to ensure context propagation #9555

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2023 Oracle and/or its affiliates.
* Copyright (c) 2020, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,11 +20,11 @@
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;

import io.helidon.common.LazyValue;
import io.helidon.common.configurable.ThreadPoolSupplier;
import io.helidon.config.Config;

import static java.lang.System.Logger.Level.ERROR;
Expand Down Expand Up @@ -54,9 +54,11 @@ public final class FaultTolerance {
private static final AtomicReference<Config> CONFIG = new AtomicReference<>(Config.empty());

static {
EXECUTOR.set(LazyValue.create(() -> Executors.newThreadPerTaskExecutor(Thread.ofVirtual()
.name("helidon-ft-", 0)
.factory())));
EXECUTOR.set(LazyValue.create(() -> ThreadPoolSupplier.builder()
.threadNamePrefix("helidon-ft-")
.virtualThreads(true)
.build()
.get()));
}

private FaultTolerance() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
* Copyright (c) 2022, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,9 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

import io.helidon.common.context.Context;
import io.helidon.common.context.Contexts;

import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.endsWith;
Expand Down Expand Up @@ -61,6 +64,24 @@ void testThreadName() throws Exception {
assertThat(threadName, endsWith(": async"));
}

@Test
void testContextPropagation() throws Exception {
Context context = Context.create();
CompletableFuture<Context> cf = new CompletableFuture<>();
Contexts.runInContext(context, () -> {
try {
Async async = Async.create();
async.invoke(() -> {
cf.complete(Contexts.context().orElse(null));
return null;
});
} catch (Exception e) {
throw new RuntimeException(e);
}
});
assertThat(cf.get(WAIT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS), is(context));
}

private Thread testAsync(Async async) {
try {
CompletableFuture<Thread> cf = new CompletableFuture<>();
Expand Down