Skip to content

Commit

Permalink
Make keep alive optional in PointInTimeBuilder (#62720)
Browse files Browse the repository at this point in the history
Remove the keepAlive parameter from the constructor of PointInTimeBuilder
as it's optional.
  • Loading branch information
dnhatn committed Sep 22, 2020
1 parent 7ffea46 commit 663b85b
Show file tree
Hide file tree
Showing 14 changed files with 192 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.search.Scroll;
import org.elasticsearch.search.builder.PointInTimeBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.internal.SearchContext;
import org.elasticsearch.tasks.TaskId;
Expand Down Expand Up @@ -479,7 +480,7 @@ public SearchSourceBuilder source() {
return source;
}

public SearchSourceBuilder.PointInTimeBuilder pointInTimeBuilder() {
public PointInTimeBuilder pointInTimeBuilder() {
if (source != null) {
return source.pointInTimeBuilder();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.elasticsearch.search.Scroll;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.PipelineAggregationBuilder;
import org.elasticsearch.search.builder.PointInTimeBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.collapse.CollapseBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
Expand Down Expand Up @@ -554,13 +555,10 @@ public SearchRequestBuilder setCollapse(CollapseBuilder collapse) {
}

/**
* Specifies the search context that Elasticsearch should use to perform the query
*
* @param searchContextId the base64 encoded string of the search context id
* @param keepAlive the extended time to live for the search context
* If specified, Elasticsearch will execute this search request using reader contexts from that point in time.
*/
public SearchRequestBuilder setSearchContext(String searchContextId, TimeValue keepAlive) {
sourceBuilder().pointInTimeBuilder(new SearchSourceBuilder.PointInTimeBuilder(searchContextId, keepAlive));
public SearchRequestBuilder setPointInTime(PointInTimeBuilder pointInTimeBuilder) {
sourceBuilder().pointInTimeBuilder(pointInTimeBuilder);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.search.builder;

import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;

import java.io.IOException;
import java.util.Objects;

/**
* A search request with a point in time will execute using the reader contexts associated with that point time
* instead of the latest reader contexts.
*/
public final class PointInTimeBuilder implements Writeable, ToXContentObject {
private static final ParseField ID_FIELD = new ParseField("id");
private static final ParseField KEEP_ALIVE_FIELD = new ParseField("keep_alive");
private static final ObjectParser<XContentParams, Void> PARSER;

static {
PARSER = new ObjectParser<>(SearchSourceBuilder.POINT_IN_TIME.getPreferredName(), XContentParams::new);
PARSER.declareString((params, id) -> params.id = id, ID_FIELD);
PARSER.declareField((params, keepAlive) -> params.keepAlive = keepAlive,
(p, c) -> TimeValue.parseTimeValue(p.text(), KEEP_ALIVE_FIELD.getPreferredName()),
KEEP_ALIVE_FIELD, ObjectParser.ValueType.STRING);
}

private static final class XContentParams {
private String id;
private TimeValue keepAlive;
}

private final String id;
private TimeValue keepAlive;

public PointInTimeBuilder(String id) {
this.id = Objects.requireNonNull(id);
}

public PointInTimeBuilder(StreamInput in) throws IOException {
id = in.readString();
keepAlive = in.readOptionalTimeValue();
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(id);
out.writeOptionalTimeValue(keepAlive);
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(SearchSourceBuilder.POINT_IN_TIME.getPreferredName());
builder.field(ID_FIELD.getPreferredName(), id);
if (keepAlive != null) {
builder.field(KEEP_ALIVE_FIELD.getPreferredName(), keepAlive);
}
builder.endObject();
return builder;
}

public static PointInTimeBuilder fromXContent(XContentParser parser) throws IOException {
final XContentParams params = PARSER.parse(parser, null);
if (params.id == null) {
throw new IllegalArgumentException("point int time id is not provided");
}
return new PointInTimeBuilder(params.id).setKeepAlive(params.keepAlive);
}

/**
* Returns the id of this point in time
*/
public String getId() {
return id;
}

/**
* If specified, the search layer will keep this point in time around for at least the given keep-alive.
* Otherwise, the point in time will be kept around until the original keep alive elapsed.
*/
public PointInTimeBuilder setKeepAlive(TimeValue keepAlive) {
this.keepAlive = keepAlive;
return this;
}

@Nullable
public TimeValue getKeepAlive() {
return keepAlive;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final PointInTimeBuilder that = (PointInTimeBuilder) o;
return Objects.equals(id, that.id) && Objects.equals(keepAlive, that.keepAlive);
}

@Override
public int hashCode() {
return Objects.hash(id, keepAlive);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.ToXContentFragment;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
Expand Down Expand Up @@ -1693,84 +1692,4 @@ public String toString(Params params) {
}
}

/**
* Specify whether this search should use specific reader contexts instead of the latest ones.
*/
public static final class PointInTimeBuilder implements Writeable, ToXContentObject {
private static final ParseField ID_FIELD = new ParseField("id");
private static final ParseField KEEP_ALIVE_FIELD = new ParseField("keep_alive");
private static final ObjectParser<XContentParams, Void> PARSER;

static {
PARSER = new ObjectParser<>(POINT_IN_TIME.getPreferredName(), XContentParams::new);
PARSER.declareString((params, id) -> params.id = id, ID_FIELD);
PARSER.declareField((params, keepAlive) -> params.keepAlive = keepAlive,
(p, c) -> TimeValue.parseTimeValue(p.text(), KEEP_ALIVE_FIELD.getPreferredName()),
KEEP_ALIVE_FIELD, ObjectParser.ValueType.STRING);
}

private static final class XContentParams {
private String id;
private TimeValue keepAlive;
}

private final String id;
private final TimeValue keepAlive;

public PointInTimeBuilder(String id, TimeValue keepAlive) {
this.id = Objects.requireNonNull(id);
this.keepAlive = keepAlive;
}

public PointInTimeBuilder(StreamInput in) throws IOException {
id = in.readString();
keepAlive = in.readOptionalTimeValue();
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(id);
out.writeOptionalTimeValue(keepAlive);
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(POINT_IN_TIME.getPreferredName());
builder.field(ID_FIELD.getPreferredName(), id);
if (keepAlive != null) {
builder.field(KEEP_ALIVE_FIELD.getPreferredName(), keepAlive);
}
builder.endObject();
return builder;
}

public static PointInTimeBuilder fromXContent(XContentParser parser) throws IOException {
final XContentParams params = PARSER.parse(parser, null);
if (params.id == null) {
throw new IllegalArgumentException("point int time id is not provided");
}
return new PointInTimeBuilder(params.id, params.keepAlive);
}

public TimeValue getKeepAlive() {
return keepAlive;
}

public String getId() {
return id;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final PointInTimeBuilder that = (PointInTimeBuilder) o;
return Objects.equals(id, that.id) && Objects.equals(keepAlive, that.keepAlive);
}

@Override
public int hashCode() {
return Objects.hash(id, keepAlive);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.AbstractSearchTestCase;
import org.elasticsearch.search.Scroll;
import org.elasticsearch.search.builder.PointInTimeBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.rescore.QueryRescorerBuilder;
import org.elasticsearch.tasks.TaskId;
Expand Down Expand Up @@ -208,8 +209,7 @@ public void testValidate() throws IOException {
{
// Reader context with scroll
SearchRequest searchRequest = new SearchRequest()
.source(new SearchSourceBuilder().pointInTimeBuilder(
new SearchSourceBuilder.PointInTimeBuilder("id", TimeValue.timeValueMillis(randomIntBetween(1, 10)))))
.source(new SearchSourceBuilder().pointInTimeBuilder(new PointInTimeBuilder("id")))
.scroll(TimeValue.timeValueMillis(randomIntBetween(1, 100)));
ActionRequestValidationException validationErrors = searchRequest.validate();
assertNotNull(validationErrors);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ public void testToXContentWithPointInTime() throws IOException {
XContentType xContentType = randomFrom(XContentType.values());
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
TimeValue keepAlive = randomBoolean() ? TimeValue.timeValueHours(1) : null;
searchSourceBuilder.pointInTimeBuilder(new SearchSourceBuilder.PointInTimeBuilder("id", keepAlive));
searchSourceBuilder.pointInTimeBuilder(new PointInTimeBuilder("id").setKeepAlive(keepAlive));
XContentBuilder builder = XContentFactory.contentBuilder(xContentType);
searchSourceBuilder.toXContent(builder, ToXContent.EMPTY_PARAMS);
BytesReference bytes = BytesReference.bytes(builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptType;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.builder.PointInTimeBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.collapse.CollapseBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
Expand Down Expand Up @@ -374,8 +375,11 @@ public static SearchSourceBuilder randomSearchSourceBuilder(
builder.collapse(randomCollapseBuilder.get());
}
if (randomBoolean()) {
TimeValue keepAlive = randomBoolean() ? TimeValue.timeValueMinutes(randomIntBetween(1, 60)) : null;
builder.pointInTimeBuilder(new SearchSourceBuilder.PointInTimeBuilder(randomAlphaOfLengthBetween(3, 10), keepAlive));
PointInTimeBuilder pit = new PointInTimeBuilder(randomAlphaOfLengthBetween(3, 10));
if (randomBoolean()) {
pit.setKeepAlive(TimeValue.timeValueMinutes(randomIntBetween(1, 60)));
}
builder.pointInTimeBuilder(pit);
}
return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.elasticsearch.plugins.SearchPlugin;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.aggregations.bucket.filter.InternalFilter;
import org.elasticsearch.search.builder.PointInTimeBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.tasks.TaskId;
import org.elasticsearch.test.ESIntegTestCase;
Expand Down Expand Up @@ -235,7 +236,11 @@ protected SearchResponseIterator assertBlockingIterator(String indexName,
null,
null);
pitId = client().execute(OpenPointInTimeAction.INSTANCE, openPIT).actionGet().getSearchContextId();
source.pointInTimeBuilder(new SearchSourceBuilder.PointInTimeBuilder(pitId, TimeValue.timeValueMinutes(1)));
final PointInTimeBuilder pit = new PointInTimeBuilder(pitId);
if (randomBoolean()) {
pit.setKeepAlive(TimeValue.timeValueMillis(randomIntBetween(1, 3600)));
}
source.pointInTimeBuilder(pit);
request = new SubmitAsyncSearchRequest(source);
} else {
pitId = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.search.builder.PointInTimeBuilder;
import org.elasticsearch.test.AbstractMultiClustersTestCase;
import org.elasticsearch.xpack.core.LocalStateCompositeXPackPlugin;
import org.elasticsearch.xpack.core.search.action.ClosePointInTimeAction;
Expand Down Expand Up @@ -79,7 +80,7 @@ public void testBasic() {
SearchResponse resp = localClient.prepareSearch()
.setPreference(null)
.setQuery(new MatchAllQueryBuilder())
.setSearchContext(pitId, TimeValue.timeValueMinutes(2))
.setPointInTime(new PointInTimeBuilder(pitId))
.setSize(1000)
.get();
assertNoFailures(resp);
Expand Down

0 comments on commit 663b85b

Please sign in to comment.