File bsc#1173668-0001-Low-attrd-handle-shutdown-more-cleanly.patch of Package pacemaker.19778

From 6ddb87f6a364bd5c3be651d0ede0ec1c8f48666c Mon Sep 17 00:00:00 2001
From: Ken Gaillot <kgaillot@redhat.com>
Date: Thu, 18 Oct 2018 14:33:24 -0500
Subject: [PATCH] Low: attrd: handle shutdown more cleanly

Once shutdown has started, avoid wasting time on such things as updating the
alert configuration, responding to most peer messages, starting a new election,
or writing out attributes after a CIB replace.

This doesn't really matter much since shutdown is self-contained at the moment.
There were plans to change that, but they wound up being unnecessary. These
changes still seem worthwhile, though.
---
 attrd/attrd_common_alerts.c    |  2 +-
 attrd/commands.c  |  8 ++++++++
 attrd/attrd_elections.c |  9 +++++++--
 attrd/attrd_common.c     | 25 +++++++++++++++++++++----
 attrd/main.c |  6 +++++-
 5 files changed, 42 insertions(+), 8 deletions(-)

Index: pacemaker-1.1.19+20181105.ccd6b5b10/attrd/attrd_common_alerts.c
===================================================================
--- pacemaker-1.1.19+20181105.ccd6b5b10.orig/attrd/attrd_common_alerts.c
+++ pacemaker-1.1.19+20181105.ccd6b5b10/attrd/attrd_common_alerts.c
@@ -140,7 +140,7 @@ attrd_read_options(gpointer user_data)
 void
 attrd_cib_updated_cb(const char *event, xmlNode * msg)
 {
-    if (crm_patchset_contains_alert(msg, FALSE)) {
+    if (!attrd_shutting_down() && crm_patchset_contains_alert(msg, FALSE)) {
         mainloop_set_trigger(attrd_config_read);
     }
 }
Index: pacemaker-1.1.19+20181105.ccd6b5b10/attrd/commands.c
===================================================================
--- pacemaker-1.1.19+20181105.ccd6b5b10.orig/attrd/commands.c
+++ pacemaker-1.1.19+20181105.ccd6b5b10/attrd/commands.c
@@ -577,6 +577,14 @@ attrd_peer_message(crm_node_t *peer, xml
         }
     }
 
+    if (attrd_shutting_down()) {
+        /* If we're shutting down, we want to continue responding to election
+         * ops as long as we're a cluster member (because our vote may be
+         * needed). Ignore all other messages.
+         */
+        return;
+    }
+
     peer_won = attrd_check_for_new_writer(peer, xml);
 
     if (safe_str_eq(op, ATTRD_OP_UPDATE) || safe_str_eq(op, ATTRD_OP_UPDATE_BOTH) || safe_str_eq(op, ATTRD_OP_UPDATE_DELAY)) {
Index: pacemaker-1.1.19+20181105.ccd6b5b10/attrd/attrd_elections.c
===================================================================
--- pacemaker-1.1.19+20181105.ccd6b5b10.orig/attrd/attrd_elections.c
+++ pacemaker-1.1.19+20181105.ccd6b5b10/attrd/attrd_elections.c
@@ -32,7 +32,9 @@ void
 attrd_start_election_if_needed()
 {
     if ((peer_writer == NULL)
-        && (election_state(writer) != election_in_progress)) {
+        && (election_state(writer) != election_in_progress)
+        && !attrd_shutting_down()) {
+
         crm_info("Starting an election to determine the writer");
         election_vote(writer);
     }
@@ -51,7 +53,10 @@ attrd_handle_election_op(const crm_node_
     enum election_result previous = election_state(writer);
 
     crm_xml_add(xml, F_CRM_HOST_FROM, peer->uname);
-    rc = election_count_vote(writer, xml, TRUE);
+
+    // Don't become writer if we're shutting down
+    rc = election_count_vote(writer, xml, !attrd_shutting_down());
+
     switch(rc) {
         case election_start:
             free(peer_writer);
Index: pacemaker-1.1.19+20181105.ccd6b5b10/attrd/attrd_common.c
===================================================================
--- pacemaker-1.1.19+20181105.ccd6b5b10.orig/attrd/attrd_common.c
+++ pacemaker-1.1.19+20181105.ccd6b5b10/attrd/attrd_common.c
@@ -8,6 +8,7 @@
 #include <crm_internal.h>
 
 #include <stdio.h>
+#include <stdbool.h>
 #include <errno.h>
 #include <glib.h>
 #include <regex.h>
@@ -21,7 +22,9 @@
 
 cib_t *the_cib = NULL;
 
-static gboolean shutting_down = FALSE;
+// volatile because attrd_shutdown() can be called for a signal
+static volatile bool shutting_down = FALSE;
+
 static GMainLoop *mloop = NULL;
 
 /*!
@@ -45,11 +48,25 @@ attrd_shutting_down()
 void
 attrd_shutdown(int nsig)
 {
+    // Tell various functions not to do anthing
     shutting_down = TRUE;
-    if ((mloop != NULL) && g_main_is_running(mloop)) {
-        g_main_quit(mloop);
-    } else {
+
+    // Don't respond to signals while shutting down
+    mainloop_destroy_signal(SIGTERM);
+    mainloop_destroy_signal(SIGCHLD);
+    mainloop_destroy_signal(SIGPIPE);
+    mainloop_destroy_signal(SIGUSR1);
+    mainloop_destroy_signal(SIGUSR2);
+    mainloop_destroy_signal(SIGTRAP);
+
+    if ((mloop == NULL) || !g_main_loop_is_running(mloop)) {
+        /* If there's no main loop active, just exit. This should be possible
+         * only if we get SIGTERM in brief windows at start-up and shutdown.
+         */
         crm_exit(pcmk_ok);
+    } else {
+        g_main_loop_quit(mloop);
+        g_main_loop_unref(mloop);
     }
 }
 
Index: pacemaker-1.1.19+20181105.ccd6b5b10/attrd/main.c
===================================================================
--- pacemaker-1.1.19+20181105.ccd6b5b10.orig/attrd/main.c
+++ pacemaker-1.1.19+20181105.ccd6b5b10/attrd/main.c
@@ -94,8 +94,12 @@ attrd_cpg_destroy(gpointer unused)
 static void
 attrd_cib_replaced_cb(const char *event, xmlNode * msg)
 {
-    crm_notice("Updating all attributes after %s event", event);
+    if (attrd_shutting_down()) {
+        return;
+    }
+
     if (attrd_election_won()) {
+        crm_notice("Updating all attributes after %s event", event);
         write_attributes(TRUE, FALSE);
     }
 }
openSUSE Build Service is sponsored by