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

Fix validation of close_pit request #88702

Merged
merged 5 commits into from
Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions docs/changelog/88702.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 88702
summary: Fix validation of `close_pit` request
area: Search
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.tasks.TaskInfo;
import org.elasticsearch.test.ESIntegTestCase;

import java.util.HashSet;
Expand All @@ -42,6 +43,7 @@
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures;
import static org.hamcrest.Matchers.arrayWithSize;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.everyItem;
import static org.hamcrest.Matchers.in;
Expand Down Expand Up @@ -418,6 +420,12 @@ public void testPITTiebreak() throws Exception {
}
}

public void testCloseInvalidPointInTime() {
expectThrows(Exception.class, () -> client().execute(ClosePointInTimeAction.INSTANCE, new ClosePointInTimeRequest("")).actionGet());
List<TaskInfo> tasks = client().admin().cluster().prepareListTasks().setActions(ClosePointInTimeAction.NAME).get().getTasks();
assertThat(tasks, empty());
}

@SuppressWarnings({ "rawtypes", "unchecked" })
private void assertPagination(PointInTimeBuilder pit, int expectedNumDocs, int size, SortBuilder<?>... sorts) throws Exception {
Set<String> seen = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.ValidateActions;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
Expand Down Expand Up @@ -41,7 +42,7 @@ public String getId() {
@Override
public ActionRequestValidationException validate() {
if (Strings.isEmpty(id)) {
throw new IllegalArgumentException("reader id must be specified");
return ValidateActions.addValidationError("id is empty", null);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,19 @@ protected TransportAction(String actionName, ActionFilters actionFilters, TaskMa
* Use this method when the transport action should continue to run in the context of the current task
*/
public final void execute(Task task, Request request, ActionListener<Response> listener) {
ActionRequestValidationException validationException = request.validate();
final ActionRequestValidationException validationException;
try {
validationException = request.validate();
} catch (Exception e) {
Copy link
Contributor

@mayya-sharipova mayya-sharipova Jul 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am trying to understand this part of code. If assertion is always false, it means we will always throw AssertionError, right? So the code after assert false will never be executed?

Or are we checking assertions only during testing mode? And in runtime mode we don't execute assertions?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mayya-sharipova Here we fail with AssertionError in tests, but log a warning message and fail the request with an exception in production.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dnhatn Thanks for clarifying, I was also thinking the same.

assert false : new AssertionError("validating of request [" + request + "] threw exception", e);
logger.warn("validating of request [" + request + "] threw exception", e);
listener.onFailure(e);
return;
}
if (validationException != null) {
listener.onFailure(validationException);
return;
}

if (task != null && request.getShouldStoreResult()) {
listener = new TaskResultStoringActionListener<>(taskManager, task, listener);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.ActionType;
import org.elasticsearch.action.ValidateActions;
import org.elasticsearch.action.support.master.AcknowledgedRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.common.Strings;
Expand Down Expand Up @@ -62,8 +63,12 @@ public LifecyclePolicy getPolicy() {

@Override
public ActionRequestValidationException validate() {
this.policy.validate();
ActionRequestValidationException err = null;
try {
this.policy.validate();
} catch (IllegalArgumentException iae) {
err = ValidateActions.addValidationError(iae.getMessage(), null);
}
String phaseTimingErr = TimeseriesLifecycleType.validateMonotonicallyIncreasingPhaseTimings(this.policy.getPhases().values());
if (Strings.hasText(phaseTimingErr)) {
err = new ActionRequestValidationException();
Expand Down