Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create PIT API #2745

Merged
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
469738f
create pit changes
bharath-techie Mar 24, 2022
0d1f337
Merge branch 'main' of https://github.com/opensearch-project/OpenSear…
bharath-techie Mar 29, 2022
751c320
two phase create pit
bharath-techie Mar 30, 2022
eba05e5
Adding unit tests
bharath-techie Apr 6, 2022
130a597
addressing review comments
bharath-techie Apr 9, 2022
c6f9398
Merge branch 'main' of https://github.com/opensearch-project/OpenSear…
bharath-techie Apr 12, 2022
87ec605
fixing ci, adding tests and java docs
bharath-techie Apr 12, 2022
f8f6367
Segregating create pit logic into separate controller
bharath-techie Apr 13, 2022
0fcd1df
Adding cleanup logic if create pit fails
bharath-techie Apr 14, 2022
3002c20
Merge branch 'opensearch-project:main' into createpit
bharath-techie Apr 22, 2022
1f6f466
Addressing comments
bharath-techie Apr 26, 2022
de5c4e4
Merge branch 'createpit' of github.com:bharath-techie/OpenSearch into…
bharath-techie Apr 26, 2022
3359986
Addressing review comments
bharath-techie Apr 29, 2022
232179e
Merge branch 'main' of https://github.com/opensearch-project/OpenSear…
bharath-techie May 2, 2022
730afa8
Adding java docs
bharath-techie May 3, 2022
4747018
Addressing comments and making pit naming uniform
bharath-techie May 6, 2022
7340feb
Changes to include rest high level client tests and addressing comments
bharath-techie May 9, 2022
eec2480
Addressing comments
bharath-techie May 10, 2022
b87f0fd
Merge branch 'main' of https://github.com/opensearch-project/OpenSear…
bharath-techie May 11, 2022
939fafc
Addressing comments
bharath-techie May 12, 2022
02c7537
Addressing comments
bharath-techie May 12, 2022
c6cef2b
Addressing comments
bharath-techie May 12, 2022
bd0105c
addressing comments
bharath-techie May 12, 2022
35d8cc4
addressing comments
bharath-techie May 13, 2022
11c5195
addressing comments
bharath-techie May 16, 2022
471a64a
addressing comments
bharath-techie May 17, 2022
47bb3e7
Merge branch 'main' of https://github.com/opensearch-project/OpenSear…
bharath-techie May 17, 2022
c28762e
fixing broken tests
bharath-techie May 17, 2022
f272405
Merge branch 'main' of https://github.com/opensearch-project/OpenSear…
bharath-techie May 18, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@

package org.opensearch.search.searchafter;

import org.opensearch.action.ActionFuture;
import org.opensearch.action.admin.indices.create.CreateIndexRequestBuilder;
import org.opensearch.action.index.IndexRequestBuilder;
import org.opensearch.action.search.SearchPhaseExecutionException;
import org.opensearch.action.search.SearchRequestBuilder;
import org.opensearch.action.search.SearchResponse;
import org.opensearch.action.search.ShardSearchFailure;
import org.opensearch.action.search.*;
import org.opensearch.common.UUIDs;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.common.xcontent.XContentBuilder;
import org.opensearch.search.SearchHit;
import org.opensearch.search.builder.PointInTimeBuilder;
import org.opensearch.search.sort.SortOrder;
import org.opensearch.test.OpenSearchIntegTestCase;
import org.hamcrest.Matchers;
Expand All @@ -50,7 +50,6 @@
import java.util.Comparator;
import java.util.Collections;
import java.util.Arrays;

import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked;
import static org.opensearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.opensearch.index.query.QueryBuilders.matchAllQuery;
Expand Down Expand Up @@ -155,6 +154,57 @@ public void testsShouldFail() throws Exception {
}
}

public void testPitWithSearchAfter() throws Exception {
assertAcked(client().admin().indices().prepareCreate("test").setMapping("field1", "type=long", "field2", "type=keyword").get());
ensureGreen();
indexRandom(
true,
client().prepareIndex("test").setId("0").setSource("field1", 0),
client().prepareIndex("test").setId("1").setSource("field1", 100, "field2", "toto"),
client().prepareIndex("test").setId("2").setSource("field1", 101),
client().prepareIndex("test").setId("3").setSource("field1", 99)
);

CreatePITRequest request = new CreatePITRequest(TimeValue.timeValueDays(1), true);
request.setIndices(new String[] { "test" });
ActionFuture<CreatePITResponse> execute = client().execute(CreatePITAction.INSTANCE, request);
CreatePITResponse pitResponse = execute.get();
SearchResponse sr = client().prepareSearch()
.addSort("field1", SortOrder.ASC)
.setQuery(matchAllQuery())
.searchAfter(new Object[] { 99 })
.setPointInTime(new PointInTimeBuilder(pitResponse.getId()))
.get();
assertEquals(2, sr.getHits().getHits().length);
sr = client().prepareSearch()
.addSort("field1", SortOrder.ASC)
.setQuery(matchAllQuery())
.searchAfter(new Object[] { 100 })
.setPointInTime(new PointInTimeBuilder(pitResponse.getId()))
.get();
assertEquals(1, sr.getHits().getHits().length);
sr = client().prepareSearch()
.addSort("field1", SortOrder.ASC)
.setQuery(matchAllQuery())
.searchAfter(new Object[] { 0 })
.setPointInTime(new PointInTimeBuilder(pitResponse.getId()))
.get();
assertEquals(3, sr.getHits().getHits().length);
/**
* Add new data and assert PIT results remain the same and normal search results gets refreshed
*/
indexRandom(true, client().prepareIndex("test").setId("4").setSource("field1", 102));
sr = client().prepareSearch()
.addSort("field1", SortOrder.ASC)
.setQuery(matchAllQuery())
.searchAfter(new Object[] { 0 })
.setPointInTime(new PointInTimeBuilder(pitResponse.getId()))
.get();
assertEquals(3, sr.getHits().getHits().length);
sr = client().prepareSearch().addSort("field1", SortOrder.ASC).setQuery(matchAllQuery()).searchAfter(new Object[] { 0 }).get();
assertEquals(4, sr.getHits().getHits().length);
}

public void testWithNullStrings() throws InterruptedException {
assertAcked(client().admin().indices().prepareCreate("test").setMapping("field2", "type=keyword").get());
ensureGreen();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,12 @@
import org.opensearch.action.main.MainAction;
import org.opensearch.action.main.TransportMainAction;
import org.opensearch.action.search.ClearScrollAction;
import org.opensearch.action.search.CreatePITAction;
import org.opensearch.action.search.MultiSearchAction;
import org.opensearch.action.search.SearchAction;
import org.opensearch.action.search.SearchScrollAction;
import org.opensearch.action.search.TransportClearScrollAction;
import org.opensearch.action.search.TransportCreatePITAction;
import org.opensearch.action.search.TransportMultiSearchAction;
import org.opensearch.action.search.TransportSearchAction;
import org.opensearch.action.search.TransportSearchScrollAction;
Expand Down Expand Up @@ -400,6 +402,7 @@
import org.opensearch.rest.action.ingest.RestSimulatePipelineAction;
import org.opensearch.rest.action.search.RestClearScrollAction;
import org.opensearch.rest.action.search.RestCountAction;
import org.opensearch.rest.action.search.RestCreatePITAction;
import org.opensearch.rest.action.search.RestExplainAction;
import org.opensearch.rest.action.search.RestMultiSearchAction;
import org.opensearch.rest.action.search.RestSearchAction;
Expand Down Expand Up @@ -660,6 +663,7 @@ public <Request extends ActionRequest, Response extends ActionResponse> void reg
actions.register(ImportDanglingIndexAction.INSTANCE, TransportImportDanglingIndexAction.class);
actions.register(DeleteDanglingIndexAction.INSTANCE, TransportDeleteDanglingIndexAction.class);
actions.register(FindDanglingIndexAction.INSTANCE, TransportFindDanglingIndexAction.class);
actions.register(CreatePITAction.INSTANCE, TransportCreatePITAction.class);

return unmodifiableMap(actions.getRegistry());
}
Expand Down Expand Up @@ -832,6 +836,9 @@ public void initRestHandlers(Supplier<DiscoveryNodes> nodesInCluster) {
registerHandler.accept(new RestRepositoriesAction());
registerHandler.accept(new RestSnapshotAction());
registerHandler.accept(new RestTemplatesAction());

// Point in time API
registerHandler.accept(new RestCreatePITAction());
for (ActionPlugin plugin : actionPlugins) {
for (RestHandler handler : plugin.getRestHandlers(
settings,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.action.search;

import org.opensearch.action.ActionType;

bharath-techie marked this conversation as resolved.
Show resolved Hide resolved
public class CreatePITAction extends ActionType<CreatePITResponse> {
public static final CreatePITAction INSTANCE = new CreatePITAction();
public static final String NAME = "indices:data/write/pit";

private CreatePITAction() {
super(NAME, CreatePITResponse::new);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.action.search;

import org.opensearch.action.ActionRequest;
import org.opensearch.action.ActionRequestValidationException;
import org.opensearch.action.IndicesRequest;
import org.opensearch.action.support.IndicesOptions;
import org.opensearch.common.Nullable;
import org.opensearch.common.Strings;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.tasks.Task;
import org.opensearch.tasks.TaskId;

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

import static org.opensearch.action.ValidateActions.addValidationError;

/**
* A request to make create point in time against one or more indices.
*/
public class CreatePITRequest extends ActionRequest implements IndicesRequest.Replaceable {
bharath-techie marked this conversation as resolved.
Show resolved Hide resolved

// keep alive for pit reader context
private TimeValue keepAlive;

// this describes whether PIT can be created with partial failures
private Boolean allowPartialPitCreation;
@Nullable
private String routing = null;
@Nullable
private String preference = null;
private String[] indices = Strings.EMPTY_ARRAY;
private IndicesOptions indicesOptions = SearchRequest.DEFAULT_INDICES_OPTIONS;

public CreatePITRequest(TimeValue keepAlive, Boolean allowPartialPitCreation, String... indices) {
this.keepAlive = keepAlive;
this.allowPartialPitCreation = allowPartialPitCreation;
this.indices = indices;
}

public CreatePITRequest(StreamInput in) throws IOException {
super(in);
indices = in.readStringArray();
indicesOptions = IndicesOptions.readIndicesOptions(in);
routing = in.readOptionalString();
preference = in.readOptionalString();
keepAlive = in.readTimeValue();
routing = in.readOptionalString();
allowPartialPitCreation = in.readOptionalBoolean();
}

@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeStringArray(indices);
indicesOptions.writeIndicesOptions(out);
out.writeOptionalString(preference);
out.writeTimeValue(keepAlive);
out.writeOptionalString(routing);
out.writeOptionalBoolean(allowPartialPitCreation);
}

public String getRouting() {
return routing;
}

public String getPreference() {
return preference;
}

public String[] getIndices() {
return indices;
}

public IndicesOptions getIndicesOptions() {
return indicesOptions;
}

public TimeValue getKeepAlive() {
return keepAlive;
}

/**
* Sets if this request should allow partial results.
*/
public void allowPartialPitCreation(Boolean allowPartialPitCreation) {
this.allowPartialPitCreation = allowPartialPitCreation;
}

public boolean shouldAllowPartialPitCreation() {
return allowPartialPitCreation;
}

public void setRouting(String routing) {
this.routing = routing;
}

public void setPreference(String preference) {
this.preference = preference;
}

public void setIndices(String[] indices) {
this.indices = indices;
}

public void setIndicesOptions(IndicesOptions indicesOptions) {
this.indicesOptions = Objects.requireNonNull(indicesOptions, "indicesOptions must not be null");
}

@Override
public ActionRequestValidationException validate() {
ActionRequestValidationException validationException = null;
bharath-techie marked this conversation as resolved.
Show resolved Hide resolved
if (keepAlive == null) {
validationException = addValidationError("keep alive not specified", validationException);
}
return validationException;
}

@Override
public String[] indices() {
return indices;
}

@Override
public IndicesOptions indicesOptions() {
return indicesOptions;
}

public CreatePITRequest indicesOptions(IndicesOptions indicesOptions) {
this.indicesOptions = Objects.requireNonNull(indicesOptions, "indicesOptions must not be null");
return this;
}

public void setKeepAlive(TimeValue keepAlive) {
this.keepAlive = keepAlive;
}

public final String buildDescription() {
StringBuilder sb = new StringBuilder();
sb.append("indices[");
Strings.arrayToDelimitedString(indices, ",", sb);
sb.append("], ");
sb.append("pointintime[").append(keepAlive).append("], ");
bharath-techie marked this conversation as resolved.
Show resolved Hide resolved
return sb.toString();
}

@Override
public Task createTask(long id, String type, String action, TaskId parentTaskId, Map<String, String> headers) {
bharath-techie marked this conversation as resolved.
Show resolved Hide resolved
return new Task(id, type, action, this.buildDescription(), parentTaskId, headers);
}

private void validateIndices(String... indices) {
Objects.requireNonNull(indices, "indices must not be null");
for (String index : indices) {
Objects.requireNonNull(index, "index must not be null");
}
}

@Override
public CreatePITRequest indices(String... indices) {
validateIndices(indices);
this.indices = indices;
return this;
}
}
Loading