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)
Sat, Jul 6, 5:03 PM
Unknown Object (File)
Sat, Jul 6, 5:03 PM
Unknown Object (File)
Sat, Jul 6, 5:03 PM
Unknown Object (File)
Sat, Jul 6, 5:03 PM
Unknown Object (File)
Sat, Jul 6, 5:03 PM
Unknown Object (File)
Sat, Jul 6, 5:02 PM
Unknown Object (File)
Fri, Jul 5, 3:14 AM
Unknown Object (File)
Thu, Jul 4, 12:10 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
Lint Not Applicable
Unit
Tests Not Applicable

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