Skip to content

perf: Stop capturing a stack trace for every database query - #6003

Open
abdelaziz-mahdy wants to merge 5 commits into
serverpod:mainfrom
abdelaziz-mahdy:perf/query-stack-traces
Open

abdelaziz-mahdy wants to merge 5 commits into
serverpod:mainfrom
abdelaziz-mahdy:perf/query-stack-traces

Conversation

@abdelaziz-mahdy

Copy link
Copy Markdown
Contributor

Every successful query paid for two StackTrace.current captures it almost
never used:

  1. The Postgres driver captured the caller's stack for each statement, in
    case the statement failed.
  2. PostgresDatabaseConnection._logQuery (and the SQLite one) captured one
    for session.logQuery, which drops it unless the query is logged (all
    queries, slow, or failed).

In an AOT build a capture walks the async awaiter chain. In a profile of
Note.db.findById the captures were ~15% of the request's CPU.

This PR:

  • creates the pool with the driver's ignoreStackTraces (postgres >= 3.5.19,
    constraint bumped), and takes StackTrace.current in _query's catch blocks
    instead. Taken there, in the awaiting async function, the trace is the
    awaiter chain of the code that ran the query. Errors keep the caller's
    frames, and only failed queries pay.
  • passes StackTrace.empty for successful queries to logQuery. The session
    log manager captures the trace itself when it records the query. It runs
    synchronously from the query, so the logged trace keeps the caller's frames.
    LogQueryFunction's docs describe this.

Part of #6002

Measurements

Benchmark app with findById, find(limit: 20), insertRow and a no-DB
endpoint
(backend-benchmark),
AOT, macOS arm64. Retired CPU instructions per request, mean of 3 runs in
shuffled order:

Request main this PR Change
no database 402k 398k -0.9%
findById 975k 892k -8.5%
find, 20 rows 1,667k 1,580k -5.2%
insertRow 1,008k 890k -11.7%

Every run of this PR was below every run of main for the three DB
requests.

Throughput in Docker (app on 2 vCPUs, Postgres 16, k6 open model; highest
rate held at p99 < 100 ms and < 1% errors), 3 rounds:

Scenario main this PR Change
no database 21,000 21,000 0%
findById 5,750 6,250 +8.7%
insertRow 6,250 7,000 +12.0%
mixed read/write 6,000 6,750 +12.5%

Medians; for the three DB scenarios every round of this PR held a higher rate
than every round of main. The harness steps in about 4-8% increments, so
these are step counts, not exact percentages.

How it was measured: the app is compiled AOT and run natively; for each request type, k6 sends 15 s at 1,000 req/s, and the server process's retired instructions (proc_pid_rusage(RUSAGE_INFO_V4)) before and after are divided by the number of requests. Builds of main and this PR were run alternately in shuffled order. I can share the scripts.

Tests

  • New: test_integration/database_operations/exceptions/query_error_stack_trace_test.dart.
    A failed query's stack trace still includes the function that ran the
    query.
  • New: endpoint_method_call_logging_test.dart. A logged query's stack
    trace still includes the calling endpoint method.
  • Full Serverpod test run on the combined branch (the PR branches are subsets): unit tests of serverpod, serverpod_serialization, serverpod_shared and serverpod_database pass; integration tests 1,683 (serial) + 529 (util/run_tests_integration_concurrently) pass; dart analyze is clean on this branch.

Pre-launch Checklist

  • I read the Contribute page and followed the process for submitting PRs.
  • This update contains only one single feature or bug fix and nothing else.
  • I followed the Dart Style Guide and formatted the code with dart format.
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making.
  • All existing and new tests are passing.
  • Any breaking changes are documented below.

Breaking changes

None for apps. Code that implements DatabaseSession.logQuery itself now
gets StackTrace.empty for successful queries. It can call
StackTrace.current, which is equivalent because it runs synchronously from
the query. The minimum postgres version is now 3.5.19.

Every successful query captured StackTrace.current and passed it to
session.logQuery, which drops it unless the query is logged (all queries,
slow or failed). Walking the async stack was about 2% of CPU on a read
workload.

Successful queries now pass StackTrace.empty. logQuery is called
synchronously from the query, so the session log manager captures the
trace itself when it records the query and keeps the caller's frames.
The driver captured StackTrace.current for every statement, so that an
error could carry the caller's frames. In an AOT build that was the
largest single cost of a query: about 15% of the CPU of a findById call,
because every capture walks the async awaiter chain.

The pool now sets ignoreStackTraces, and _query takes StackTrace.current
in its catch blocks instead. Taken there, in the awaiting async function,
the trace is the awaiter chain of the code that ran the query, so errors
keep the caller's frames and only failed queries pay for it. Requires
postgres 3.5.19, which added the setting.
The pubspec check requires every package to use the same postgres
constraint. Raise serverpod_embedded_postgres, serverpod_test and
serverpod_cli to ^3.5.19 to match serverpod and serverpod_database.
Resolve pubspec conflicts with the 4.1.2 version bump by regenerating the
pubspecs from the templates.
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.

1 participant