Page MenuHomePhabricator

[server] Use single connection with DB for migrations
ClosedPublic

Authored by jacek on Jul 13 2022, 6:55 AM.
Tags
None
Referenced Files
Unknown Object (File)
Tue, Nov 26, 1:37 AM
Unknown Object (File)
Tue, Nov 26, 12:29 AM
Unknown Object (File)
Mon, Nov 25, 10:47 PM
Unknown Object (File)
Thu, Nov 21, 3:22 PM
Unknown Object (File)
Sat, Nov 2, 9:48 AM
Unknown Object (File)
Oct 25 2024, 3:54 AM
Unknown Object (File)
Oct 7 2024, 5:06 PM
Unknown Object (File)
Oct 7 2024, 5:06 PM

Details

Summary

Database migrations are performed wrapped in transactions that require single connection with database.
This diff fixes problem with deadlocks, that appeared when trying to update permissions for all threads, which was caused by using connection pool.
After the change, until all migrations are performed, only one connection to database is established.

Test Plan

Run multiple times migration from D4071, that caused problems with locks. The issue no longer appears.

Diff Detail

Repository
rCOMM Comm
Lint
No Lint Coverage
Unit
No Test Coverage

Event Timeline

ashoat requested changes to this revision.Jul 13 2022, 8:33 AM

Run multiple times migration from D4071, that caused problems with locks. The issue no longer appears.

WOO!!! :)

keyserver/src/database/database.js
20–22 ↗(On Diff #14430)

Let's move this to the top so we can skip the await if migrationConnection is already set

26 ↗(On Diff #14430)

We can probably skip this and the scriptContext stuff, since the migrations won't ever be running in a script context

105 ↗(On Diff #14430)

One big change this will cause is that any script will be using just a single connection. To keep the scope of the change narrowly focused on resolving the issue at hand, it would probably be best to default to using the standard loadPool mechanism.

We can replace setMigrationsConcluded() with setConnectionContext() that takes something like:

type ConnectionContext = {
  +migrationsActive?: boolean,
};

Then we can set it to { migrationsActive: true } when starting the migrations, and to { migrationsActive: false } after they finish. What do you think?

This revision now requires changes to proceed.Jul 13 2022, 8:33 AM
keyserver/src/database/database.js
105 ↗(On Diff #14430)

Yes, it seems to be better solution

Introduce ConnectionContext

ashoat added inline comments.
keyserver/src/database/database.js
110 ↗(On Diff #14457)

Looks great!! One last thing – can we add this?

if (!connectionContext.migrationsActive) {
  migrationConnection.end();
  migrationConnection = undefined;
}
This revision is now accepted and ready to land.Jul 14 2022, 6:40 AM

end migrationConnection when migrations are deactivated