Skip to content

Commit

Permalink
[7.x][Transform] Fix casting in ExceptionRootCauseFinder (#66123) (#6…
Browse files Browse the repository at this point in the history
…6150)

Method `getRootCauseException` is checking whether the unwrapped
exception is an instance of `SearchPhaseExecutionException` but then
proceeds to cast the parent exception. This would lead to a casting
error. This commit fixes this and adds a unit test to guard it.

Backport of #66123
  • Loading branch information
dimitris-athanasiou authored Dec 10, 2020
1 parent 23c025e commit d82b892
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static Throwable getRootCauseException(Throwable t) {
Throwable unwrappedThrowable = org.elasticsearch.ExceptionsHelper.unwrapCause(t);

if (unwrappedThrowable instanceof SearchPhaseExecutionException) {
SearchPhaseExecutionException searchPhaseException = (SearchPhaseExecutionException) t;
SearchPhaseExecutionException searchPhaseException = (SearchPhaseExecutionException) unwrappedThrowable;
for (ShardSearchFailure shardFailure : searchPhaseException.shardFailures()) {
Throwable unwrappedShardFailure = org.elasticsearch.ExceptionsHelper.unwrapCause(shardFailure.getCause());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,29 @@

package org.elasticsearch.xpack.transform.utils;

import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ElasticsearchSecurityException;
import org.elasticsearch.ResourceNotFoundException;
import org.elasticsearch.action.DocWriteRequest.OpType;
import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.search.SearchPhaseExecutionException;
import org.elasticsearch.action.search.ShardSearchFailure;
import org.elasticsearch.common.util.concurrent.EsRejectedExecutionException;
import org.elasticsearch.index.mapper.MapperParsingException;
import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.index.translog.TranslogException;
import org.elasticsearch.indices.IndexCreationException;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.test.ESTestCase;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import static org.hamcrest.Matchers.equalTo;

public class ExceptionRootCauseFinderTests extends ESTestCase {

public void testFetFirstIrrecoverableExceptionFromBulkResponses() {
Map<Integer, BulkItemResponse> bulkItemResponses = new HashMap<>();

Expand Down Expand Up @@ -151,10 +158,21 @@ public void testFetFirstIrrecoverableExceptionFromBulkResponses() {
assertNull(ExceptionRootCauseFinder.getFirstIrrecoverableExceptionFromBulkResponses(bulkItemResponses.values()));
}

public void testGetRootCauseException_GivenWrappedSearchPhaseException() {
SearchPhaseExecutionException searchPhaseExecutionException = new SearchPhaseExecutionException("test-phase",
"partial shards failure", new ShardSearchFailure[] { new ShardSearchFailure(new ElasticsearchException("for the cause!")) });

Throwable rootCauseException = ExceptionRootCauseFinder.getRootCauseException(
new IndexCreationException("test-index", searchPhaseExecutionException));

assertThat(rootCauseException.getMessage(), equalTo("for the cause!"));
}

private static void assertFirstException(Collection<BulkItemResponse> bulkItemResponses, Class<?> expectedClass, String message) {
Throwable t = ExceptionRootCauseFinder.getFirstIrrecoverableExceptionFromBulkResponses(bulkItemResponses);
assertNotNull(t);
assertEquals(t.getClass(), expectedClass);
assertEquals(t.getMessage(), message);
}

}

0 comments on commit d82b892

Please sign in to comment.