From 99a157fa4ad174c4ff979414af2edb67a98eb9fe Mon Sep 17 00:00:00 2001 From: Viraj Jasani Date: Fri, 3 Sep 2021 06:55:33 +0530 Subject: [PATCH] HADOOP-17874. ExceptionsHandler to add terse/suppressed Exceptions in thread-safe manner (#3343) Signed-off-by: Akira Ajisaka --- .../java/org/apache/hadoop/ipc/Server.java | 38 +++++++------------ 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Server.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Server.java index bc51a700e4..922c6e937f 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Server.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Server.java @@ -68,6 +68,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; +import java.util.stream.Collectors; import javax.security.sasl.Sasl; import javax.security.sasl.SaslException; @@ -184,8 +185,11 @@ public void setAlignmentContext(AlignmentContext alignmentContext) { * e.g., terse exception group for concise logging messages */ static class ExceptionsHandler { - private volatile Set terseExceptions = new HashSet<>(); - private volatile Set suppressedExceptions = new HashSet<>(); + + private final Set terseExceptions = + ConcurrentHashMap.newKeySet(); + private final Set suppressedExceptions = + ConcurrentHashMap.newKeySet(); /** * Add exception classes for which server won't log stack traces. @@ -193,8 +197,10 @@ static class ExceptionsHandler { * @param exceptionClass exception classes */ void addTerseLoggingExceptions(Class... exceptionClass) { - // Thread-safe replacement of terseExceptions. - terseExceptions = addExceptions(terseExceptions, exceptionClass); + terseExceptions.addAll(Arrays + .stream(exceptionClass) + .map(Class::toString) + .collect(Collectors.toSet())); } /** @@ -203,9 +209,10 @@ void addTerseLoggingExceptions(Class... exceptionClass) { * @param exceptionClass exception classes */ void addSuppressedLoggingExceptions(Class... exceptionClass) { - // Thread-safe replacement of suppressedExceptions. - suppressedExceptions = addExceptions( - suppressedExceptions, exceptionClass); + suppressedExceptions.addAll(Arrays + .stream(exceptionClass) + .map(Class::toString) + .collect(Collectors.toSet())); } boolean isTerseLog(Class t) { @@ -216,23 +223,6 @@ boolean isSuppressedLog(Class t) { return suppressedExceptions.contains(t.toString()); } - /** - * Return a new set containing all the exceptions in exceptionsSet - * and exceptionClass. - * @return - */ - private static Set addExceptions( - final Set exceptionsSet, Class[] exceptionClass) { - // Make a copy of the exceptionSet for performing modification - final HashSet newSet = new HashSet<>(exceptionsSet); - - // Add all class names into the HashSet - for (Class name : exceptionClass) { - newSet.add(name.toString()); - } - - return Collections.unmodifiableSet(newSet); - } }