From 6edcc14fe84ec27ddb1cf485d517114799b07745 Mon Sep 17 00:00:00 2001 From: Nick Dimiduk Date: Mon, 17 Apr 2023 14:22:04 +0200 Subject: [PATCH] HBASE-27762 Include EventType and ProcedureV2 pid in logging via MDC (#5145) Signed-off-by: Duo Zhang (cherry picked from commit 1d3d900957a58031854f5d90b1a3fcf0f75dd543) Change-Id: Id2271f761dc7e858068d42f46e55a40eda4c5e36 --- hbase-logging/src/test/resources/log4j.properties | 2 +- .../java/org/apache/hadoop/hbase/executor/EventHandler.java | 5 +++++ .../hbase/regionserver/handler/AssignRegionHandler.java | 2 ++ .../hbase/regionserver/handler/RSProcedureHandler.java | 2 ++ .../hbase/regionserver/handler/UnassignRegionHandler.java | 2 ++ 5 files changed, 12 insertions(+), 1 deletion(-) diff --git a/hbase-logging/src/test/resources/log4j.properties b/hbase-logging/src/test/resources/log4j.properties index c322699ced24..41035d18638a 100644 --- a/hbase-logging/src/test/resources/log4j.properties +++ b/hbase-logging/src/test/resources/log4j.properties @@ -48,7 +48,7 @@ log4j.appender.DRFA.layout.ConversionPattern=%d{ISO8601} %-5p [%t] %C{2}(%L): %m log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.target=System.err log4j.appender.console.layout=org.apache.log4j.PatternLayout -log4j.appender.console.layout.ConversionPattern=%d{ISO8601} %-5p [%t] %C{2}(%L): %m%n +log4j.appender.console.layout.ConversionPattern=%d{ISO8601} %-5p [%t (%X)] %C{2}(%L): %m%n # Custom Logging levels diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/executor/EventHandler.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/executor/EventHandler.java index ece244fda4fc..730ba22c4849 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/executor/EventHandler.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/executor/EventHandler.java @@ -27,6 +27,7 @@ import org.apache.yetus.audience.InterfaceAudience; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.slf4j.MDC; /** * Abstract base class for all HBase event handlers. Subclasses should implement the @@ -94,14 +95,18 @@ public EventHandler prepare() throws Exception { @Override public void run() { + // assume that this is the top of an execution on a new or reused thread, that we're safe to + // blast any existing MDC state. Span span = TraceUtil.getGlobalTracer().spanBuilder(getClass().getSimpleName()) .setParent(Context.current().with(parent)).startSpan(); try (Scope scope = span.makeCurrent()) { + MDC.put("event_type", eventType.toString()); process(); } catch (Throwable t) { handleException(t); } finally { span.end(); + MDC.clear(); } } diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/handler/AssignRegionHandler.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/handler/AssignRegionHandler.java index 7f9f95bee687..a9ab6f502a35 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/handler/AssignRegionHandler.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/handler/AssignRegionHandler.java @@ -38,6 +38,7 @@ import org.apache.yetus.audience.InterfaceAudience; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.slf4j.MDC; import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos.RegionStateTransition.TransitionCode; @@ -93,6 +94,7 @@ private void cleanUpAndReportFailure(IOException error) throws IOException { @Override public void process() throws IOException { + MDC.put("pid", Long.toString(openProcId)); HRegionServer rs = getServer(); String encodedName = regionInfo.getEncodedName(); byte[] encodedNameBytes = regionInfo.getEncodedNameAsBytes(); diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/handler/RSProcedureHandler.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/handler/RSProcedureHandler.java index d4ddebe727d7..d3ecc8a51e22 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/handler/RSProcedureHandler.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/handler/RSProcedureHandler.java @@ -23,6 +23,7 @@ import org.apache.yetus.audience.InterfaceAudience; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.slf4j.MDC; /** * A event handler for running procedure. @@ -46,6 +47,7 @@ public RSProcedureHandler(HRegionServer rs, long procId, RSProcedureCallable cal public void process() { Throwable error = null; try { + MDC.put("pid", Long.toString(procId)); callable.call(); } catch (Throwable t) { LOG.error("pid=" + this.procId, t); diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/handler/UnassignRegionHandler.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/handler/UnassignRegionHandler.java index 0d54ec1a6e57..f150006d0298 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/handler/UnassignRegionHandler.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/handler/UnassignRegionHandler.java @@ -35,6 +35,7 @@ import org.apache.yetus.audience.InterfaceAudience; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.slf4j.MDC; import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos.RegionStateTransition.TransitionCode; @@ -86,6 +87,7 @@ private HRegionServer getServer() { @Override public void process() throws IOException { + MDC.put("pid", Long.toString(closeProcId)); HRegionServer rs = getServer(); byte[] encodedNameBytes = Bytes.toBytes(encodedName); Boolean previous = rs.getRegionsInTransitionInRS().putIfAbsent(encodedNameBytes, Boolean.FALSE);