Skip to content

Commit

Permalink
Make keep alive of point in time optional in search (#62184)
Browse files Browse the repository at this point in the history
A search request should not be required to extend the keep_alive of a point in time. 
This change makes that parameter optional.
  • Loading branch information
dnhatn authored Sep 10, 2020
1 parent 25955bb commit 8a95e3c
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public SearchShardIterator(@Nullable String clusterAlias, ShardId shardId,
this.clusterAlias = clusterAlias;
this.searchContextId = searchContextId;
this.searchContextKeepAlive = searchContextKeepAlive;
assert (searchContextId == null) == (searchContextKeepAlive == null);
assert searchContextKeepAlive == null || searchContextId != null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1676,33 +1676,35 @@ private static final class XContentParams {

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

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

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(id);
out.writeTimeValue(keepAlive);
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);
builder.field(KEEP_ALIVE_FIELD.getPreferredName(), keepAlive);
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 || params.keepAlive == null) {
throw new IllegalArgumentException("id and keep_alive must be specified");
if (params.id == null) {
throw new IllegalArgumentException("point int time id is not provided");
}
return new PointInTimeBuilder(params.id, params.keepAlive);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ private ShardSearchRequest(OriginalIndices originalIndices,
this.originalIndices = originalIndices;
this.readerId = readerId;
this.keepAlive = keepAlive;
assert (readerId != null) == (keepAlive != null);
assert keepAlive == null || readerId != null : "readerId: " + readerId + " keepAlive: " + keepAlive;
}

public ShardSearchRequest(StreamInput in) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,17 +386,23 @@ public void testToXContent() throws IOException {
public void testToXContentWithPointInTime() throws IOException {
XContentType xContentType = randomFrom(XContentType.values());
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.pointInTimeBuilder(new SearchSourceBuilder.PointInTimeBuilder("id", TimeValue.timeValueHours(1)));
TimeValue keepAlive = randomBoolean() ? TimeValue.timeValueHours(1) : null;
searchSourceBuilder.pointInTimeBuilder(new SearchSourceBuilder.PointInTimeBuilder("id", keepAlive));
XContentBuilder builder = XContentFactory.contentBuilder(xContentType);
searchSourceBuilder.toXContent(builder, ToXContent.EMPTY_PARAMS);
BytesReference bytes = BytesReference.bytes(builder);
Map<String, Object> sourceAsMap = XContentHelper.convertToMap(bytes, false, xContentType).v2();
assertEquals(1, sourceAsMap.size());
@SuppressWarnings("unchecked")
Map<String, Object> pit = (Map<String, Object>) sourceAsMap.get("pit");
assertEquals(2, pit.size());
assertEquals("id", pit.get("id"));
assertEquals("1h", pit.get("keep_alive"));
if (keepAlive != null) {
assertEquals("1h", pit.get("keep_alive"));
assertEquals(2, pit.size());
} else {
assertNull(pit.get("keep_alive"));
assertEquals(1, pit.size());
}
}

public void testParseIndicesBoost() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,8 @@ public static SearchSourceBuilder randomSearchSourceBuilder(
builder.collapse(randomCollapseBuilder.get());
}
if (randomBoolean()) {
builder.pointInTimeBuilder(new SearchSourceBuilder.PointInTimeBuilder(randomAlphaOfLengthBetween(3, 10),
TimeValue.timeValueMinutes(randomIntBetween(1, 60))));
TimeValue keepAlive = randomBoolean() ? TimeValue.timeValueMinutes(randomIntBetween(1, 60)) : null;
builder.pointInTimeBuilder(new SearchSourceBuilder.PointInTimeBuilder(randomAlphaOfLengthBetween(3, 10), keepAlive));
}
return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ setup:
search_after: [24, 172]
pit:
id: "$point_in_time_id"
keep_alive: 1m

- match: {hits.total: 3 }
- length: {hits.hits: 1 }
Expand All @@ -106,7 +105,6 @@ setup:
search_after: [18, 42]
pit:
id: "$point_in_time_id"
keep_alive: 1m

- match: {hits.total: 3}
- length: {hits.hits: 1 }
Expand Down

0 comments on commit 8a95e3c

Please sign in to comment.