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

Concurrent sandbox support with begin concurrent #99

Open
josevalim opened this issue Jan 20, 2023 · 15 comments
Open

Concurrent sandbox support with begin concurrent #99

josevalim opened this issue Jan 20, 2023 · 15 comments

Comments

@josevalim
Copy link

Hi everyone,

SQLite3 does not currently support async tests in the sandbox. At first, it makes sense because it is single writer, but then I realized that we never commit transactions anyway so we should never be writing. That's when I found SQLite3 supports a feature called "BEGIN CONCURRENT", which allows concurrent transactions, and serializes only the commit operation, which we never do!

Therefore, would it be worth giving a try to support this? Perhaps there could be a config on this project that chooses the transaction type (concurrent or not) and we set it to concurrent with the sandbox.

WDYT?

@warmwaffles
Copy link
Member

I wonder what the drawback is for using BEGIN CONCURRENT all the time is.

@josevalim
Copy link
Author

A concurrent transaction can fail to commit as a whole, so I think outside of concurrent tests, it should certainly be opt-in behavior.

@ruslandoga
Copy link

ruslandoga commented Jan 24, 2023

I think BEGIN CONCURRENT requires a special build from begin-concurrent branch

This branch contains the "BEGIN CONCURRENT" patch. Trunk changes are periodically merged directly into this branch.

In the stock version it fails during parsing:

sqlite> BEGIN CONCURRENT;
Parse error: near "CONCURRENT": syntax error
  BEGIN CONCURRENT;
        ^--- error here

@warmwaffles
Copy link
Member

It's a shame that BEGIN CONCURRENT isn't implied because the WAL mode is enabled signalling that it would be a potentially concurrent database.

@ruslandoga
Copy link

ruslandoga commented Jan 24, 2023

@josevalim
Copy link
Author

Given this project ships with sqlite3, maybe we can ship with the begin concurrent version?

@ruslandoga
Copy link

ruslandoga commented Jan 24, 2023

Personally, I'd prefer running the official releases. Also begin-concurrent isn't merged with all released versions, e.g. it doesn't have a commit corresponding to 3.40.1.

Maybe we'd get BEGIN CONCURRENT if or once HC-tree gets more popular...

Is there maybe another way to enable concurrent tests?

Like starting and migrating pool_size in-memory databases with after_connect

Sync:

..............................................................................................................................................
Finished in 0.8 seconds (0.3s async, 0.5s sync)
142 tests, 0 failures

Async:

..............................................................................................................................................
Finished in 0.6 seconds (0.6s async, 0.00s sync)
142 tests, 0 failures

@warmwaffles
Copy link
Member

Personally, I'd prefer running the official releases.

This is generally my feelings as well. I haven't followed this feature too closely and don't know if it is going to be mainlined.

Given this project ships with sqlite3, maybe we can ship with the begin concurrent version?

I believe we'd need to change a few things in order to take that amalgamation work. It would need to be automated here and the following would cause tests to break:

@kevinlang
Copy link
Member

kevinlang commented Feb 16, 2023

If the begin-concurrent stuff was in trunk, I think that would definitely be the way to go, as it aligns more with how other Ecto adapters interface with the Sandbox. Unfortunately, it is not, yet - and I don't see any immediate plans to merge it :(

I like the idea of testing against an in-memory database, and I think I explored that approach at one point. However, it has some friction with how the existing Sandbox adapter works and ends up feeling hacky.

To expand, the current adapter has normal connections, but proxies those connections and does the necessary transaction wrapping statements on checkout/checkin. This allows e.g., Ecto migrations to run as expected, as this proxying is only enabled once the test_helper.exs enables manual mode.

If we follow a similar mechanism, what we want need to do in our custom Sandbox adapter is to, on checkout when manual mode is enabled, "convert" those connections into in-memory ones (via the serialization/deserialization interface), and on checkin "convert" them back into normal connections (where we get rid of the in-memory connection and re-connect to the real database), which feels a bit hacky. Hacky in that this approach modifies the actual connection itself and isn't a proxy in the true sense.

@ruslandoga
Copy link

ruslandoga commented Feb 16, 2023

Why not use the in-memory databases from the start? It seems like I didn't encounter any problems with the sandbox using that approach.

@warmwaffles
Copy link
Member

I would love to use memory for the database, but with how the connections are pooled, that is impossible to do. Once the connection goes idle, it closes the database and that in memory database is lost.

It's currently being discussed here elixir-sqlite/exqlite#192

@ruslandoga
Copy link

ruslandoga commented Feb 16, 2023

Once the connection goes idle, it closes the database and that in memory database is lost.

Even if so, on re-open, after_connect would be executed again (running the migrations) putting the database in a clean state for the next test. Note that I'm only suggesting using in memory databases for tests: ruslandoga/sqlite3-memory-async@master...async

By idle here, what do you mean? (I think the issue link is wrong)

@warmwaffles
Copy link
Member

Even if so, on re-open, after_connect would be executed again (running the migrations)

There will be people who use a structure.sql as well and don't build from scratch completely. I know this because I am one of them 😞.

By idle here, what do you mean?

I don't remember the details, but I was running into issues where the connection pool had 1 connection during a test and the in memory database just disappeared. I chalked that up to the connection going "idle" and closing the database. I threw some debugging in and saw that Exqlite.Connection.disconnect was called.

@josevalim
Copy link
Author

Is begin concurrent an extension? If so, could this be used to install it? https://observablehq.com/@asg017/making-sqlite-extensions-npm-installable-and-deno

@ruslandoga
Copy link

It doesn't seem to be an extension, but rather a fork.

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

No branches or pull requests

4 participants