Refactor thread management and shutdown

This introduces a new module to handle thread creation and shutdown.
All page server threads are now registered in a global hash map, and
there's a function to request individual threads to shut down gracefully.

Thread shutdown request is signalled to the thread with a flag, as well
as a Future that can be used to wake up async operations if shutdown is
requested. Use that facility to have the libpq listener thread respond
to pageserver shutdown, based on Kirill's earlier prototype
(https://github.com/zenithdb/zenith/pull/1088). That addresses
https://github.com/zenithdb/zenith/issues/1036, previously the libpq
listener thread would not exit until one more connection arrives.

This also eliminates a resource leak in the accept() loop. Previously,
we added the JoinHanlde of each new thread to a vector but old handles
for threads that had already exited were never removed.
This commit is contained in:
Heikki Linnakangas
2022-01-14 18:36:10 +02:00
parent bad1dd9759
commit dab30c27b6
14 changed files with 590 additions and 395 deletions

View File

@@ -117,7 +117,11 @@ fn main() -> anyhow::Result<()> {
.name("Http thread".into())
.spawn(move || {
let router = http::make_router();
endpoint::serve_thread_main(router, http_listener)
endpoint::serve_thread_main(
router,
http_listener,
std::future::pending(), // never shut down
)
})?,
// Spawn a thread to listen for connections. It will spawn further threads
// for each connection.