From 07bdc09ee5030bc302a24ad7051cc85515f4d247 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 --- hbase-logging/src/test/resources/log4j2.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/log4j2.properties b/hbase-logging/src/test/resources/log4j2.properties index 56d67c85aa1b..4f29bb455624 100644 --- a/hbase-logging/src/test/resources/log4j2.properties +++ b/hbase-logging/src/test/resources/log4j2.properties @@ -26,7 +26,7 @@ appender.console.target = SYSTEM_ERR appender.console.name = Console appender.console.maxSize = 1G appender.console.layout.type = PatternLayout -appender.console.layout.pattern = %d{ISO8601} %-5p [%t] %C{2}(%L): %m%n +appender.console.layout.pattern = %d{ISO8601} %-5p [%t (%X)] %C{2}(%L): %m%n rootLogger = INFO,Console 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..1244259fc176 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 @@ -96,12 +97,16 @@ public EventHandler prepare() throws Exception { public void run() { Span span = TraceUtil.getGlobalTracer().spanBuilder(getClass().getSimpleName()) .setParent(Context.current().with(parent)).startSpan(); + // 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. 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 33eaf675c6a5..e312df9862d5 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; @@ -78,6 +79,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);