Skip to content

Commit

Permalink
Reimplement the local context map with context keys
Browse files Browse the repository at this point in the history
  • Loading branch information
vietj committed Feb 28, 2024
1 parent aab89de commit aecfc91
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 33 deletions.
9 changes: 0 additions & 9 deletions src/main/java/io/vertx/core/impl/ContextImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public final class ContextImpl extends ContextBase implements ContextInternal {
private final EventLoop eventLoop;
private final EventExecutor executor;
private ConcurrentMap<Object, Object> data;
private ConcurrentMap<Object, Object> localData;
private volatile Handler<Throwable> exceptionHandler;
final TaskQueue internalOrderedTasks;
final WorkerPool internalWorkerPool;
Expand Down Expand Up @@ -213,14 +212,6 @@ public synchronized ConcurrentMap<Object, Object> contextData() {
return data;
}

@Override
public synchronized ConcurrentMap<Object, Object> localContextData() {
if (localData == null) {
localData = new ConcurrentHashMap<>();
}
return localData;
}

public void reportException(Throwable t) {
Handler<Throwable> handler = exceptionHandler;
if (handler == null) {
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/io/vertx/core/impl/ContextInternal.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.netty.channel.EventLoop;
import io.vertx.codegen.annotations.GenIgnore;
import io.vertx.core.*;
import io.vertx.core.Future;
import io.vertx.core.impl.future.FailedFuture;
import io.vertx.core.impl.future.PromiseImpl;
import io.vertx.core.impl.future.PromiseInternal;
Expand All @@ -22,10 +23,7 @@
import io.vertx.core.spi.context.locals.ContextKey;
import io.vertx.core.spi.tracing.VertxTracer;

import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.*;
import java.util.function.Supplier;

/**
Expand All @@ -37,6 +35,8 @@
*/
public interface ContextInternal extends Context {

ContextKey<ConcurrentMap<Object, Object>> LOCAL_MAP = new ContextKeyImpl<>(0);

/**
* @return the current context
*/
Expand Down Expand Up @@ -308,7 +308,9 @@ default boolean remove(Object key) {
/**
* @return the {@link ConcurrentMap} used to store local context data
*/
ConcurrentMap<Object, Object> localContextData();
default ConcurrentMap<Object, Object> localContextData() {
return getLocal(LOCAL_MAP, ConcurrentHashMap::new);
}

/**
* Get some local data from the context.
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/io/vertx/core/impl/ContextKeyImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@
*/
public class ContextKeyImpl<T> implements ContextKey<T> {

final int index = KeySeq.next();
final int index;

public ContextKeyImpl(int index) {
this.index = index;
}

public ContextKeyImpl() {
this.index = KeySeq.next();
}
}
13 changes: 1 addition & 12 deletions src/main/java/io/vertx/core/impl/DuplicatedContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@
*/
final class DuplicatedContext extends ContextBase implements ContextInternal {

protected final ContextImpl delegate;
private ConcurrentMap<Object, Object> localData;
final ContextImpl delegate;

DuplicatedContext(ContextImpl delegate, Object[] locals) {
super(locals);
Expand Down Expand Up @@ -120,16 +119,6 @@ public final ConcurrentMap<Object, Object> contextData() {
return delegate.contextData();
}

@Override
public final ConcurrentMap<Object, Object> localContextData() {
synchronized (this) {
if (localData == null) {
localData = new ConcurrentHashMap<>();
}
return localData;
}
}

@Override
public <T> Future<T> executeBlockingInternal(Callable<T> action) {
return ContextImpl.executeBlocking(this, action, delegate.internalWorkerPool, delegate.internalOrderedTasks);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/io/vertx/core/impl/KeySeq.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
*/
class KeySeq {

// 0 : reserved slot for local context map
private static final AtomicInteger seq = new AtomicInteger();

/**
* Hook for testing purposes
*/
static void reset() {
seq.set((0));
seq.set((1));
}

static int get() {
Expand Down
5 changes: 0 additions & 5 deletions src/test/java/io/vertx/core/FakeContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,6 @@ public ConcurrentMap<Object, Object> contextData() {
return null;
}

@Override
public ConcurrentMap<Object, Object> localContextData() {
return null;
}

@Override
public ClassLoader classLoader() {
return tccl;
Expand Down

0 comments on commit aecfc91

Please sign in to comment.