Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.util.concurrent.ConcurrentCollections;
import org.elasticsearch.common.xcontent.ChunkedToXContentHelper;
import org.elasticsearch.common.xcontent.ChunkedToXContentObject;
import org.elasticsearch.core.Predicates;
Expand All @@ -37,6 +36,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.BiFunction;
import java.util.function.Predicate;
Expand Down Expand Up @@ -66,7 +66,7 @@ public class EsqlExecutionInfo implements ChunkedToXContentObject, Writeable {
// The Map itself is immutable after construction - all Clusters will be accounted for at the start of the search.
// 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 Map<String, Cluster> clusterInfo;
public final ConcurrentMap<String, Cluster> clusterInfo;
// 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 All @@ -90,7 +90,7 @@ public EsqlExecutionInfo(boolean includeCCSMetadata) {
* @param includeCCSMetadata (user defined setting) whether to include the CCS metadata in the HTTP response
*/
public EsqlExecutionInfo(Predicate<String> skipUnavailablePredicate, boolean includeCCSMetadata) {
this.clusterInfo = ConcurrentCollections.newConcurrentMap();
this.clusterInfo = new ConcurrentHashMap<>();
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the difference here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

public static <K, V> ConcurrentMap<K, V> newConcurrentMap() {
return new ConcurrentHashMap<>();
}

newConcurrentMap simply delegates to new ConcurrentHashMap. Not sure why we have it. I am replacing it with what it actually is.

this.skipUnavailablePredicate = skipUnavailablePredicate;
this.includeCCSMetadata = includeCCSMetadata;
this.relativeStart = TimeSpan.start();
Expand All @@ -108,26 +108,9 @@ public EsqlExecutionInfo(Predicate<String> skipUnavailablePredicate, boolean inc

public EsqlExecutionInfo(StreamInput in) throws IOException {
this.overallTook = in.readOptionalTimeValue();
List<EsqlExecutionInfo.Cluster> clusterList = in.readCollectionAsList(EsqlExecutionInfo.Cluster::new);
if (clusterList.isEmpty()) {
this.clusterInfo = ConcurrentCollections.newConcurrentMap();
} else {
Map<String, EsqlExecutionInfo.Cluster> m = ConcurrentCollections.newConcurrentMap();
clusterList.forEach(c -> m.put(c.getClusterAlias(), c));
this.clusterInfo = m;
}
if (in.getTransportVersion().onOrAfter(TransportVersions.V_8_16_0)) {
this.includeCCSMetadata = in.readBoolean();
} else {
this.includeCCSMetadata = false;
}

if (in.getTransportVersion().onOrAfter(TransportVersions.ESQL_RESPONSE_PARTIAL)) {
this.isPartial = in.readBoolean();
} else {
this.isPartial = false;
}

this.clusterInfo = new ConcurrentHashMap<>(in.readMapValues(EsqlExecutionInfo.Cluster::new, Cluster::getClusterAlias));
Copy link
Contributor

Choose a reason for hiding this comment

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

The only thing that slightly bothers me here is that we're creating two maps here and there's really no good reason for that, especially given we already have newConcurrentHashMapWithExpectedSize. Maybe this can be improved? Doesn't have to be in this pull, just a thought.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Previously we created an intermediate list when reading the map, but you are completely right!
We can improve this one by supplying the map factory, updating

this.includeCCSMetadata = in.getTransportVersion().onOrAfter(TransportVersions.V_8_16_0) && in.readBoolean();
Copy link
Contributor

@smalyshev smalyshev Jul 24, 2025

Choose a reason for hiding this comment

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

To be honest, I prefer the if style here. It's much easier to read and understand and follow the logic. This expression mixes two unrelated logic things (versioning and data deserialization) and while it is true that for booleans it's reduced to the same thing, it creates a mental stumble each time I read this.

this.isPartial = in.getTransportVersion().onOrAfter(TransportVersions.ESQL_RESPONSE_PARTIAL) && in.readBoolean();
this.skipUnavailablePredicate = Predicates.always();
this.relativeStart = null;
if (in.getTransportVersion().onOrAfter(TransportVersions.ESQL_QUERY_PLANNING_DURATION)
Expand All @@ -141,7 +124,7 @@ public EsqlExecutionInfo(StreamInput in) throws IOException {
public void writeTo(StreamOutput out) throws IOException {
out.writeOptionalTimeValue(overallTook);
if (clusterInfo != null) {
out.writeCollection(clusterInfo.values().stream().toList());
out.writeCollection(clusterInfo.values());
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I do not think we need to copy this value before serializing.
clusterInfo is concurrent but it not be changed by the time we serialize it.
Please let me know if you believe otherwise.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think you are right here, the nodes would only send the info at the end of the computation, AFAIR.

} else {
out.writeCollection(Collections.emptyList());
}
Expand Down