From 50f565b158573dd1142be46b8e93e1a9e55953f6 Mon Sep 17 00:00:00 2001 From: Jim Ferenczi Date: Wed, 23 Oct 2019 10:19:34 +0200 Subject: [PATCH] SearchSlowLog uses a non thread-safe object to escape json (#48363) This commit fixes the usage of JsonStringEncoder#quoteAsUTF8 in the SearchSlowLog. JsonStringEncoder#getInstance should always be called to get a thread local object but this assumption was broken by #44642. This means that any slow log can throw an AIOOBE since it uses the same byte array concurrently. Closes #48358 --- .../org/elasticsearch/common/logging/ESLogMessage.java | 8 -------- .../main/java/org/elasticsearch/index/SearchSlowLog.java | 9 +++++++++ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/common/logging/ESLogMessage.java b/server/src/main/java/org/elasticsearch/common/logging/ESLogMessage.java index 0d063f5a0d1..9044b00df72 100644 --- a/server/src/main/java/org/elasticsearch/common/logging/ESLogMessage.java +++ b/server/src/main/java/org/elasticsearch/common/logging/ESLogMessage.java @@ -19,11 +19,9 @@ package org.elasticsearch.common.logging; -import com.fasterxml.jackson.core.io.JsonStringEncoder; import org.apache.logging.log4j.message.ParameterizedMessage; import org.elasticsearch.common.SuppressLoggerChecks; -import java.nio.charset.Charset; import java.util.Map; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -32,7 +30,6 @@ import java.util.stream.Stream; * A base class for custom log4j logger messages. Carries additional fields which will populate JSON fields in logs. */ public abstract class ESLogMessage extends ParameterizedMessage { - private static final JsonStringEncoder JSON_STRING_ENCODER = JsonStringEncoder.getInstance(); private final Map fields; /** @@ -45,11 +42,6 @@ public abstract class ESLogMessage extends ParameterizedMessage { this.fields = fields; } - public static String escapeJson(String text) { - byte[] sourceEscaped = JSON_STRING_ENCODER.quoteAsUTF8(text); - return new String(sourceEscaped, Charset.defaultCharset()); - } - public String getValueFor(String key) { Object value = fields.get(key); return value != null ? value.toString() : null; diff --git a/server/src/main/java/org/elasticsearch/index/SearchSlowLog.java b/server/src/main/java/org/elasticsearch/index/SearchSlowLog.java index 2fc4955efd0..220718a12a9 100644 --- a/server/src/main/java/org/elasticsearch/index/SearchSlowLog.java +++ b/server/src/main/java/org/elasticsearch/index/SearchSlowLog.java @@ -19,6 +19,7 @@ package org.elasticsearch.index; +import com.fasterxml.jackson.core.io.JsonStringEncoder; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.common.Strings; @@ -33,6 +34,7 @@ import org.elasticsearch.search.internal.SearchContext; import org.elasticsearch.tasks.Task; import java.util.Arrays; +import java.nio.charset.Charset; import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -40,6 +42,8 @@ import java.util.concurrent.TimeUnit; import java.util.stream.Stream; public final class SearchSlowLog implements SearchOperationListener { + private static final Charset UTF_8 = Charset.forName("UTF-8"); + private long queryWarnThreshold; private long queryInfoThreshold; private long queryDebugThreshold; @@ -227,6 +231,11 @@ public final class SearchSlowLog implements SearchOperationListener { } return sb.toString(); } + + private static String escapeJson(String text) { + byte[] sourceEscaped = JsonStringEncoder.getInstance().quoteAsUTF8(text); + return new String(sourceEscaped, UTF_8); + } } private void setQueryWarnThreshold(TimeValue warnThreshold) {