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: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
# 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
2 changes: 1 addition & 1 deletion trpc-code-generator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<exclusions>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<artifactId>log4j-slf4j2-impl</artifactId>
</exclusion>
</exclusions>
</dependency>
Expand Down
2 changes: 1 addition & 1 deletion trpc-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<artifactId>log4j-slf4j2-impl</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
Expand Down
89 changes: 86 additions & 3 deletions trpc-core/src/main/java/org/slf4j/TrpcMDCAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

import com.alibaba.ttl.TransmittableThreadLocal;
import com.tencent.trpc.core.utils.StringUtils;
import java.util.Deque;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -47,7 +49,15 @@ public class TrpcMDCAdapter implements MDCAdapter {

static {
mtcMDCAdapter = new TrpcMDCAdapter();
MDC.mdcAdapter = mtcMDCAdapter;
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);
}
}

public static MDCAdapter init() {
Expand Down Expand Up @@ -143,9 +153,9 @@ public Map<String, String> getCopyOfContextMap() {

@SuppressWarnings("unchecked")
@Override
public void setContextMap(Map contextMap) {
public void setContextMap(Map<String, String> contextMap) {
lastOperation.set(WRITE_OPERATION);
Map<String, String> newMap = new ConcurrentHashMap<String, String>(contextMap);
Map<String, String> newMap = new ConcurrentHashMap<>(contextMap);
// the newMap replaces the old one for serialisation's sake
copyOnInheritThreadLocal.set(newMap);
}
Expand Down Expand Up @@ -178,4 +188,77 @@ 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);
}
}

/**
* 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) {
return null;
}
Integer lastOp = getAndSetLastOperation();
if (wasLastOpReadOrNull(lastOp)) {
Map<String, String> newMap = duplicateAndInsertNewMap(oldMap);
return newMap.remove(key);
} else {
return oldMap.remove(key);
}
}

/**
* 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);
}

/**
* 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;
}

}
4 changes: 2 additions & 2 deletions trpc-dependencies/trpc-dependencies-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
<javassist.version>3.28.0-GA</javassist.version>
<javax.servlet-api.version>3.1.0</javax.servlet-api.version>
<jsr305.version>3.0.2</jsr305.version>
<logback.version>1.2.13</logback.version>
<logback.version>1.4.14</logback.version>
<logging.log4j.version>2.24.3</logging.log4j.version>
<log4j2.ttl.thread.context.map.version>1.3.3</log4j2.ttl.thread.context.map.version>
<maven.gpg.version>3.1.0</maven.gpg.version>
Expand Down Expand Up @@ -137,7 +137,7 @@

<sentinel-transport-simple-http.version>1.8.6</sentinel-transport-simple-http.version>
<sentinel.version>1.8.6</sentinel.version>
<slf4j.version>1.7.36</slf4j.version>
<slf4j.version>2.0.17</slf4j.version>
<snakeyaml.version>2.0</snakeyaml.version>
<snappy.version>1.1.10.4</snappy.version>
<spring.version>6.2.7</spring.version>
Expand Down
2 changes: 1 addition & 1 deletion trpc-logger/trpc-logger-admin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<artifactId>log4j-slf4j2-impl</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
Expand Down
2 changes: 1 addition & 1 deletion trpc-spring-boot-starters/trpc-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<artifactId>log4j-slf4j-impl</artifactId>
<artifactId>log4j-slf4j2-impl</artifactId>
<groupId>org.apache.logging.log4j</groupId>
</exclusion>
<exclusion>
Expand Down
Loading