WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content
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
20 changes: 0 additions & 20 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,5 @@
# Change Log

## v2.0.1-SNAPSHOT (Unreleased)

### Breaking Changes

- **deps**: Upgrade slf4j-api from 1.7.36 to 2.0.17 for Spring Boot 3.5.0 compatibility
- **deps**: Upgrade logback from 1.2.13 to 1.4.14 for SLF4J 2.0 support
- **deps**: Replace log4j-slf4j-impl with log4j-slf4j2-impl (2.24.3) for SLF4J 2.0 bridge

### Enhancements

- **core**: Implement SLF4J 2.0 new MDCAdapter interface methods (pushByKey, popByKey, clearDequeByKey, getCopyOfDequeByKey) in TrpcMDCAdapter
- **core**: Use reflection to initialize MDC adapter in SLF4J 2.0 compatible way
- **deps**: Ensure all slf4j dependencies are upgraded to 2.0.17 across all modules

### Compatibility

- **java**: Fully compatible with JDK 17
- **spring**: Fully compatible with Spring Boot 3.5.0
- **jakarta**: Aligned with Jakarta EE 10

## v1.1.0 (2023-12-20)

### Features
Expand Down
100 changes: 35 additions & 65 deletions trpc-core/src/main/java/org/slf4j/TrpcMDCAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@

import com.alibaba.ttl.TransmittableThreadLocal;
import com.tencent.trpc.core.utils.StringUtils;
import org.slf4j.spi.MDCAdapter;

import java.util.Deque;
import java.lang.reflect.Field;
import java.util.ArrayDeque;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import org.slf4j.spi.MDCAdapter;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.ConcurrentMap;

/**
* TrpcMDCAdapter
Expand All @@ -42,22 +45,17 @@ public class TrpcMDCAdapter implements MDCAdapter {

private final ThreadLocal<Map<String, String>> copyOnInheritThreadLocal = new TransmittableThreadLocal<>();

private final ThreadLocal<ConcurrentMap<String, ConcurrentLinkedDeque<String>>> threadLocalDequeMap =
ThreadLocal.withInitial(ConcurrentHashMap::new);

/**
* Keeps track of the last operation performed
*/
private final ThreadLocal<Integer> lastOperation = new ThreadLocal<>();

static {
mtcMDCAdapter = new TrpcMDCAdapter();
try {
// In SLF4J 2.0, MDC.mdcAdapter is no longer directly accessible
// We need to use reflection to set it
Field field = MDC.class.getDeclaredField("mdcAdapter");
field.setAccessible(true);
field.set(null, mtcMDCAdapter);
} catch (Exception e) {
throw new RuntimeException("Failed to initialize TrpcMDCAdapter", e);
}
MDC.setMDCAdapter(mtcMDCAdapter);
}

public static MDCAdapter init() {
Expand Down Expand Up @@ -188,77 +186,49 @@ private Map<String, String> duplicateAndInsertNewMap(Map<String, String> oldMap)
return newMap;
}

/**
* Push a context value as identified with the key parameter into the current thread's context map.
* This is a SLF4J 2.0 new method for supporting MDC stack operations.
*
* @param key the key
* @param value the value to push
* @throws NullPointerException in case the "key" parameter is null
*/
@Override
public void pushByKey(String key, String value) {
Objects.requireNonNull(key, "key cannot be null");
Map<String, String> oldMap = copyOnInheritThreadLocal.get();
Integer lastOp = getAndSetLastOperation();
if (wasLastOpReadOrNull(lastOp) || oldMap == null) {
Map<String, String> newMap = duplicateAndInsertNewMap(oldMap);
newMap.put(key, value);
} else {
oldMap.put(key, value);
}
ConcurrentMap<String, ConcurrentLinkedDeque<String>> dequeMap = threadLocalDequeMap.get();
ConcurrentLinkedDeque<String> deque = dequeMap.computeIfAbsent(key, k -> new ConcurrentLinkedDeque<>());
deque.push(value);
}

/**
* Pop the context identified by key parameter from the current thread's context map.
* This is a SLF4J 2.0 new method for supporting MDC stack operations.
*
* @param key the key
* @return the value removed, or null if there was no value for the key
* @throws NullPointerException in case the "key" parameter is null
*/
@Override
public String popByKey(String key) {
Objects.requireNonNull(key, "key cannot be null");
Map<String, String> oldMap = copyOnInheritThreadLocal.get();
if (oldMap == null) {
ConcurrentMap<String, ConcurrentLinkedDeque<String>> dequeMap = threadLocalDequeMap.get();
ConcurrentLinkedDeque<String> deque = dequeMap.get(key);
if (deque == null || deque.isEmpty()) {
return null;
}
Integer lastOp = getAndSetLastOperation();
if (wasLastOpReadOrNull(lastOp)) {
Map<String, String> newMap = duplicateAndInsertNewMap(oldMap);
return newMap.remove(key);
} else {
return oldMap.remove(key);
String value = deque.pollFirst();
if (deque.isEmpty()) {
dequeMap.remove(key);
}
return value;
}

/**
* Clear all the entries in the deque identified by the key parameter.
* This is a SLF4J 2.0.7+ new method for clearing MDC stacks.
*
* @param key the key
* @throws NullPointerException in case the "key" parameter is null
*/
@Override
public void clearDequeByKey(String key) {
Objects.requireNonNull(key, "key cannot be null");
remove(key);
public Deque<String> getCopyOfDequeByKey(String key) {
if (key == null) {
return null;
}
ConcurrentMap<String, ConcurrentLinkedDeque<String>> dequeMap = threadLocalDequeMap.get();
ConcurrentLinkedDeque<String> deque = dequeMap.get(key);
if (deque == null) {
return null;
}
return new ArrayDeque<>(deque);
}

/**
* Get a copy of the deque identified by the key parameter.
* This is a SLF4J 2.0 new method for supporting MDC stack operations.
* Since our implementation uses a simple Map, we return null to indicate no deque.
*
* @param key the key
* @return null (this implementation does not support deques)
*/
@Override
public Deque<String> getCopyOfDequeByKey(String key) {
// This implementation uses a simple Map structure, not Deque
// Return null to indicate no deque exists for the key
return null;
public void clearDequeByKey(String key) {
if (key == null) {
return;
}
ConcurrentMap<String, ConcurrentLinkedDeque<String>> dequeMap = threadLocalDequeMap.get();
dequeMap.remove(key);
}

}
Loading