forked from EnterpriseDB/repmgr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepmgr-action-bdr.c
416 lines (338 loc) · 10.4 KB
/
repmgr-action-bdr.c
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
* repmgr-action-standby.c
*
* Implements BDR-related actions for the repmgr command line utility
*
* Copyright (c) 2ndQuadrant, 2010-2017
*/
#include "repmgr.h"
#include "repmgr-client-global.h"
#include "repmgr-action-bdr.h"
/*
* do_bdr_register()
*
* As each BDR node is its own master, registering a BDR node
* will create the repmgr metadata schema if necessary.
*/
void
do_bdr_register(void)
{
PGconn *conn = NULL;
BdrNodeInfoList bdr_nodes = T_BDR_NODE_INFO_LIST_INITIALIZER;
ExtensionStatus extension_status;
t_node_info node_info = T_NODE_INFO_INITIALIZER;
RecordStatus record_status;
PQExpBufferData event_details;
bool success = true;
char dbname[MAXLEN];
/* sanity-check configuration for BDR-compatability */
if (config_file_options.replication_type != REPLICATION_TYPE_BDR)
{
log_error(_("cannot run BDR REGISTER on a non-BDR node"));
exit(ERR_BAD_CONFIG);
}
/* store the database name for future reference */
get_conninfo_value(config_file_options.conninfo, "dbname", dbname);
conn = establish_db_connection(config_file_options.conninfo, true);
if (!is_bdr_db(conn))
{
log_error(_("database \"%s\" is not BDR-enabled"), dbname);
log_hint(_("when using repmgr with BDR, the repmgr schema must be stored in the BDR database"));
PQfinish(conn);
exit(ERR_BAD_CONFIG);
}
/* Check that there are at most 2 BDR nodes */
get_all_bdr_node_records(conn, &bdr_nodes);
if (bdr_nodes.node_count == 0)
{
log_error(_("database \"%s\" is BDR-enabled but no BDR nodes were found"), dbname);
PQfinish(conn);
exit(ERR_BAD_CONFIG);
}
if (bdr_nodes.node_count > 2)
{
log_error(_("repmgr can only support BDR clusters with 2 nodes"));
log_detail(_("this BDR cluster has %i nodes"), bdr_nodes.node_count);
PQfinish(conn);
exit(ERR_BAD_CONFIG);
}
/* check whether repmgr extension exists, and that any other nodes are BDR */
extension_status = get_repmgr_extension_status(conn);
if (extension_status == REPMGR_UNKNOWN)
{
log_error(_("unable to determine status of \"repmgr\" extension in database \"%s\""),
dbname);
PQfinish(conn);
}
if (extension_status == REPMGR_UNAVAILABLE)
{
log_error(_("\"repmgr\" extension is not available"));
PQfinish(conn);
}
if (extension_status == REPMGR_INSTALLED)
{
if (!is_bdr_repmgr(conn))
{
log_error(_("repmgr metadatabase contains records for non-BDR nodes"));
exit(ERR_BAD_CONFIG);
}
}
else
{
log_debug("creating repmgr extension in database \"%s\"", dbname);
begin_transaction(conn);
if (!create_repmgr_extension(conn))
{
log_error(_("unable to create repmgr extension - see preceding error message(s); aborting"));
rollback_transaction(conn);
PQfinish(conn);
exit(ERR_BAD_CONFIG);
}
commit_transaction(conn);
}
/* check for a matching BDR node */
{
bool node_exists = bdr_node_exists(conn, config_file_options.node_name);
if (node_exists == false)
{
log_error(_("no BDR node with node_name \"%s\" found"), config_file_options.node_name);
log_hint(_("\"node_name\" in repmgr.conf must match \"node_name\" in bdr.bdr_nodes\n"));
PQfinish(conn);
exit(ERR_BAD_CONFIG);
}
}
/*
* before adding the extension tables to the replication set,
* if any other BDR nodes exist, populate repmgr.nodes with a copy
* of existing entries
*
* currently we won't copy the contents of any other tables
*
*/
{
NodeInfoList local_node_records = T_NODE_INFO_LIST_INITIALIZER;
get_all_node_records(conn, &local_node_records);
if (local_node_records.node_count == 0)
{
BdrNodeInfoList bdr_nodes = T_BDR_NODE_INFO_LIST_INITIALIZER;
BdrNodeInfoListCell *bdr_cell;
get_all_bdr_node_records(conn, &bdr_nodes);
if (bdr_nodes.node_count == 0)
{
log_error(_("unable to retrieve any BDR node records"));
PQfinish(conn);
exit(ERR_BAD_CONFIG);
}
for (bdr_cell = bdr_nodes.head; bdr_cell; bdr_cell = bdr_cell->next)
{
PGconn *bdr_node_conn = NULL;
NodeInfoList existing_nodes = T_NODE_INFO_LIST_INITIALIZER;
NodeInfoListCell *cell;
ExtensionStatus other_node_extension_status;
/* skip the local node */
if (strncmp(node_info.node_name, bdr_cell->node_info->node_name, MAXLEN) == 0)
{
continue;
}
log_debug("connecting to BDR node \"%s\" (conninfo: \"%s\")",
bdr_cell->node_info->node_name,
bdr_cell->node_info->node_local_dsn);
bdr_node_conn = establish_db_connection_quiet(bdr_cell->node_info->node_local_dsn);
if (PQstatus(bdr_node_conn) != CONNECTION_OK)
{
continue;
}
/* check repmgr schema exists, skip if not */
other_node_extension_status = get_repmgr_extension_status(bdr_node_conn);
if (other_node_extension_status != REPMGR_INSTALLED)
{
continue;
}
get_all_node_records(bdr_node_conn, &existing_nodes);
for (cell = existing_nodes.head; cell; cell = cell->next)
{
log_debug("creating record for node \"%s\" (ID: %i)",
cell->node_info->node_name, cell->node_info->node_id);
create_node_record(conn, "bdr register", cell->node_info);
}
PQfinish(bdr_node_conn);
break;
}
}
}
/* Add the repmgr extension tables to a replication set */
add_extension_tables_to_bdr_replication_set(conn);
initPQExpBuffer(&event_details);
begin_transaction(conn);
/*
* we'll check if a record exists (even if the schema was just created),
* as there's a faint chance of a race condition
*/
record_status = get_node_record(conn, config_file_options.node_id, &node_info);
/* Update internal node record */
node_info.type = BDR;
node_info.node_id = config_file_options.node_id;
node_info.upstream_node_id = NO_UPSTREAM_NODE;
node_info.active = true;
node_info.priority = config_file_options.priority;
strncpy(node_info.node_name, config_file_options.node_name, MAXLEN);
strncpy(node_info.location, config_file_options.location, MAXLEN);
strncpy(node_info.conninfo, config_file_options.conninfo, MAXLEN);
if (record_status == RECORD_FOUND)
{
bool node_updated;
/*
* At this point we will have established there are no non-BDR records,
* so no need to verify the node type
*/
if (!runtime_options.force)
{
log_error(_("this node is already registered"));
log_hint(_("use -F/--force to overwrite the existing node record"));
rollback_transaction(conn);
PQfinish(conn);
exit(ERR_BAD_CONFIG);
}
/*
* don't permit changing the node name - this must match the
* BDR node name set when the node was registered.
*/
if (strncmp(node_info.node_name, config_file_options.node_name, MAXLEN) != 0)
{
log_error(_("a record for node %i is already registered with node_name '%s'"),
config_file_options.node_id, node_info.node_name);
log_hint(_("node_name configured in repmgr.conf is '%s'"), config_file_options.node_name);
rollback_transaction(conn);
PQfinish(conn);
exit(ERR_BAD_CONFIG);
}
node_updated = update_node_record(conn, "bdr register", &node_info);
if (node_updated == true)
{
appendPQExpBuffer(&event_details, _("node record updated for node '%s' (%i)"),
config_file_options.node_name, config_file_options.node_id);
log_verbose(LOG_NOTICE, "%s\n", event_details.data);
}
{
success = false;
}
}
else
{
/* create new node record */
bool node_created = create_node_record(conn, "bdr register", &node_info);
if (node_created == true)
{
appendPQExpBuffer(&event_details,
_("node record created for node '%s' (ID: %i)"),
config_file_options.node_name, config_file_options.node_id);
log_notice("%s", event_details.data);
}
else
{
success = false;
}
}
if (success == false)
{
rollback_transaction(conn);
PQfinish(conn);
exit(ERR_DB_QUERY);
}
commit_transaction(conn);
/* Log the event */
create_event_notification(
conn,
&config_file_options,
config_file_options.node_id,
"bdr_register",
true,
event_details.data);
termPQExpBuffer(&event_details);
PQfinish(conn);
log_notice(_("BDR node %i registered (conninfo: %s)"),
config_file_options.node_id, config_file_options.conninfo);
return;
}
void
do_bdr_unregister(void)
{
PGconn *conn;
ExtensionStatus extension_status;
int target_node_id;
t_node_info node_info = T_NODE_INFO_INITIALIZER;
RecordStatus record_status;
bool node_record_deleted;
PQExpBufferData event_details;
char dbname[MAXLEN];
/* sanity-check configuration for BDR-compatability */
if (config_file_options.replication_type != REPLICATION_TYPE_BDR)
{
log_error(_("cannot run BDR UNREGISTER on a non-BDR node"));
exit(ERR_BAD_CONFIG);
}
/* store the database name for future reference */
get_conninfo_value(config_file_options.conninfo, "dbname", dbname);
conn = establish_db_connection(config_file_options.conninfo, true);
if (!is_bdr_db(conn))
{
log_error(_("database \"%s\" is not BDR-enabled"), dbname);
exit(ERR_BAD_CONFIG);
}
extension_status = get_repmgr_extension_status(conn);
if (extension_status != REPMGR_INSTALLED)
{
log_error(_("repmgr is not installed on database \"%s\""), dbname);
exit(ERR_BAD_CONFIG);
}
if (!is_bdr_repmgr(conn))
{
log_error(_("repmgr metadatabase contains records for non-BDR nodes"));
exit(ERR_BAD_CONFIG);
}
initPQExpBuffer(&event_details);
if (runtime_options.node_id != UNKNOWN_NODE_ID)
target_node_id = runtime_options.node_id;
else
target_node_id = config_file_options.node_id;
/* Check node exists and is really a BDR node */
record_status = get_node_record(conn, target_node_id, &node_info);
if (record_status != RECORD_FOUND)
{
log_error(_("no record found for node %i"), target_node_id);
PQfinish(conn);
exit(ERR_BAD_CONFIG);
}
begin_transaction(conn);
log_debug("unregistering node %i", target_node_id);
node_record_deleted = delete_node_record(conn, target_node_id);
if (node_record_deleted == false)
{
appendPQExpBuffer(&event_details,
"unable to delete node record for node \"%s\" (ID: %i)",
node_info.node_name,
target_node_id);
rollback_transaction(conn);
}
else
{
appendPQExpBuffer(&event_details,
"node record deleted for node \"%s\" (ID: %i)",
node_info.node_name,
target_node_id);
commit_transaction(conn);
}
/* Log the event */
create_event_notification(
conn,
&config_file_options,
config_file_options.node_id,
"bdr_unregister",
true,
event_details.data);
PQfinish(conn);
log_notice(_("bdr node \"%s\" (ID: %i) successfully unregistered"),
node_info.node_name, target_node_id);
termPQExpBuffer(&event_details);
return;
}