Skip to content

SQL: Fix bug caused by empty composites #30343

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

Merged
merged 2 commits into from
May 3, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,36 @@ public void nextPage(Configuration cfg, Client client, NamedWriteableRegistry re

SearchRequest search = Querier.prepareRequest(client, query, cfg.pageTimeout(), indices);

client.search(search, ActionListener.wrap(r -> {
updateCompositeAfterKey(r, query);
CompositeAggsRowSet rowSet = new CompositeAggsRowSet(extractors, r, limit,
serializeQuery(query), indices);
listener.onResponse(rowSet);
}, listener::onFailure));
client.search(search, new ActionListener<SearchResponse>() {
@Override
public void onResponse(SearchResponse r) {
try {
// retry
if (shouldRetryDueToEmptyPage(r)) {
CompositeAggregationCursor.updateCompositeAfterKey(r, search.source());
client.search(search, this);
Copy link
Member Author

Choose a reason for hiding this comment

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

@nik9000 is this okay for chaining listeners, in particular the return for the current call?

Copy link
Member

Choose a reason for hiding this comment

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

In general chaining listeners is fine. I don't tend to use the return early style in listeners because I feel like they should look symmetric. They just feel better to me when they do. I use the early return style everywhere else though.

The only concern with chaining listeners like this is that some APIs are synchronous sometimes and if you chain on a synchronous API you'll get a stack overflow. The initial phase of search has this problem. I don't know if you have that problem here but I'd dig through the code to make super sure you don't.

return;
}

updateCompositeAfterKey(r, query);
CompositeAggsRowSet rowSet = new CompositeAggsRowSet(extractors, r, limit, serializeQuery(query), indices);
listener.onResponse(rowSet);
} catch (Exception ex) {
Copy link
Member

Choose a reason for hiding this comment

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

You shouldn't need to catch (Exception here because the caller already does and will forward any exceptions you throw to onFailure.

Copy link
Member Author

Choose a reason for hiding this comment

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

got it.

Copy link
Member Author

Choose a reason for hiding this comment

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

While updating the code, I was reminded why I added it in the first place - serializeQuery throws an IOException which needs to be caught so it's easy to just pass it to the listener.
I cannot use wrap directly because I need to pass the listener instance to it again...

Copy link
Member

Choose a reason for hiding this comment

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

I understand with wrap. I think it'd be a little more clear if you just caught the IOException and sent it to onFailure but what you have will work as well.

listener.onFailure(ex);
}
}

@Override
public void onFailure(Exception ex) {
listener.onFailure(ex);
}
});
}

static boolean shouldRetryDueToEmptyPage(SearchResponse response) {
CompositeAggregation composite = getComposite(response);
// if there are no buckets but a next page, go fetch it instead of sending an empty response to the client
return composite != null && composite.getBuckets().isEmpty() && composite.afterKey() != null && !composite.afterKey().isEmpty();
}

static CompositeAggregation getComposite(SearchResponse response) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,15 @@ static class CompositeActionListener extends BaseAggActionListener {
protected void handleResponse(SearchResponse response, ActionListener<SchemaRowSet> listener) {
// there are some results
if (response.getAggregations().asList().size() > 0) {
CompositeAggregationCursor.updateCompositeAfterKey(response, request.source());

// retry
if (CompositeAggregationCursor.shouldRetryDueToEmptyPage(response)) {
CompositeAggregationCursor.updateCompositeAfterKey(response, request.source());
client.search(request, this);
Copy link
Member

Choose a reason for hiding this comment

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

Same deal here. Make sure it isn't going to stack overflow. It probably won't be we should make sure.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've ran the test suite with the troublesome seed and without (plus ran the test separately with the page set to 1 - this is what triggered the bug). Anything else I should watch out for?

Copy link
Member

Choose a reason for hiding this comment

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

You pretty much have to read the code to make sure it won't happen. With search it only came up because dozens of shards for the search were on the local node. With this it'd come up if the composite agg needed to be retried hundreds of times and the API doesn't otherwise go async.

I'd dig through the client implementation. I think this is safe because you are being called on the listener thread pool and the search is running on the search threadpool but it is worth double checking.

return;
Copy link
Member Author

Choose a reason for hiding this comment

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

I like the return in this case because it bails out from the listening method. the if/else might be nested inside another block (like here) and some code might be hanging towards the end.

}

CompositeAggregationCursor.updateCompositeAfterKey(response, request.source());
byte[] nextSearch = null;
try {
nextSearch = CompositeAggregationCursor.serializeQuery(request.source());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
*/
package org.elasticsearch.xpack.qa.sql.security;

import org.apache.lucene.util.LuceneTestCase.AwaitsFix;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.xpack.qa.sql.jdbc.SqlSpecTestCase;

import java.util.Properties;

@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/30292")
public class JdbcSqlSpecIT extends SqlSpecTestCase {
public JdbcSqlSpecIT(String fileName, String groupName, String testName, Integer lineNumber, String query) {
super(fileName, groupName, testName, lineNumber, query);
Expand Down