Skip to content
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

SQLite: Don't bill for internal queries. #3605

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

kentonv
Copy link
Member

@kentonv kentonv commented Feb 25, 2025

We bill for:

  • SQL queries issued via the JavaScript SQL API.
  • DO old-style key/value storage operations.

Anything else, we assume is an internal query, and not billed.

We bill for:
- SQL queries issued via the JavaScript SQL API.
- DO old-style key/value storage operations.

Anything else, we assume is an internal query, and not billed.
@kentonv kentonv requested review from a team as code owners February 25, 2025 21:10
@shrima-cf
Copy link
Contributor

LGTM

@@ -15,6 +15,11 @@ namespace workerd {
// of KJ exceptions which become internal errors.
class SqliteKvRegulator: public SqliteDatabase::Regulator {
void onError(kj::Maybe<int> sqliteErrorCode, kj::StringPtr message) const override;

// We bill for KV operations as rows read/written.
virtual bool shouldAddQueryStats() const override {
Copy link
Contributor

Choose a reason for hiding this comment

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

(I guess the stmtCountKeys prepared statement doesn't use this regulator yet, so would be excluded from stats, but not sure if that query represents notable overhead.)

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah good catch, probably should fix that.

@jclee
Copy link
Contributor

jclee commented Feb 26, 2025

Nice... When I was thinking about how to implement something similar, I was considering adding a "reportingId" value to queries, which could then be passed as an additional parameter to addQueryStats()... That might have the advantage of allowing us to separately track both system query overhead (maybe for internal metrics?) and user query overhead. But not sure if we need that, and if not, this approach seems simpler and more performant.

I guess I also don't know enough about the query stats recording to know if running "ROLLBACK" as a system query -- after running some user queries -- results in significant overhead that we'd want included in user metrics. But maybe that's not a big deal, as long as the stats of the initial execution get recorded and are roughly proportional to what the rollback metrics would be.

@kentonv
Copy link
Member Author

kentonv commented Feb 27, 2025

I guess I also don't know enough about the query stats recording to know if running "ROLLBACK" as a system query -- after running some user queries -- results in significant overhead that we'd want included in user metrics.

The overhead of ROLLBACK is pretty minimal, the WAL just gets truncated back to an earlier point. In fact, any writes that end up rolled back never even leave the local machine, so you could argue a rollback should actually roll back the billing counters! But that would be complicated to implement and awkward to explain.

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.

5 participants