-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathgalera_check.pl
More file actions
2764 lines (2332 loc) · 114 KB
/
galera_check.pl
File metadata and controls
2764 lines (2332 loc) · 114 KB
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
# This tool is "fat-packed": most of its dependent modules are embedded
# in this file.
#######################################
#
# ProxySQL galera check v1
#
# Author Marco Tusa
# Copyright (C) (2016 - 2020)
#
#
#THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
#WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
#MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
#This program 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, version 2; OR the Perl Artistic License. On UNIX and similar
#systems, you can issue `man perlgpl' or `man perlartistic' to read these
#licenses.
#
#You should have received a copy of the GNU General Public License along with
#this program; if not, write to the Free Software Foundation, Inc., 59 Temple
#Place, Suite 330, Boston, MA 02111-1307 USA.
#######################################
package galera_check ;
use Time::HiRes qw(gettimeofday);
use strict;
use DBI;
use Getopt::Long;
use Pod::Usage;
$Getopt::Long::ignorecase = 0;
my $Param = {};
my $user = "admin";
my $pass = "admin";
my $help = '';
my $host = '' ;
my $debug = 0 ;
my %hostgroups;
my $mysql_connect_timeout=6;
my %processState;
my %processCommand;
my @HGIds;
######################################################################
#Local functions
######################################################################
sub URLDecode {
my $theURL = $_[0];
$theURL =~ tr/+/ /;
$theURL =~ s/%([a-fA-F0-9]{2,2})/chr(hex($1))/eg;
$theURL =~ s/<!--(.|\n)*-->//g;
return $theURL;
}
sub URLEncode {
my $theURL = $_[0];
$theURL =~ s/([\W])/"%" . uc(sprintf("%2.2x",ord($1)))/eg;
return $theURL;
}
# return a proxy object
sub get_proxy($$$$){
my $dns = shift;
my $user = shift;
my $pass = shift;
my $debug = shift;
my $proxynode = ProxySqlNode->new();
$proxynode->dns($dns);
$proxynode->user($user);
$proxynode->password($pass);
$proxynode->debug($debug);
return $proxynode;
}
sub main{
# ============================================================================
#+++++ INITIALIZATION
# ============================================================================
if($#ARGV < 0){
pod2usage(-verbose => 2) ;
exit 1;
}
if($#ARGV < 3){
#given a ProxySQL scheduler
#limitation we will pass the whole set of params as one
# and will split after
@ARGV = split('\ ',$ARGV[0]);
}
$Param->{user} = '';
$Param->{log} = undef ;
$Param->{password} = '';
$Param->{host} = '';
$Param->{port} = 3306;
$Param->{debug} = 0;
$Param->{processlist} = 0;
$Param->{OS} = $^O;
$Param->{main_segment} = 1;
$Param->{retry_up} = 0;
$Param->{retry_down} = 0;
$Param->{print_execution} = 1;
$Param->{development} = 0;
$Param->{development_time} = 2;
$Param->{active_failover} = 0;
$Param->{single_writer} = 1;
$Param->{writer_is_reader} = 1;
$Param->{check_timeout} = 800;
$Param->{ssl_certs_path} = undef;
my $run_pid_dir = "/tmp" ;
#if (
GetOptions(
'user|u:s' => \$Param->{user},
'password|p:s' => \$Param->{password},
'host|h:s' => \$host,
'port|P:i' => \$Param->{port},
'debug|d:i' => \$Param->{debug},
'log:s' => \$Param->{log},
'hostgroups|H:s'=> \$Param->{hostgroups},
'main_segment|S:s'=> \$Param->{main_segment},
'retry_up:i' => \$Param->{retry_up},
'retry_down:i' => \$Param->{retry_down},
'execution_time:i' => \$Param->{print_execution},
'development:i' => \$Param->{development},
'development_time:i' => \$Param->{development_time},
'single_writer:i' => \$Param->{single_writer},
'writer_is_also_reader:i' => \$Param->{writer_is_reader},
'active_failover:i' => \$Param->{active_failover},
'check_timeout:i' => \$Param->{check_timeout},
'ssl_certs_path:s' => \$Param->{ssl_certs_path},
'help|?' => \$Param->{help}
) or pod2usage(2);
pod2usage(-verbose => 2) if $Param->{help};
die print Utils->print_log(1,"Option --hostgroups not specified.\n") unless defined($Param->{hostgroups});
die print Utils->print_log(1,"Option --host not specified.\n") unless defined $Param->{host};
die print Utils->print_log(1,"Option --user not specified.\n") unless defined $Param->{user};
die print Utils->print_log(1,"Option --port not specified.\n") unless defined $Param->{port};
die print Utils->print_log(1,"Option --active_failover has an invalid value ($Param->{active_failover}).\n"
."Valid values are:\n"
." 0 [default] do not make failover\n"
." 1 make failover only if HG 8000 is specified in ProxySQL mysl_servers\n"
." 2 use PXC_CLUSTER_VIEW to identify a server in the same segment\n"
." 3 do whatever to keep service up also failover to another segment (use PXC_CLUSTER_VIEW) ") unless $Param->{active_failover} < 4;
#die "Option --log not specified. We need a place to log what is going on, don't we?\n" unless defined $Param->{log};
print Utils->print_log(2,"Option --log not specified. We need a place to log what is going on, don't we?\n") unless defined $Param->{log};
if($Param->{debug}){
Utils::debugEnv();
}
$Param->{host} = URLDecode($host);
my $dsn = "DBI:mysql:host=$Param->{host};port=$Param->{port}";
if(defined $Param->{user}){
$user = "$Param->{user}";
}
if(defined $Param->{password}){
$pass = "$Param->{password}";
}
my $hg =$Param->{hostgroups};
$hg =~ s/[\:,\,]/_/g;
my $base_path = "${run_pid_dir}/proxysql_galera_check_${hg}.pid";
#============================================================================
# Execution
#============================================================================
if(defined $Param->{log}){
open(FH, '>>', $Param->{log}."_".$hg.".log") or die Utils->print_log(1,"cannot open file");
FH->autoflush if $Param->{development} < 2;
select FH;
}
#checks for ssl cert path and identify if accessible.
#If defined and not accessible exit with an error
if(defined $Param->{ssl_certs_path}){
my $ssl_path = $Param->{ssl_certs_path};
if (-d $ssl_path) {
# directory called cgi-bin exists
if(-e $ssl_path."/client-key.pem"
&& -e $ssl_path."/client-cert.pem"
&& -e $ssl_path."/ca.pem"){
print Utils->print_log(4," SSL Directory exists and all the files are there $ssl_path")
}
else{
print Utils->print_log(1,"SSL Path (ssl_certs_path) declared and accessible [$ssl_path]. But certification files must have specific names:\n \t\t client-key.pem \n \t\t client-cert.pem \n \t\t ca.pem \n");
exit 1
}
}
else{
# ssl path declared but not exists, exit with error
print Utils->print_log(1,"SSL Path (ssl_certs_path) declared but not existing \n \t\t $ssl_path \n \t\t Please create directory and assign the right to access it to the ProxySQL user \n");
exit 1;
}
}
if($Param->{development} < 2){
if(!-e $base_path){
`echo "$$" > $base_path`
}
else{
my $existing_pid=`cat $base_path`;
my $exists = kill 0, $existing_pid;
if($exists > 0){
print STDOUT "Another process is running using the same HostGroup and settings,\n Or orphan pid file. check in $base_path \n";
print Utils->print_log(1,"Another process is running using the same HostGroup and settings,\n Or orphan pid file. check in $base_path \n");
exit 1;
}
else{
`echo "$$" > $base_path`;
}
}
}
# for test only purpose comment for prod
my $xx =1;
my $y =0;
$xx=2000000000 if($Param->{development} > 0);
while($y < $xx){
++$y ;
my $start = gettimeofday();
if($Param->{debug} >= 1){
print Utils->print_log(3,"START EXECUTION\n");
}
my $proxy_sql_node = get_proxy($dsn, $user, $pass ,$Param->{debug}) ;
$proxy_sql_node->retry_up($Param->{retry_up});
$proxy_sql_node->retry_down($Param->{retry_down});
$proxy_sql_node->hostgroups($Param->{hostgroups}) ;
$proxy_sql_node->require_failover($Param->{active_failover});
$proxy_sql_node->check_timeout($Param->{check_timeout});
$proxy_sql_node->connect();
# create basic galera cluster object and fill info
$proxy_sql_node->set_galera_cluster();
my $galera_cluster = $proxy_sql_node->get_galera_cluster();
if( defined $galera_cluster){
$galera_cluster->main_segment($Param->{main_segment});
$galera_cluster->cluster_identifier($hg);
$galera_cluster->get_nodes();
}
# Retrive the nodes state
if(defined $galera_cluster->nodes){
$galera_cluster->process_nodes();
}
#Analyze nodes state from ProxySQL prospective;
if(defined $galera_cluster->nodes){
my %action_node = $proxy_sql_node->evaluate_nodes($galera_cluster);
}
if(defined $proxy_sql_node->action_nodes){
$proxy_sql_node->push_changes;
}
my $end = gettimeofday();
print Utils->print_log(3,"END EXECUTION Total Time(ms):".($end - $start) * 1000 ."\n") if $Param->{print_execution} >0;
if($Param->{debug} >= 1){
print Utils->print_log(3,"\n");
}
FH->flush();
$proxy_sql_node->disconnect();
#debug braket
sleep $Param->{development_time} if($Param->{development} > 0);
}
if(defined $Param->{log}){
close FH; # in the end
}
`rm -f $base_path`;
exit(0);
}
# ############################################################################
# Run the program.
# ############################################################################
exit main(@ARGV);
{
package Galeracluster;
use threads;
use threads::shared;
use strict;
use warnings;
use Time::HiRes qw(gettimeofday usleep);
sub new {
my $class = shift;
my $SQL_get_mysql_servers=" SELECT a.* FROM runtime_mysql_servers a join stats_mysql_connection_pool b on a.hostname=b.srv_host and a.port=b.srv_port and a.hostgroup_id=b.hostgroup WHERE b.status not in ('OFFLINE_HARD','SHUNNED') ";
# Variable section for looping values
#Generalize object for now I have conceptualize as:
# Cluster (generic container)
# Cluster->{name} This is the cluster name
# Cluster->{nodes} the nodes in the cluster Map by node name
# Cluster->{status} cluster status [Primary|not Primary]
# Cluster->{size} cluster status [Primary|not Primary]
# Cluster->{singlenode}=0; 0 if false 1 if true meaning only one ACTIVE node in the cluster
# Cluster->{haswriter}=0; 0 if false 1 if true at least a node is fully active as writer
# Cluster->{singlewriter}=1; 0 if false 1 if true this cluster can have ONLY one writer a time [true default]
my $self = {
_name => undef,
_hosts => {},
_status => undef,
_size => {},
_singlenode => 0,
_haswriter => 0,
_singlewriter => 1,
_main_segment => 0,
_SQL_get_mysql_servers => $SQL_get_mysql_servers,
_hostgroups => undef,
_dbh_proxy => undef,
_debug => 0,
_monitor_user => undef,
_monitor_password => undef,
_nodes => {},
_nodes_maint => {},
_check_timeout => 100, #timeout in ms
_cluster_identifier => undef,
_hg_writer_id => 0,
_hg_reader_id => 0,
_ssl_certificates_path => undef,
_writer_is_reader => 0,
_reader_nodes => [] ,
_writer_nodes => [] ,
_has_failover_node =>0,
_writers =>0,
#_hg => undef,
};
bless $self, $class;
return $self;
}
sub ssl_certificates_path{
my ( $self, $in ) = @_;
$self->{_ssl_certificates_path} = $in if defined($in);
return $self->{_ssl_certificates_path};
}
sub has_failover_node{
my ( $self, $in ) = @_;
$self->{_has_failover_node} = $in if defined($in);
return $self->{_has_failover_node};
}
sub writer_nodes{
my ( $self, $in ) = @_;
$self->{_writer_nodes} = $in if defined($in);
return $self->{_writer_nodes};
}
sub reader_nodes{
my ( $self, $in ) = @_;
$self->{_reader_nodes} = $in if defined($in);
return $self->{_reader_nodes};
}
sub cluster_identifier{
my ( $self, $in ) = @_;
$self->{_cluster_identifier} = $in if defined($in);
return $self->{_cluster_identifier};
}
sub main_segment{
my ( $self, $main_segment ) = @_;
$self->{_main_segment} = $main_segment if defined($main_segment);
return $self->{_main_segment};
}
sub check_timeout{
my ( $self, $check_timeout ) = @_;
$self->{_check_timeout} = $check_timeout if defined($check_timeout);
return $self->{_check_timeout};
}
sub debug{
my ( $self, $debug ) = @_;
$self->{_debug} = $debug if defined($debug);
return $self->{_debug};
}
sub dbh_proxy{
my ( $self, $dbh_proxy ) = @_;
$self->{_dbh_proxy} = $dbh_proxy if defined($dbh_proxy);
return $self->{_dbh_proxy};
}
sub name {
my ( $self, $name ) = @_;
$self->{_name} = $name if defined($name);
return $self->{_name};
}
sub nodes {
my ( $self, $nodes ) = @_;
$self->{_nodes} = $nodes if defined($nodes);
return $self->{_nodes};
}
sub nodes_maint {
my ( $self, $nodes ) = @_;
$self->{_nodes_maint} = $nodes if defined($nodes);
return $self->{_nodes_maint};
}
sub status {
my ( $self, $status ) = @_;
$self->{_status} = $status if defined($status);
return $self->{_status};
}
sub size {
my ( $self, $size ) = @_;
$self->{_size} = $size if defined($size);
return $size->{_size};
}
sub singlenode {
my ( $self, $singlenode ) = @_;
$self->{_singlenode} = $singlenode if defined($singlenode);
return $self->{_singlenode};
}
sub haswriter {
my ( $self, $haswriter ) = @_;
$self->{_haswriter} = $haswriter if defined($haswriter);
return $self->{_haswriter};
}
sub singlewriter {
my ( $self, $singlewriter ) = @_;
$self->{_singlewriter} = $singlewriter if defined($singlewriter);
return $self->{_singlewriter};
}
sub writer_is_reader {
my ( $self, $writer_is_reader ) = @_;
$self->{_writer_is_reader} = $writer_is_reader if defined($writer_is_reader);
return $self->{_writer_is_reader};
}
sub writers {
my ( $self, $in ) = @_;
$self->{_writers} = $in if defined($in);
return $self->{_writers};
}
sub hostgroups {
my ( $self, $hostgroups ) = @_;
$self->{_hostgroups} = $hostgroups if defined($hostgroups);
return $self->{_hostgroups};
}
sub hg_writer_id {
my ( $self, $hostgroups ) = @_;
$self->{_hg_writer_id} = $hostgroups if defined($hostgroups);
return $self->{_hg_writer_id};
}
sub hg_reader_id {
my ( $self, $hostgroups ) = @_;
$self->{_hg_reader_id} = $hostgroups if defined($hostgroups);
return $self->{_hg_reader_id};
}
sub monitor_user{
my ( $self, $monitor_user ) = @_;
$self->{_monitor_user} = $monitor_user if defined($monitor_user);
return $self->{_monitor_user};
}
sub monitor_password {
my ( $self, $monitor_password ) = @_;
$self->{_monitor_password} = $monitor_password if defined($monitor_password);
return $self->{_monitor_password};
}
# this function is used to identify the nodes in the cluster
# using the HG as reference
sub get_nodes{
my ( $self) = @_;
my $dbh = $self->{_dbh_proxy};
my $cmd =$self->{_SQL_get_mysql_servers}." AND hostgroup_id IN (".join(",",sort keys(%{$self->hostgroups})).") order by hostgroup_id, hostname";
my $sth = $dbh->prepare($cmd);
$sth->execute();
my $i = 1;
my $locHg = $self->{_hostgroups};
my $ssl_certificates = "";
#if a ssl certificate path is defined, will create the path for each certificate and add to the dns string
if(defined $self->{_ssl_certificates_path}){
$ssl_certificates = ";mysql_ssl_client_key=".$self->{_ssl_certificates_path}."/client-key.pem"
.";mysql_ssl_client_cert=".$self->{_ssl_certificates_path}."/client-cert.pem"
.";mysql_ssl_ca_file=".$self->{_ssl_certificates_path}."/ca.pem"
}
while (my $ref = $sth->fetchrow_hashref()) {
my $ssl_options="" ;
my $node = GaleraNode->new();
$node->debug($self->debug);
$node->use_ssl($ref->{use_ssl});
$node->hostgroups($ref->{hostgroup_id});
if($node->{_hostgroups} > 8000
&& exists $locHg->{$node->{_hostgroups}}){
$self->{_has_failover_node} = 1;
}
$node->ip($ref->{hostname});
$node->port($ref->{port});
if($node->use_ssl gt 0 ){
$ssl_options = ";mysql_ssl=1";
if($self->debug){print Utils->print_log(4," Galera cluster node " . $node->ip.":". $node->port.":HG=".$node->hostgroups." Using SSL ($ssl_options)\n" ) }
if(defined $self->{_ssl_certificates_path}){
$ssl_options = $ssl_options . $ssl_certificates;
if($self->debug){print Utils->print_log(4," Certificates also in use ($self->{_ssl_certificates_path})\n")}
}
}
$node->dns("DBI:mysql:host=".$node->ip.";port=".$node->port.";mysql_connect_timeout=$mysql_connect_timeout".$ssl_options);
$node->weight($ref->{weight});
$node->connections($ref->{max_connections});
$node->user($self->{_monitor_user});
$node->password($self->{_monitor_password});
$node->proxy_status($ref->{status});
$node->comment($ref->{comment});
$node->set_retry_up_down($self->{_cluster_identifier});
$node->gtid_port($ref->{gtid_port});
$node->compression($ref->{compression});
$node->max_latency($ref->{max_latency_ms});
$node->max_replication_lag($ref->{max_replication_lag});
$self->{_nodes}->{$i++}=$node;
$node->debug($self->debug);
if($self->debug){print Utils->print_log(3," Galera cluster node " . $node->ip.":". $node->port.":HG=".$node->hostgroups."\n" ) }
}
if($self->debug){print Utils->print_log(3," Galera cluster nodes loaded \n") ; }
}
#Processing the nodes in the cluster and identify which node is active and which is to remove
sub process_nodes{
my ( $self ) = @_;
my $nodes = $self->{_nodes} ;
my $start = gettimeofday();
my $run_milliseconds=0;
my $init =0;
my $irun = 1;
my %Threads;
my $new_nodes ={} ;
my $processed_nodes ={} ;
#using multiple threads to connect if a node is present in more than one HG it will have 2 threads
while($irun){
$irun = 0;
foreach my $key (sort keys %{$self->{_nodes}}){
if(!exists $Threads{$key}){
if($self->debug){print Utils->print_log(3, " Creating new thread to manage server check:".
$self->{_nodes}->{$key}->ip.":".
$self->{_nodes}->{$key}->port.":HG".$self->{_nodes}->{$key}->hostgroups."\n" ) }
$new_nodes->{$key} = $self->{_nodes}->{$key};
$new_nodes->{$key}->{_process_status} = -1;
$new_nodes->{$key}->{_ssl_certificates_path} = $self->ssl_certificates_path;
# debug senza threads comment next line
$Threads{$key}=threads->create(sub {return get_node_info($self,$key)});
#DEBUG Without threads uncomment from here
#next unless $new_nodes->{$key} = get_node_info($self,$key);
#evaluate_joined_node($self, $key, $new_nodes, $processed_nodes) ;
# to here
}
}
##DEBUG SENZA THREADS commenta da qui
foreach my $thr (sort keys %Threads) {
if($new_nodes->{$thr}->{_process_status} eq -100){
next;
}
if ($Threads{$thr}->is_running()) {
my $tid = $Threads{$thr}->tid;
#print " - Thread $tid running\n";
if($run_milliseconds > $self->{_check_timeout} ){
if($self->debug >=0){
my $timeout = ($run_milliseconds - $self->{_check_timeout});
print print Utils->print_log(2,"Check timeout Node ip : $new_nodes->{$thr}->{_ip} , THID " . $tid." (taken: $run_milliseconds max_allowed: $self->{_check_timeout} over for ms: $timeout \n")
}
$irun = 0 ;
}
else{
$irun = 1;
}
}
elsif ( $Threads{$thr}->is_joinable()) {
my $tid = $Threads{$thr}->tid;
( $new_nodes->{$thr} ) = $Threads{$thr}->join;
#$processed_nodes =
evaluate_joined_node($self, $thr, $new_nodes, $processed_nodes) ;
if($self->debug){print Utils->print_log(3," Thread joined : " . $tid."\n" ) }
#print " - Results for thread $tid:\n";
#print " - Thread $tid has been joined\n";
}
#print ".";
}
## a qui
if($self->debug){$run_milliseconds = (gettimeofday() -$start ) *1000};
#sleep for a time equal to the half of the timeout to save cpu cicle
#usleep(($self->{_check_timeout} * 1000)/2);
}
$self->{_nodes} = $new_nodes;
if($self->debug){$run_milliseconds = (gettimeofday() -$start ) *1000};
if($debug>=3){
foreach my $key (sort keys %{$new_nodes}){
if($new_nodes->{$key}->{_process_status} == 1){
print Utils->print_log(4,$new_nodes->{$key}->{_ip}.":".$new_nodes->{$key}->{_hostgroups}." Processed \n");
}
else{
print Utils->print_log(4,$new_nodes->{$key}->{_ip}.":".$new_nodes->{$key}->{_hostgroups}." NOT Processed\n");
}
}
}
if($self->debug){print Utils->print_log(3," Multi Thread execution done in : " . $run_milliseconds. "(ms) \n" )}
}
sub evaluate_joined_node($$$$){
my $self = shift;
my $thr = shift;
my $new_nodes = shift;
my $processed_nodes = shift;
#count the number of nodes by segment
if($new_nodes->{$thr}->{_proxy_status} ne "OFFLINE_SOFT"
&& $new_nodes->{$thr}->{_proxy_status} ne "SHUNNED"
&& ($new_nodes->{$thr}->{_process_status} < 0 ||
!exists $processed_nodes->{$new_nodes->{$thr}->{_ip}})
&& defined $new_nodes->{$thr}->{_wsrep_segment}
){
$self->{_size}->{$new_nodes->{$thr}->{_wsrep_segment}} = (($self->{_size}->{$new_nodes->{$thr}->{_wsrep_segment}}|| 0) +1);
$processed_nodes->{$new_nodes->{$thr}->{_ip}}=$self->{_size}->{$new_nodes->{$thr}->{_wsrep_segment}};
}
#assign size to HG
if($new_nodes->{$thr}->{_proxy_status} ne "OFFLINE_SOFT"
&& defined $new_nodes->{$thr}->{_wsrep_segment}
){
$self->{_hostgroups}->{$new_nodes->{$thr}->{_hostgroups}}->{_size} = ($self->{_hostgroups}->{$new_nodes->{$thr}->{_hostgroups}}->{_size}) + 1;
}
#checks for ONLINE writer(s)
if(defined $new_nodes->{$thr}->{_read_only}
&& $new_nodes->{$thr}->{_read_only} eq "OFF"
&& ($new_nodes->{$thr}->{_proxy_status} eq "ONLINE" || $new_nodes->{$thr}->{_proxy_status} eq "OFFLINE_SOFT")
&& ($new_nodes->{$thr}->{_hostgroups} == $self->hg_writer_id || $new_nodes->{$thr}->{_hostgroups} == ($self->hg_writer_id +9000))
){
if($new_nodes->{$thr}->{_hostgroups} == $self->hg_writer_id
&& $new_nodes->{$thr}->{_proxy_status} eq "ONLINE"
){
$self->{_haswriter} = 1 ;
$self->{_writers} = $self->{_writers} +1;
}
push (@{$self->{_writer_nodes}}, "$new_nodes->{$thr}->{_ip}:$new_nodes->{$thr}->{_port}");
}
elsif(($new_nodes->{$thr}->{_proxy_status} eq "ONLINE" || $new_nodes->{$thr}->{_proxy_status} eq "OFFLINE_SOFT")
&& ($new_nodes->{$thr}->{_hostgroups} == $self->hg_reader_id || $new_nodes->{$thr}->{_hostgroups} == ($self->hg_reader_id +9000))
){
push (@{$self->{_reader_nodes}}, "$new_nodes->{$thr}->{_ip}:$new_nodes->{$thr}->{_port}");
}
else{
if($self->debug
&& $new_nodes->{$thr}->{_hostgroups} == $self->hg_writer_id){
print Utils->print_log(3," Not a writer :" .$new_nodes->{$thr}->{_ip} . " HG: $new_nodes->{$thr}->{_hostgroups} \n" )
}
}
# check if under maintenance
if($new_nodes->{$thr}->{_proxy_status} eq "OFFLINE_SOFT"
&& $new_nodes->{$thr}->{_pxc_maint_mode} eq "MAINTENANCE"){
$self->{_nodes_maint}->{$thr} = $new_nodes->{$thr};
}
#return $processed_nodes;
}
sub get_node_info($$){
my $self = shift;
my $key = shift;
my $nodes =shift;
my ( $node ) = $self->{_nodes}->{$key};
if(!defined $node->get_node_info()){
$node->{_process_status}=-100;
}
return $node;
}
}
{
package GaleraNode;
#Node Proxy States
sub new {
my $class = shift;
my $SQL_get_variables="SHOW GLOBAL VARIABLES LIKE 'wsrep%";
my $SQL_get_status="SHOW GLOBAL STATUS LIKE 'wsrep%";
my $SQL_get_read_only="SHOW GLOBAL VARIABLES LIKE 'read_only'";
# Variable section for looping values
#Generalize object for now I have conceptualize as:
# Node (generic container)
# Node->{name} This is the cluster name
# Node->{IP}
# Node->{hostgroups}
# Node->{clustername} This is the cluster name
# Node->{read_only} Read only node
# Node->{wsrep_status} node status (OPEN 0,Primary 1,Joiner 2,Joined 3,Synced 4,Donor 5)
# Node->{wsrep_rejectqueries} (NON, ALL,ALL_KILL)
# Node->{wsrep_donorrejectqueries} If true the node when donor
# Node->{wsrep_connected}=0; if false 1 if true meaning only one ACTIVE node in the cluster
# Node->{wsrep_desinccount}=0; 0 if false 1 if true at least a node is fully active as writer
# Node->{wsrep_ready} ON -OFF
my $self = {
_name => undef,
_ip => undef,
_port => 3306,
_hostgroups => undef,
_clustername => undef,
_read_only => undef,
_wsrep_status => -1,
_wsrep_rejectqueries => undef,
_wsrep_donorrejectqueries => undef,
_wsrep_connected => undef,
_wsrep_desinccount => undef,
_wsrep_ready => undef,
_wsrep_provider => [],
_wsrep_segment => 1000,
_wsrep_pc_weight => 1,
_SQL_get_variables => $SQL_get_variables,
_SQL_get_status=> $SQL_get_status,
_SQL_get_read_only=> $SQL_get_read_only,
_dns => undef,
_user => undef,
_password => undef,
_debug => 0,
_port => undef,
_proxy_status => undef,
_weight => 1,
_connections => 2000,
_cluster_status => undef,
_cluster_size => 0,
_process_status => -1,
_MOVE_UP_OFFLINE => 1000, #move a node from OFFLINE_SOFT
_MOVE_UP_HG_CHANGE => 1010, #move a node from HG 9000 (plus hg id) to reader HG
_MOVE_DOWN_HG_CHANGE => 3001, #move a node from original HG to maintenance HG (HG 9000 (plus hg id) ) kill all existing connections
_MOVE_DOWN_OFFLINE => 3010 , # move node to OFFLINE_soft keep existing connections, no new connections.
_MOVE_TO_MAINTENANCE => 3020 , # move node to OFFLINE_soft keep existing connections, no new connections because maintenance.
_MOVE_OUT_MAINTENANCE => 3030 , # move node to OFFLINE_soft keep existing connections, no new connections because maintenance.
_INSERT_READ => 4010, # Insert a node in the reader host group
_INSERT_WRITE => 4020, # Insert a node in the writer host group
_DELETE_NODE => 5000, # this remove the node from the hostgroup
_SAVE_RETRY => 9999, # this reset the retry counter in the comment
#_MOVE_SWAP_READER_TO_WRITER => 5001, #Future use
#_MOVE_SWAP_WRITER_TO_READER => 5010, #Future use
_retry_down_saved => 0, # number of retry on a node before declaring it as failed.
_retry_up_saved => 0, # number of retry on a node before declaring it OK.
_comment => undef,
_gtid_port => 0,
_compression => 0,
_use_ssl => 0,
_ssl_certificates_path => undef,
_max_latency => 0,
_max_replication_lag => 0,
_wsrep_gcomm_uuid => undef,
_wsrep_local_index => 0,
_pxc_maint_mode => undef,
};
bless $self, $class;
return $self;
}
sub ssl_certificates_path{
my ( $self, $in ) = @_;
$self->{_ssl_certificates_path} = $in if defined($in);
return $self->{_ssl_certificates_path};
}
sub max_replication_lag{
my ( $self, $in ) = @_;
$self->{_max_replication_lag} = $in if defined($in);
return $self->{_max_replication_lag};
}
sub max_latency{
my ( $self, $in ) = @_;
$self->{_max_latency} = $in if defined($in);
return $self->{_max_latency};
}
sub use_ssl{
my ( $self, $in ) = @_;
$self->{_use_ssl} = $in if defined($in);
return $self->{_use_ssl};
}
sub compression{
my ( $self, $in ) = @_;
$self->{_compression} = $in if defined($in);
return $self->{_compression};
}
sub gtid_port{
my ( $self, $in ) = @_;
$self->{_gtid_port} = $in if defined($in);
return $self->{_gtid_port};
}
sub pxc_maint_mode{
my ( $self, $in ) = @_;
$self->{_pxc_maint_mode} = $in if defined($in);
return $self->{_pxc_maint_mode};
}
sub wsrep_local_index{
my ( $self, $in ) = @_;
$self->{_wsrep_local_index} = $in if defined($in);
return $self->{_wsrep_local_index};
}
sub comment{
my ( $self, $in ) = @_;
$self->{_comment} = $in if defined($in);
return $self->{_comment};
}
sub wsrep_gcomm_uuid{
my ( $self, $in ) = @_;
$self->{_wsrep_gcomm_uuid} = $in if defined($in);
return $self->{_wsrep_gcomm_uuid};
}
sub retry_down_saved{
my ( $self, $in ) = @_;
$self->{_retry_down_saved} = $in if defined($in);
return $self->{_retry_down_saved};
}
sub retry_up_saved{
my ( $self, $in ) = @_;
$self->{_retry_up_saved} = $in if defined($in);
return $self->{_retry_up_saved};
}
sub process_status {
my ( $self, $process_status ) = @_;
$self->{_process_status} = $process_status if defined($process_status);
return $self->{_process_status};
}
sub debug{
my ( $self, $debug ) = @_;
$self->{_debug} = $debug if defined($debug);
return $self->{_debug};
}
sub SAVE_RETRY {
my ( $self) = @_;
return $self->{_SAVE_RETRY};
}
sub MOVE_UP_OFFLINE {
my ( $self) = @_;
return $self->{_MOVE_UP_OFFLINE};
}
sub MOVE_UP_HG_CHANGE {
my ( $self) = @_;
return $self->{_MOVE_UP_HG_CHANGE};
}
sub MOVE_DOWN_OFFLINE {
my ( $self) = @_;
return $self->{_MOVE_DOWN_OFFLINE};
}
sub MOVE_TO_MAINTENANCE {
my ( $self) = @_;
return $self->{_MOVE_TO_MAINTENANCE};
}
sub MOVE_OUT_MAINTENANCE {
my ( $self) = @_;
return $self->{_MOVE_OUT_MAINTENANCE};
}
sub MOVE_DOWN_HG_CHANGE {
my ( $self) = @_;
return $self->{_MOVE_DOWN_HG_CHANGE};
}
sub DELETE_NODE {
my ( $self) = @_;
return $self->{_DELETE_NODE};
}
sub INSERT_READ {
my ( $self) = @_;
return $self->{_INSERT_READ};
}
sub INSERT_WRITE {
my ( $self) = @_;
return $self->{_INSERT_WRITE};
}
sub cluster_status {
my ( $self, $status ) = @_;
$self->{_cluster_status} = $status if defined($status);
return $self->{_cluster_status};
}
sub cluster_size {
my ( $self, $size ) = @_;
$self->{_cluster_size} = $size if defined($size);
return $size->{_cluster_size};
}
sub weight {
my ( $self, $weight ) = @_;
$self->{_weight} = $weight if defined($weight);
return $self->{_weight};
}
sub connections {
my ( $self, $connections ) = @_;
$self->{_connections} = $connections if defined($connections);
return $self->{_connections};
}
sub proxy_status {
my ( $self, $status ) = @_;
$self->{_proxy_status} = $status if defined($status);
return $self->{_proxy_status};
}
sub dns {
my ( $self, $dns ) = @_;
$self->{_dns} = $dns if defined($dns);
return $self->{_dns};
}
sub user{
my ( $self, $user ) = @_;
$self->{_user} = $user if defined($user);