Skip to content

clientdb: append protojson elements directly when encoding attribute lists - #14626

Merged
sipsma merged 2 commits into
dagger:mainfrom
sipsma:g22-span-json
Oct 14, 2026
Merged

sipsma merged 2 commits into
dagger:mainfrom
sipsma:g22-span-json

Conversation

@sipsma

@sipsma sipsma commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

The engine stores every span's attribute, event and link lists in the per-client telemetry store as a JSON array of protojson messages (clientdb.MarshalProtoJSONs). It wrapped each protojson encoding in json.RawMessage and passed the slice to json.Marshal, which validates and compacts every element a second time. For span attributes, which carry the encoded call, that second pass cost more than protojson itself.

This change appends each element to one buffer instead.

Effect on stored data

The array decodes to the same values. Only insignificant whitespace (protojson's deliberately unstable spacing) and HTML escaping (< vs <) may differ.

Every reader either parses the array (UnmarshalProtoJSONs and its callers: span/log reconstruction, the trace subscriber, the Cloud forwarder, archive restore, trace import and inspection) or scans the bytes for a quoted key. Those scans (protoJSONStringAttr, and the markers in call_index.go, telemetry_store.go, archive_index.go and session_cloud_forwarder.go) already tolerate protojson whitespace. The empty-list encoding is still exactly [], which causalLinkTargets relies on.

Stored rows can be slightly smaller or larger. Spill and archive size budgets count those bytes, so they can trip at a slightly different point.

Measurements

  • Microbenchmark (BenchmarkMarshalProtoJSONs, a realistic span attribute set including a 2 KiB encoded call): 46.3 µs → 12.5 µs per encode, 15.3 → 13.9 KB and 211 → 170 allocations.

  • Engine CPU profiles of per-package Go builds (yq v4.49.2, one exec per package, 262 packages, concurrency 16, on a 12-CPU VM):

    • over repeated no-change rebuilds, checkValid + appendCompact were about 3.5% of engine CPU;
    • on a cold first build, json.Marshal was about 2.4% and checkValid + appendCompact about 2.5%.
  • End to end: an ABBA series (4 fresh engines per arm) measured this together with a second, independent telemetry fix (cache evidence no longer clones the call's recipe; separate PR). Medians of wall time, main → both fixes:

    • edit of one yqlib string (3 packages rebuild): 2185 → 1940 ms; ranges overlap (1930–2260 vs 1920–2000);
    • first build: 11830 → 11715 ms, within noise;
    • no change: 935 → 895 ms, within noise.

    The edit result is suggestive rather than established. Nothing regressed, and every run rebuilt exactly the expected packages.

Tests

TestMarshalProtoJSONs covers:

  • decoded values compared against the previous encoder, through both protojson and plain JSON, including HTML characters, unicode, nested lists and kvlists, bytes and empty values;
  • events and links;
  • that the byte scan for a digest attribute still finds it;
  • [] for nil and empty input, nil elements, and invalid UTF-8 still failing.

It runs in the package tests (test-split/test-packages). BenchmarkMarshalProtoJSONs compares the two encoders.

In CI (on this PR's head commit, rebased onto main): TestMarshalProtoJSONs and its five subtests ran and passed in the test-split/test-packages check (trace): dagger trace f0bdaf18fa6e092b5217ee5a6ea020e2 --test TestMarshalProtoJSONs. All checks are green.

sipsma added a commit to sipsma/dagger that referenced this pull request Oct 10, 2026
sipsma added a commit to sipsma/dagger that referenced this pull request Oct 13, 2026
sipsma added a commit to sipsma/dagger that referenced this pull request Oct 13, 2026
sipsma added a commit to sipsma/dagger that referenced this pull request Oct 14, 2026
…lists

MarshalProtoJSONs wrapped each protojson encoding in json.RawMessage and
passed the slice to json.Marshal, which validates and compacts every element
again. For span attributes that carry an encoded call this second pass costs
more than protojson itself; in an engine CPU profile of repeated no-change
rebuilds, checkValid and appendCompact alone were about 3.5% of engine CPU.

Append each element to one buffer instead. The stored array decodes to the
same values; only insignificant whitespace and HTML escaping may differ, and
the byte scans over stored attributes already tolerate protojson whitespace.

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma
sipsma merged commit ce1387e into dagger:main Oct 14, 2026
94 checks passed
sipsma added a commit to sipsma/dagger that referenced this pull request Oct 20, 2026
…en its producer's session is removed

Add a test for a failure first seen when a superseded check was cancelled:
a php-client/test run of dagger#14627 failed with "acquire shared client scope:
session ... is closed", naming the session of dagger#14626's php-client/php-stan
check on the same engine (traces 51ab194ff7d2c48ac494ff453b55daf7 and
68db9b2801268c97c90d34fcc9d617f5).

dagger#14047 gave client work typed lifecycle leases: a call shared across
sessions runs under a lease cloned from the session that started it, and
a session marked removed refuses every further clone, even from a lease
it still holds. Session removal marks the session removed before it
waits for its leases to drain, so shared work can still be running when
its producer's session is marked removed. A nested call that work then makes fails
to clone the producer's scope, and the call's other waiters receive that
error although their own sessions are alive.

The test builds two sessions with the client lifecycle tests' harness, a
real dagql cache, and a call both share. It waits for the second session
to join the call, marks the producer's session removed as session
removal begins, then lets the shared work make its nested call, and
asserts the desired behaviour: the surviving waiter gets the call's
value, whether from the shared work or by taking it over. It fails today
with the producer's closure, every run. It covers the early teardown
window, before the cache releases the producer's session.

Signed-off-by: Erik Sipsma <[email protected]>
sipsma added a commit to sipsma/dagger that referenced this pull request Oct 21, 2026
…en its producer's session is removed

Add a test for a failure first seen when a superseded check was cancelled:
a php-client/test run of dagger#14627 failed with "acquire shared client scope:
session ... is closed", naming the session of dagger#14626's php-client/php-stan
check on the same engine (traces 51ab194ff7d2c48ac494ff453b55daf7 and
68db9b2801268c97c90d34fcc9d617f5).

dagger#14047 gave client work typed lifecycle leases: a call shared across
sessions runs under a lease cloned from the session that started it, and
a session marked removed refuses every further clone, even from a lease
it still holds. Session removal marks the session removed before it
waits for its leases to drain, so shared work can still be running when
its producer's session is marked removed. A nested call that work then makes fails
to clone the producer's scope, and the call's other waiters receive that
error although their own sessions are alive.

The test builds two sessions with the client lifecycle tests' harness, a
real dagql cache, and a call both share. It waits for the second session
to join the call, marks the producer's session removed as session
removal begins, then lets the shared work make its nested call, and
asserts the desired behaviour: the surviving waiter gets the call's
value, whether from the shared work or by taking it over. It fails today
with the producer's closure, every run. It covers the early teardown
window, before the cache releases the producer's session.

Signed-off-by: Erik Sipsma <[email protected]>
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.

2 participants