HADOOP-17874. ExceptionsHandler to add terse/suppressed Exceptions in thread-safe manner (#3343)

Signed-off-by: Akira Ajisaka <aajisaka@apache.org>
This commit is contained in:
Viraj Jasani 2021-09-03 06:55:33 +05:30 committed by GitHub
parent 051207375b
commit 99a157fa4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -68,6 +68,7 @@
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;
import javax.security.sasl.Sasl; import javax.security.sasl.Sasl;
import javax.security.sasl.SaslException; import javax.security.sasl.SaslException;
@ -184,8 +185,11 @@ public void setAlignmentContext(AlignmentContext alignmentContext) {
* e.g., terse exception group for concise logging messages * e.g., terse exception group for concise logging messages
*/ */
static class ExceptionsHandler { static class ExceptionsHandler {
private volatile Set<String> terseExceptions = new HashSet<>();
private volatile Set<String> suppressedExceptions = new HashSet<>(); private final Set<String> terseExceptions =
ConcurrentHashMap.newKeySet();
private final Set<String> suppressedExceptions =
ConcurrentHashMap.newKeySet();
/** /**
* Add exception classes for which server won't log stack traces. * Add exception classes for which server won't log stack traces.
@ -193,8 +197,10 @@ static class ExceptionsHandler {
* @param exceptionClass exception classes * @param exceptionClass exception classes
*/ */
void addTerseLoggingExceptions(Class<?>... exceptionClass) { void addTerseLoggingExceptions(Class<?>... exceptionClass) {
// Thread-safe replacement of terseExceptions. terseExceptions.addAll(Arrays
terseExceptions = addExceptions(terseExceptions, exceptionClass); .stream(exceptionClass)
.map(Class::toString)
.collect(Collectors.toSet()));
} }
/** /**
@ -203,9 +209,10 @@ void addTerseLoggingExceptions(Class<?>... exceptionClass) {
* @param exceptionClass exception classes * @param exceptionClass exception classes
*/ */
void addSuppressedLoggingExceptions(Class<?>... exceptionClass) { void addSuppressedLoggingExceptions(Class<?>... exceptionClass) {
// Thread-safe replacement of suppressedExceptions. suppressedExceptions.addAll(Arrays
suppressedExceptions = addExceptions( .stream(exceptionClass)
suppressedExceptions, exceptionClass); .map(Class::toString)
.collect(Collectors.toSet()));
} }
boolean isTerseLog(Class<?> t) { boolean isTerseLog(Class<?> t) {
@ -216,23 +223,6 @@ boolean isSuppressedLog(Class<?> t) {
return suppressedExceptions.contains(t.toString()); return suppressedExceptions.contains(t.toString());
} }
/**
* Return a new set containing all the exceptions in exceptionsSet
* and exceptionClass.
* @return
*/
private static Set<String> addExceptions(
final Set<String> exceptionsSet, Class<?>[] exceptionClass) {
// Make a copy of the exceptionSet for performing modification
final HashSet<String> newSet = new HashSet<>(exceptionsSet);
// Add all class names into the HashSet
for (Class<?> name : exceptionClass) {
newSet.add(name.toString());
}
return Collections.unmodifiableSet(newSet);
}
} }