Skip to content

Commit

Permalink
♻️ refactor or remove TtlMDCAdapter #187
Browse files Browse the repository at this point in the history
  • Loading branch information
trydofor committed Jan 16, 2024
1 parent 21a3eff commit e3cc668
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 203 deletions.
1 change: 1 addition & 0 deletions WingsBoot.t.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Use `t.md` as local [Test Management](https://www.jetbrains.com/help/idea/test-m
* 11027 WingsEnabledDefaultTest: default enabled config and bean
* 11028 WingsEnabledFalseTest: disable config and bean
* 11029 WingsEnabledTopFalseTest: disable top config
* 11030 TtlMDCAdapterTest: ttl MDC in multiple thread

## 12 Faceless

Expand Down
198 changes: 0 additions & 198 deletions wings/silencer-curse/src/main/java/org/slf4j/TtlMDCAdapter.java

This file was deleted.

17 changes: 17 additions & 0 deletions wings/silencer-curse/src/main/java/org/slf4j/TweakMDC.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.slf4j;

import org.jetbrains.annotations.NotNull;
import org.slf4j.spi.MDCAdapter;

/**
* @author trydofor
* @since 2024-01-16
*/
public class TweakMDC {
public static void adapt(@NotNull MDCAdapter mdc) {
// init static block
var ignore = MDC.getMDCAdapter();
// replace current adapter
MDC.mdcAdapter = mdc;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.slf4j.LoggerFactory;
import org.slf4j.TtlMDCAdapter;
import org.slf4j.TweakMDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.logging.LogLevel;
Expand All @@ -18,6 +18,7 @@
import pro.fessional.wings.silencer.spring.boot.ConditionalWingsEnabled;
import pro.fessional.wings.silencer.spring.prop.SilencerEnabledProp;
import pro.fessional.wings.silencer.spring.prop.SilencerTweakProp;
import pro.fessional.wings.silencer.tweak.TtlMDCAdapter;
import pro.fessional.wings.silencer.tweak.TweakLogger;

import java.time.Clock;
Expand Down Expand Up @@ -60,7 +61,7 @@ public void auto(SilencerTweakProp prop,
@Value("${trace:false}") boolean trace
) {
log.info("SilencerCurse spring-auto autowireLogbackTweak, init TtlMDC");
TtlMDCAdapter.initMdc();// init as early as possible
TweakMDC.adapt(new TtlMDCAdapter());// init as early as possible

if (prop.isMdcThreshold()) {
log.info("SilencerCurse spring-conf autowireLogbackTweak WingsMdcThresholdFilter");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package pro.fessional.wings.silencer.tweak;

import com.alibaba.ttl.TransmittableThreadLocal;
import org.jetbrains.annotations.NotNull;
import org.slf4j.spi.MDCAdapter;

import java.util.ArrayDeque;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
* @author trydofor
* @see ch.qos.logback.classic.util.LogbackMDCAdapter
* @since 2022-10-29
*/
public class TtlMDCAdapter implements MDCAdapter {

private final ThreadLocal<Map<String, String>> valueMap = TransmittableThreadLocal.withInitial(HashMap::new);
private final ThreadLocal<Map<String, String>> cacheMap = TransmittableThreadLocal.withInitial(Collections::emptyMap);
private final ThreadLocal<Map<String, Deque<String>>> dequeMap = TransmittableThreadLocal.withInitial(HashMap::new);

@Override
public void put(String key, String val) {
if (key == null) return;
valueMap.get().put(key, val);
clearCache();
}

@Override
public String get(String key) {
return key == null ? null : valueMap.get().get(key);
}

@Override
public void remove(String key) {
if (key == null) return;
valueMap.get().remove(key);
clearCache();
}

@Override
public void clear() {
valueMap.remove();
cacheMap.remove();
}

private void clearCache() {
cacheMap.set(Collections.emptyMap());
}

@NotNull
public Map<String, String> getReadOnlyMap() {
var map = cacheMap.get();
if (map == Collections.EMPTY_MAP) {
map = Map.copyOf(valueMap.get());
cacheMap.set(map);
}
return map;
}

public Map<String, String> getCopyOfContextMap() {
return new HashMap<>(getReadOnlyMap());
}

public Set<String> getKeys() {
return getReadOnlyMap().keySet();
}

public void setContextMap(Map<String, String> ctx) {
Map<String, String> map = ctx == null ? new HashMap<>() : new HashMap<>(ctx);
valueMap.set(map);
clearCache();
}


@Override
public void pushByKey(String key, String value) {
if (key != null) {
dequeMap.get()
.computeIfAbsent(key, k -> new ArrayDeque<>())
.push(value);
}
}

@Override
public String popByKey(String key) {
if (key == null) return null;

var deque = dequeMap.get().get(key);
return deque == null ? null : deque.pop();
}

@Override
public Deque<String> getCopyOfDequeByKey(String key) {
if (key == null) return null;

var deque = dequeMap.get().get(key);
return deque == null ? null : new ArrayDeque<>(deque);
}

@Override
public void clearDequeByKey(String key) {
if (key == null) return;

var deque = dequeMap.get().get(key);
if (deque != null) deque.clear();
}
}
Loading

0 comments on commit e3cc668

Please sign in to comment.