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

Support Transactions for Indexer Code Execution #340

Open
Tracked by #213
pkudinov opened this issue Oct 31, 2023 · 5 comments · Fixed by #413
Open
Tracked by #213

Support Transactions for Indexer Code Execution #340

pkudinov opened this issue Oct 31, 2023 · 5 comments · Fixed by #413
Assignees

Comments

@pkudinov
Copy link
Collaborator

pkudinov commented Oct 31, 2023

see https://github.com/near/queryapi-mvp/blob/0c9fa36142538e5d009b633209272fa412c47560/runner/src/pg-client.ts#L33

@darunrs
Copy link
Collaborator

darunrs commented Nov 17, 2023

So after a lot of testing, I found the problem. It turns out that the lazy loading implementation I used for context.db was set up so that any calls to a db function would first check if the dmlHandler exists (Load on access). If not, then instantiate it. This is helpful for situations where the functions aren't used. However, this had a side effect: If a bunch of db function calls come in simultaneously, they will all poll dmlHandler and see it is uninitialized and all try and create a new one. Each initialization will try to create a pool with 10 available clients. And then all of them will create at least one client each. This will exhaust the clients very quick if there's enough of them. Changing it so that the dmlHandler is lazy loaded immediately instead. And on access, it will await its completion before calling any functions. This ensures there is no more than 1 client active for that invocation. I wrote a test as well which tries 100 simultaneous calls and verified 1 pool is created.

@darunrs
Copy link
Collaborator

darunrs commented Nov 17, 2023

We do have the intention of having transaction support as well. This requires using one connection and opening up the dmlHandler to be accessible by the Indexer class, so more work is needed there. And more tests too.

@darunrs
Copy link
Collaborator

darunrs commented Nov 18, 2023

Changes to fix connection issues: #413

@darunrs darunrs changed the title Use single DB connection per indexer invocation Resolve DB Connection Issues and Support Transactions Nov 20, 2023
@darunrs darunrs linked a pull request Nov 21, 2023 that will close this issue
@darunrs darunrs reopened this Nov 21, 2023
@darunrs
Copy link
Collaborator

darunrs commented Dec 1, 2023

Introducing the PR as it is might add more scaling barriers, which have become a greater concern lately after a prod crashing issue. It's probably more prudent to simply implement this with PgBouncer as there is no rush.

@darunrs darunrs linked a pull request Dec 1, 2023 that will close this issue
@darunrs darunrs changed the title Resolve DB Connection Issues and Support Transactions Support Transactions for Indexer Code Execution Mar 18, 2024
@pkudinov
Copy link
Collaborator Author

pgBouncer was a blocker for this, we can start working on it now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants