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 validate query listener invocation bug #56157

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Fix validate query listener invocation bug
When the index we are validating a query does not exist, we try to send
back a response letting the client know that the index does not
exist. Yet, we accidentally fallthrough into the case that the
validation failed for some other reason. This means that we end up
notifying the channel twice. Sometimes the notification occurs after the
failure has been written out and the channel closed (so the second
invocation leads to a silent failed to write to a closed channel issue),
and sometimes the response does end up in the channel, creating garbled
responses to the client. This commit fixes that issue by avoiding the
fallthrough.
  • Loading branch information
jasontedor committed May 4, 2020
commit fa5c836717b3072a15c771f48a1dbc59b942284b
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ protected void doExecute(Task task, ValidateQueryRequest request, ActionListener
if (ex instanceof IndexNotFoundException ||
ex instanceof IndexClosedException) {
listener.onFailure(ex);
return;
}
List<QueryExplanation> explanations = new ArrayList<>();
explanations.add(new QueryExplanation(null,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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.action.admin.indices.validate.query;

import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.NotifyOnceListener;
import org.elasticsearch.test.ESSingleNodeTestCase;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

import static org.hamcrest.Matchers.equalTo;

public class TransportValidateQueryActionTests extends ESSingleNodeTestCase {

/*
* This test covers a fallthrough bug that we had, where if the index we were validating against did not exist, we would invoke the
* failure listener, and then fallthrough and invoke the success listener too. This would cause problems when the listener was
* ultimately wrapping sending a response on the channel, as it could lead to us sending both a failure or success responses, and having
* them garbled together, or trying to write one after the channel had closed, etc.
*/
public void testListenerOnlyInvokedOnceWhenIndexDoesNotExist() {
final ValidateQueryRequest request = new ValidateQueryRequest("non-existent-index");

final AtomicBoolean invoked = new AtomicBoolean();

final ActionListener<ValidateQueryResponse> listener = new ActionListener<>() {

@Override
public void onResponse(final ValidateQueryResponse validateQueryResponse) {
fail("onResponse should not be invoked in this failure case");
}

@Override
public void onFailure(final Exception e) {
if (invoked.compareAndSet(false, true) == false) {
fail("onFailure invoked more than once");
}
}

};
client().admin().indices().validateQuery(request, listener);
assertThat(invoked.get(), equalTo(true)); // ensure that onFailure was invoked
}

}