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

Refactor Postgres code #1743

Merged
merged 17 commits into from
Mar 31, 2021
Merged

Refactor Postgres code #1743

merged 17 commits into from
Mar 31, 2021

Conversation

pm47
Copy link
Member

@pm47 pm47 commented Mar 26, 2021

More symmetry between postgres and sqlite, and better tests.

@pm47 pm47 force-pushed the postgres-refactor branch from 93cab7f to e0f6921 Compare March 26, 2021 10:47
pm47 added 2 commits March 26, 2021 11:49
Those classes implement traits `Databases`, `FileBackup` and
`ExclusiveLock`.

The goal is to have access to backend-specific attributes, particularly
in tests. It arguably makes the `Databases` cleaner and simpler, with a
nice symmetry between the `apply methods`.
- replace exceptions by failures+Either
- cleaner failure handlers
private def acquireExclusiveTableLock()(implicit connection: Connection): Unit = {
using(connection.createStatement()) {
statement =>
statement.executeUpdate(s"SET lock_timeout TO '${LockTimeout.toSeconds}s'")
Copy link
Member Author

Choose a reason for hiding this comment

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

@rorp. Why not use NOWAIT here?

Copy link
Member Author

Choose a reason for hiding this comment

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

I went ahead and changed it in 723d926, but I'm still interested in your rationale opting for a non-zero timeout.

Copy link
Contributor

Choose a reason for hiding this comment

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

AFAIR there was some weirdness in exceptions Pg driver threw in NOWAIT mode. Should work in both ways.

Copy link
Member Author

Choose a reason for hiding this comment

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

Seeing that lock errors seem to be handled fine in tests, I think we can go ahead with NOWAIT.

Copy link
Member Author

@pm47 pm47 Apr 9, 2021

Choose a reason for hiding this comment

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

Okay I think I know why it was done that way. There can be a conflict between a regular SQL requests checking the lock, and the background task that regularly renew the lease. If those happen at the same time, one of them will fail which will make the app stop. Adding a 5 seconds timeout provided some leeway, one of the lock attempts would just wait after the first one is completed.

Hmm no, that's wrong. Regular SQL requests only check the lease and do not attempt locking, so it can't be that. Apparently I ran into the "weird exceptions" that you were mentioning. Will dig further...

@pm47 pm47 marked this pull request as ready for review March 29, 2021 14:08
It is more consistent with sqlite, and makes sense because we don't want
to mix up testnet and mainnet databases.
rorp
rorp previously approved these changes Mar 31, 2021
Copy link
Contributor

@rorp rorp left a comment

Choose a reason for hiding this comment

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

👍

@pm47 pm47 merged commit 936f36b into master Mar 31, 2021
@pm47 pm47 deleted the postgres-refactor branch March 31, 2021 14:12
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.

3 participants