From dc57f9706326fb06b0371df1a55c77812999b2e4 Mon Sep 17 00:00:00 2001 From: Julien Viet Date: Wed, 28 Feb 2024 17:16:06 +0100 Subject: [PATCH] Rework SPI --- .../java/io/vertx/core/impl/ContextBase.java | 16 +-- .../io/vertx/core/impl/ContextInternal.java | 57 ++------ ...textKeyImpl.java => ContextLocalImpl.java} | 8 +- .../core/spi/context/locals/ContextKey.java | 36 ----- .../{locals => storage}/AccessMode.java | 2 +- .../spi/context/storage/ContextLocal.java | 132 ++++++++++++++++++ src/test/java/io/vertx/core/ContextTest.java | 13 +- src/test/java/io/vertx/core/FakeContext.java | 10 +- .../spi/tracing/EventBusTracerTestBase.java | 10 +- .../core/spi/tracing/HttpTracerTestBase.java | 42 +++--- .../java/io/vertx/it/CustomContextKey.java | 4 +- ...yTest.java => CustomContextLocalTest.java} | 2 +- .../io/vertx/test/faketracer/FakeTracer.java | 4 +- 13 files changed, 197 insertions(+), 139 deletions(-) rename src/main/java/io/vertx/core/impl/{ContextKeyImpl.java => ContextLocalImpl.java} (76%) delete mode 100644 src/main/java/io/vertx/core/spi/context/locals/ContextKey.java rename src/main/java/io/vertx/core/spi/context/{locals => storage}/AccessMode.java (98%) create mode 100644 src/main/java/io/vertx/core/spi/context/storage/ContextLocal.java rename src/test/java/io/vertx/it/{CustomContextKeyTest.java => CustomContextLocalTest.java} (87%) diff --git a/src/main/java/io/vertx/core/impl/ContextBase.java b/src/main/java/io/vertx/core/impl/ContextBase.java index 263e63554e5..6d7d65ec76c 100644 --- a/src/main/java/io/vertx/core/impl/ContextBase.java +++ b/src/main/java/io/vertx/core/impl/ContextBase.java @@ -10,8 +10,8 @@ */ package io.vertx.core.impl; -import io.vertx.core.spi.context.locals.AccessMode; -import io.vertx.core.spi.context.locals.ContextKey; +import io.vertx.core.spi.context.storage.AccessMode; +import io.vertx.core.spi.context.storage.ContextLocal; import java.util.function.Supplier; @@ -28,8 +28,8 @@ class ContextBase { this.locals = locals; } - public final T getLocal(ContextKey key, AccessMode accessMode) { - ContextKeyImpl internalKey = (ContextKeyImpl) key; + public final T getLocal(ContextLocal key, AccessMode accessMode) { + ContextLocalImpl internalKey = (ContextLocalImpl) key; int index = internalKey.index; if (index >= locals.length) { throw new IllegalArgumentException(); @@ -38,8 +38,8 @@ public final T getLocal(ContextKey key, AccessMode accessMode) { return (T) res; } - public final T getLocal(ContextKey key, AccessMode accessMode, Supplier initialValueSupplier) { - ContextKeyImpl internalKey = (ContextKeyImpl) key; + public final T getLocal(ContextLocal key, AccessMode accessMode, Supplier initialValueSupplier) { + ContextLocalImpl internalKey = (ContextLocalImpl) key; int index = internalKey.index; if (index >= locals.length) { throw new IllegalArgumentException("Invalid key index: " + index); @@ -48,8 +48,8 @@ public final T getLocal(ContextKey key, AccessMode accessMode, Supplier void putLocal(ContextKey key, AccessMode accessMode, T value) { - ContextKeyImpl internalKey = (ContextKeyImpl) key; + public final void putLocal(ContextLocal key, AccessMode accessMode, T value) { + ContextLocalImpl internalKey = (ContextLocalImpl) key; int index = internalKey.index; if (index >= locals.length) { throw new IllegalArgumentException(); diff --git a/src/main/java/io/vertx/core/impl/ContextInternal.java b/src/main/java/io/vertx/core/impl/ContextInternal.java index 990815c9490..f0964460334 100644 --- a/src/main/java/io/vertx/core/impl/ContextInternal.java +++ b/src/main/java/io/vertx/core/impl/ContextInternal.java @@ -19,8 +19,8 @@ import io.vertx.core.impl.future.PromiseImpl; import io.vertx.core.impl.future.PromiseInternal; import io.vertx.core.impl.future.SucceededFuture; -import io.vertx.core.spi.context.locals.AccessMode; -import io.vertx.core.spi.context.locals.ContextKey; +import io.vertx.core.spi.context.storage.AccessMode; +import io.vertx.core.spi.context.storage.ContextLocal; import io.vertx.core.spi.tracing.VertxTracer; import java.util.concurrent.*; @@ -35,7 +35,7 @@ */ public interface ContextInternal extends Context { - ContextKey> LOCAL_MAP = new ContextKeyImpl<>(0); + ContextLocal> LOCAL_MAP = new ContextLocalImpl<>(0); /** * @return the current context @@ -309,7 +309,7 @@ default boolean remove(Object key) { * @return the {@link ConcurrentMap} used to store local context data */ default ConcurrentMap localContextData() { - return getLocal(LOCAL_MAP, ConcurrentHashMap::new); + return LOCAL_MAP.get(this, ConcurrentHashMap::new); } /** @@ -320,49 +320,10 @@ default ConcurrentMap localContextData() { * @return the local data */ @GenIgnore - default T getLocal(ContextKey key) { + default T getLocal(ContextLocal key) { return getLocal(key, AccessMode.CONCURRENT); } - /** - * Get some local data from the context, when it does not exist the {@code initialValueSupplier} is called to obtain - * the initial value. - * - *

The {@code initialValueSupplier} might be called multiple times when multiple threads call this method concurrently. - * - * @param key the key of the data - * @param initialValueSupplier the supplier of the initial value optionally called - * @param the type of the data - * @return the local data - */ - @GenIgnore - default T getLocal(ContextKey key, Supplier initialValueSupplier) { - return getLocal(key, AccessMode.CONCURRENT, initialValueSupplier); - } - - /** - * Put some local data in the context. - *

- * This can be used to share data between different handlers that share a context - * - * @param key the key of the data - * @param value the data - */ - @GenIgnore - default void putLocal(ContextKey key, T value) { - putLocal(key, AccessMode.CONCURRENT, value); - } - - /** - * Remove some local data from the context. - * - * @param key the key to remove - */ - @GenIgnore - default void removeLocal(ContextKey key) { - putLocal(key, AccessMode.CONCURRENT, null); - } - /** * Get some local data from the context. * @@ -371,7 +332,7 @@ default void removeLocal(ContextKey key) { * @return the local data */ @GenIgnore - T getLocal(ContextKey key, AccessMode accessMode); + T getLocal(ContextLocal key, AccessMode accessMode); /** * Get some local data from the context, when it does not exist the {@code initialValueSupplier} is called to obtain @@ -385,7 +346,7 @@ default void removeLocal(ContextKey key) { * @return the local data */ @GenIgnore - T getLocal(ContextKey key, AccessMode accessMode, Supplier initialValueSupplier); + T getLocal(ContextLocal key, AccessMode accessMode, Supplier initialValueSupplier); /** * Put some local data in the context. @@ -396,7 +357,7 @@ default void removeLocal(ContextKey key) { * @param value the data */ @GenIgnore - void putLocal(ContextKey key, AccessMode accessMode, T value); + void putLocal(ContextLocal key, AccessMode accessMode, T value); /** * Remove some local data from the context. @@ -404,7 +365,7 @@ default void removeLocal(ContextKey key) { * @param key the key to remove */ @GenIgnore - default void removeLocal(ContextKey key, AccessMode accessMode) { + default void removeLocal(ContextLocal key, AccessMode accessMode) { putLocal(key, accessMode, null); } diff --git a/src/main/java/io/vertx/core/impl/ContextKeyImpl.java b/src/main/java/io/vertx/core/impl/ContextLocalImpl.java similarity index 76% rename from src/main/java/io/vertx/core/impl/ContextKeyImpl.java rename to src/main/java/io/vertx/core/impl/ContextLocalImpl.java index 58151d189f9..d33e3b4a651 100644 --- a/src/main/java/io/vertx/core/impl/ContextKeyImpl.java +++ b/src/main/java/io/vertx/core/impl/ContextLocalImpl.java @@ -10,20 +10,20 @@ */ package io.vertx.core.impl; -import io.vertx.core.spi.context.locals.ContextKey; +import io.vertx.core.spi.context.storage.ContextLocal; /** * @author Julien Viet */ -public class ContextKeyImpl implements ContextKey { +public class ContextLocalImpl implements ContextLocal { final int index; - public ContextKeyImpl(int index) { + public ContextLocalImpl(int index) { this.index = index; } - public ContextKeyImpl() { + public ContextLocalImpl() { this.index = KeySeq.next(); } } diff --git a/src/main/java/io/vertx/core/spi/context/locals/ContextKey.java b/src/main/java/io/vertx/core/spi/context/locals/ContextKey.java deleted file mode 100644 index a6bcabc4851..00000000000 --- a/src/main/java/io/vertx/core/spi/context/locals/ContextKey.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2011-2023 Contributors to the Eclipse Foundation - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 - * which is available at https://www.apache.org/licenses/LICENSE-2.0. - * - * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 - */ -package io.vertx.core.spi.context.locals; - -import io.vertx.core.impl.ContextKeyImpl; - -/** - * A context key to address local context data. - * - * @author Julien Viet - */ -public interface ContextKey { - - /** - * Registers a context key. - * - *

Keys should be registered before creating a {@link io.vertx.core.Vertx} instance, once registered a key cannot be unregistered. - * - *

It is recommended to initialize keys as static fields of a {@link io.vertx.core.spi.VertxServiceProvider}, since providers - * are discovered before the capture of known keys. - * - * @param type the type of context data - * @return the context key - */ - static ContextKey registerKey(Class type) { - return new ContextKeyImpl<>(); - } -} diff --git a/src/main/java/io/vertx/core/spi/context/locals/AccessMode.java b/src/main/java/io/vertx/core/spi/context/storage/AccessMode.java similarity index 98% rename from src/main/java/io/vertx/core/spi/context/locals/AccessMode.java rename to src/main/java/io/vertx/core/spi/context/storage/AccessMode.java index 1d10a149e41..d0363b8ec82 100644 --- a/src/main/java/io/vertx/core/spi/context/locals/AccessMode.java +++ b/src/main/java/io/vertx/core/spi/context/storage/AccessMode.java @@ -8,7 +8,7 @@ * * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 */ -package io.vertx.core.spi.context.locals; +package io.vertx.core.spi.context.storage; import java.lang.invoke.MethodHandles; import java.lang.invoke.VarHandle; diff --git a/src/main/java/io/vertx/core/spi/context/storage/ContextLocal.java b/src/main/java/io/vertx/core/spi/context/storage/ContextLocal.java new file mode 100644 index 00000000000..7d3db6a2ac5 --- /dev/null +++ b/src/main/java/io/vertx/core/spi/context/storage/ContextLocal.java @@ -0,0 +1,132 @@ +/* + * Copyright (c) 2011-2023 Contributors to the Eclipse Foundation + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 + * which is available at https://www.apache.org/licenses/LICENSE-2.0. + * + * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 + */ +package io.vertx.core.spi.context.storage; + +import io.vertx.codegen.annotations.GenIgnore; +import io.vertx.core.Context; +import io.vertx.core.impl.ContextInternal; +import io.vertx.core.impl.ContextLocalImpl; + +import java.util.function.Supplier; + +/** + * A context local storage to address local context data. + * + * @author Julien Viet + */ +public interface ContextLocal { + + /** + * Registers a context local. + * + *

Locals should be registered before creating a {@link io.vertx.core.Vertx} instance, once registered a local cannot be unregistered. + * + *

It is recommended to initialize locals as static fields of a {@link io.vertx.core.spi.VertxServiceProvider}, since providers + * are discovered before the capture of known locals. + * + * @param type the type of context data + * @return the context key + */ + static ContextLocal registerLocal(Class type) { + return new ContextLocalImpl<>(); + } + + /** + * Get some local data from the context. + * + * @return the local data + */ + @GenIgnore + default T get(Context context) { + return get(context, AccessMode.CONCURRENT); + } + + /** + * Get some local data from the context, when it does not exist the {@code initialValueSupplier} is called to obtain + * the initial value. + * + *

The {@code initialValueSupplier} might be called multiple times when multiple threads call this method concurrently. + * + * @param initialValueSupplier the supplier of the initial value optionally called + * @return the local data + */ + @GenIgnore + default T get(Context context, Supplier initialValueSupplier) { + return get(context, AccessMode.CONCURRENT, initialValueSupplier); + } + + /** + * Put some local data in the context. + *

+ * This can be used to share data between different handlers that share a context + * + * @param value the data + */ + @GenIgnore + default void put(Context context, T value) { + put(context, AccessMode.CONCURRENT, value); + } + + /** + * Remove some local data from the context. + * + */ + @GenIgnore + default void remove(Context context) { + put(context, AccessMode.CONCURRENT, null); + } + + /** + * Get some local data from the context. + * + * @return the local data + */ + @GenIgnore + default T get(Context context, AccessMode accessMode) { + return ((ContextInternal)context).getLocal(this, accessMode); + } + + /** + * Get some local data from the context, when it does not exist the {@code initialValueSupplier} is called to obtain + * the initial value. + * + *

The {@code initialValueSupplier} might be called multiple times when multiple threads call this method concurrently. + * + * @param initialValueSupplier the supplier of the initial value optionally called + * @return the local data + */ + @GenIgnore + default T get(Context context, AccessMode accessMode, Supplier initialValueSupplier) { + return ((ContextInternal)context).getLocal(this, accessMode, initialValueSupplier); + } + + /** + * Put some local data in the context. + *

+ * This can be used to share data between different handlers that share a context + * + * @param value the data + */ + @GenIgnore + default void put(Context context, AccessMode accessMode, T value) { + ((ContextInternal)context).putLocal(this, accessMode, value); + } + + /** + * Remove some local data from the context. + * + */ + @GenIgnore + default void remove(Context context, AccessMode accessMode) { + put(context, accessMode, null); + } + +} diff --git a/src/test/java/io/vertx/core/ContextTest.java b/src/test/java/io/vertx/core/ContextTest.java index 1faf09da8a9..ec2acfb8ef3 100644 --- a/src/test/java/io/vertx/core/ContextTest.java +++ b/src/test/java/io/vertx/core/ContextTest.java @@ -14,7 +14,8 @@ import io.netty.channel.EventLoop; import io.vertx.core.impl.*; import io.vertx.core.impl.future.PromiseInternal; -import io.vertx.core.spi.context.locals.ContextKey; +import io.vertx.core.spi.context.storage.AccessMode; +import io.vertx.core.spi.context.storage.ContextLocal; import io.vertx.test.core.VertxTestBase; import org.junit.Assume; import org.junit.Test; @@ -39,7 +40,7 @@ */ public class ContextTest extends VertxTestBase { - private ContextKey contextKey; + private ContextLocal contextLocal; private ExecutorService workerExecutor; private ContextInternal createWorkerContext() { @@ -48,7 +49,7 @@ private ContextInternal createWorkerContext() { @Override public void setUp() throws Exception { - contextKey = ContextKey.registerKey(Object.class); + contextLocal = ContextLocal.registerLocal(Object.class); workerExecutor = Executors.newFixedThreadPool(2, r -> new VertxThread(r, "vert.x-worker-thread", true, 10, TimeUnit.SECONDS)); super.setUp(); } @@ -448,9 +449,9 @@ private void checkDuplicate(ContextInternal ctx, ContextInternal duplicated) thr Object shared = new Object(); Object local = new Object(); ctx.put("key", shared); - ctx.putLocal(contextKey, local); + ctx.putLocal(contextLocal, local); assertSame(shared, duplicated.get("key")); - assertNull(duplicated.getLocal(contextKey)); + assertNull(duplicated.getLocal(contextLocal)); assertTrue(duplicated.remove("key")); assertNull(ctx.get("key")); @@ -1076,7 +1077,7 @@ public void testConcurrentLocalAccess() throws Exception { } catch (Exception e) { return; } - values[val] = (int)ctx.getLocal(contextKey, supplier); + values[val] = (int)ctx.getLocal(contextLocal, AccessMode.CONCURRENT, supplier); }); } for (int i = 0;i < numThreads;i++) { diff --git a/src/test/java/io/vertx/core/FakeContext.java b/src/test/java/io/vertx/core/FakeContext.java index 75b5cc860d8..548ed33fd9b 100644 --- a/src/test/java/io/vertx/core/FakeContext.java +++ b/src/test/java/io/vertx/core/FakeContext.java @@ -10,8 +10,8 @@ import io.vertx.core.impl.VertxInternal; import io.vertx.core.impl.WorkerPool; import io.vertx.core.json.JsonObject; -import io.vertx.core.spi.context.locals.AccessMode; -import io.vertx.core.spi.context.locals.ContextKey; +import io.vertx.core.spi.context.storage.AccessMode; +import io.vertx.core.spi.context.storage.ContextLocal; import io.vertx.core.spi.tracing.VertxTracer; import java.util.concurrent.Callable; @@ -168,17 +168,17 @@ public CloseFuture closeFuture() { } @Override - public T getLocal(ContextKey key, AccessMode accessMode) { + public T getLocal(ContextLocal key, AccessMode accessMode) { return null; } @Override - public T getLocal(ContextKey key, AccessMode accessMode, Supplier initialValueSupplier) { + public T getLocal(ContextLocal key, AccessMode accessMode, Supplier initialValueSupplier) { return null; } @Override - public void putLocal(ContextKey key, AccessMode accessMode, T value) { + public void putLocal(ContextLocal key, AccessMode accessMode, T value) { } } diff --git a/src/test/java/io/vertx/core/spi/tracing/EventBusTracerTestBase.java b/src/test/java/io/vertx/core/spi/tracing/EventBusTracerTestBase.java index 8d2fcef9a51..267f6989166 100644 --- a/src/test/java/io/vertx/core/spi/tracing/EventBusTracerTestBase.java +++ b/src/test/java/io/vertx/core/spi/tracing/EventBusTracerTestBase.java @@ -16,7 +16,7 @@ import io.vertx.core.eventbus.Message; import io.vertx.core.eventbus.ReplyException; import io.vertx.core.impl.ContextKeyHelper; -import io.vertx.core.spi.context.locals.ContextKey; +import io.vertx.core.spi.context.storage.ContextLocal; import io.vertx.core.tracing.TracingPolicy; import io.vertx.test.core.VertxTestBase; import org.junit.Test; @@ -28,16 +28,16 @@ public abstract class EventBusTracerTestBase extends VertxTestBase { - ContextKey receiveKey; - ContextKey sendKey; + ContextLocal receiveKey; + ContextLocal sendKey; VertxTracer tracer; Vertx vertx1; Vertx vertx2; @Override public void setUp() throws Exception { - receiveKey = ContextKey.registerKey(Object.class); - sendKey = ContextKey.registerKey(Object.class); + receiveKey = ContextLocal.registerLocal(Object.class); + sendKey = ContextLocal.registerLocal(Object.class); super.setUp(); } diff --git a/src/test/java/io/vertx/core/spi/tracing/HttpTracerTestBase.java b/src/test/java/io/vertx/core/spi/tracing/HttpTracerTestBase.java index e9092061c98..7ac6f1c0cad 100644 --- a/src/test/java/io/vertx/core/spi/tracing/HttpTracerTestBase.java +++ b/src/test/java/io/vertx/core/spi/tracing/HttpTracerTestBase.java @@ -18,7 +18,7 @@ import io.vertx.core.http.RequestOptions; import io.vertx.core.impl.ContextInternal; import io.vertx.core.impl.ContextKeyHelper; -import io.vertx.core.spi.context.locals.ContextKey; +import io.vertx.core.spi.context.storage.ContextLocal; import io.vertx.core.spi.observability.HttpRequest; import io.vertx.core.spi.observability.HttpResponse; import io.vertx.core.tracing.TracingPolicy; @@ -32,11 +32,11 @@ public abstract class HttpTracerTestBase extends HttpTestBase { private VertxTracer tracer; - private ContextKey key; + private ContextLocal key; @Override public void setUp() throws Exception { - key = ContextKey.registerKey(Object.class); + key = ContextLocal.registerLocal(Object.class); super.setUp(); } @@ -58,8 +58,8 @@ public void testHttpServer() throws Exception { setTracer(new VertxTracer() { @Override public Object receiveRequest(Context context, SpanKind kind, TracingPolicy policy, Object request, String operation, Iterable headers, TagExtractor tagExtractor) { - assertNull(context.getLocal(key)); - context.putLocal(key, val); + assertNull(key.get(context)); + key.put(context, val); assertTrue(seq.compareAndSet(0, 1)); return request; } @@ -69,17 +69,17 @@ public void sendResponse(Context context, Object response, Object payload, Throw assertNotNull(response); assertTrue(response instanceof HttpServerResponse); assertNull(failure); - assertSame(val, context.getLocal(key)); - context.removeLocal(key); + assertSame(val, key.get(context)); + key.remove(context); } }); CountDownLatch latch = new CountDownLatch(1); server.requestHandler(req -> { assertEquals(1, seq.get()); ContextInternal ctx = (ContextInternal) Vertx.currentContext(); - assertSame(val, ctx.getLocal(key)); + assertSame(val, key.get(ctx)); req.response().closeHandler(v -> { - assertNull(ctx.getLocal(key)); + assertNull(key.get(ctx)); assertEquals(2, seq.get()); }); req.response().end(); @@ -103,8 +103,8 @@ public void testHttpServerError() throws Exception { setTracer(new VertxTracer() { @Override public Object receiveRequest(Context context, SpanKind kind, TracingPolicy policy, Object request, String operation, Iterable headers, TagExtractor tagExtractor) { - assertNull(context.getLocal(key)); - context.putLocal(key, val); + assertNull(key.get(context)); + key.put(context, val); assertTrue(seq.compareAndSet(0, 1)); return request; } @@ -120,7 +120,7 @@ public void sendResponse(Context context, Object response, Object payload, Throw server.requestHandler(req -> { assertEquals(1, seq.get()); ContextInternal ctx = (ContextInternal) Vertx.currentContext(); - assertSame(val, ctx.getLocal(key)); + assertSame(val, key.get(ctx)); req.exceptionHandler(v -> { // assertNull(ctx.localContextData().get(key)); // assertEquals(2, seq.get()); @@ -163,7 +163,7 @@ private void testHttpClientRequest(RequestOptions request, String expectedOperat setTracer(new VertxTracer() { @Override public Object sendRequest(Context context, SpanKind kind, TracingPolicy policy, Object request, String operation, BiConsumer headers, TagExtractor tagExtractor) { - assertSame(val, context.getLocal(key)); + assertSame(val, key.get(context)); assertTrue(seq.compareAndSet(0, 1)); headers.accept("X-B3-TraceId", traceId); assertNotNull(request); @@ -173,8 +173,8 @@ public Object sendRequest(Context context, SpanKind kind, TracingPolicy policy, } @Override public void receiveResponse(Context context, Object response, Object payload, Throwable failure, TagExtractor tagExtractor) { - assertSame(val, context.getLocal(key)); - context.removeLocal(key); + assertSame(val, key.get(context)); + key.remove(context); assertNotNull(response); assertTrue(response instanceof HttpResponse); assertNull(failure); @@ -191,14 +191,14 @@ public void receiveResponse(Context context, Object response, Object payload, Th awaitLatch(latch); Context ctx = vertx.getOrCreateContext(); ctx.runOnContext(v1 -> { - ctx.putLocal(key, val); + key.put(ctx, val); client.request(request).onComplete(onSuccess(req -> { req.send().onComplete(onSuccess(resp -> { resp.endHandler(v2 -> { // Updates are done on the HTTP client context, so we need to run task on this context // to avoid data race ctx.runOnContext(v -> { - assertNull(ctx.getLocal(key)); + assertNull(key.get(ctx)); testComplete(); }); }); @@ -217,15 +217,15 @@ public void testHttpClientError() throws Exception { setTracer(new VertxTracer() { @Override public Object sendRequest(Context context, SpanKind kind, TracingPolicy policy, Object request, String operation, BiConsumer headers, TagExtractor tagExtractor) { - assertSame(val, context.getLocal(key)); + assertSame(val, key.get(context)); assertTrue(seq.compareAndSet(0, 1)); headers.accept("X-B3-TraceId", traceId); return request; } @Override public void receiveResponse(Context context, Object response, Object payload, Throwable failure, TagExtractor tagExtractor) { - assertSame(val, context.getLocal(key)); - context.removeLocal(key); + assertSame(val, key.get(context)); + key.remove(context); assertNull(response); assertNotNull(failure); assertTrue(seq.compareAndSet(1, 2)); @@ -242,7 +242,7 @@ public void receiveResponse(Context context, Object response, Object payload, Th awaitLatch(latch); Context ctx = vertx.getOrCreateContext(); ctx.runOnContext(v1 -> { - ctx.putLocal(key, val); + key.put(ctx, val); client.request(HttpMethod.GET, DEFAULT_HTTP_PORT, "localhost", "/").onComplete(onSuccess(req -> { req.send().onComplete(onFailure(err -> { // assertNull(tracerMap.get(key)); diff --git a/src/test/java/io/vertx/it/CustomContextKey.java b/src/test/java/io/vertx/it/CustomContextKey.java index fc95722172b..26056d039d9 100644 --- a/src/test/java/io/vertx/it/CustomContextKey.java +++ b/src/test/java/io/vertx/it/CustomContextKey.java @@ -2,11 +2,11 @@ import io.vertx.core.impl.VertxBuilder; import io.vertx.core.spi.VertxServiceProvider; -import io.vertx.core.spi.context.locals.ContextKey; +import io.vertx.core.spi.context.storage.ContextLocal; public class CustomContextKey implements VertxServiceProvider { - public static ContextKey CUSTOM_KEY = ContextKey.registerKey(Object.class); + public static ContextLocal CUSTOM_KEY = ContextLocal.registerLocal(Object.class); public static volatile boolean initialized; @Override diff --git a/src/test/java/io/vertx/it/CustomContextKeyTest.java b/src/test/java/io/vertx/it/CustomContextLocalTest.java similarity index 87% rename from src/test/java/io/vertx/it/CustomContextKeyTest.java rename to src/test/java/io/vertx/it/CustomContextLocalTest.java index a336f12c13d..1f12b576dbf 100644 --- a/src/test/java/io/vertx/it/CustomContextKeyTest.java +++ b/src/test/java/io/vertx/it/CustomContextLocalTest.java @@ -4,7 +4,7 @@ import io.vertx.test.core.VertxTestBase; import org.junit.Test; -public class CustomContextKeyTest extends VertxTestBase { +public class CustomContextLocalTest extends VertxTestBase { @Test public void testResolver() { diff --git a/src/test/java/io/vertx/test/faketracer/FakeTracer.java b/src/test/java/io/vertx/test/faketracer/FakeTracer.java index 039e9192c05..548a9fc5c71 100644 --- a/src/test/java/io/vertx/test/faketracer/FakeTracer.java +++ b/src/test/java/io/vertx/test/faketracer/FakeTracer.java @@ -14,7 +14,7 @@ import io.vertx.core.Context; import io.vertx.core.Vertx; import io.vertx.core.impl.ContextKeyHelper; -import io.vertx.core.spi.context.locals.ContextKey; +import io.vertx.core.spi.context.storage.ContextLocal; import io.vertx.core.spi.tracing.SpanKind; import io.vertx.core.spi.tracing.TagExtractor; import io.vertx.core.spi.tracing.VertxTracer; @@ -32,7 +32,7 @@ */ public class FakeTracer implements VertxTracer { - private final ContextKey scopeKey = ContextKey.registerKey(Scope.class); + private final ContextLocal scopeKey = ContextLocal.registerLocal(Scope.class); private AtomicInteger idGenerator = new AtomicInteger(0); List finishedSpans = new CopyOnWriteArrayList<>(); private AtomicInteger closeCount = new AtomicInteger();