Add “took” timing info to response for _msearch/template API (#30961)

Add “took” timing info to response for _msearch/template API
Closes #30957
This commit is contained in:
markharwood 2018-05-30 17:57:28 +01:00 committed by GitHub
parent b716b08197
commit facbb2b2dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 3 deletions

View File

@ -20,12 +20,14 @@
package org.elasticsearch.script.mustache; package org.elasticsearch.script.mustache;
import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.Version;
import org.elasticsearch.action.ActionResponse; import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Streamable; import org.elasticsearch.common.io.stream.Streamable;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.ToXContentObject; import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
@ -107,12 +109,14 @@ public class MultiSearchTemplateResponse extends ActionResponse implements Itera
} }
private Item[] items; private Item[] items;
private long tookInMillis;
MultiSearchTemplateResponse() { MultiSearchTemplateResponse() {
} }
public MultiSearchTemplateResponse(Item[] items) { public MultiSearchTemplateResponse(Item[] items, long tookInMillis) {
this.items = items; this.items = items;
this.tookInMillis = tookInMillis;
} }
@Override @Override
@ -127,6 +131,13 @@ public class MultiSearchTemplateResponse extends ActionResponse implements Itera
return this.items; return this.items;
} }
/**
* How long the msearch_template took.
*/
public TimeValue getTook() {
return new TimeValue(tookInMillis);
}
@Override @Override
public void readFrom(StreamInput in) throws IOException { public void readFrom(StreamInput in) throws IOException {
super.readFrom(in); super.readFrom(in);
@ -134,6 +145,9 @@ public class MultiSearchTemplateResponse extends ActionResponse implements Itera
for (int i = 0; i < items.length; i++) { for (int i = 0; i < items.length; i++) {
items[i] = Item.readItem(in); items[i] = Item.readItem(in);
} }
if (in.getVersion().onOrAfter(Version.V_7_0_0_alpha1)) {
tookInMillis = in.readVLong();
}
} }
@Override @Override
@ -143,11 +157,15 @@ public class MultiSearchTemplateResponse extends ActionResponse implements Itera
for (Item item : items) { for (Item item : items) {
item.writeTo(out); item.writeTo(out);
} }
if (out.getVersion().onOrAfter(Version.V_7_0_0_alpha1)) {
out.writeVLong(tookInMillis);
}
} }
@Override @Override
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException {
builder.startObject(); builder.startObject();
builder.field("took", tookInMillis);
builder.startArray(Fields.RESPONSES); builder.startArray(Fields.RESPONSES);
for (Item item : items) { for (Item item : items) {
if (item.isFailure()) { if (item.isFailure()) {

View File

@ -94,7 +94,7 @@ public class TransportMultiSearchTemplateAction extends HandledTransportAction<M
items[originalSlot].getResponse().setResponse(item.getResponse()); items[originalSlot].getResponse().setResponse(item.getResponse());
} }
} }
listener.onResponse(new MultiSearchTemplateResponse(items)); listener.onResponse(new MultiSearchTemplateResponse(items, r.getTook().millis()));
}, listener::onFailure)); }, listener::onFailure));
} }
} }

View File

@ -37,6 +37,7 @@ import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
import static org.hamcrest.Matchers.arrayWithSize; import static org.hamcrest.Matchers.arrayWithSize;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.core.Is.is; import static org.hamcrest.core.Is.is;
@ -140,6 +141,7 @@ public class MultiSearchTemplateIT extends ESIntegTestCase {
MultiSearchTemplateResponse response = client().execute(MultiSearchTemplateAction.INSTANCE, multiRequest).get(); MultiSearchTemplateResponse response = client().execute(MultiSearchTemplateAction.INSTANCE, multiRequest).get();
assertThat(response.getResponses(), arrayWithSize(5)); assertThat(response.getResponses(), arrayWithSize(5));
assertThat(response.getTook().millis(), greaterThan(0L));
MultiSearchTemplateResponse.Item response1 = response.getResponses()[0]; MultiSearchTemplateResponse.Item response1 = response.getResponses()[0];
assertThat(response1.isFailure(), is(false)); assertThat(response1.isFailure(), is(false));