Skip to content

Keep indices field caps could not check when pruning - #5845

Draft
ahkcs wants to merge 3 commits into
opensearch-project:mainfrom
ahkcs:fix/index-pruning-keep-field-caps-failures
Draft

ahkcs wants to merge 3 commits into
opensearch-project:mainfrom
ahkcs:fix/index-pruning-keep-field-caps-failures

Conversation

@ahkcs

@ahkcs ahkcs commented Oct 7, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Draft: depends on opensearch-project/OpenSearch#23262 (for opensearch-project/OpenSearch#23193). This uses FieldCapabilitiesResponse.getFailures(), which doesn't exist in core yet, so CI won't compile until that lands.

Index pruning (#5766) keeps the indices that _field_caps with the query's time range as index_filter reports as matching. Today an index whose shards couldn't be checked is missing from that answer exactly like an index with nothing in range, so it gets pruned and its documents are lost with no warning (#5808).

The core change makes field caps report such an index under a new failures section instead of leaving it out. This PR keeps those indices, so the search reaches the unavailable shards and #5807's PARTIAL_RESULT warning reports the gap:

Set<String> keep = new LinkedHashSet<>(Arrays.asList(candidates));
keep.addAll(probe.getFailures().keySet());

An unreachable remote cluster is covered the same way: #23262 lists it under failures as the expression asked of it, such as remote:logs-*, so it's kept and the search applies skip_unavailable. Without that, local-*,remote:logs-* narrowed to the local indices and dropped the remote silently, so cross-cluster expressions need no special case.

One limit: a reachable remote on a version before the core change can't report its own unchecked indices, so those can still be pruned.

Compared with #5819

#5819 fixes the same bug without a core change, by sending a second, unfiltered field caps probe and keeping indices absent from both. This version:

#5819 stays open until the core change is settled.

Testing

  • IndexPrunerTest (33): kept failed index, kept unreachable remote, nothing left to prune, and nothing matched. Run against a locally built core with the change.
  • Core side: FieldCapabilitiesIT covers an index with no available shard (with and without a filter) and one with a single unavailable shard.

Related Issues

Resolves #5808

Check List

  • New functionality includes testing.
  • New functionality has been documented. n/a, behavior fix.
  • API changes companion pull request created, if applicable. n/a here; the core change needs one.
  • Commits are signed per the DCO using --signoff.
  • Public documentation issue/PR created, if applicable. n/a.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

Index pruning keeps the indices a field caps probe with the query's time
range reports as matching. An index whose shards could not be checked was
missing from that answer just like an index with nothing in range, so it
was pruned and its documents were lost without any sign of it.

With field caps reporting such indices as failures, keep them: the search
then reaches the unavailable shards and reports the gap. Also stop pruning
an expression that names a remote cluster, since field caps omits an
unreachable remote without reporting it.

Signed-off-by: Kai Huang <[email protected]>
@github-actions

github-actions Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

PR Reviewer Guide 🔍

(Review updated until commit 66650a7)

Here are some key observations to aid the review process:

🧪 PR contains tests
🔒 No security concerns identified
✅ No TODO sections
🔀 No multiple PR themes
⚡ Recommended focus areas for review

Nested Pruning Check

The nested isPrunedBy(keep.size()) check at line 79 can cause pruning to be skipped even when indices were successfully narrowed. If keep.size() equals the original count (e.g., all candidates failed), pruning is declined despite having valid candidates. This differs from the original logic which only checked candidates.length. The condition should verify that keeping failed indices still results in meaningful pruning compared to the original index count, not just compared to candidates.

if (indexExpr.isPrunedBy(keep.size())) {
  return new IndexName(String.join(",", keep));
}

@github-actions

github-actions Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

PR Code Suggestions ✨

Latest suggestions up to 66650a7
Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
Possible issue
Add null check for failures

The code should verify that probe.getFailures() is not null before calling keySet()
on it. If the FieldCapabilitiesResponse returns null for failures, this will cause a
NullPointerException. Add a null check to ensure safe access.

opensearch/src/main/java/org/opensearch/sql/opensearch/request/IndexPruner.java [77-81]

 Set<String> keep = new LinkedHashSet<>(Arrays.asList(candidates));
-keep.addAll(probe.getFailures().keySet());
+if (probe.getFailures() != null) {
+  keep.addAll(probe.getFailures().keySet());
+}
 if (indexExpr.isPrunedBy(keep.size())) {
   return new IndexName(String.join(",", keep));
 }
Suggestion importance[1-10]: 7

__

Why: Adding a null check for probe.getFailures() before accessing keySet() is a reasonable defensive programming practice to prevent potential NullPointerException. However, the test code shows FieldCapabilitiesResponse is constructed with explicit failures maps (including Collections.emptyMap()), suggesting the API may guarantee non-null returns. The suggestion is valid but may be addressing a theoretical rather than practical issue.

Medium

Previous suggestions

Suggestions up to commit 4b4996c
CategorySuggestion                                                                                                                                    Impact
Possible issue
Handle potential null from getFailedIndices

Check if probe.getFailedIndices() returns null before adding to the set. The method
may return null when there are no failures, which would cause a NullPointerException
when passed to Arrays.asList().

opensearch/src/main/java/org/opensearch/sql/opensearch/request/IndexPruner.java [78-79]

 Set<String> keep = new LinkedHashSet<>(Arrays.asList(candidates));
-keep.addAll(Arrays.asList(probe.getFailedIndices()));
+String[] failedIndices = probe.getFailedIndices();
+if (failedIndices != null) {
+  keep.addAll(Arrays.asList(failedIndices));
+}
Suggestion importance[1-10]: 8

__

Why: This is a valid concern about potential NullPointerException. If getFailedIndices() returns null, Arrays.asList(null) would throw an exception. The suggestion correctly identifies a defensive programming need, though the actual behavior of getFailedIndices() should be verified in the OpenSearch API documentation.

Medium
General
Verify pruning logic with failed indices

The nested isPrunedBy check may incorrectly prevent pruning when failed indices are
added. Consider validating that the combined set still represents meaningful pruning
compared to the original index expression size.

opensearch/src/main/java/org/opensearch/sql/opensearch/request/IndexPruner.java [78-82]

 Set<String> keep = new LinkedHashSet<>(Arrays.asList(candidates));
-keep.addAll(Arrays.asList(probe.getFailedIndices()));
+String[] failedIndices = probe.getFailedIndices();
+if (failedIndices != null) {
+  keep.addAll(Arrays.asList(failedIndices));
+}
 if (indexExpr.isPrunedBy(keep.size())) {
   return new IndexName(String.join(",", keep));
 }
Suggestion importance[1-10]: 3

__

Why: While the suggestion adds null checking (which is valid), the claim about "incorrectly prevent pruning" is not substantiated. The logic appears intentional: failed indices are kept to avoid dropping indices that couldn't be checked. The improved code only adds null handling without addressing the stated concern about pruning logic.

Low

@github-actions

github-actions Bot commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Persistent review updated to latest commit b9a524a

OpenSearch now lists an unreachable remote cluster under failures as the expression asked of it, so keeping failed indices keeps it too and cross-cluster expressions no longer need to skip pruning.

Signed-off-by: Kai Huang <[email protected]>
@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Persistent review updated to latest commit 66650a7

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Index pruning silently drops an index it cannot reach, hiding missing shards

1 participant