aboutsummaryrefslogtreecommitdiff
path: root/ansible/roles/mariadb/tasks/recover_cluster.yml
blob: b2eae10089e839955bcc758c78f6727628de40a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
---
- name: Checking for mariadb cluster
  fail:
    msg: "MariaDB cluster was not found. Is your inventory correct?"
  when: not mariadb_cluster_exists

- name: Cleaning up temp file on localhost
  file:
    path: "{{ item }}"
    state: absent
  delegate_to: localhost
  connection: local
  changed_when: false
  check_mode: no
  run_once: true
  with_fileglob: "/tmp/kolla_mariadb_recover_inventory_name_*"

- block:
    - name: Stop MariaDB containers
      become: true
      kolla_container:
        name: "{{ mariadb_service.container_name }}"
        action: "stop_container"
        common_options: "{{ docker_common_options }}"
        ignore_missing: true

    # Run wsrep recovery with detach=false to block until completion. Use a
    # different container name to avoid the mariadb container being removed.
    - name: Run MariaDB wsrep recovery
      become: true
      kolla_container:
        action: "start_container"
        common_options: "{{ docker_common_options }}"
        detach: false
        environment:
          KOLLA_CONFIG_STRATEGY: "{{ config_strategy }}"
          BOOTSTRAP_ARGS: "--wsrep-recover"
        image: "{{ mariadb_service.image }}"
        labels:
          BOOTSTRAP:
        name: mariadb_wsrep_recovery
        restart_policy: oneshot
        volumes: "{{ mariadb_service.volumes }}"

    - name: Copying MariaDB log file to /tmp
      become: true
      command: "cp {{ container_engine_volumes_path }}/kolla_logs/_data/mariadb/mariadb.log /tmp/mariadb_tmp.log"

    # Look for sequence number in logs. Format is:
    # WSREP: Recovered position: <UUID>:<seqno>.
    - name: Get MariaDB wsrep recovery seqno
      become: true
      shell: awk -F" " '/Recovered position/{seqno=$NF} END{split(seqno, a, ":"); print a[2]}' /tmp/mariadb_tmp.log
      register: wsrep_recovery_seqno

    - name: Removing MariaDB log file from /tmp
      become: true
      file:
        path: /tmp/mariadb_tmp.log
        state: absent
      changed_when: false
      check_mode: no

    - name: Registering MariaDB seqno variable
      set_fact:
        seqno: "{{ wsrep_recovery_seqno.stdout_lines[0] }}"
      changed_when: false

    - name: Comparing seqno value on all mariadb hosts
      shell:
        cmd: |
          if [[ ! -z {{ hostvars[inventory_hostname]['seqno'] }} && ! -z {{ hostvars[item]['seqno'] }} &&
          {{ hostvars[inventory_hostname]['seqno'] }} =~ ^-?[0-9]+$ && {{ hostvars[item]['seqno'] }} =~ ^-?[0-9]+$ &&
          {{ hostvars[inventory_hostname]['seqno'] }} -lt {{ hostvars[item]['seqno'] }} ]]; then echo {{ hostvars[item]['seqno'] }}; fi
      with_items: "{{ groups[mariadb_shard_group] }}"
      register: seqno_compare
      args:
        executable: /bin/bash
      changed_when: false

    - name: Writing hostname of host with the largest seqno to temp file
      template:
        src: "hostname.j2"
        dest: "{{ mariadb_recover_tmp_file_path }}"
        mode: "0644"
      delegate_to: localhost
      connection: local
      changed_when: false
      when: seqno_compare.results | map(attribute='stdout') | join('') == ""

    - name: Registering mariadb_recover_inventory_name from temp file
      connection: local
      set_fact:
        mariadb_recover_inventory_name: "{{ lookup('file', mariadb_recover_tmp_file_path) }}"
  when:
    - mariadb_recover_inventory_name is not defined

- name: Store bootstrap and master hostnames into facts
  set_fact:
    bootstrap_host: "{{ mariadb_recover_inventory_name }}"

- name: Set grastate.dat file from MariaDB container in bootstrap host
  become: true
  lineinfile:
    create: yes
    dest: "{{ container_engine_volumes_path }}/mariadb/_data/grastate.dat"
    regexp: 'safe_to_bootstrap:(.*)$'
    line: 'safe_to_bootstrap: 1'
    state: present
  when:
    - bootstrap_host is defined
    - bootstrap_host == inventory_hostname

- name: Refresh galera.cnf to set first MariaDB container as primary
  vars:
    service_name: "mariadb"
    service: "{{ mariadb_services[service_name] }}"
    primary_host_on_recovery: "{{ bootstrap_host == inventory_hostname }}"
  merge_configs:
    sources:
      - "{{ role_path }}/templates/galera.cnf.j2"
      - "{{ node_custom_config }}/galera.cnf"
      - "{{ node_custom_config }}/mariadb/{{ inventory_hostname }}/galera.cnf"
    dest: "{{ node_config_directory }}/{{ service_name }}/galera.cnf"
    mode: "0660"
  become: true
  when:
    - bootstrap_host is defined

- name: Starting first MariaDB container
  become: true
  kolla_container:
    action: "start_container"
    common_options: "{{ docker_common_options }}"
    environment:
      KOLLA_CONFIG_STRATEGY: "{{ config_strategy }}"
      BOOTSTRAP_ARGS: "--wsrep-new-cluster"
    image: "{{ mariadb_service.image }}"
    labels:
      BOOTSTRAP:
    name: "{{ mariadb_service.container_name }}"
    restart_policy: oneshot
    volumes: "{{ mariadb_service.volumes }}"
  when:
    - bootstrap_host is defined
    - bootstrap_host == inventory_hostname

- name: Wait for first MariaDB container
  wait_for:
    host: "{{ api_interface_address }}"
    port: "{{ mariadb_port }}"
    connect_timeout: 1
    timeout: 60
    search_regex: "MariaDB"
  register: check_mariadb_port
  until: check_mariadb_port is success
  retries: 10
  delay: 6
  when:
    - bootstrap_host is defined
    - bootstrap_host == inventory_hostname

- name: Wait for MariaDB to become operational
  become: true
  kolla_toolbox:
    container_engine: "{{ kolla_container_engine }}"
    module_name: mysql_query
    module_args:
      login_db: "mysql"
      login_host: "{{ api_interface_address }}"
      login_port: "{{ mariadb_port }}"
      login_user: "{{ database_user }}"
      login_password: "{{ database_password }}"
      query: 'SHOW STATUS LIKE "wsrep_evs_state"'
  register: result
  until: result.query_result[0][0]['Value'] == "OPERATIONAL"
  retries: 10
  delay: 6
  when:
    - bootstrap_host is defined
    - bootstrap_host == inventory_hostname

- name: Restart slave MariaDB container(s)
  become: true
  kolla_container:
    action: "start_container"
    common_options: "{{ docker_common_options }}"
    name: "{{ item.value.container_name }}"
    image: "{{ item.value.image }}"
    volumes: "{{ item.value.volumes }}"
    dimensions: "{{ item.value.dimensions }}"
    environment: "{{ item.value.environment | default({}) }}"
    healthcheck: "{{ item.value.healthcheck | default(omit) }}"
  with_dict: "{{ mariadb_services | select_services_enabled_and_mapped_to_host }}"
  when:
    - bootstrap_host is defined
    - bootstrap_host != inventory_hostname

- name: Wait for slave MariaDB
  wait_for:
    host: "{{ api_interface_address }}"
    port: "{{ mariadb_port }}"
    connect_timeout: 1
    timeout: 60
    search_regex: "MariaDB"
  register: check_mariadb_port
  until: check_mariadb_port is success
  retries: 10
  delay: 6
  when:
    - bootstrap_host is defined
    - bootstrap_host != inventory_hostname

- name: Unset pc.bootstrap for primary MariaDB galera.cnf for next restart
  vars:
    service_name: "mariadb"
    service: "{{ mariadb_services[service_name] }}"
    primary_host_on_recovery: false
  merge_configs:
    sources:
      - "{{ role_path }}/templates/galera.cnf.j2"
      - "{{ node_custom_config }}/galera.cnf"
      - "{{ node_custom_config }}/mariadb/{{ inventory_hostname }}/galera.cnf"
    dest: "{{ node_config_directory }}/{{ service_name }}/galera.cnf"
    mode: "0660"
  become: true
  when:
    - bootstrap_host is defined
    - bootstrap_host == inventory_hostname

- name: Restart master MariaDB container(s)
  become: true
  kolla_container:
    action: "recreate_or_restart_container"
    common_options: "{{ docker_common_options }}"
    name: "{{ item.value.container_name }}"
    image: "{{ item.value.image }}"
    volumes: "{{ item.value.volumes }}"
    dimensions: "{{ item.value.dimensions }}"
    environment: "{{ item.value.environment | default({}) }}"
    healthcheck: "{{ item.value.healthcheck | default(omit) }}"
  with_dict: "{{ mariadb_services | select_services_enabled_and_mapped_to_host }}"
  when:
    - bootstrap_host is defined
    - bootstrap_host == inventory_hostname

- name: Wait for master mariadb
  wait_for:
    host: "{{ api_interface_address }}"
    port: "{{ mariadb_port }}"
    connect_timeout: 1
    timeout: 60
    search_regex: "MariaDB"
  register: check_mariadb_port
  until: check_mariadb_port is success
  retries: 10
  delay: 6
  when:
    - bootstrap_host is defined
    - bootstrap_host == inventory_hostname

- import_tasks: check.yml