From 0cb1fcc9e2570b5c32959bc805fbfc718d3f2263 Mon Sep 17 00:00:00 2001 From: Rajesh Balamohan Date: Wed, 11 Dec 2019 17:57:29 +0530 Subject: [PATCH 1/2] HADOOP-16751: DurationInfo text parsing/formatting should be moved out of hotpath --- .../org/apache/hadoop/util/DurationInfo.java | 26 ++++++++++++++----- .../apache/hadoop/util/TestDurationInfo.java | 8 ++++++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/DurationInfo.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/DurationInfo.java index 605d060270f8e..b1f4008fd8784 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/DurationInfo.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/DurationInfo.java @@ -18,11 +18,14 @@ package org.apache.hadoop.util; +import com.google.common.base.Suppliers; import org.slf4j.Logger; import org.apache.hadoop.classification.InterfaceAudience.Public; import org.apache.hadoop.classification.InterfaceStability.Unstable; +import java.util.function.Supplier; + /** * A duration with logging of final state at info or debug * in the {@code close()} call. @@ -33,7 +36,10 @@ @Unstable public class DurationInfo extends OperationDuration implements AutoCloseable { - private final String text; + + private final Supplier text; + + private String textStr; private final Logger log; @@ -65,19 +71,25 @@ public DurationInfo(Logger log, boolean logAtInfo, String format, Object... args) { - this.text = String.format(format, args); + this.text = () -> String.format(format, args); this.log = log; this.logAtInfo = logAtInfo; if (logAtInfo) { - log.info("Starting: {}", text); + log.info("Starting: {}", getFormattedText()); } else { - log.debug("Starting: {}", text); + if (log.isDebugEnabled()) { + log.debug("Starting: {}", getFormattedText()); + } } } + private String getFormattedText() { + return (textStr == null) ? (textStr = text.get()) : textStr; + } + @Override public String toString() { - return text + ": duration " + super.toString(); + return getFormattedText() + ": duration " + super.toString(); } @Override @@ -86,7 +98,9 @@ public void close() { if (logAtInfo) { log.info("{}", this); } else { - log.debug("{}", this); + if (log.isDebugEnabled()) { + log.debug("{}", this); + } } } } diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestDurationInfo.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestDurationInfo.java index d1fa70319eb84..b6abde8762902 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestDurationInfo.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/util/TestDurationInfo.java @@ -35,6 +35,14 @@ public void testDurationInfoCreation() throws Exception { Thread.sleep(1000); info.finished(); Assert.assertTrue(info.value() > 0); + + info = new DurationInfo(log, true, "test format %s", "value"); + Assert.assertEquals("test format value: duration 0:00.000s", + info.toString()); + + info = new DurationInfo(log, false, "test format %s", "value"); + Assert.assertEquals("test format value: duration 0:00.000s", + info.toString()); } @Test From b481459ed73aee22423f49a0bd0f8e482894c83e Mon Sep 17 00:00:00 2001 From: Rajesh Balamohan Date: Wed, 11 Dec 2019 17:59:05 +0530 Subject: [PATCH 2/2] HADOOP-16751: DurationInfo text parsing/formatting should be moved out of hotpath --- .../src/main/java/org/apache/hadoop/util/DurationInfo.java | 1 - 1 file changed, 1 deletion(-) diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/DurationInfo.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/DurationInfo.java index b1f4008fd8784..e0e690ac2f858 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/DurationInfo.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/DurationInfo.java @@ -18,7 +18,6 @@ package org.apache.hadoop.util; -import com.google.common.base.Suppliers; import org.slf4j.Logger; import org.apache.hadoop.classification.InterfaceAudience.Public;