-
Notifications
You must be signed in to change notification settings - Fork 0
/
slony_failover.pl
executable file
·2636 lines (2397 loc) · 121 KB
/
slony_failover.pl
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
# Script: failover.pl
# Copyright: 08/04/2012: v1.0.2 Glyn Astill <[email protected]>
# Requires: Perl 5.10.1+, Data::UUID, File::Slurp
# PostgreSQL 9.0+ Slony-I 1.2+ / 2.0+
#
# This script is a command-line utility to manage switchover and failover
# of replication sets in Slony-I clusters.
#
# This script is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this script. If not, see <http://www.gnu.org/licenses/>.
use strict;
use warnings;
use experimental 'smartmatch';
use DBI;
use Getopt::Long qw/GetOptions/;
use Data::UUID;
use File::Slurp;
use v5.10.1;
use sigtrap 'handler' => \&sigExit, 'HUP', 'INT','ABRT','QUIT','TERM';
use Time::HiRes qw/usleep gettimeofday/;
use POSIX qw/strftime/;
use Config qw/%Config/;
use constant false => 0;
use constant true => 1;
my $g_script_version = '1.0.3';
my $g_debug = false;
my $g_pidfile = '/var/run/slony_failover.pid';
my $g_pid_written = false;
my $g_lang = 'en';
my $g_prefix = '/tmp/slony_failovers';
my $g_separate_working = true;
my $g_log_prefix = '%t';
my $g_log_to_db = false;
my $g_logdb_name;
my $g_logdb_host;
my $g_logdb_port;
my $g_logdb_user;
my $g_logdb_pass;
my $g_slonikpath;
my $g_use_try_blocks = false;
my $g_lockset_method = 'multiple';
my $g_logfile = 'failover.log';
my $g_input;
my $g_silence_notice = false;
my $g_reason;
my $g_script;
my $g_node_from;
my $g_node_to;
my $g_clname;
my $g_dbname;
my $g_dbhost;
my $g_dbport = 5432;
my $g_dbconninfo;
my $g_dbpass = '';
my $g_dbuser = 'slony';
my $g_node_count;
my $g_available_node_count;
my $g_critical_node_count;
my $g_subs_follow_origin = false;
my $g_use_comment_aliases = false;
my @g_cluster; # Array refs of node info. In hindsight this should have been a hash, should be fairly simple to switch.
my @g_failed;
my @g_sets;
my @g_lags;
my $g_result;
my $g_version;
my $g_failover_method = 'old';
my $g_resubscribe_method = 'subscribe';
my $g_failover = false;
my $g_fail_subonly = false;
my $g_drop_failed = false;
my $g_autofailover = false;
my $g_autofailover_poll_interval = 500;
my $g_autofailover_retry = 2;
my $g_autofailover_retry_sleep = 1000;
my $g_autofailover_provs = false;
my $g_autofailover_config_any = true;
my $g_autofailover_perspective_sleep = 20000;
my $g_autofailover_majority_only = false;
my $g_autofailover_is_quorum = false;
my @g_unresponsive;
my %g_unresponsive_subonly;
my %g_backups;
my $g_pid = $$;
# Hash containing messages used by lookupMsg()
my %message = (
'en' => {
'usage' => q{-h <host> -p <port> -db <database> -cl <cluster name> -u <username> -P <password> -f <config file> (Password option not recommended; use pgpass instead)},
'title' => q{Slony-I failover script version $1},
'cluster_fixed' => q{Aborting failover action: all origin/provider nodes now responsive},
'cluster_failed' => q{Found $1 failed nodes, sleeping for $2ms before retry $3 of $4},
'load_cluster' => q{Getting a list of database nodes...},
'load_cluster_fail' => q{Unable to read cluster configuration $1},
'load_cluster_success' => q{I Loaded Slony-I v$1 cluster "$2" with $3 nodes read from node at $4:$5/$6},
'lag_detail' => q{Current node lag information from configuration node:},
'script_settings' => q{Using $1 batches of lock set, $2 FAILOVER and $3},
'generated_script' => q{Generated script "$1"},
'autofailover_init' => q{Entering autofailover mode},
'autofailover_init_cnf' => q{Slony configuration will be read from $1 node},
'autofailover_init_pol' => q{Polling every $1ms},
'autofailover_init_ret' => q{Failed nodes will be retried $1 times with $2ms sleep},
'autofailover_init_set' => q{Failed forwarding providers $1 be failed over},
'autofailover_load_cluster' => q{$1 Slony-I v$2 cluster "$3" with $4 nodes read from node $5},
'autofailover_proceed' => q{Proceeding with failover:},
'autofailover_detail' => q{Failed node: $1, Backup node: $2},
'autofailover_halt' => q{Unable to perform any failover for $1 failed nodes},
'autofailover_check_sub' => q{Checking subscriber node $1},
'autofailover_check_sub_fail' => q{Unable to check subscriber node $1},
'autofailover_promote_find' => q{Finding most up to date subscriber to all sets ($1) on unresponsive node $2},
'autofailover_promote_found' => q{Using previously found most up to date subscriber to all sets ($1) on unresponsive node $2},
'autofailover_promote_skip' => q{No failover required for unresponsive node $1 as it is neither the origin or an active forwarder of any sets},
'autofailover_promote_fail' => q{Could not find suitable backup node for promotion},
'autofailover_node_detail' => q{Node $1 is $2 subscribed to ($3) node $4 and provides sets $5 at $6 seconds lag (on event $7)},
'autofailover_promote_best' => q{Best node for promotion is node $1 lag = $2 seconds (event $3)},
'autofailover_promote_unsuitable' => q{Node $1 is unsuitable for promotion},
'autofailover_unresponsive' => q{Detected unresponsive origin node: $1},
'autofailover_unresponsive_prov' => q{Detected unresponsive provider node: $1},
'autofailover_unresponsive_subonly'=> q{Detected unresponsive subscriber only node: $1},
'autofailover_recovery_subonly' => q{Detected recovery of previously unresponsive subscriber only node: $1},
'autofailover_pspec_check_fail' => q{Failed to connect to node $1: $2},
'autofailover_pspec_check' => q{Getting objective judgement from other nodes, apparent unresponsive nodes are : $1 (Failed nodes = $2 of $3)},
'autofailover_pspec_check_sleep' => q{Sleeping for $1 ms},
'autofailover_pspec_check_data' => q{$1: Node $2 says lag from node $3 -> $4 is $5 seconds},
'autofailover_pspec_check_true' => q{All detected failed nodes confirmed as lagging by other nodes},
'autofailover_pspec_check_false' => q{Not all nodes confirmed as lagging},
'autofailover_pspec_check_unknown' => q{Unable to confirm lag status of all nodes},
'autofailover_split_check' => q{Surviving nodes ($1 of $2) are the majority},
'autofailover_split_check_fail' => q{Surviving nodes ($1) are not the majority},
'interactive_head_id' => q{ID},
'interactive_head_name' => q{Name},
'interactive_head_status' => q{Status},
'interactive_head_providers' => q{Provider IDs},
'interactive_head_config' => q{Configuration},
'interactive_detail_1' => q{Origin for sets: },
'interactive_detail_2' => q{Providing sets: },
'interactive_detail_3' => q{Subscriptions: },
'interactive_choose_node' => q{Please choose the node to move all sets $1:},
'interactive_confirm' => q{You chose to move sets $1 node $2 ($3). Is this correct [y/n]? },
'interactive_action' => q{Best course of action is most likely to do a "$1". Do you wish to continue [y/n]?},
'interactive_surrender' => q{Uable to determine best course of action},
'interactive_write_script' => q{Writing a script to $1 node $2 to $3},
'interactive_check_nodes' => q{Checking availability of database nodes...},
'interactive_continue' => q{Do you wish to continue [y/n]?},
'interactive_drop_nodes' => q{Do you want to also drop the failed nodes from the slony configuration [y/n]?},
'interactive_preserve' => q{Preserve subscription paths to follow the origin node (choose no if unsure) [y/n]?},
'interactive_aliases' => q{Generate aliases based on sl_node/set comments in parentheses (choose no if unsure) [y/n]?},
'interactive_summary' => q{Summary of nodes to be passed to failover:},
'interactive_node_info' => q{Node : $1 ($2) $3 (conninfo $4)},
'interactive_run_script' => q{Would you like to run this script now [y/n]?},
'interactive_running' => q{Running the script now. This may take some time; please be patient!},
'interactive_reason' => q{Please enter a brief reson for taking this action: },
'interactive_failover_detail_1' => q{Before you go any further please consider the impact of a full failover:},
'interactive_failover_detail_2' => q{The node you are failing over from will cease to participate in the cluster permanently until it is rebuild & subscribed},
'interactive_failover_detail_3' => q{If the outage is temporary (i.e. network/power/easily replaceable hardware related) consider waiting it out},
'interactive_failover_detail_4' => q{This type of failover is likely to be more a business decision than a technical one},
'info_all_nodes_available' => q{INFO: All nodes are available},
'info_req_nodes_available' => q{INFO: $1 of $2 nodes are available. No unavailable nodes are subscribed to the old origin},
'wrn_node_unavailable' => q{WARNING: Node $1 unavailable},
'wrn_req_unavailable' => q{WARNING: Old origin node ($1) is available, however $2 subscribers are unavailable},
'wrn_not_tested' => q{WARNING: Script not tested with Slony-I v$1},
'wrn_failover_issues' => q{WARNING: Slony-I v$1 may struggle to failover correctly with multiple failed nodes (affects v2.0-2.1)},
'note_autofail_fwd_only' => q{NOTICE: Slony versions prior to 2.2 cannot initiate failover from only failed forwarding providers},
'note_fail_sub_only' => q{NOTICE: Slony versions prior to 2.2 cannot failover subscriber only nodes, reverting to failover_offline_subscriber_only = false},
'note_multiple_try' => q{NOTICE: Cannot lock multiple sets within try blocks in version $1 dropping back to single sets},
'note_reshape_cluster' => q{NOTICE: Either drop the failed subscribers or bring them back up, then retry to MOVE SET},
'dbg_generic' => q{DEBUG: $1},
'dbg_cluster' => q{DEBUG: NodeID $1/ProvIDs $2/Conninfo $3/OrigSets $4/NodeName $5/ProvTree $6/Active $7/FwdSets $8/ActSubSets $9},
'dbg_resubscribe' => q{DEBUG: Checking possibility to resubscribe set $1 ($2) to node $3 ($4) which pulls $5 ($6) from $7 ($8)},
'dbg_failover_method' => q{DEBUG: Failover method is $1},
'dbg_cluster_load' => q{DEBUG: Loading cluster configuration from $1},
'dbg_cluster_good' => q{DEBUG: Cluster state good},
'dbg_autofailover_check' => q{DEBUG: Checking node $1 ($2) role is $3 (conninfo: $4)},
'dbg_autofailover_active_check' => q{DEBUG: Initiate active check of $1 node $2},
'dbg_slonik_script' => q{DEBUG: Running slonik script $1},
'err_generic' => q{ERROR: $1},
'err_no_database' => q{ERROR: Please specify a database name},
'err_no_cluster' => q{ERROR: Please specify a slony cluster name},
'err_no_host' => q{ERROR: Please specify a host},
'err_no_config' => q{ERROR: No valid config found},
'err_fail_config' => q{ERROR: Failed to load configuration},
'err_write_fail' => q{ERROR: Could not write to $1 "$2"},
'err_read_fail' => q{ERROR: Could not read from $1 "$2"},
'err_unlink_fail' => q{ERROR: Could not delete $1 "$2"},
'err_mkdir_fail' => q{ERROR: Unable to create $1 directory "$2"},
'err_execute_fail' => q{ERROR: Could not execute $1 "$2"},
'err_inactive' => q{ERROR: Node $1 is not active (state = $2)},
'err_cluster_empty' => q{ERROR: Loaded cluster contains no nodes},
'err_cluster_offline' => q{ERROR: Loaded cluster contains no reachable nodes},
'err_cluster_lone' => q{ERROR: Loaded cluster contains only 1 node},
'err_not_origin' => q{ERROR: Node $1 is not the origin of any sets},
'err_not_provider' => q{ERROR: Node $1 is not a provider of any sets},
'err_not_provider_sets' => q{ERROR: Node $1 does not provide the sets required: need ($2) but provides ($3)},
'err_no_configuration' => q{ERROR: Could not read configuration for node $1},
'err_must_enter_node_id' => q{ERROR: You must enter a node id},
'err_not_a_node_id' => q{ERROR: I have no knowledge of a node $1},
'err_same_node' => q{ERROR: Cant move from and to the same node},
'err_node_offline' => q{ERROR: $1 node ($2) is not available},
'err_incomplete_preamble' => q{ERROR: Incomplete preamble},
'err_running_slonik' => q{ERROR: Could not run slonik: $1},
'err_pgsql_connect' => q{ERROR: Cannot connect to postgres server},
'slonik_output' => q{SLONIK: $1},
'exit_noaction' => q{Exiting, no action has been taken},
'exit' => q{Exited by $1}
},
'fr' => {
'usage' => q{-h <host> -p <port> -db <database> -cl <cluster name> -u <username> -P <password> -f <config file> (Option mot de passe pas recommandé; utiliser pgpass place)},
'title' => q{Slony-I failover (basculement) version de script $1},
'cluster_fixed' => q{Abandon de l'action de basculement: tous les noeuds d'origine / de fournisseurs maintenant sensible},
'cluster_failed' => q{Trouvé $1 échoué noeuds, couchage pour $2 ms avant réessayer $3 de $4},
'load_cluster' => q{Obtenir une liste de noeuds de base de donnees...},
'load_cluster_fail' => q{Impossible de lire la configuration du cluster $1},
'load_cluster_success' => q{Chargé Slony-I v$1 groupe "$2" avec $3 noeuds lire à partir du noeud à $4:$5/$6},
'lag_detail' => q{Current informations noeud de décalage à partir du noeud de configuration:},
'script_settings' => q{Utilisation de $1 lots de système de verrouillage, $2 FAILOVER et $3},
'generated_script' => q{Script généré "$1"},
'autofailover_init' => q{Entrer dans le mode de autofailover},
'autofailover_init_cnf' => q{Configuration Slony sera lu à partir de $1 noeud},
'autofailover_init_pol' => q{Vérifier toutes les $1ms},
'autofailover_init_ret' => q{Noeuds défaillants seront rejugés $1 fois avec $2 ms sommeil},
'autofailover_init_set' => q{Fournisseurs d'expédition échoué $1 être échoué sur},
'autofailover_load_cluster' => q{$1 Slony-I v$2 grappe "$3" avec $4 noeuds lire à noeud $5},
'autofailover_proceed' => q{De procéder à failover:},
'autofailover_detail' => q{Noeud défaillant: $1, noeud de sauvegarde: $2},
'autofailover_halt' => q{Noeuds Impossible d'effectuer une failover pour $1 échoué},
'autofailover_check_sub' => q{Vérification noeud abonné $1},
'autofailover_check_sub_fail' => q{Impossible de vérifier noeud abonné $1},
'autofailover_promote_find' => q{Trouver plus à jour abonné à tous les jeux ($1) sur le noeud ne répond pas $2},
'autofailover_promote_found' => q{Utilisation précédemment trouvé plus à jour abonné à tous les jeux ($1) sur le noeud ne répond pas $2},
'autofailover_promote_skip' => q{Pas de failover requis pour le noeud ne répond pas $1 car il n'est ni l'origine ou un transitaire active de tous les jeux},
'autofailover_promote_fail' => q{Impossible de trouver le noeud de sauvegarde approprié pour la promotion},
'autofailover_node_detail' => q{Noeud $1 est souscrit à $2 ($3) noeud $4 et fournit des ensembles de $5 à retard $6 secondes (en cas d'événement $7)},
'autofailover_promote_best' => q{Meilleur noeud pour la promotion est noeud $1 décalage = $2 secondes (événement $3)},
'autofailover_promote_unsuitable' => q{Noeud $1 est inadapté pour la promotion},
'autofailover_unresponsive' => q{Noeud d'origine ne répond pas détecté: $1},
'autofailover_unresponsive_prov' => q{Noeud fournisseur ne répond pas détecté: $1},
'autofailover_unresponsive_subonly'=> q{Abonné ne répond pas détecté seulement de noeud: $1},
'autofailover_recovery_subonly' => q{Recouvrement détecté de l'abonné ne répond pas seulement auparavant de noeud: $1},
'autofailover_pspec_check_fail' => q{Impossible de se connecter au noeud $1: $2},
'autofailover_pspec_check' => q{Obtenir un jugement objectif à partir d'autres noeuds, les noeuds qui ne répondent pas apparentes sont : $1 (Noeuds défaillants = $2 de $3)},
'autofailover_pspec_check_sleep' => q{Dormir pour $1 ms},
'autofailover_pspec_check_data' => q{$1: Noeud $2 dit décalage de $3 -> $4 noeud est $5 secondes},
'autofailover_pspec_check_true' => q{Tous les noeuds détectés pas confirmés comme à la traîne par d'autres noeuds},
'autofailover_pspec_check_false' => q{Pas tous les noeuds confirmé retard},
'autofailover_pspec_check_unknown' => q{Impossible de confirmer le statut de latence de tous les noeuds},
'autofailover_split_check' => q{Autres noeuds ($1 sur $2) sont la majorité},
'autofailover_split_check_fail' => q{Autres noeuds ($1) ne sont pas la majorité},
'interactive_head_name' => q{Nom},
'interactive_head_status' => q{Statut},
'interactive_head_providers' => q{Fournisseur IDs},
'interactive_detail_1' => q{Origine pour les jeux: },
'interactive_detail_2' => q{Fournir des ensembles: },
'interactive_detail_3' => q{Abonnements: },
'interactive_choose_node' => q{S'il vous plaît choisissez le noeud à déplacer tous les ensembles $1:},
'interactive_confirm' => q{Vous avez choisi de passer ensembles $1 noeud $2 ($3). Est-ce correct [o/n]? },
'interactive_drop_nodes' => q{Voulez-vous laisser tomber aussi les noeuds défaillants de la configuration de slony [o/n]?},
'interactive_action' => q{Meilleur plan d'action est le plus susceptible de faire une $1. Voulez-vous continuer [o/n]?},
'interactive_surrender' => q{Uable pour déterminer le meilleur plan d'action},
'interactive_write_script' => q{Rédaction d'un script à $1 $2 noeud à $3},
'interactive_check_nodes' => q{Vérification de la disponibilité des noeuds de base de donnees...},
'interactive_continue' => q{Voulez-vous continuer [o/n]?},
'interactive_preserve' => q{Préserver les chemins de souscription à suivre le noeud d'origine (ne pas choisir en cas de doute) [o/n]?},
'interactive_aliases' => q{Générer des alias sur la base de sl_node / set commentaires entre parenthèses (ne pas choisir en cas de doute) [o/n]?},
'interactive_summary' => q{Résumé des noeuds à passer à failover:},
'interactive_node_info' => q{Noeud : $1 ($2) $3 (conninfo $4)},
'interactive_run_script' => q{Voulez-vous exécuter ce script maintenant [o/n]?},
'interactive_running' => q{L'exécution du script maintenant. Cela peut prendre un certain temps; s'il vous plaît être patient!},
'interactive_reason' => q{S'il vous plaît entrer une brève reson pour cette action: },
'interactive_failover_detail_1' => q{Avant d'aller plus loin s'il vous plaît envisager l'impact d'un failover (basculement) complet:},
'interactive_failover_detail_2' => q{Le noeud vous ne parviennent pas au-dessus de cesse de participer au groupe de façon permanente jusqu'à ce qu'il soit à reconstruire et souscrit},
'interactive_failover_detail_3' => q{Si la panne est temporaire (c.-à-réseau / alimentation / facilement remplaçable matériel connexe) envisager d'attendre dehors},
'interactive_failover_detail_4' => q{Ce type de failover est susceptible d'être plus une décision d'affaires que technique},
'info_all_nodes_available' => q{INFO: Tous les noeuds sont disponibles},
'info_req_nodes_available' => q{INFO: $1 of $2 noeuds sont disponibles. Pas de noeuds indisponibles sont souscrites à l'ancienne origine},
'wrn_node_unavailable' => q{ATTENTION: Noeud $1 disponible},
'wrn_req_unavailable' => q{ATTENTION: Noeud Old origine ($1) est disponible, mais $2 abonnés ne sont pas disponibles},
'wrn_not_tested' => q{ATTENTION: Script pas testé avec Slony-I v$1},
'wrn_failover_issues' => q{ATTENTION: Slony-I v$1 peut lutter pour basculer correctement avec plusieurs nœuds défaillants (affecte v2.0-2.1)},
'note_autofail_fwd_only' => q{AVIS: Versions antérieures à la 2.2 Slony ne peuvent pas initier le basculement de seulement échoué transmettre fournisseurs},
'note_fail_sub_only' => q{AVIS: Versions antérieures à la 2.2 Slony ne peuvent pas basculer abonnes seuls les noeuds, revenant à failover_offile_subscriber_only = false},
'note_multiple_try' => q{AVIS: Vous ne pouvez pas verrouiller plusieurs ensembles dans des blocs try dans la version $1 de retomber à des jeux simples},
'note_reshape_cluster' => q{AVIS: Vous devez supprimer les abonnés défaillants ou les ramener, puis réessayez à MOVE SET},
'err_generic' => q{ERREUR: $1},
'err_no_database' => q{ERREUR: S'il vous plaît spécifier un base de donnees nom},
'err_no_cluster' => q{ERREUR: S'il vous plaît indiquez un nom de cluster slony},
'err_no_host' => q{ERREUR: S'il vous plaît spécifier un hôte},
'err_no_config' => q{ERREUR: Aucune configuration valide n'a été trouvée},
'err_fail_config' => q{ERREUR: Impossible de charger la configuration},
'err_write_fail' => q{ERREUR: Impossible d'écrire dans $1 "$2"},
'err_read_fail' => q{ERREUR: Impossible de lire $1 "$2"},
'err_unlink_fail' => q{ERREUR: Impossible de supprimer $1 "$2"},
'err_mkdir_fail' => q{ERREUR: Impossible de créer $1 répertoire "$2"},
'err_execute_fail' => q{ERREUR: Impossible d'exécuter $1 "$2"},
'err_inactive' => q{ERREUR: Noeud $1 n'est pas active (état = $2)},
'err_cluster_empty' => q{ERREUR: Groupe chargé contient pas de noeuds},
'err_cluster_offline' => q{ERREUR: Groupe chargé contient pas de noeuds accessibles},
'err_cluster_lone' => q{ERRRUE: Groupe chargé ne contient que 1 noeud},
'err_not_origin' => q{ERREUR: Noeud $1 n'est pas à l'origine de tous les jeux},
'err_not_provider' => q{ERREUR: Noeud $1 n'est pas un fournisseur de tous les jeux},
'err_not_provider_sets' => q{ERREUR: Noeud $1 ne fournit pas les ensembles nécessaires: le besoin ($2), mais fournit ($3)},
'err_no_configuration' => q{ERREUR: Impossible de lire la configuration pour le noeud $1},
'err_must_enter_node_id' => q{ERREUR: Vous devez entrer un id de noeud},
'err_not_a_node_id' => q{ERREUR: Je n'ai pas connaissance d'un $1 de noeud},
'err_same_node' => q{ERREUR: Cant déplacer depuis et vers le même noeud},
'err_node_offline' => q{ERREUR: $1 noeud ($2) n'est pas disponible},
'err_incomplete_preamble' => q{ERREUR: Préambule incomplète},
'err_running_slonik' => q{ERREUR: Ne pouvait pas courir slonik: $1},
'err_pgsql_connect' => q{ERREUR: Impossible de se connecter au serveur postgres},
'slonik_output' => q{SLONIK: $1},
'exit_noaction' => q{Quitter, aucune action n'a été prise},
'exit' => q{Quitter par $1}
}
);
# Setup date variables
my ($g_year, $g_month, $g_day, $g_hour, $g_min, $g_sec) = (localtime(time))[5,4,3,2,1,0];
my $g_date = sprintf ("%02d:%02d:%02d on %02d/%02d/%04d", $g_hour, $g_min, $g_sec, $g_day, $g_month+1, $g_year+1900);
# Handle command line options
Getopt::Long::Configure('no_ignore_case');
use vars qw{%opt};
die lookupMsg('usage') unless GetOptions(\%opt, 'host|H=s', 'port|p=i', 'dbname|db=s', 'clname|cl=s', 'dbuser|u=s', 'dbpass|P=s', 'cfgfile|f=s', 'infoprint|I', ) and keys %opt and ! @ARGV;
# Read configuration
if (defined($opt{cfgfile})) {
unless (getConfig($opt{cfgfile})) {
println(lookupMsg('err_no_config'));
exit(1);
}
}
else {
if (defined($opt{dbname})) {
$g_dbname = $opt{dbname};
}
if (defined($opt{clname})) {
$g_clname = $opt{clname};
}
if (defined($opt{host})) {
$g_dbhost = $opt{host};
}
if (defined($opt{port})) {
$g_dbport = $opt{port};
}
if (defined($opt{dbuser})) {
$g_dbuser = $opt{dbuser};
}
if (defined($opt{dbpass})) {
$g_dbpass = $opt{dbpass};
}
}
# Display message and die if any of the required configuration variables are missing
if (!defined($g_dbname)) {
println(lookupMsg('err_no_database'));
die lookupMsg('usage');
}
if (!defined($g_clname)) {
println(lookupMsg('err_no_cluster'));
die lookupMsg('usage');
}
if (!defined($g_dbhost)) {
println(lookupMsg('err_no_host'));
die lookupMsg('usage');
}
# Build conninfo from supplied datbase name/host/port
$g_dbconninfo = "dbname=$g_dbname;host=$g_dbhost;port=$g_dbport";
if (!defined($opt{infoprint})) {
# Check prefix directory and create if not present
unless(-e $g_prefix or mkdir $g_prefix) {
println(lookupMsg('err_mkdir_fail', 'prefix', $g_prefix));
exit(2);
}
if ($g_separate_working) {
if ($g_prefix !~ m/\/$/) {
$g_prefix .= "/";
}
# Get a uuid for working directory
$g_prefix .= getUUID($g_date);
# Create a working directory and setup log file
unless(-e $g_prefix or mkdir $g_prefix) {
println(lookupMsg('err_mkdir_fail', 'work', $g_prefix));
}
}
}
# Set postgres path if provided
if (defined($g_slonikpath) && ($g_slonikpath ne "")) {
$ENV{PATH} .= ":$g_slonikpath";
}
# Check if autofailover is enabled, if so check configuration and enter autofailover mode
if (($g_autofailover) && !defined($opt{infoprint})) {
# Write out a PID file
if (writePID($g_prefix, $g_logfile, $g_log_prefix, $g_pidfile)) {
$g_pid_written = true;
}
else {
cleanExit(1, "system");
}
# Go into endless loop for autofailover
autoFailover($g_dbconninfo, $g_clname, $g_dbuser, $g_dbpass, $g_prefix, $g_logfile, $g_log_prefix);
}
# Read slony configuration and output some basic information
eval {
#local $| = 1;
println(lookupMsg('load_cluster', $g_prefix));
($g_node_count, $g_version) = loadCluster($g_dbconninfo, $g_clname, $g_dbuser, $g_dbpass, $g_prefix, $g_logfile, $g_log_prefix);
};
if ($@) {
println(lookupMsg('load_cluster_fail', 'from supplied configuration'));
cleanExit(2, "system");
}
if (defined($opt{infoprint})) {
println(lookupMsg('load_cluster_success', $g_version, $g_clname, $g_node_count, $g_dbhost, $g_dbport, $g_dbname) . ":");
chooseNode("info", undef, undef, undef, 0);
exit(0);
}
else {
printlog($g_prefix,$g_logfile,$g_log_prefix,"*"x68 . "\n* ");
printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('title', $g_script_version));
printlogln($g_prefix,$g_logfile,$g_log_prefix,"*"x68);
}
if ($g_node_count <= 0) {
printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('err_cluster_empty'));
cleanExit(3, "system");
}
else {
printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('load_cluster_success', $g_version, $g_clname, $g_node_count, $g_dbhost, $g_dbport, $g_dbname));
printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('script_settings', $g_lockset_method, $g_failover_method, uc($g_resubscribe_method)));
}
# Output lag information between each node and node configuration was read from
if (loadLag($g_dbconninfo, $g_clname, $g_dbuser, $g_dbpass, $g_prefix, $g_logfile, $g_log_prefix) > 0) {
printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('lag_detail'));
foreach (@g_lags) {
printlogln($g_prefix,$g_logfile,$g_log_prefix,"\t$_");
}
printlog($g_prefix,$g_logfile,$g_log_prefix,"\n");
}
# Prompt user to choose nodes to move sets from / to
$g_node_from = chooseNode("from", $g_prefix, $g_logfile, $g_log_prefix, 0);
if ($g_node_from == 0) {
cleanExit(4, "user");
}
elsif ($g_node_from == -1) {
cleanExit(5, "system");
}
$g_node_to = chooseNode("to", $g_prefix, $g_logfile, $g_log_prefix, $g_node_from);
if ($g_node_to == 0) {
cleanExit(6, "user");
}
elsif ($g_node_to == -1) {
cleanExit(7, "system");
}
elsif ($g_node_from == $g_node_to) {
printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('err_same_node'));
cleanExit(8, "system");
}
# Check nodes are available and decide on action to take
printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('interactive_check_nodes'));
($g_available_node_count, $g_critical_node_count) = checkNodes($g_clname, $g_dbuser, $g_dbpass, $g_node_from, $g_node_to, $g_prefix, $g_logfile, $g_log_prefix);
if ($g_available_node_count <= 0) {
printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('err_cluster_offline'));
cleanExit(9, "system");
}
elsif ($g_critical_node_count == -1) {
printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('err_node_offline', 'Target new origin', $g_node_to));
cleanExit(10, "system");
}
elsif ($g_critical_node_count == -2) {
printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('err_node_offline', 'Old origin', $g_node_from));
printlog($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('interactive_action', 'FAILOVER'));
$g_failover = true;
}
elsif ($g_critical_node_count == 0) {
if ($g_node_count == $g_available_node_count) {
printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('info_all_nodes_available'));
}
else {
printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('info_req_nodes_available', $g_available_node_count, $g_node_count));
}
printlog($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('interactive_action', 'MOVE SET'));
}
elsif ($g_critical_node_count > 0) {
printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('wrn_req_unavailable', $g_node_from, $g_critical_node_count));
printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('note_reshape_cluster'));
printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('exit_noaction'));
cleanExit(11, "user");
}
else {
printlog($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('interactive_surrender'));
cleanExit(12, "system");
}
$g_input = <>;
chomp($g_input);
if ($g_input !~ /^[Y|O]$/i) {
printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('exit_noaction'));
cleanExit(13, "user");
}
if (!$g_use_comment_aliases) {
printlog($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('interactive_aliases'));
$g_input = <>;
chomp($g_input);
if ($g_input =~ /^[Y|O]$/i) {
$g_use_comment_aliases = true;
}
}
if ($g_failover) {
printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('interactive_summary'));
foreach (@g_failed) {
printlogln($g_prefix,$g_logfile,$g_log_prefix,"\t" . lookupMsg('interactive_node_info',$_->[0],($_->[4] // "unnamed"),(defined($_->[9]) ? "providing sets $_->[9]" : "sole subscriber"), $_->[2]));
}
printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('interactive_failover_detail_1'));
printlogln($g_prefix,$g_logfile,$g_log_prefix,"\t" . lookupMsg('interactive_failover_detail_2'));
printlogln($g_prefix,$g_logfile,$g_log_prefix,"\t" . lookupMsg('interactive_failover_detail_3'));
printlogln($g_prefix,$g_logfile,$g_log_prefix,"\t" . lookupMsg('interactive_failover_detail_4'));
printlog($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('interactive_drop_nodes'));
$g_input = <>;
if ($g_input ~~ /^[Y|O]$/i) {
$g_drop_failed = true;
}
printlog($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('interactive_reason'));
$g_reason = <>;
printlog($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('interactive_continue'));
$g_input = <>;
chomp($g_input);
if ($g_input !~ /^[Y|O]$/i) {
printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('exit_noaction'));
cleanExit(14, "user");
}
printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('interactive_write_script', 'failover from', $g_node_from, $g_node_to));
$g_script = writeFailover($g_prefix, $g_dbconninfo, $g_clname, $g_dbuser, $g_dbpass, $g_node_from, $g_node_to, $g_subs_follow_origin, $g_use_comment_aliases, $g_logfile, $g_log_prefix);
}
else {
printlog($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('interactive_preserve'));
$g_input = <>;
chomp($g_input);
if ($g_input =~ /^[Y|O]$/i) {
$g_subs_follow_origin = true;
}
printlog($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('interactive_reason'));
$g_reason = <>;
printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('interactive_write_script', 'move all sets provided by', $g_node_from, $g_node_to));
$g_script = writeMoveSet($g_prefix, $g_dbconninfo, $g_clname, $g_dbuser, $g_dbpass, $g_node_from, $g_node_to, $g_subs_follow_origin, $g_use_comment_aliases, $g_logfile, $g_log_prefix);
}
# Complete and run script if required
if (-e $g_script) {
printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('generated_script', $g_script));
printlog($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('interactive_run_script', $g_script));
$g_input = <>;
chomp($g_input);
if ($g_input =~ /^[Y|O]$/i) {
printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('interactive_running'));
unless (runSlonik($g_script, $g_prefix, $g_logfile, $g_log_prefix)) {
printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('err_execute_fail', 'slonik script', $g_script));
}
}
else {
printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('exit_noaction'));
}
}
else {
printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('err_read_fail', 'slonik script', $g_script));
cleanExit(15, "system");
}
cleanExit(0, "script completion");
###########################################################################################################################################
# Display exit message, insert log file into database if requested, delete any pid files and exit with the requested code
sub cleanExit {
my $exit_code = shift;
my $type = shift;
printlogln($g_prefix,$g_logfile,$g_log_prefix,lookupMsg('exit', $type));
if ($g_log_to_db) {
eval {
logDB("dbname=$g_logdb_name;host=$g_logdb_host;port=$g_logdb_port", $g_logdb_user, $g_logdb_pass, $exit_code, $g_reason, $g_prefix, $g_logfile, $g_log_prefix, $g_clname, $g_script);
};
}
if ($g_pid_written) {
removePID($g_prefix, $g_logfile, $g_log_prefix, $g_pidfile);
}
exit($exit_code);
}
# Exit on caught signal
sub sigExit {
cleanExit(100,'signal');
}
# Check we can reach each node in the cluster and that it contains the Slony schema
sub checkNodes {
my $clname = shift;
my $dbuser = shift;
my $dbpass = shift;
my $from = shift;
my $to = shift;
my $prefix = shift;
my $logfile = shift;
my $log_prefix = shift;
my $dsn;
my $dbh;
my $sth;
my $query;
my $result_count = 0;
my $critical_count = 0;
my @subsets;
my @origsets;
undef @g_failed;
undef @g_unresponsive;
undef %g_backups;
foreach (@g_cluster) {
if ($_->[0] == $from) {
@origsets = split(',', $_->[3]);
last;
}
}
foreach (@g_cluster) {
if ($g_debug) {
printlogln($prefix,$logfile,$log_prefix,lookupMsg('dbg_cluster', $_->[0],($_->[1] // "<NONE>"),$_->[2],($_->[3] // "<NONE>"),$_->[4],($_->[5] // "<NONE>") . "(" . ($_->[8] // "<NONE>") . ")",$_->[6],($_->[7] // "<NONE>"),($_->[9] // "<NONE>") . " (" . ($_->[10] // "<NONE>") . ")"));
}
$dsn = "DBI:Pg:$_->[2];";
eval {
$dbh = DBI->connect($dsn, $dbuser, $dbpass, {RaiseError => 1});
$query = "SELECT count(*) FROM pg_namespace WHERE nspname = ?";
$sth = $dbh->prepare($query);
$sth->bind_param(1, "_" . $clname);
$sth->execute();
$result_count = $result_count+$sth->rows;
$sth->finish;
$dbh->disconnect();
};
if ($@) {
# Critical count will be -1 if the new origin is down, -2 if the old origin is down or positive if subscribers to sets on old origin are down.
printlogln($prefix,$logfile,$log_prefix,lookupMsg('wrn_node_unavailable', $_->[0]));
if ($g_debug) {
printlogln($prefix,$logfile,$log_prefix,lookupMsg('dbg_generic', $@));
}
if ($_->[0] == $to) {
$critical_count = -1;
}
elsif ($_->[0] == $from) {
$critical_count = -2;
}
else {
foreach my $subprov (split(';', $_->[5])) {
my ($node, $setlist) = (split('->', $subprov)) ;
$node =~ s/n//g;
$setlist =~ s/(\)|\(|s)//g;
@subsets = (split(',', $setlist));
if (($critical_count >= 0) && (checkSubscribesAnySets(\@origsets, \@subsets))) {
$critical_count++;
}
}
}
# Only push nodes with active subscribers to sets into failed list unless explicitly told to
if (($g_fail_subonly) || (defined($_->[9]))) {
push(@g_failed, \@$_);
$g_backups{$_->[0]} = $to;
}
push(@g_unresponsive, \@$_);
}
}
return ($result_count, $critical_count);
}
# Load information on all nodes in the Slony cluster into global @g_cluster:
# 0) no_id = Node id of this node
# 1) no_provs = Comma separated list of all provider node ids
# 2) no_conninfo = Conninfo as recorded in sl_path
# 3) origin_sets = Comma separated list of set ids originating on this node
# 4) no_name = Node name; this is extracted from text between parentheses in sl_node.no_comment
# 5) no_sub_tree = Text representation of subscriptions in the form n<provider node id>->(s<set id>, ..)
# 6) no_status = Text representing the state of the node; either ACTIVE,INACTIVE or FAILED
# 7) sub_sets = Comma separated list of all set ids this node is subscribed to
# 8) no_sub_tree_name = As per no_sub_tree but holds textual names extracted from sl_node.no_comment
# 9) prov_sets_active = Comma separated list of all set ids this node is actively forwarding
# 10) prov_sets = Comma separated list of all set ids this node is subscribed to and able to forward
sub loadCluster {
my $dbconninfo = shift;
my $clname = shift;
my $dbuser = shift;
my $dbpass = shift;
my $prefix = shift;
my $logfile = shift;
my $log_prefix = shift;
my $dsn;
my $dbh;
my $sth;
my $query;
my $version;
my $qw_clname;
undef @g_cluster;
if ($g_debug) {
printlogln($prefix,$logfile,$log_prefix,lookupMsg('dbg_cluster_load', $dbconninfo));
}
$dsn = "DBI:Pg:$dbconninfo;";
eval {
$dbh = DBI->connect($dsn, $dbuser, $dbpass, {RaiseError => 1});
$qw_clname = $dbh->quote_identifier("_" . $clname);
$query = "SELECT $qw_clname.getModuleVersion()";
$sth = $dbh->prepare($query);
$sth->execute();
($version) = $sth->fetchrow;
$sth->finish;
$query = "WITH z AS (
SELECT a.no_id, b.sub_provider AS no_prov,
COALESCE(c.pa_conninfo,(SELECT pa_conninfo FROM $qw_clname.sl_path WHERE pa_server = $qw_clname.getlocalnodeid(?) LIMIT 1)) AS no_conninfo,
array_to_string(array(SELECT set_id FROM $qw_clname.sl_set WHERE set_origin = a.no_id ORDER BY set_id),',') AS origin_sets,
string_agg(CASE WHEN b.sub_receiver = a.no_id AND b.sub_forward AND b.sub_active THEN b.sub_set::text END, ',' ORDER BY b.sub_set) AS sub_sets,
coalesce(trim(regexp_replace(substring(a.no_comment from E'\\\\((.+)\\\\)'), '[^0-9A-Za-z]','_','g')), 'node' || a.no_id) AS no_name,
'n' || b.sub_provider || '->(' || string_agg(CASE WHEN b.sub_receiver = a.no_id THEN 's' || b.sub_set END,',' ORDER BY b.sub_set,',') || ')' AS sub_tree,
coalesce(trim(regexp_replace(substring(d.no_comment from E'\\\\((.+)\\\\)'), '[^0-9A-Za-z]','_','g')), 'node' || b.sub_provider, '')
|| '->(' || string_agg(CASE WHEN b.sub_receiver = a.no_id THEN coalesce(trim(regexp_replace(e.set_comment, '[^0-9A-Za-z]', '_', 'g')), 'set' || b.sub_set) END,',' ORDER BY b.sub_set) || ')' AS sub_tree_name,
CASE " . ((substr($version,0,3) >= 2.2) ? "WHEN a.no_failed THEN 'FAILED' " : "") . "WHEN a.no_active THEN 'ACTIVE' ELSE 'INACTIVE' END AS no_status,
array_to_string(array(SELECT DISTINCT sub_set::text FROM $qw_clname.sl_subscribe WHERE sub_provider = a.no_id AND sub_active ORDER BY sub_set),',') AS prov_sets_active,
string_agg(CASE WHEN b.sub_receiver = a.no_id THEN b.sub_set::text END,',' ORDER BY b.sub_set,',') AS prov_sets
FROM $qw_clname.sl_node a
LEFT OUTER JOIN $qw_clname.sl_subscribe b ON a.no_id = b.sub_receiver
LEFT OUTER JOIN $qw_clname.sl_path c ON c.pa_server = a.no_id AND c.pa_client = $qw_clname.getlocalnodeid(?)
LEFT OUTER JOIN $qw_clname.sl_node d ON b.sub_provider = d.no_id
LEFT OUTER JOIN $qw_clname.sl_set e ON b.sub_set = e.set_id
GROUP BY b.sub_provider, a.no_id, a.no_comment, c.pa_conninfo, d.no_comment, a.no_active
ORDER BY a.no_id
)
SELECT no_id,
nullif(string_agg(no_prov::text, ',' ORDER BY no_prov),'') AS no_provs,
no_conninfo,
nullif(string_agg(origin_sets::text, ',' ORDER BY origin_sets),'') AS origin_sets,
no_name,
nullif(string_agg(sub_tree, ';' ORDER BY sub_tree),'') AS no_sub_tree,
no_status,
nullif(string_agg(sub_sets::text, ',' ORDER BY prov_sets),'') AS sub_sets,
nullif(string_agg(sub_tree_name, ';' ORDER BY sub_tree_name),'') AS no_sub_tree_name,
nullif(string_agg(prov_sets_active::text, ',' ORDER BY prov_sets_active),'') AS prov_sets_active,
nullif(string_agg(prov_sets::text, ',' ORDER BY sub_sets),'') AS prov_sets
FROM z GROUP BY no_id, no_conninfo, no_name, no_status";
$sth = $dbh->prepare($query);
$sth->bind_param(1, "_" . $clname);
$sth->bind_param(2, "_" . $clname);
$sth->execute();
while (my @node = $sth->fetchrow) {
#printlogln($prefix,$logfile,$log_prefix,lookupMsg('dbg_generic', join(' - ', @node)));
push(@g_cluster, \@node);
}
$sth->finish;
$dbh->disconnect();
};
if ($@) {
if ($g_debug) {
printlogln($prefix,$logfile,$log_prefix,lookupMsg('dbg_generic', $@));
}
die lookupMsg('err_pgsql_connect');
}
else {
#if (substr($version,0,1) < 2) {
# printlogln($prefix,$logfile,$log_prefix,lookupMsg('wrn_not_tested', $version));
#}
if (($g_use_try_blocks) && ($g_lockset_method eq 'multiple') && (substr($version,0,3) <= 9.9)) {
# It's currently not possible to lock multiple sets at a time within a try block (v2.2.2), leave the logic in and set a high version number for now.
printlogln($prefix,$logfile,$log_prefix, lookupMsg('note_multiple_try', $version));
$g_lockset_method = 'single';
}
if (substr($version,0,3) >= 2.2) {
$g_failover_method = 'new';
$g_resubscribe_method = 'resubscribe';
}
else {
unless ($g_silence_notice) {
if ((substr($version,0,3) >= 2.0) && (substr($version,0,3) < 2.2)) {
printlogln($prefix,$logfile,$log_prefix,lookupMsg('wrn_failover_issues', $version));
}
printlogln($prefix,$logfile,$log_prefix,lookupMsg('note_autofail_fwd_only'));
$g_silence_notice = true;
}
if ($g_fail_subonly) {
printlogln($prefix,$logfile,$log_prefix,lookupMsg('note_fail_sub_only'));
$g_fail_subonly = false;
}
}
}
return (scalar(@g_cluster), $version);
}
# Load all sets originating on a node into global @g_sets
sub loadSets {
my $dbconninfo = shift;
my $clname = shift;
my $nodenumber = shift;
my $dbuser = shift;
my $dbpass = shift;
my $prefix = shift;
my $logfile = shift;
my $log_prefix = shift;
my $dsn;
my $dbh;
my $sth;
my $query;
my $qw_clname;
@g_sets = ();
$dsn = "DBI:Pg:$dbconninfo;";
eval {
$dbh = DBI->connect($dsn, $dbuser, $dbpass, {RaiseError => 1});
$qw_clname = $dbh->quote_identifier("_" . $clname);
$query = "SELECT set_id, trim(regexp_replace(set_comment,'[^0-9,A-Z,a-z]','_','g')) FROM $qw_clname.sl_set WHERE set_origin = ? ORDER BY set_id;";
$sth = $dbh->prepare($query);
$sth->bind_param(1, $nodenumber);
$sth->execute();
while (my @set = $sth->fetchrow) {
push(@g_sets, \@set);
}
$sth->finish;
$dbh->disconnect();
};
if ($@) {
if ($g_debug) {
printlogln($prefix,$logfile,$log_prefix,lookupMsg('dbg_generic', $@));
}
die lookupMsg('err_pgsql_connect');
}
return scalar(@g_sets);
}
# Load information regarding replication lag from sl_status into @g_lags
# If loading from a node that is not the intended origin then this information might not be that accurate/useful
sub loadLag {
my $dbconninfo = shift;
my $clname = shift;
my $dbuser = shift;
my $dbpass = shift;
my $prefix = shift;
my $logfile = shift;
my $log_prefix = shift;
my $dsn;
my $dbh;
my $sth;
my $query;
my $qw_clname;
@g_lags = ();
$dsn = "DBI:Pg:$dbconninfo;";
eval {
$dbh = DBI->connect($dsn, $dbuser, $dbpass, {RaiseError => 1});
$qw_clname = $dbh->quote_identifier("_" . $clname);
$query = "SELECT a.st_origin || ' (' || coalesce(trim(regexp_replace(substring(b.no_comment from E'\\\\((.+)\\\\)'), '[^0-9A-Za-z]','_', 'g')), 'node' || b.no_id) || ')<->'
|| a.st_received || ' (' || coalesce(trim(regexp_replace(substring(c.no_comment from E'\\\\((.+)\\\\)'), '[^0-9A-Za-z]','_', 'g')), 'node' || c.no_id) || ') Events: '
|| a.st_lag_num_events || ' Time: ' || a.st_lag_time
FROM $qw_clname.sl_status a
INNER JOIN $qw_clname.sl_node b on a.st_origin = b.no_id
INNER JOIN $qw_clname.sl_node c on a.st_received = c.no_id";
$sth = $dbh->prepare($query);
$sth->execute();
while (my $lag = $sth->fetchrow) {
push(@g_lags, $lag);
}
$sth->finish;
$dbh->disconnect();
};
if ($@) {
if ($g_debug) {
printlogln($prefix,$logfile,$log_prefix,lookupMsg('dbg_generic', $@));
}
die lookupMsg('err_pgsql_connect');
}
return scalar(@g_lags);
}
# Prompt user for nodes to an from in interactive mode and do some checking
sub chooseNode {
my $type = shift;
my $prefix = shift;
my $logfile = shift;
my $log_prefix = shift;
my $last_choice = shift;
my $line;
my $choice;
my %options;
my $ok;
my @sets_from;
my @sets_to;
my $found = false;
$line = sprintf "%-4s %-14s %-10s %-24s %-s\n", lookupMsg('interactive_head_id'), lookupMsg('interactive_head_name'), lookupMsg('interactive_head_status'), lookupMsg('interactive_head_providers'), lookupMsg('interactive_head_config');
printlog($prefix,$logfile,$log_prefix,"$line");
$line = sprintf "%-4s %-14s %-10s %-24s %-s\n", "="x(length(lookupMsg('interactive_head_id'))), "="x(length(lookupMsg('interactive_head_name'))), "="x(length(lookupMsg('interactive_head_status'))), "="x(length(lookupMsg('interactive_head_providers'))), "="x(length(lookupMsg('interactive_head_config')));
printlog($prefix,$logfile,$log_prefix,"$line");
foreach (@g_cluster) {
$line = sprintf "%-4s %-14s %-10s %-24s %-s\n", $_->[0], $_->[4], $_->[6], ($_->[1] // "<NONE>"), (lookupMsg('interactive_detail_1') . ($_->[3] // "<NONE>"));
printlog($prefix,$logfile,$log_prefix,"$line");
$line = sprintf "%-55s %-s\n", " ", (lookupMsg('interactive_detail_2') . ($_->[7] // "<NONE>"));
printlog($prefix,$logfile,$log_prefix,"$line");
$line = sprintf "%-55s %-s\n", " ", (lookupMsg('interactive_detail_3') . ($_->[5] // "<NONE>"));
printlogln($prefix,$logfile,$log_prefix,"$line");
$options{$_->[0]} = {name => $_->[4], sets => ($_->[3] // ""), status => $_->[6], provider => $_->[7]};
}
if ($type !~ m/info/i) {
printlog($prefix,$logfile,$log_prefix,lookupMsg('interactive_choose_node', $type));
$choice = <>;
chomp($choice);
if(exists($options{$choice})) {
if ($options{$choice}->{status} ne "ACTIVE") {
printlogln($prefix,$logfile,$log_prefix,lookupMsg('err_inactive', $choice, lc($options{$choice}->{status})));
$choice = -1;
}
elsif (($type =~ m/from/i) && (length(trim($options{$choice}->{sets})) <= 0)) {
printlogln($prefix,$logfile,$log_prefix,lookupMsg('err_not_origin', $choice));
$choice = -1;
}
elsif ($type =~ m/to/i) {
if (length(trim($options{$choice}->{provider})) <= 0) {
printlogln($prefix,$logfile,$log_prefix,lookupMsg('err_not_provider', $choice));
$choice = -1;
}
else {
foreach my $old_origin (@g_cluster) {