-
Notifications
You must be signed in to change notification settings - Fork 0
Gzip-compress Solr cache payloads; cap on compressed size (100 MB) #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
695561c
Gzip-compress Solr cache payloads; cap on compressed size (100 MB)
Robbie1977 268a2d8
Address PR review: defensive cache decode, stronger cap test, drop ma…
Robbie1977 b9b6eef
Address review round 2: self-heal corrupt cache entries; comment accu…
Robbie1977 0711ff3
Potential fix for pull request finding
Robbie1977 ac30b35
Potential fix for pull request finding
Robbie1977 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| """Unit tests for gzip-compressed Solr cache payloads (no network).""" | ||
| import json | ||
| from vfbquery.solr_result_cache import ( | ||
| _encode_cache_field, _decode_cache_field, _CACHE_GZIP_PREFIX, SolrResultCache, | ||
| ) | ||
|
|
||
|
|
||
| def test_roundtrip_compresses_and_restores(): | ||
| env = json.dumps({"result": {"rows": list(range(5000))}, "cached_at": "x"}) | ||
| enc = _encode_cache_field(env) | ||
| assert enc.startswith(_CACHE_GZIP_PREFIX) | ||
| assert len(enc) < len(env) | ||
| assert _decode_cache_field(enc) == env | ||
|
|
||
|
|
||
| def test_decode_returns_raw_string_on_corrupt_gz_payload(): | ||
| # A gz:-prefixed but undecodable value must not raise; it returns the raw | ||
| # string so the caller's json.loads fails and the entry is treated as invalid. | ||
| for bad in (_CACHE_GZIP_PREFIX + "!!!not-base64!!!", _CACHE_GZIP_PREFIX + "AAAA"): | ||
| assert _decode_cache_field(bad) == bad | ||
|
|
||
|
|
||
| def test_decode_handles_legacy_plain_json_and_list_shape(): | ||
| legacy = json.dumps({"result": 1}) | ||
| assert _decode_cache_field(legacy) == legacy | ||
| assert _decode_cache_field([legacy]) == legacy | ||
| enc = _encode_cache_field(legacy) | ||
| assert _decode_cache_field([enc]) == legacy | ||
|
|
||
|
|
||
| def test_cap_is_enforced_on_compressed_not_raw_size(): | ||
| # Small cap + a highly compressible payload: the RAW JSON must exceed the cap | ||
| # while the gzip+base64 form stays under it, proving the cap is on the stored | ||
| # (compressed) size, not the raw size. Kept fast/memory-light via repetition. | ||
| cap_mb = 1 | ||
| c = SolrResultCache(max_result_size_mb=cap_mb) | ||
| cap = cap_mb * 1024 * 1024 | ||
| assert c.max_result_size_bytes == cap | ||
| payload = json.dumps({"result": {"rows": ["x" * 100] * 50000}}) # ~5 MB raw, compresses hard | ||
| raw = len(payload.encode("utf-8")) | ||
| compressed = len(_encode_cache_field(payload).encode("utf-8")) | ||
| assert raw > cap, f"raw {raw} should exceed cap {cap}" | ||
| assert compressed < cap, f"compressed {compressed} should be under cap {cap}" | ||
|
|
||
|
|
||
| def test_env_override(monkeypatch): | ||
| monkeypatch.setenv("VFBQUERY_MAX_RESULT_MB", "250") | ||
| assert SolrResultCache().max_result_size_mb == 250 | ||
|
|
||
|
|
||
| def test_create_metadata_no_longer_rejects_large_raw(): | ||
| c = SolrResultCache(max_result_size_mb=1) | ||
| meta = c._create_cache_metadata({"rows": list(range(200000))}) | ||
| assert meta is not None | ||
| assert meta["result_size"] > 1024 * 1024 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.