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
This commit is contained in:
Jim Ferenczi 2019-10-23 10:19:34 +02:00 committed by jimczi
parent 4790ee4c32
commit 50f565b158
2 changed files with 9 additions and 8 deletions

View File

@ -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<String, Object> 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;

View File

@ -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) {