Skip to content

Commit

Permalink
Add “took” timing info to response for _msearch/template API (#30961)
Browse files Browse the repository at this point in the history
Add “took” timing info to response for _msearch/template API
Closes #30957
  • Loading branch information
markharwood authored May 30, 2018
1 parent b716b08 commit facbb2b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
package org.elasticsearch.script.mustache;

import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.Version;
import org.elasticsearch.action.ActionResponse;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Streamable;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
Expand Down Expand Up @@ -107,12 +109,14 @@ public Exception getFailure() {
}

private Item[] items;

private long tookInMillis;

MultiSearchTemplateResponse() {
}

public MultiSearchTemplateResponse(Item[] items) {
public MultiSearchTemplateResponse(Item[] items, long tookInMillis) {
this.items = items;
this.tookInMillis = tookInMillis;
}

@Override
Expand All @@ -126,6 +130,13 @@ public Iterator<Item> iterator() {
public Item[] getResponses() {
return this.items;
}

/**
* How long the msearch_template took.
*/
public TimeValue getTook() {
return new TimeValue(tookInMillis);
}

@Override
public void readFrom(StreamInput in) throws IOException {
Expand All @@ -134,6 +145,9 @@ public void readFrom(StreamInput in) throws IOException {
for (int i = 0; i < items.length; i++) {
items[i] = Item.readItem(in);
}
if (in.getVersion().onOrAfter(Version.V_7_0_0_alpha1)) {
tookInMillis = in.readVLong();
}
}

@Override
Expand All @@ -143,11 +157,15 @@ public void writeTo(StreamOutput out) throws IOException {
for (Item item : items) {
item.writeTo(out);
}
if (out.getVersion().onOrAfter(Version.V_7_0_0_alpha1)) {
out.writeVLong(tookInMillis);
}
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException {
builder.startObject();
builder.field("took", tookInMillis);
builder.startArray(Fields.RESPONSES);
for (Item item : items) {
if (item.isFailure()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ protected void doExecute(MultiSearchTemplateRequest request, ActionListener<Mult
items[originalSlot].getResponse().setResponse(item.getResponse());
}
}
listener.onResponse(new MultiSearchTemplateResponse(items));
listener.onResponse(new MultiSearchTemplateResponse(items, r.getTook().millis()));
}, listener::onFailure));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
import static org.hamcrest.Matchers.arrayWithSize;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.core.Is.is;

Expand Down Expand Up @@ -140,6 +141,7 @@ public void testBasic() throws Exception {

MultiSearchTemplateResponse response = client().execute(MultiSearchTemplateAction.INSTANCE, multiRequest).get();
assertThat(response.getResponses(), arrayWithSize(5));
assertThat(response.getTook().millis(), greaterThan(0L));

MultiSearchTemplateResponse.Item response1 = response.getResponses()[0];
assertThat(response1.isFailure(), is(false));
Expand Down

0 comments on commit facbb2b

Please sign in to comment.