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

Reload database on SIGHUP #2422

Merged
merged 4 commits into from
Sep 12, 2022
Merged

Reload database on SIGHUP #2422

merged 4 commits into from
Sep 12, 2022

Conversation

hugoghx
Copy link
Collaborator

@hugoghx hugoghx commented Sep 6, 2022

If the database url is updated on Boundary's config and then a SIGHUP
triggered, we now connect to the new database, ensure it's OK to use
and use that one going forwards instead.

@hugoghx hugoghx self-assigned this Sep 6, 2022
@hugoghx hugoghx requested a review from tmessi September 6, 2022 16:14
@hugoghx hugoghx force-pushed the hugoamvieira-controller-dbswap branch 2 times, most recently from 4c26993 to 1ebeb0e Compare September 6, 2022 17:20
@louisruch louisruch added this to the 0.10.4 milestone Sep 6, 2022
@jefferai jefferai force-pushed the hugoamvieira-controller-dbswap branch 2 times, most recently from 4b9cbe4 to e5db129 Compare September 9, 2022 17:44
@hugoghx hugoghx force-pushed the hugoamvieira-controller-dbswap branch 2 times, most recently from bca8007 to 34d500a Compare September 12, 2022 15:30
Hugo Vieira and others added 4 commits September 12, 2022 17:55
This commit introduces closing the Schema Manager's underlying driver
connection. We were only releasing the shared lock but not actually
cleaning up the db connection and relied on the fact that the process
would exit to do that for us.
This commit extracts 3 separate pieces of functionality into their own
function/method:

- Extracts the database opening with the intention to have a function
  that performs all that functionality but returns the created database
  object rather than setting it to the Server structure.
- Extracts database validation logic, to provide a common code path for
  upcoming functionality.
- Expose schema manager on the Command struct as well as providing
  common functions to acquire shared lock/close.
If the database url is updated on Boundary's config and then a SIGHUP
triggered, we now connect to the new database, ensure it's OK to use
and use that one going forwards instead.

This allows a controller to be deployed in a more dynamic infrastructure
where, for example, database credentials could change, without having
to restart the controller. In this case only a database url config
change and a SIGHUP signal to all controllers would be required.

This uses an atomic.Pointer to store wrapped DB objects. This allows
atomically swapping to the new object. A timeout is introduced so that
if the object is swapped after the existing one has been looked up it
isn't immediately invalidated. The timeout is a maximum; a ticker once a
second that looks at stats to determine how many database connections
are still in use and then closes the driver as soon as none are in use.

This could be done with mutexes to avoid checking stats in a loop, but
that carries its own risk: if a write lock is attempted to be acquired
while a query is holding a read lock, the write lock will block, and any
subsequent read locks will also block since write locks take priority.
As a result, if a query is long-running or timing out, all other queries
in the system will become blocked. Our default user request timeout is
90 seconds and some jobs have no timeout at all, so this is not a purely
theoretical concern, even if it's unlikely.

Refs: #2435
Co-authored-by: Jeff Mitchell <[email protected]>
@hugoghx hugoghx force-pushed the hugoamvieira-controller-dbswap branch from 34d500a to 9002930 Compare September 12, 2022 16:55
@hugoghx hugoghx merged commit f3f0c55 into main Sep 12, 2022
@hugoghx hugoghx deleted the hugoamvieira-controller-dbswap branch September 12, 2022 17:05
tmessi pushed a commit that referenced this pull request Sep 12, 2022
If the database url is updated on Boundary's config and then a SIGHUP
triggered, we now connect to the new database, ensure it's OK to use
and use that one going forwards instead.

This allows a controller to be deployed in a more dynamic infrastructure
where, for example, database credentials could change, without having
to restart the controller.

This operation is done atomically to prevent errors and inconsistency.
A timeout is present so that if the object is swapped after the
existing one has been looked up it isn't immediately invalidated.
The timeout is a maximum; a ticker once a second that looks at
stats to determine how many database connections are still in use
and then closes the driver as soon as none are in use.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants