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

Avoid execute ESQL planning on refresh thread #104591

Merged
merged 2 commits into from
Jan 25, 2024
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/104591.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 104591
summary: Avoid execute ESQL planning on refresh thread
area: ES|QL
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.ActionListenerResponseHandler;
import org.elasticsearch.action.ActionRunnable;
import org.elasticsearch.action.OriginalIndices;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchShardsGroup;
Expand All @@ -22,7 +23,6 @@
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.util.BigArrays;
import org.elasticsearch.common.util.concurrent.CountDown;
import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.compute.data.BlockFactory;
import org.elasticsearch.compute.data.Page;
Expand Down Expand Up @@ -435,51 +435,60 @@ private void acquireSearchContexts(
Map<Index, AliasFilter> aliasFilters,
ActionListener<List<SearchContext>> listener
) {
final List<IndexShard> targetShards = new ArrayList<>();
try {
List<IndexShard> targetShards = new ArrayList<>();
for (ShardId shardId : shardIds) {
var indexShard = searchService.getIndicesService().indexServiceSafe(shardId.getIndex()).getShard(shardId.id());
targetShards.add(indexShard);
}
if (targetShards.isEmpty()) {
listener.onResponse(List.of());
return;
} catch (Exception e) {
listener.onFailure(e);
return;
}
final var doAcquire = ActionRunnable.supply(listener, () -> {
final List<SearchContext> searchContexts = new ArrayList<>(targetShards.size());
boolean success = false;
try {
for (IndexShard shard : targetShards) {
var aliasFilter = aliasFilters.getOrDefault(shard.shardId().getIndex(), AliasFilter.EMPTY);
var shardRequest = new ShardSearchRequest(
shard.shardId(),
configuration.absoluteStartedTimeInMillis(),
aliasFilter,
clusterAlias
);
SearchContext context = searchService.createSearchContext(shardRequest, SearchService.NO_TIMEOUT);
searchContexts.add(context);
}
for (SearchContext searchContext : searchContexts) {
searchContext.preProcess();
}
success = true;
return searchContexts;
} finally {
if (success == false) {
IOUtils.close(searchContexts);
}
}
});
final AtomicBoolean waitedForRefreshes = new AtomicBoolean();
try (RefCountingRunnable refs = new RefCountingRunnable(() -> {
if (waitedForRefreshes.get()) {
esqlExecutor.execute(doAcquire);
} else {
doAcquire.run();
}
CountDown countDown = new CountDown(targetShards.size());
})) {
for (IndexShard targetShard : targetShards) {
targetShard.ensureShardSearchActive(ignored -> {
if (countDown.countDown()) {
ActionListener.completeWith(listener, () -> {
final List<SearchContext> searchContexts = new ArrayList<>(targetShards.size());
boolean success = false;
try {
for (IndexShard shard : targetShards) {
var aliasFilter = aliasFilters.getOrDefault(shard.shardId().getIndex(), AliasFilter.EMPTY);
var shardRequest = new ShardSearchRequest(
shard.shardId(),
configuration.absoluteStartedTimeInMillis(),
aliasFilter,
clusterAlias
);
SearchContext context = searchService.createSearchContext(shardRequest, SearchService.NO_TIMEOUT);
searchContexts.add(context);
}
for (SearchContext searchContext : searchContexts) {
searchContext.preProcess();
}
success = true;
return searchContexts;
} finally {
if (success == false) {
IOUtils.close(searchContexts);
}
}
});
final Releasable ref = refs.acquire();
targetShard.ensureShardSearchActive(await -> {
try (ref) {
if (await) {
waitedForRefreshes.set(true);
}
}
});
}
} catch (Exception e) {
listener.onFailure(e);
}
}

Expand Down Expand Up @@ -585,6 +594,7 @@ public void messageReceived(DataNodeRequest request, TransportChannel channel, T
configuration,
request.aliasFilters(),
ActionListener.wrap(searchContexts -> {
assert ThreadPool.assertCurrentThreadPool(ESQL_THREAD_POOL_NAME);
Copy link
Member Author

Choose a reason for hiding this comment

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

Without the fix, the test EsqlActionIT#testRefreshSearchIdleShards trips this assertion .

Copy link
Member

Choose a reason for hiding this comment

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

👍

var computeContext = new ComputeContext(sessionId, clusterAlias, searchContexts, configuration, null, exchangeSink);
runCompute(parentTask, computeContext, request.plan(), ActionListener.wrap(driverProfiles -> {
// don't return until all pages are fetched
Expand Down