Skip to content
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 @@ -67,6 +67,8 @@ public class EsqlExecutionInfo implements ChunkedToXContentObject, Writeable {
// Updates to the Cluster occur with the updateCluster method that given the key to map transforms an
// old Cluster Object to a new Cluster Object with the remapping function.
public final ConcurrentMap<String, Cluster> clusterInfo;
// Is the clusterInfo map iinitialization in progress? If so, we should not try to serialize it.
private transient volatile boolean clusterInfoInitializing;
// whether the user has asked for CCS metadata to be in the JSON response (the overall took will always be present)
private final boolean includeCCSMetadata;

Expand Down Expand Up @@ -124,10 +126,8 @@ public EsqlExecutionInfo(StreamInput in) throws IOException {
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeOptionalTimeValue(overallTook);
if (clusterInfo != null) {
// .stream().toList() creates an immutable copy of the cluster info entries
// as today they might be still changing while serialization is happening
out.writeCollection(clusterInfo.values().stream().toList());
if (clusterInfo != null && clusterInfoInitializing == false) {
out.writeCollection(clusterInfo.values());
} else {
out.writeCollection(Collections.emptyList());
}
Expand Down Expand Up @@ -354,6 +354,10 @@ public boolean isStopped() {
return isStopped;
}

public void clusterInfoInitializing(boolean clusterInfoInitializing) {
this.clusterInfoInitializing = clusterInfoInitializing;
}

/**
* Represents the search metadata about a particular cluster involved in a cross-cluster search.
* The Cluster object can represent either the local cluster or a remote cluster.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,15 +338,20 @@ public static void initCrossClusterState(
patterns.getFirst().indexPattern()
);

executionInfo.clusterInfoInitializing(true);
// initialize the cluster entries in EsqlExecutionInfo before throwing the invalid license error
// so that the CCS telemetry handler can recognize that this error is CCS-related
for (var entry : groupedIndices.entrySet()) {
final String clusterAlias = entry.getKey();
final String indexExpr = Strings.arrayToCommaDelimitedString(entry.getValue().indices());
executionInfo.swapCluster(clusterAlias, (k, v) -> {
assert v == null : "No cluster for " + clusterAlias + " should have been added to ExecutionInfo yet";
return new EsqlExecutionInfo.Cluster(clusterAlias, indexExpr, executionInfo.shouldSkipOnFailure(clusterAlias));
});
try {
for (var entry : groupedIndices.entrySet()) {
final String clusterAlias = entry.getKey();
final String indexExpr = Strings.arrayToCommaDelimitedString(entry.getValue().indices());
executionInfo.swapCluster(clusterAlias, (k, v) -> {
assert v == null : "No cluster for " + clusterAlias + " should have been added to ExecutionInfo yet";
return new EsqlExecutionInfo.Cluster(clusterAlias, indexExpr, executionInfo.shouldSkipOnFailure(clusterAlias));
});
}
} finally {
executionInfo.clusterInfoInitializing(false);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe it could protect from concurrent addition while serializing, however I am not following what could trigger that.

This completes synchronously before any of the listeners in analyzedPlan could fail. Am I missing something?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The problem can happen when we're running an async query, and the query runner has just started to construct the executionInfo here, and we get an async GET request at the same time. That request will also use executionInfo, but when serializing it writeCollection has a race condition - it reads size first and then serializes elements. So we get broken serialization data as the result.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see. Since it is just starting, it does not contain any meaningful information worth waiting for?

Copy link
Contributor Author

@smalyshev smalyshev Aug 15, 2025

Choose a reason for hiding this comment

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

Precisely. We don't want to actually show anybody half-initialized cluster info, even if it didn't cause the race. And since we didn't initialize it yet, it can't contain anything important.


// check if it is a cross-cluster query
Expand Down