下面列出了org.slf4j.MDC#clear ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void run() {
try {
MDC.put("grabID", LogIdGenerator.genGrabTransactionID(vsCrawlerContext.getCrawlerName()));
activeTasks.incrementAndGet();
// 为了性能,不打印json
log.info("handle seed: {}", seed.getData());
processSeed(seed);
} catch (Exception e) {
log.error("process request {} error", JSONViewWrapper.wrap(seed), e);
} finally {
if (activeTasks.decrementAndGet() < threadPool.getMaximumPoolSize()) {
activeDispatchThread();
}
MDC.clear();
}
}
@Test
public void mdc() throws NoSuchMethodException {
// given
listAppender.list.clear();
// when
invoke(() -> example.outer(), DEBUG);
// then
String expected = "```\n" +
"DEBUG [] r.t.eclair.example.Example.outer >\n" +
"DEBUG [beanReference={\"i\":0,\"s\":null}, sum=2, static=string, staticMethod=01234567-89ab-cdef-ghij-klmnopqrstuv] r.t.eclair.example.Example.mdc >\n" +
"DEBUG [beanReference={\"i\":0,\"s\":null}, sum=2, static=string, staticMethod=01234567-89ab-cdef-ghij-klmnopqrstuv] r.t.eclair.example.Example.inner >\n" +
"DEBUG [beanReference={\"i\":0,\"s\":null}, sum=2, static=string, staticMethod=01234567-89ab-cdef-ghij-klmnopqrstuv] r.t.eclair.example.Example.mdc <\n" +
"DEBUG [sum=2] r.t.eclair.example.Example.outer <\n" +
"```";
String actual = exampleCodeBuilder.buildMultilineBlock(listAppender.list);
MDC.clear();
listAppender.list.clear();
System.out.println(actual);
assertEquals(expected, actual);
}
@Test
public void testResponseTimeIn() throws JSONObjectException, IOException {
MDC.clear();
String layer = "testResponseTimeIn";
rrec = new RequestRecord(layer);
long start = rrec.start();
doWait(150);
long end = rrec.stop();
logger.info(Markers.REQUEST_MARKER, rrec.toString());
assertThat(getField(Fields.LAYER), is(layer));
assertThat(getField(Fields.DIRECTION), is(Direction.IN.toString()));
assertThat(Double.valueOf(getField(Fields.RESPONSE_TIME_MS)).longValue(), lessThanOrEqualTo(Double.valueOf(end -
start)
.longValue()));
assertThat(getField(Fields.RESPONSE_SENT_AT), not(nullValue()));
assertThat(getField(Fields.REQUEST_RECEIVED_AT), not(nullValue()));
assertThat(getField(Fields.WRITTEN_TS), is(notNullValue()));
}
@Test
public void mdcByDefault() throws NoSuchMethodException {
// given
listAppender.list.clear();
String first = "content";
Double second = Math.PI;
// when
invoke(() -> example.mdcByDefault(first, second), DEBUG);
// then
String expected = "```\n" +
"DEBUG [first=content, second=3.141592653589793] r.t.e.example.Example.mdcByDefault > first=\"content\", second=3.141592653589793\n" +
"```";
String actual = exampleCodeBuilder.buildMultilineBlock(listAppender.list);
MDC.clear();
listAppender.list.clear();
System.out.println(actual);
assertEquals(expected, actual);
}
@Override
public T call() throws Exception {
try {
threadLocalState.propagate();
if (currentMdcState != null) {
MDC.setContextMap(currentMdcState);
}
return wrapped.call();
} finally {
threadLocalState.cleanup();
MDC.clear();
}
}
@BeforeEach
void setUp(TestInfo testInfo) {
rootLogger.getLoggerContext().getStatusManager().clear();
MDC.clear();
testLogger = (Logger) LoggerFactory.getLogger("loggerTest." + testInfo.getDisplayName());
testLogger.setLevel(Level.ALL);
}
@Test
void getAll() {
final ServiceRequestContext ctx = newContext();
MDC.put("foo", "1");
MDC.put("bar", "2");
RequestScopedMdc.put(ctx, "bar", "3");
RequestScopedMdc.put(ctx, "baz", "4");
assertThat(MDC.getCopyOfContextMap()).containsOnly(
Maps.immutableEntry("foo", "1"),
Maps.immutableEntry("bar", "2"));
assertThat(RequestScopedMdc.getAll(ctx)).containsOnly(
Maps.immutableEntry("bar", "3"),
Maps.immutableEntry("baz", "4"));
try (SafeCloseable ignored = ctx.push()) {
// The case where thread-local and request-scoped maps are both non-empty.
assertThat(MDC.getCopyOfContextMap()).containsOnly(
Maps.immutableEntry("foo", "1"),
Maps.immutableEntry("bar", "3"),
Maps.immutableEntry("baz", "4"));
// The case where only request-scoped map is available.
MDC.clear();
assertThat(MDC.getCopyOfContextMap()).containsOnly(
Maps.immutableEntry("bar", "3"),
Maps.immutableEntry("baz", "4"));
// The case where thread-local and request-scoped maps are both empty.
RequestScopedMdc.clear(ctx);
assertThat(MDC.getCopyOfContextMap()).isIn(Collections.emptyMap(), null);
// The case where only thread-local map is available.
MDC.put("qux", "5");
assertThat(MDC.getCopyOfContextMap()).containsOnly(
Maps.immutableEntry("qux", "5"));
}
}
/**
* Stop the Jetty server
*
* @throws Exception if an error occurs on shutdown
*/
public void stop() throws Exception {
// Do not let Jetty/Solr pollute the MDC for this thread
Map<String,String> prevContext = MDC.getCopyOfContextMap();
MDC.clear();
try {
Filter filter = dispatchFilter.getFilter();
// we want to shutdown outside of jetty cutting us off
SolrDispatchFilter sdf = getSolrDispatchFilter();
ExecutorService customThreadPool = null;
if (sdf != null) {
customThreadPool = ExecutorUtil.newMDCAwareCachedThreadPool(new SolrNamedThreadFactory("jettyShutDown"));
sdf.closeOnDestroy(false);
// customThreadPool.submit(() -> {
// try {
// sdf.close();
// } catch (Throwable t) {
// log.error("Error shutting down Solr", t);
// }
// });
try {
sdf.close();
} catch (Throwable t) {
log.error("Error shutting down Solr", t);
}
}
QueuedThreadPool qtp = (QueuedThreadPool) server.getThreadPool();
ReservedThreadExecutor rte = qtp.getBean(ReservedThreadExecutor.class);
server.stop();
if (server.getState().equals(Server.FAILED)) {
filter.destroy();
if (extraFilters != null) {
for (FilterHolder f : extraFilters) {
f.getFilter().destroy();
}
}
}
// stop timeout is 0, so we will interrupt right away
while(!qtp.isStopped()) {
qtp.stop();
if (qtp.isStopped()) {
Thread.sleep(50);
}
}
// we tried to kill everything, now we wait for executor to stop
qtp.setStopTimeout(Integer.MAX_VALUE);
qtp.stop();
qtp.join();
if (rte != null) {
// we try and wait for the reserved thread executor, but it doesn't always seem to work
// so we actually set 0 reserved threads at creation
rte.stop();
TimeOut timeout = new TimeOut(30, TimeUnit.SECONDS, TimeSource.NANO_TIME);
timeout.waitFor("Timeout waiting for reserved executor to stop.", ()
-> rte.isStopped());
}
if (customThreadPool != null) {
ExecutorUtil.shutdownAndAwaitTermination(customThreadPool);
}
do {
try {
server.join();
} catch (InterruptedException e) {
// ignore
}
} while (!server.isStopped());
} finally {
if (enableProxy) {
proxy.close();
}
if (prevContext != null) {
MDC.setContextMap(prevContext);
} else {
MDC.clear();
}
}
}
private void shutdownAllContext(HystrixRequestContext hystrixRequestContext) {
RequestContext.shutDown();
ServiceContext.shutDown();
hystrixRequestContext.shutdown();
MDC.clear();
}
private void resetTracing() {
MDC.clear();
Tracer.getInstance().unregisterFromThread();
}
private void resetTracingAndMdc() {
MDC.clear();
Tracer.getInstance().completeRequestSpan();
}
@Override
public void destroy() {
System.out.printf("TdsInit.destroy() is called%n");
// prefs
try {
store.save();
} catch (IOException ioe) {
ioe.printStackTrace();
startupLog.error("TdsInit: Prefs save failed", ioe);
}
// background threads
if (cdmDiskCacheTimer != null)
cdmDiskCacheTimer.cancel();
FileCache.shutdown(); // this handles background threads for all instances of FileCache
DiskCache2.exit(); // this handles background threads for all instances of DiskCache2
executor.shutdownNow();
/*
* try {
* catalogWatcher.close();
* } catch (IOException ioe) {
* ioe.printStackTrace();
* startupLog.error("catalogWatcher close failed", ioe);
* }
*/
// open file caches
RandomAccessFile.shutdown();
NetcdfDataset.shutdown();
NetcdfDatasets.shutdown();
// memory caches
GribCdmIndex.shutdown();
datasetManager.setDatasetTracker(null); // closes the existing tracker
collectionUpdater.shutdown();
startupLog.info("TdsInit shutdown");
MDC.clear();
}
private void resetTracing() {
MDC.clear();
Tracer.getInstance().unregisterFromThread();
}
@Override
public String savePipeline(
String user,
String name,
String rev,
String description,
SourceOffset offset,
final PipelineConfiguration pipelineConfiguration,
RuleDefinitions ruleDefinitions,
Acl acl,
Map<String, Object> metadata
) {
UUID uuid = null;
try {
uuid = GroupsInScope.executeIgnoreGroups(() -> {
// Due to some reason, if pipeline folder doesn't exist but state file exists then remove the state file.
if (!pipelineStore.hasPipeline(name) && pipelineStateExists(name, rev)) {
LOG.warn("Deleting state file for pipeline {} as pipeline is deleted", name);
pipelineStateStore.delete(name, rev);
}
UUID uuidRet = pipelineStore.create(user, name, name, description, true, false, metadata).getUuid();
pipelineConfiguration.setUuid(uuidRet);
pipelineConfiguration.setPipelineId(name);
PipelineConfigurationValidator validator = new PipelineConfigurationValidator(stageLibrary,
buildInfo,
name,
pipelineConfiguration
);
PipelineConfiguration validatedPipelineConfig = validator.validate();
//By default encrypt credentials from Remote data collector
pipelineStore.save(user, name, rev, description, validatedPipelineConfig, true);
pipelineStore.storeRules(name, rev, ruleDefinitions, false);
if (acl != null) { // can be null for old dpm or when DPM jobs have no acl
aclStoreTask.saveAcl(name, acl);
}
LOG.info("Offset for remote pipeline '{}:{}' is {}", name, rev, offset);
if (offset != null) {
OffsetFileUtil.saveSourceOffset(runtimeInfo, name, rev, offset);
}
return uuidRet;
});
} catch (Exception ex) {
LOG.warn(Utils.format("Error while saving pipeline: {} is {}", name, ex), ex);
if (ex.getCause() != null) {
ExceptionUtils.throwUndeclared(ex.getCause());
} else {
ExceptionUtils.throwUndeclared(ex);
}
} finally {
MDC.clear();
}
return uuid.toString();
}
/**
* Links the given distributed tracing and logging MDC info to the current thread. Any existing tracing and MDC info
* on the current thread will be wiped out and overridden, so if you need to go back to them in the future you'll
* need to store the copy info returned by this method for later.
*
* @param distributedTraceStackToLink
* The stack of distributed traces that should be associated with the current thread. This can be null - if it
* is null then {@link Tracer} will be setup with an empty trace stack (wiping out any existing in-progress
* traces).
* @param mdcContextMapToLink
* The MDC context map to associate with the current thread. This can be null - if it is null then {@link
* org.slf4j.MDC#clear()} will be called (wiping out any existing MDC info).
*
* @return A *COPY* of the original trace stack and MDC info on the thread when this method was called (before being
* replaced with the given arguments). The {@link Pair} object will never be null, but the values it contains may be
* null. A copy is returned rather than the original to prevent undesired behavior (storing the return value and
* then passing it in to {@link #unlinkTracingAndMdcFromCurrentThread(Pair)} later should *guarantee* that after
* calling that unlink method the thread state is exactly as it was right *before* calling this link method. If we
* returned the original trace stack this contract guarantee could be violated).
*/
public static Pair<Deque<Span>, Map<String, String>> linkTracingAndMdcToCurrentThread(
Deque<Span> distributedTraceStackToLink, Map<String, String> mdcContextMapToLink
) {
// Unregister the trace stack so that if there's already a trace on the stack we don't get exceptions when
// registering the desired stack with the thread, and keep a copy of the results.
Map<String, String> callingThreadMdcContextMap = MDC.getCopyOfContextMap();
Deque<Span> callingThreadTraceStack = Tracer.getInstance().unregisterFromThread();
// Now setup the trace stack and MDC as desired
if (mdcContextMapToLink == null)
MDC.clear();
else
MDC.setContextMap(mdcContextMapToLink);
Tracer.getInstance().registerWithThread(distributedTraceStackToLink);
// Return the copied original data so that it can be re-linked later (if the caller wants)
return Pair.of(callingThreadTraceStack, callingThreadMdcContextMap);
}
private void resetTracingAndMdc() {
MDC.clear();
Tracer.getInstance().unregisterFromThread();
}
private void resetTracing() {
MDC.clear();
Tracer.getInstance().unregisterFromThread();
}
private void resetTracing() {
MDC.clear();
Tracer.getInstance().unregisterFromThread();
}
@After
public void tearDown() {
MDC.clear();
}
@Test
public void mdc() throws Exception {
HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
Mockito.when(request.getUserPrincipal()).thenReturn(null);
Mockito.when(request.getMethod()).thenReturn("METHOD");
Mockito.when(request.getPathInfo()).thenReturn("/pathinfo");
ServletResponse response = Mockito.mock(ServletResponse.class);
final AtomicBoolean invoked = new AtomicBoolean();
FilterChain chain = new FilterChain() {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse)
throws IOException, ServletException {
assertEquals(MDC.get("hostname"), null);
assertEquals(MDC.get("user"), null);
assertEquals(MDC.get("method"), "METHOD");
assertEquals(MDC.get("path"), "/pathinfo");
invoked.set(true);
}
};
MDC.clear();
Filter filter = new MDCFilter();
filter.init(null);
filter.doFilter(request, response, chain);
assertTrue(invoked.get());
assertNull(MDC.get("hostname"));
assertNull(MDC.get("user"));
assertNull(MDC.get("method"));
assertNull(MDC.get("path"));
Mockito.when(request.getUserPrincipal()).thenReturn(new Principal() {
@Override
public String getName() {
return "name";
}
});
invoked.set(false);
chain = new FilterChain() {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse)
throws IOException, ServletException {
assertEquals(MDC.get("hostname"), null);
assertEquals(MDC.get("user"), "name");
assertEquals(MDC.get("method"), "METHOD");
assertEquals(MDC.get("path"), "/pathinfo");
invoked.set(true);
}
};
filter.doFilter(request, response, chain);
assertTrue(invoked.get());
HostnameFilter.HOSTNAME_TL.set("HOST");
invoked.set(false);
chain = new FilterChain() {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse)
throws IOException, ServletException {
assertEquals(MDC.get("hostname"), "HOST");
assertEquals(MDC.get("user"), "name");
assertEquals(MDC.get("method"), "METHOD");
assertEquals(MDC.get("path"), "/pathinfo");
invoked.set(true);
}
};
filter.doFilter(request, response, chain);
assertTrue(invoked.get());
HostnameFilter.HOSTNAME_TL.remove();
filter.destroy();
}