From af322ac89718bbf9bd4973bf5d83879c8b3f40eb Mon Sep 17 00:00:00 2001 From: Michael Tharp Date: Fri, 18 Mar 2011 10:45:31 -0400 Subject: [PATCH] Add KILL command to pause a db and terminate all connections. --- include/janitor.h | 1 + src/admin.c | 40 ++++++++++++++++++++++++++++++++++++++++ src/janitor.c | 2 +- 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/include/janitor.h b/include/janitor.h index e0e1dc8..eaa813a 100644 --- a/include/janitor.h +++ b/include/janitor.h @@ -21,4 +21,5 @@ void config_postprocess(void); void resume_all(void); void per_loop_maint(void); bool suspend_socket(PgSocket *sk, bool force) _MUSTCHECK; +void kill_pool(PgPool *pool); diff --git a/src/admin.c b/src/admin.c index 0125de7..3498020 100644 --- a/src/admin.c +++ b/src/admin.c @@ -911,6 +911,45 @@ static bool admin_cmd_pause(PgSocket *admin, const char *arg) return true; } +/* Command: KILL */ +static bool admin_cmd_kill(PgSocket *admin, const char *arg) +{ + struct List *item, *tmp; + PgDatabase *db; + PgPool *pool; + + if (!admin->admin_user) + return admin_error(admin, "admin access needed"); + + if (cf_pause_mode) + return admin_error(admin, "already suspended/paused"); + + if (!arg[0]) + return admin_error(admin, "a database is required"); + + log_info("KILL '%s' command issued", arg); + db = find_database(arg); + if (db == NULL) { + db = register_auto_database(arg); + if (db == NULL) { + return admin_error(admin, "no such database: %s", arg); + } else { + slog_info(admin, "registered new auto-database for KILL: %s", arg); + } + } + if (db == admin->pool->db) + return admin_error(admin, "cannot kill admin db: %s", arg); + + db->db_paused = 1; + statlist_for_each_safe(item, &pool_list, tmp) { + pool = container_of(item, PgPool, head); + if (pool->db == db) + kill_pool(pool); + } + + return admin_ready(admin, "KILL"); +} + /* extract substring from regex group */ static void copy_arg(const char *src, regmatch_t *glist, int gnum, char *dst, unsigned dstmax) @@ -1015,6 +1054,7 @@ static bool admin_cmd_show(PgSocket *admin, const char *arg) } static struct cmd_lookup cmd_list [] = { + {"kill", admin_cmd_kill}, {"pause", admin_cmd_pause}, {"reload", admin_cmd_reload}, {"resume", admin_cmd_resume}, diff --git a/src/janitor.c b/src/janitor.c index bed166d..350d3d4 100644 --- a/src/janitor.c +++ b/src/janitor.c @@ -564,7 +564,7 @@ void janitor_setup(void) safe_evtimer_add(&full_maint_ev, &full_maint_period); } -static void kill_pool(PgPool *pool) +void kill_pool(PgPool *pool) { const char *reason = "database removed"; -- 2.40.0