Repository navigation
fix(telemetry): binary search truncation drops - #995
Conversation
5d3c940 to
386b860
Compare
Truncation renders and measures the whole record before every drop. A by-name record with 2,000 operations over a 256,000-byte limit takes 2,680 renders on main. This test asserts fewer than 40 renders. So it fails until the drop count is found by a binary search.
Truncation dropped one result or operation at a time. After each drop it rendered and measured the whole record. So a record with n operations took up to about 2n renders of size n, which is O(n^2). Each phase now finds its drop count with a binary search. So a record takes about 2 * log2(n) renders, with any render function. The search assumes that each drop makes the record smaller. The by-name map can break this. Then truncation can drop more old operations than the fewest that fit. The chosen count is always measured, so the output never goes over the limit. Fixes #908 Co-authored-by: pcbeingused333 <[email protected]> Co-authored-by: jay23606 <[email protected]>
386b860 to
413ad01
Compare
Clearing a result replaces "result":<json> with "truncated":true. When the result JSON is 7 bytes or shorter, the record grows. So the size after clearing j results was not monotonic, and the binary search could clear every result and drop operations that the one-at-a-time check kept. Only results longer than 7 bytes are now cleared. Shorter results stay in the record, unmarked. The record after dropping causes is measured before the results search, so a record that fits then keeps every result. The array format now drops the fewest. Extra drops can happen only in the by-name format.
The by-name map writes no truncated marker. So in by-name, clearing a result of 7 bytes or less still makes the record smaller. Keeping such results made by-name truncation drop operations that the base kept. Before the results search, one render clears every small result. If it is smaller than the record after dropping causes, small results stay candidates, as in the base. Otherwise only longer results are cleared. The extra render runs only when small results exist.
The binary search can drop more results or operations than the one-at-a-time check. This happens with escaped string bodies (OTel) and with repeated names in the by-name map. The output never goes over the limit while dropping everything droppable makes it fit.
Remove the small-result probe and the separate check before the results search. Every operation with a result is a candidate again, oldest first, as in the first binary-search commit. The output never goes over the limit, as long as dropping everything droppable makes it fit. Truncation can drop more results and operations than the one-at-a-time code. That trade-off is accepted for simpler code. Tests: remove the small-result and cause-only tests. Keep the one-at-a-time comparison only for results longer than 20 bytes, where each drop makes the record smaller. Keep the upper-bound tests.
Codex AI reviewOne approach finding at Reviewed commit |
| ops[idx] = trimmed | ||
| any_result = True | ||
| count = len(ops) | ||
| found = first_fit(0, count, lambda j: fits(j, 0)) |
There was a problem hiding this comment.
Should-fix: Python searches over every operation, but JS #1293 searches only over the operations that have a result. So the two can drop different things.
Example: 5 operations, oldest first. Only the first 2 have a result: B (a big one) and C (true).
| Python tries | Results cleared | Fits? |
|---|---|---|
| 1 | B | yes |
| 2 | B and C | no. Clearing C makes the record 3 bytes bigger. |
| 3 to 5 | B and C | no. There is nothing left to clear. |
- JS only tries 0 to 2. It tries 1 first, so it clears B and stops.
- Python tries 0 to 5. It tries 2, then 4, then 5. None fit, so it drops whole operations.
With no results at all, Python also renders about 11 times where JS renders once.
Could Python search only over the operations that have a result?
🤖 AI-assisted comment
| def first_fit(low: int, high: int, fits_at: Callable[[int], bool]) -> int | None: | ||
| # Binary search for the smallest count in ``low..high`` that fits. The | ||
| # search assumes that a larger count never makes the record larger. | ||
| # Some records break this assumption, for example a by-name map with | ||
| # repeated names, a short result replaced by the truncated marker, or | ||
| # an escaped string body. Then the search can return a larger count | ||
| # than the smallest one that fits. So truncation can drop more results | ||
| # or operations than the one-at-a-time check. The returned count was | ||
| # always measured. So the output never goes over the limit, as long as | ||
| # dropping everything droppable makes it fit. | ||
| found = None | ||
| while low <= high: | ||
| middle = (low + high) // 2 | ||
| if fits_at(middle): | ||
| found, high = middle, middle - 1 | ||
| else: | ||
| low = middle + 1 | ||
| return found |
There was a problem hiding this comment.
Nit: this loop could use the standard library. bisect.bisect_left takes a key= function since Python 3.10, and this package needs 3.11 or newer:
import bisect
def first_fit(low: int, high: int, fits_at: Callable[[int], bool]) -> int | None:
counts = range(low, high + 1)
i = bisect.bisect_left(counts, True, key=fits_at)
return counts[i] if i < len(counts) else Nonerange is lazy, so this probes the same number of counts as the loop, and the count it returns was always measured to fit. One thing to know: bisect_left uses the upper midpoint, like Go's sort.Search, Java and Rust, while this loop and JS use the lower one. For low..high = 0..3 it probes 2 first, not 1. So this fits if the SDKs settle on the upper midpoint as the shared rule.
🤖 AI-assisted comment
Issue #, if available
Fixes #908
This fix grew out of aws/aws-durable-execution-sdk-java#881 by @pcbeingused333 and aws/aws-durable-execution-sdk-js#1175 by @jay23606. Their analysis and tests showed where the cost comes from and what a correct result looks like. Thank you both.
Guarantee
The output never goes over the limit, as long as dropping everything droppable makes it fit. If the record is still over the limit after every droppable part is dropped, the output is over the limit, the same as main.
Truncation can drop more results and operations than main. That is accepted for simpler and faster code. Example: one old result of 2,000 bytes, then 1,999 results of
true, array format, limit = full size - 1 byte. Main clears the old result only. This branch clears every result and drops 41 operations.Description of changes
truncate_recorddropped one result or one operation at a time. After each drop it rendered and measured the whole record. A record with n operations needed up to 2n renders, and each render costs time proportional to n. So truncation time grew with the square of the operation count.This change replaces the one-at-a-time check with a plain binary search over the drop count. It changes only
truncation.py. The export path in_export_scheduler.pycalls the publictruncate_record. So every exporter gets the change, built-in or custom.jin0..nsuch that the record fits when the results of thejoldest operations are cleared. If nojfits, every result is cleared.jfit, a binary search finds the smallestkin1..nsuch that the record fits when thekoldest operations are removed. If nokfits, every operation is removed.The phase order, the oldest-first order (operations without a
startTimelast) and the truncation markers do not change. The chosen count always comes from a real measurement. So a record takes about2 * log2(n)renders.Why it can drop more
The binary search assumes that each drop makes the rendered record smaller. Three cases break this assumption:
"truncated":true. In thearrayformat this marker is longer than a short result such as1,trueor"ok". So clearing a short result makes the record bigger. The example above is this case.operationsByNamemap, the result and error of a repeated name appear when the other operation with that name is dropped. Example (test_by_name_truncation_can_drop_more_than_the_fewest_but_fits): main drops 1 operation, this branch drops 6.OTelExporter, changes size by a different amount for each drop.In these cases the search can return a larger count than the smallest one that fits. The returned count was measured. So the output still never goes over the limit.
Timing
Measured with Python 3.12.16 on a Linux dev host. Each row truncates one record of simple successful named steps through
truncate_record. "Renders" counts the calls ofrender. In every row, main and this branch drop the same number of results and operations and produce a record of the same size.LambdaLogExporter(by-name)LambdaLogExporter(by-name)LambdaLogExporter(by-name)"Repeated name, near limit" uses
LambdaLogExporter. The older half of the operations share one name. "Custom render" wraps each operation with an extra field and uses the default measure. "Array, short results" uses the identity render. Every tenth operation has a 200-character result, and the others havetrue,1,"ok"ornull. Both main and this branch clear 1,057 results there and drop 590, 2,590 and 6,590 operations.Tests
test_truncation_renders_a_logarithmic_number_of_times: 2,000 named operations,LambdaLogExporter, limit 256,000. It asserts fewer than 40 renders. It fails on main with 2,680 renders.test_truncation_renders_a_logarithmic_number_of_times_for_a_custom_render: the same bound for a customrender.test_truncation_never_exceeds_the_limit_and_drops_oldest_first: 300 random records in the array, by-name and both formats. The records hold quotes, backslashes, non-ASCII text, emoji, U+2028, repeated, empty and missing names, results, errors, input and output. Start times are random, so the operations are out of order. The output is over the limit only when nothing is left to drop. The kept operations are the newest ones by start time, and results are cleared oldest first.test_truncation_matches_one_at_a_time_drops_for_the_array_format: every result in these 300 random records is longer than 20 bytes. So each drop makes the record smaller, and the output equals the one-at-a-time result.test_by_name_truncation_can_drop_more_than_the_fewest_but_fits: case 2 above.test_truncation_clears_every_result_before_it_drops_an_operation: only clearing every result fits. No operation is dropped.test_truncation_drops_every_operation_before_it_drops_the_input: only dropping every operation fits. The input is kept.Design
See Rules for every change.
2 * log2(n)renders with any exporter. In some records it drops more results or operations than the fewest. See "Why it can drop more".truncate_record.truncation.py+53 / -25 (14 of the added lines are comments or blank lines), README +21. Public API members: no change.Assumptions
renderandmeasureare deterministic and have no side effects. Evidence: the code of each built-in exporter. TheLambdaLogExporterenvelope timestamp changes between renders, but it has a fixed width.Decisions not covered by the issue or the design note
Checklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.