Skip to content

SOLR-18620: validate preFetchDocs and clear the ValueSourceAugmenter prefetch cache - #5290

Open
ispringer wants to merge 8 commits into
apache:mainfrom
ispringer:SOLR-18620
Open

ispringer wants to merge 8 commits into
apache:mainfrom
ispringer:SOLR-18620

Conversation

@ispringer

@ispringer ispringer commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

https://issues.apache.org/jira/browse/SOLR-18620

Description

Follow-up to SOLR-17775. The preFetchDocs param and the prefetch cache in ValueSourceAugmenter have a few rough edges:

  • preFetchDocs=abc throws a NumberFormatException and preFetchDocs=-1 throws a NegativeArraySizeException (from new int[-1] in setContext). Both surface as 500s instead of 400s.
  • The public constructor now calls qparser.getParam(...), so passing a null QParser (previously fine, since it was only stored) throws an NPE.
  • When setContext gets a context with no DocList, it returns before replacing cachedValuesById, so a reused transformer can serve values cached against an earlier searcher.

Solution

  • Parse preFetchDocs in a helper that throws SolrException(BAD_REQUEST) for non-numeric or negative values, and falls back to the default of 1000 when the QParser is null.
  • Clear cachedValuesById at the start of every setContext, before the early return.

Developed with Claude Code (Opus 5.5) assisting; I reviewed the changes.

Tests

Added TestValueSourceAugmenterPrefetchLimit (the SOLR-18181 version of this test is on branch_9x but not main), with assertions that every returned doc has the function value and that it equals the doc's id, below and above the prefetch limit. New cases cover preFetchDocs=0, invalid values (abc, -1) returning 400, a null QParser falling back to the default limit of 1000, and a stale-cache case: values cached from one searcher, then a document added and a replacement searcher opened, then setContext with no DocList must return the new searcher's value for the same docid.

Ran TestValueSourceAugmenterPrefetchLimit, ReturnFieldsTest, TestPseudoReturnFields, and ./gradlew :solr:core:check -x test.

Checklist

  • I have reviewed the guidelines for How to Contribute and my code conforms to the standards described there to the best of my ability.
  • I have created a Jira issue and added the issue ID to my pull request title.
  • I have given Solr maintainers access to contribute to my PR branch. (optional but recommended, not available for branches on forks living under an organisation)
  • I have developed this patch against the main branch.
  • I have run ./gradlew check.
  • I have added tests for my changes.
  • I have added documentation for the Reference Guide
  • I have added a changelog entry for my change

🤖 Generated with Claude Code

Ian Springer and others added 2 commits November 4, 2026 10:18
…prefetch cache

- Reject non-numeric or negative preFetchDocs with BAD_REQUEST instead of
  a NumberFormatException / NegativeArraySizeException (500).
- Tolerate a null QParser in the constructor; use the default limit.
- Clear cachedValuesById on every setContext so a context without a
  DocList can't serve values cached against an earlier searcher.
- Add TestValueSourceAugmenterPrefetchLimit, checking function values on
  both sides of the prefetch limit, preFetchDocs=0, invalid values, and
  the stale-cache case.

Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
@nick-boss-tech

Copy link
Copy Markdown
Contributor

Looks good. One suggestion: strengthen the cache test by using a replacement searcher with a different value for the same document ID.

Use numdocs(), whose value for the same docid differs once a document
is added, so a stale cache would return a provably wrong value.

Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
@ispringer

Copy link
Copy Markdown
Contributor Author

Good call. The test now caches numdocs() values from one searcher, adds a doc, commits, and checks that setContext on the replacement searcher (with no doc list) returns the new searcher's value for the same docid. With the fix reverted, it fails with expected:<1011> but was:<1010>.


Written with Claude Code (Opus 5.5)

Ian Springer and others added 2 commits November 9, 2026 17:13
- Clear cachedValuesById before any setContext step that can throw.
- testNullQParserUsesDefault now checks the default limit of 1000 is applied.
- Trim the changelog entry to the user-visible change.

Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
@github-actions github-actions Bot added the documentation Improvements or additions to documentation label Nov 13, 2026
@ispringer

Copy link
Copy Markdown
Contributor Author

@nick-boss-tech - can you take another look when you have a chance? thanks!

Index across several segments (NoMergePolicyFactory, a commit every 300
docs) and add testOutOfDocIdOrderAcrossSegments: results sorted in reverse
docid order, with a forward-only docValues function, on both sides of the
prefetch limit. The prefetch must sort the docids and switch segments at each
docBase, or the per-segment FunctionValues reject out-of-order docs.

Also use NUM_DOCS throughout instead of MAX_PREFETCH_SIZE + 10.

Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
@nick-boss-tech

Copy link
Copy Markdown
Contributor

It looks good now. Thanks!

… honored

- An IOException during prefetch now reports the docid being computed, as the
  per-doc fallback already does.
- The preFetchDocs=2000 and preFetchDocs=0 tests now check the cache size,
  since the on-demand fallback produces the same output as the cache.

Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
@dsmiley
dsmiley self-requested a review November 24, 2026 16:33

@dsmiley dsmiley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for reporting & fixing this!

Comment on lines +61 to 77
private static int parseMaxPrefetchSize(QParser qparser) {
String raw = qparser != null ? qparser.getParam(PREFETCH_DOCS_PARAM) : null;
if (raw == null) {
return DEFAULT_MAX_PREFETCH_SIZE;
}
try {
int value = Integer.parseInt(raw);
if (value >= 0) {
return value;
}
} catch (NumberFormatException e) {
// fall through
}
throw new SolrException(
SolrException.ErrorCode.BAD_REQUEST,
PREFETCH_DOCS_PARAM + " must be a non-negative integer: " + raw);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The standard approach is to use org.apache.solr.common.params.SolrParams#getInt(java.lang.String, int) to avoid repeating all this awkward code everywhere we get an int from params. You can get the params from QParser to call it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

Co-Authored-By: Claude <svc-devxp-claude@slack-corp.com>
@ispringer
ispringer requested a review from dsmiley November 28, 2026 15:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cat:search documentation Improvements or additions to documentation tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants