Skip to content

Commit 5b3b52a

Browse files
authored
[7.16] [Transform] Report _preview warning in the face of pipeline failure. (#81972) (#82202)
1 parent 3e73eaf commit 5b3b52a

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

x-pack/plugin/transform/qa/single-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/transform/integration/TransformPivotRestIT.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
import org.apache.http.entity.ContentType;
1111
import org.apache.http.entity.StringEntity;
1212
import org.elasticsearch.client.Request;
13+
import org.elasticsearch.client.RequestOptions;
1314
import org.elasticsearch.client.Response;
1415
import org.elasticsearch.client.ResponseException;
16+
import org.elasticsearch.client.WarningsHandler;
1517
import org.elasticsearch.common.Strings;
1618
import org.elasticsearch.common.xcontent.support.XContentMapValues;
1719
import org.elasticsearch.xcontent.XContentBuilder;
@@ -29,7 +31,9 @@
2931

3032
import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder;
3133
import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.basicAuthHeaderValue;
34+
import static org.hamcrest.Matchers.allOf;
3235
import static org.hamcrest.Matchers.containsString;
36+
import static org.hamcrest.Matchers.empty;
3337
import static org.hamcrest.Matchers.equalTo;
3438
import static org.hamcrest.Matchers.hasEntry;
3539
import static org.hamcrest.Matchers.hasSize;
@@ -949,6 +953,59 @@ public void testPreviewTransformWithPipeline() throws Exception {
949953
});
950954
}
951955

956+
@SuppressWarnings("unchecked")
957+
public void testPreviewTransformWithPipelineScript() throws Exception {
958+
String pipelineId = "my-preview-pivot-pipeline-script";
959+
Request pipelineRequest = new Request("PUT", "/_ingest/pipeline/" + pipelineId);
960+
pipelineRequest.setJsonEntity(
961+
"{\n"
962+
+ " \"description\" : \"my pivot preview pipeline\",\n"
963+
+ " \"processors\" : [\n"
964+
+ " {\n"
965+
+ " \"script\" : {\n"
966+
+ " \"lang\": \"painless\",\n"
967+
+ " \"source\": \"ctx._id = ctx['non']['existing'];\"\n"
968+
+ " }\n"
969+
+ " }\n"
970+
+ " ]\n"
971+
+ "}"
972+
);
973+
client().performRequest(pipelineRequest);
974+
975+
setupDataAccessRole(DATA_ACCESS_ROLE, REVIEWS_INDEX_NAME);
976+
final Request createPreviewRequest = createRequestWithAuth("POST", getTransformEndpoint() + "_preview", null);
977+
createPreviewRequest.setOptions(RequestOptions.DEFAULT.toBuilder().setWarningsHandler(WarningsHandler.PERMISSIVE));
978+
979+
String config = "{ \"source\": {\"index\":\""
980+
+ REVIEWS_INDEX_NAME
981+
+ "\"} ,"
982+
+ "\"dest\": {\"pipeline\": \""
983+
+ pipelineId
984+
+ "\"},"
985+
+ " \"pivot\": {"
986+
+ " \"group_by\": {"
987+
+ " \"user.id\": {\"terms\": { \"field\": \"user_id\" }},"
988+
+ " \"by_day\": {\"date_histogram\": {\"fixed_interval\": \"1d\",\"field\":\"timestamp\"}}},"
989+
+ " \"aggregations\": {"
990+
+ " \"user.avg_rating\": {"
991+
+ " \"avg\": {"
992+
+ " \"field\": \"stars\""
993+
+ " } } } }"
994+
+ "}";
995+
createPreviewRequest.setJsonEntity(config);
996+
997+
Response createPreviewResponse = client().performRequest(createPreviewRequest);
998+
Map<String, Object> previewTransformResponse = entityAsMap(createPreviewResponse);
999+
List<Map<String, Object>> preview = (List<Map<String, Object>>) previewTransformResponse.get("preview");
1000+
// Pipeline failed for all the docs so the preview is empty
1001+
assertThat(preview, is(empty()));
1002+
assertThat(createPreviewResponse.getWarnings(), is(not(empty())));
1003+
assertThat(
1004+
createPreviewResponse.getWarnings().get(createPreviewResponse.getWarnings().size() - 1),
1005+
allOf(containsString("Pipeline returned 100 errors, first error:"), containsString("type=script_exception"))
1006+
);
1007+
}
1008+
9521009
public void testPivotWithMaxOnDateField() throws Exception {
9531010
String transformId = "simple_date_histogram_pivot_with_max_time";
9541011
String transformIndex = "pivot_reviews_via_date_histogram_with_max_time";

x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/action/TransportPreviewTransformAction.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,24 @@ private void getPreview(
230230

231231
ActionListener<SimulatePipelineResponse> pipelineResponseActionListener = ActionListener.wrap(simulatePipelineResponse -> {
232232
List<Map<String, Object>> docs = new ArrayList<>(simulatePipelineResponse.getResults().size());
233+
List<Map<String, Object>> errors = new ArrayList<>();
233234
for (SimulateDocumentResult simulateDocumentResult : simulatePipelineResponse.getResults()) {
234235
try (XContentBuilder xContentBuilder = XContentFactory.jsonBuilder()) {
235236
XContentBuilder content = simulateDocumentResult.toXContent(xContentBuilder, ToXContent.EMPTY_PARAMS);
236237
Map<String, Object> tempMap = XContentHelper.convertToMap(BytesReference.bytes(content), true, XContentType.JSON).v2();
237-
docs.add((Map<String, Object>) XContentMapValues.extractValue("doc._source", tempMap));
238+
Map<String, Object> doc = (Map<String, Object>) XContentMapValues.extractValue("doc._source", tempMap);
239+
if (doc != null) {
240+
docs.add(doc);
241+
}
242+
Map<String, Object> error = (Map<String, Object>) XContentMapValues.extractValue("error", tempMap);
243+
if (error != null) {
244+
errors.add(error);
245+
}
238246
}
239247
}
248+
if (errors.isEmpty() == false) {
249+
HeaderWarning.addWarning("Pipeline returned " + errors.size() + " errors, first error: " + errors.get(0));
250+
}
240251
TransformDestIndexSettings generatedDestIndexSettings = TransformIndex.createTransformDestIndexSettings(
241252
mappings.get(),
242253
transformId,

0 commit comments

Comments
 (0)