Skip to content

Commit

Permalink
Remove thread sticky context feature.
Browse files Browse the repository at this point in the history
Motivation:

Thread sticky context was introduced in Vert.x 4 in order to maintain callback ordering when submitting tasks from a non vertx thread.

Vert.x 5 introduces the sticky event-loop feature that implements the same idea for shadow contexts.

In addition shadow context references cannot be reasonnably maintained.

Dropping sticky context support does not break the original intent of the feature and brings behavior consistency when deadling with foreign contexts.

Changes:

Drop stick context feature.
  • Loading branch information
vietj committed Jan 8, 2025
1 parent 7fbc36f commit 56a122a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 29 deletions.
6 changes: 0 additions & 6 deletions vertx-core/src/main/java/io/vertx/core/impl/VertxImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ private static ThreadFactory virtualThreadFactory() {
private final Throwable transportUnavailabilityCause;
private final VertxTracer tracer;
private final ThreadLocal<WeakReference<EventLoop>> stickyEventLoop = new ThreadLocal<>();
private final ThreadLocal<WeakReference<ContextInternal>> stickyContext = new ThreadLocal<>();
private final boolean disableTCCL;
private final Boolean useDaemonThread;

Expand Down Expand Up @@ -521,7 +520,6 @@ public void execute(Runnable command) {
} else {
ctx = createEventLoopContext(eventLoop, workerPool, Thread.currentThread().getContextClassLoader());
}
stickyContext.set(new WeakReference<>(ctx));
return ctx;
}
}
Expand Down Expand Up @@ -706,10 +704,6 @@ private ContextInternal getContext(Thread thread) {
return new ShadowContext(this, new EventLoopExecutor(eventLoop), context);
}
} else {
WeakReference<ContextInternal> ref = stickyContext.get();
if (ref != null) {
return ref.get();
}
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -918,9 +918,8 @@ public void testFailedFutureContextPropagation2() {
}

@Test
public void testSticky() {
public void testStickiness() {
Context ctx = vertx.getOrCreateContext();
assertSame(ctx, vertx.getOrCreateContext());
assertSame(((ContextInternal)ctx).nettyEventLoop(), ((ContextInternal)vertx.getOrCreateContext()).nettyEventLoop());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ public void testExecuteTasks() {
toRun.pop().run();
assertEquals(1, cnt[0]);
assertNull(Vertx.currentContext());
// Sticky context
assertSame(ctx, vertx.getOrCreateContext());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,28 +128,28 @@ public void testUnordered() throws Exception {
}

@Test
public void testUseDifferentExecutorWithSameTaskQueue() throws Exception {
public void testUseDifferentExecutorWithSameTaskQueue() {
int count = 10;
waitFor(count);
WorkerExecutor exec = vertx.createSharedWorkerExecutor("vert.x-the-executor");
Thread startThread = Thread.currentThread();
AtomicReference<Thread> currentThread = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(1);
for (int i = 0;i < count;i++) {
int val = i;
exec.executeBlocking(() -> {
Thread current = Thread.currentThread();
assertNotSame(startThread, current);
if (val == 0) {
assertNull(currentThread.getAndSet(current));
awaitLatch(latch);
} else {
assertSame(current, currentThread.get());
}
return null;
}, true).onComplete(onSuccess(v -> complete()));
latch.countDown();
}
vertx.runOnContext(v1 -> {
AtomicReference<Thread> currentThread = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(1);
for (int i = 0;i < count;i++) {
int val = i;
exec.executeBlocking(() -> {
Thread current = Thread.currentThread();
if (val == 0) {
assertNull(currentThread.getAndSet(current));
awaitLatch(latch);
} else {
assertSame(current, currentThread.get());
}
return null;
}, true).onComplete(onSuccess(v2 -> complete()));
latch.countDown();
}
});
await();
}

Expand Down

0 comments on commit 56a122a

Please sign in to comment.