sqlite: add batched async Database API#62015
Draft
BurningEnlightenment wants to merge 11 commits intonodejs:mainfrom
Draft
sqlite: add batched async Database API#62015BurningEnlightenment wants to merge 11 commits intonodejs:mainfrom
BurningEnlightenment wants to merge 11 commits intonodejs:mainfrom
Conversation
The `node:sqlite` module `Initialize` would get quite large if both of the async and sync database templates were embedded. Therefore move the template creation into a seperate function. I've avoided the `GetConstructorTemplate` pattern, because it seems to imply exposing the template via `PER_ISOLATE_TEMPLATE_PROPERTIES` which is unnecessary in our case.
Previously all `SQLTagStore` instances had unique prototypes. Note that the class and its prototype are currently not exposed on `node:sqlite`, i.e. it currently can't be directly used for `instanceOf` checks.
The database opening and configuration logic can be shared between the sync and async API variants, therefore extract the shared implementation into a common base class.
Add a Database class skeleton which is currently only capable of opening and closing a sqlite db connection. Also notably missing is any support for asynchronous operations apart from changes to the close and dispose method signatures. However, the skeleton is capable enough to pass the most basic lifetime tests and therefore useful as a diff target for the asynchronous operations to be added. Re-enable the async database test suites and skip the tests on a case-by -case basis.
Trade a bit of db operation latency for throughput by batching db operations together before scheduling their execution on the thead pool. Completion notifications are currently delivered eagerly through an additional `uv_async` handle, but this is not strictly necessary for the chosen implementation strategy and might get removed again. Some care has been taken to not criss-cross memory allocations and de- allocations between threads as modern modern allocators tend to perform worse in such scenarios.
The linter doesn't seem to like these. Is it a linter bug?
Collaborator
|
Review requested:
|
Author
|
@geeksilva97 given that you spearheaded the previous attempt, what are your thoughts regarding this approach? |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This PR adds the asynchronous complements
DatabaseandStatementto thenode:sqlitemodule.Given that a single sqlite db connection cannot scale beyond a single CPU core, (unlike the old
sqlite3) this implementation avoids unordered dispatches to the thread pool by maintaining a strict FIFO operation queue. This allows usage of multiple db connections in parallel without relying on the internal db connection mutex (=>SQLITE_THREADSAFE=2is sufficient). Furthermore I've favored throughput over latency, e.g. it collects asynchronous operations on a db connection and dispatches them in batches to the thread pool. This helps to amortize the inherent synchronization overhead over multiple operations and it also improves cache locality.The implementation effort has reached a maturity level where the overall approach should only require relatively small incremental changes to reach the outlined API scope. Briefly the following things work:
Database.prototype.exec()without a prepared statement.Statement.prototype.get()with binding parameters (though currently not as fancy as the sync variant).Things which still need work:
v8::Promiselooses the async context/callstack. I need some guidance on how to fix that.Symbol.dispose/using-based (different PR?)DatabasetoDatabaseSyncand vice versa (different PR?)std::pmr::monotonic_buffer_resourcein a few places.Fixes #54307
Previous attempt #59109 (I've only taken the test suites from there).
API synopsis
Developer's Certificate of Origin 1.1
By making a contribution to this project, I certify that:
(a) The contribution was created in whole or in part by me and I
have the right to submit it under the open source license
indicated in the file; or
(b) The contribution is based upon previous work that, to the best
of my knowledge, is covered under an appropriate open source
license and I have the right under that license to submit that
work with modifications, whether created in whole or in part
by me, under the same open source license (unless I am
permitted to submit under a different license), as indicated
in the file; or
(c) The contribution was provided directly to me by some other
person who certified (a), (b) or (c) and I have not modified
it.
(d) I understand and agree that this project and the contribution
are public and that a record of the contribution (including all
personal information I submit with it, including my sign-off) is
maintained indefinitely and may be redistributed consistent with
this project or the open source license(s) involved.
Footnotes
I've followed the spirit of
DEP0137; should probably be reconciled with the sync API at some point. ↩