Skip to content

Commit

Permalink
SearchSlowLog uses a non thread-safe object to escape json (#48363)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jimczi committed Oct 28, 2019
1 parent d30c0e8 commit c4a1353
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
Expand Up @@ -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;
Expand All @@ -32,7 +30,6 @@
* 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;

/**
Expand All @@ -45,11 +42,6 @@ public ESLogMessage(Map<String, Object> fields, String messagePattern, Object...
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;
Expand Down
Expand Up @@ -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;
Expand All @@ -33,13 +34,16 @@
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;
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;
Expand Down Expand Up @@ -230,6 +234,11 @@ private static String message(SearchContext context, long tookInNanos) {
}
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) {
Expand Down

0 comments on commit c4a1353

Please sign in to comment.