From ef707fa3f3c0ed19a524b183d0a4a60e0c30c626 Mon Sep 17 00:00:00 2001 From: Vasyl Saienko Date: Sun, 28 May 2023 18:44:09 +0000 Subject: [PATCH] [mariadb] Stop running threads on sigkill Stop monitor cluster and leader election threads on sigkill. This allows to terminate all threads from start.py and actually exit earlier than terminationGracePeriod in statefulset. Drop preStop hook which is redundant with stop_mysqld() function call. Change-Id: Ibc4b7604f00b1c5b3a398370dafed4d19929fd7d --- mariadb/Chart.yaml | 2 +- mariadb/templates/bin/_start.py.tpl | 20 ++++++++++++++++---- mariadb/templates/bin/_stop.sh.tpl | 22 ---------------------- mariadb/templates/configmap-bin.yaml | 2 -- mariadb/templates/statefulset.yaml | 5 ----- releasenotes/notes/mariadb.yaml | 1 + 6 files changed, 18 insertions(+), 34 deletions(-) delete mode 100644 mariadb/templates/bin/_stop.sh.tpl diff --git a/mariadb/Chart.yaml b/mariadb/Chart.yaml index b05d73750..140c64199 100644 --- a/mariadb/Chart.yaml +++ b/mariadb/Chart.yaml @@ -15,7 +15,7 @@ apiVersion: v1 appVersion: v10.6.7 description: OpenStack-Helm MariaDB name: mariadb -version: 0.2.55 +version: 0.2.56 home: https://mariadb.com/kb/en/ icon: http://badges.mariadb.org/mariadb-badge-180x60.png sources: diff --git a/mariadb/templates/bin/_start.py.tpl b/mariadb/templates/bin/_start.py.tpl index a05e582dc..7a87724b7 100644 --- a/mariadb/templates/bin/_start.py.tpl +++ b/mariadb/templates/bin/_start.py.tpl @@ -762,18 +762,23 @@ def check_if_i_lead(): return False -def monitor_cluster(): +def monitor_cluster(stop_event): """Function to kick off grastate configmap updating thread""" while True: + if stop_event.is_set(): + logger.info("Stopped monitor_cluster thread") + break try: update_grastate_configmap() except Exception as error: logger.error("Error updating grastate configmap: {0}".format(error)) time.sleep(state_configmap_update_period) +# Stop event +stop_event = threading.Event() # Setup the thread for the cluster monitor -monitor_cluster_thread = threading.Thread(target=monitor_cluster, args=()) +monitor_cluster_thread = threading.Thread(target=monitor_cluster, args=(stop_event,)) monitor_cluster_thread.daemon = True @@ -783,9 +788,12 @@ def launch_cluster_monitor(): monitor_cluster_thread.start() -def leader_election(): +def leader_election(stop_event): """Function to kick off leader election thread""" while True: + if stop_event.is_set(): + logger.info("Stopped leader_election thread") + break try: deadmans_leader_election() except Exception as error: @@ -794,7 +802,7 @@ def leader_election(): # Setup the thread for the leader election -leader_election_thread = threading.Thread(target=leader_election, args=()) +leader_election_thread = threading.Thread(target=leader_election, args=(stop_event,)) leader_election_thread.daemon = True @@ -886,7 +894,11 @@ def mysqld_reboot(): def sigterm_shutdown(x, y): """Shutdown the instance of mysqld on shutdown signal.""" logger.info("Got a sigterm from the container runtime, time to go.") + stop_event.set() stop_mysqld() + monitor_cluster_thread.join() + leader_election_thread.join() + sys.exit(0) # Register the signal to the handler diff --git a/mariadb/templates/bin/_stop.sh.tpl b/mariadb/templates/bin/_stop.sh.tpl deleted file mode 100644 index fc57ee3d4..000000000 --- a/mariadb/templates/bin/_stop.sh.tpl +++ /dev/null @@ -1,22 +0,0 @@ -#!/bin/bash -{{/* -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/}} - -set -xe - -exec mysqladmin \ - --defaults-file=/etc/mysql/admin_user.cnf \ - --host=localhost \ - --connect-timeout 2 \ - shutdown diff --git a/mariadb/templates/configmap-bin.yaml b/mariadb/templates/configmap-bin.yaml index 991d83d8b..ed2ba827b 100644 --- a/mariadb/templates/configmap-bin.yaml +++ b/mariadb/templates/configmap-bin.yaml @@ -33,8 +33,6 @@ data: {{ tuple "bin/_liveness.sh.tpl" . | include "helm-toolkit.utils.template" | indent 4 }} start.py: | {{ tuple "bin/_start.py.tpl" . | include "helm-toolkit.utils.template" | indent 4 }} - stop.sh: | -{{ tuple "bin/_stop.sh.tpl" . | include "helm-toolkit.utils.template" | indent 4 }} test.sh: | {{ tuple "bin/_test.sh.tpl" . | include "helm-toolkit.utils.template" | indent 4 }} {{- if .Values.conf.backup.enabled }} diff --git a/mariadb/templates/statefulset.yaml b/mariadb/templates/statefulset.yaml index c4df7579a..e1cfcdfe3 100644 --- a/mariadb/templates/statefulset.yaml +++ b/mariadb/templates/statefulset.yaml @@ -208,11 +208,6 @@ spec: containerPort: {{ tuple "oslo_db" "direct" "sst" . | include "helm-toolkit.endpoints.endpoint_port_lookup" }} command: - /tmp/start.py - lifecycle: - preStop: - exec: - command: - - /tmp/stop.sh {{ dict "envAll" . "component" "server" "container" "mariadb" "type" "readiness" "probeTemplate" (include "mariadbReadinessProbe" . | fromYaml) | include "helm-toolkit.snippets.kubernetes_probe" | indent 10 }} {{ dict "envAll" . "component" "server" "container" "mariadb" "type" "liveness" "probeTemplate" (include "mariadbLivenessProbe" . | fromYaml) | include "helm-toolkit.snippets.kubernetes_probe" | indent 10 }} volumeMounts: diff --git a/releasenotes/notes/mariadb.yaml b/releasenotes/notes/mariadb.yaml index 3602ea0b5..fc6503ba8 100644 --- a/releasenotes/notes/mariadb.yaml +++ b/releasenotes/notes/mariadb.yaml @@ -71,4 +71,5 @@ mariadb: - 0.2.53 Use constant for mysql binary name - 0.2.54 Improve leader election on cold start - 0.2.55 Improve python3 compatibility + - 0.2.56 Stop running threads on sigkill ...