Skip to content

Commit c5e5248

Browse files
committed
handle multiple patterns in initCrossClusterState
1 parent c0cb7d5 commit c5e5248

File tree

3 files changed

+25
-28
lines changed

3 files changed

+25
-28
lines changed

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/index/IndexResolution.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ public static IndexResolution valid(EsIndex index) {
3636
return valid(index, index.concreteIndices(), Map.of());
3737
}
3838

39+
public static IndexResolution empty(String indexPattern) {
40+
return valid(new EsIndex(indexPattern, Map.of(), Map.of(), Set.of()));
41+
}
42+
3943
public static IndexResolution invalid(String invalid) {
4044
Objects.requireNonNull(invalid, "invalid must not be null to signal that the index is invalid");
4145
return new IndexResolution(null, invalid, Set.of(), Map.of());

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/session/EsqlCCSUtils.java

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -325,33 +325,29 @@ public static void initCrossClusterState(
325325
return;
326326
}
327327
try {
328-
// TODO it is not safe to concat multiple index patterns in case any of them contains exclusion.
329-
// This is going to be resolved in #136804
330-
String[] indexExpressions = indexPatterns.stream()
331-
.map(indexPattern -> Strings.splitStringByCommaToArray(indexPattern.indexPattern()))
332-
.reduce((a, b) -> {
333-
String[] combined = new String[a.length + b.length];
334-
System.arraycopy(a, 0, combined, 0, a.length);
335-
System.arraycopy(b, 0, combined, a.length, b.length);
336-
return combined;
337-
})
338-
.get();
339-
var groupedIndices = indicesGrouper.groupIndices(IndicesOptions.DEFAULT, indexExpressions, false);
328+
for (IndexPattern indexPattern : indexPatterns) {
329+
var groupedIndices = indicesGrouper.groupIndices(
330+
IndicesOptions.DEFAULT,
331+
Strings.splitStringByCommaToArray(indexPattern.indexPattern()),
332+
false
333+
);
340334

341-
executionInfo.clusterInfoInitializing(true);
342-
// initialize the cluster entries in EsqlExecutionInfo before throwing the invalid license error
343-
// so that the CCS telemetry handler can recognize that this error is CCS-related
344-
try {
345-
for (var entry : groupedIndices.entrySet()) {
346-
final String clusterAlias = entry.getKey();
347-
final String indexExpr = Strings.arrayToCommaDelimitedString(entry.getValue().indices());
348-
executionInfo.swapCluster(clusterAlias, (k, v) -> {
349-
assert v == null : "No cluster for " + clusterAlias + " should have been added to ExecutionInfo yet";
350-
return new EsqlExecutionInfo.Cluster(clusterAlias, indexExpr, executionInfo.shouldSkipOnFailure(clusterAlias));
335+
executionInfo.clusterInfoInitializing(true);
336+
// initialize the cluster entries in EsqlExecutionInfo before throwing the invalid license error
337+
// so that the CCS telemetry handler can recognize that this error is CCS-related
338+
try {
339+
groupedIndices.forEach((clusterAlias, indices) -> {
340+
executionInfo.swapCluster(clusterAlias, (k, v) -> {
341+
var indexExpr = Strings.arrayToCommaDelimitedString(indices.indices());
342+
if (v != null) {
343+
indexExpr = v.getIndexExpression() + "," + indexExpr;
344+
}
345+
return new EsqlExecutionInfo.Cluster(clusterAlias, indexExpr, executionInfo.shouldSkipOnFailure(clusterAlias));
346+
});
351347
});
348+
} finally {
349+
executionInfo.clusterInfoInitializing(false);
352350
}
353-
} finally {
354-
executionInfo.clusterInfoInitializing(false);
355351
}
356352

357353
if (executionInfo.isCrossClusterSearch() && EsqlLicenseChecker.isCcsAllowed(licenseState) == false) {

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/session/EsqlSession.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -835,12 +835,9 @@ private void preAnalyzeMainIndices(
835835
ThreadPool.Names.SEARCH_COORDINATION,
836836
ThreadPool.Names.SYSTEM_READ
837837
);
838-
// TODO: This is not yet index specific, but that will not matter as soon as #136804 is dealt with
839838
if (executionInfo.clusterAliases().isEmpty()) {
840839
// return empty resolution if the expression is pure CCS and resolved no remote clusters (like no-such-cluster*:index)
841-
listener.onResponse(
842-
result.withIndices(indexPattern, IndexResolution.valid(new EsIndex(indexPattern.indexPattern(), Map.of(), Map.of())))
843-
);
840+
listener.onResponse(result.withIndices(indexPattern, IndexResolution.empty(indexPattern.indexPattern())));
844841
} else {
845842
indexResolver.resolveAsMergedMappingAndRetrieveMinimumVersion(
846843
indexPattern.indexPattern(),

0 commit comments

Comments
 (0)