Skip to content

Commit 394d7bd

Browse files
committed
Do not swallow fail to convert exceptions (#29043)
When converting the source for an indexing request to JSON, the conversion can throw an I/O exception which we swallow and proceed with logging to the slow log. The cause of the I/O exception is lost. This commit changes this behavior and chooses to drop the entry from the slow logs and instead lets an exception percolate up to the indexing operation listener loop. Here, the exception will be caught and logged at the warn level.
1 parent 890e0fb commit 394d7bd

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

server/src/main/java/org/elasticsearch/index/IndexingSlowLog.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import org.elasticsearch.index.shard.ShardId;
3434

3535
import java.io.IOException;
36+
import java.io.UncheckedIOException;
37+
import java.util.Locale;
3638
import java.util.concurrent.TimeUnit;
3739

3840
public final class IndexingSlowLog implements IndexingOperationListener {
@@ -194,6 +196,12 @@ public String toString() {
194196
sb.append(", source[").append(Strings.cleanTruncate(source, maxSourceCharsToLog)).append("]");
195197
} catch (IOException e) {
196198
sb.append(", source[_failed_to_convert_[").append(e.getMessage()).append("]]");
199+
/*
200+
* We choose to fail to write to the slow log and instead let this percolate up to the post index listener loop where this
201+
* will be logged at the warn level.
202+
*/
203+
final String message = String.format(Locale.ROOT, "failed to convert source for slow log entry [%s]", sb.toString());
204+
throw new UncheckedIOException(message, e);
197205
}
198206
return sb.toString();
199207
}

server/src/test/java/org/elasticsearch/index/IndexingSlowLogTests.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch.index;
2121

22+
import com.fasterxml.jackson.core.JsonParseException;
2223
import org.apache.lucene.document.NumericDocValuesField;
2324
import org.elasticsearch.Version;
2425
import org.elasticsearch.cluster.metadata.IndexMetaData;
@@ -34,6 +35,7 @@
3435
import org.elasticsearch.test.ESTestCase;
3536

3637
import java.io.IOException;
38+
import java.io.UncheckedIOException;
3739

3840
import static org.hamcrest.Matchers.containsString;
3941
import static org.hamcrest.Matchers.hasToString;
@@ -70,9 +72,15 @@ public void testSlowLogParsedDocumentPrinterSourceToLog() throws IOException {
7072
"test", null, null, source, XContentType.JSON, null);
7173
p = new SlowLogParsedDocumentPrinter(index, pd, 10, true, 3);
7274

73-
assertThat(p.toString(), containsString("_failed_to_convert_[Unrecognized token 'invalid':"
75+
final UncheckedIOException e = expectThrows(UncheckedIOException.class, p::toString);
76+
assertThat(e, hasToString(containsString("_failed_to_convert_[Unrecognized token 'invalid':"
7477
+ " was expecting ('true', 'false' or 'null')\n"
75-
+ " at [Source: org.elasticsearch.common.bytes.BytesReference$MarkSupportingStreamInputWrapper"));
78+
+ " at [Source: org.elasticsearch.common.bytes.BytesReference$MarkSupportingStreamInputWrapper")));
79+
assertNotNull(e.getCause());
80+
assertThat(e.getCause(), instanceOf(JsonParseException.class));
81+
assertThat(e.getCause(), hasToString(containsString("Unrecognized token 'invalid':"
82+
+ " was expecting ('true', 'false' or 'null')\n"
83+
+ " at [Source: org.elasticsearch.common.bytes.BytesReference$MarkSupportingStreamInputWrapper")));
7684
}
7785

7886
public void testReformatSetting() {

0 commit comments

Comments
 (0)