-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClusterConfig.pm
183 lines (141 loc) · 4.53 KB
/
ClusterConfig.pm
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
package ClusterConfig;
use Sys::Hostname;
# This module wraps the configuration data, and makes it available to the other modules.
#
# The configuration file contains the config data for all nodes, not just this one. Clients, servers, and arbitrator.
# The cluster is divided into partitions (data centres)
# There can be some server nodes and some client nodes in each partition.
# Todo: we want to read the config from the database, and store a local persistent backup copy.
my @servers;
my @clients;
my @arbitrators;
my @commands;
# Read the actual config data.
#require Config;
open (CONFIG, "Config.pm");
while ($record = <CONFIG>) {
$config .= $record;
}
close(CONFIG);
eval ($config);
my @segments;
my %nodesByGaleraIp;
my $this_hostname;
my $this_node;
my $this_instance_name = shift;
print "This instance is $this_instance_name\n";
# In case we have 2 minders on one host, this distinguishes which one we are now.
$this_hostname = `hostname`;
$this_hostname =~ s/^\s+|\s+$//g ;
print "My hostname is $this_hostname\n";
my $counter = 0;
my $problem = 0;
my $node;
# Preen the server config information
foreach my $id (0 .. $#servers ) {
$node = $servers[$id];
generic_config($node,'Server',$id);
if ( ! defined $node->{galera_ip} ) { $node->{galera_ip} = $node->{minder_ip}; }
$nodesByGaleraIp->{$node->{galera_ip}} = $node;
print "Added $node->{galera_ip} \n";
}
foreach my $id (0 .. $#servers) {
$node = $servers[$id];
$node->{nodes_in_segment} = $segments[ $node->{segment} ];
}
# Preen the arbitrators config information
foreach my $id (0 .. $#arbitrators ) {
$node = $arbitrators[$id];
generic_config($node,'Arbitrator',$id);
}
# Preen the client config information
foreach my $id (0 .. $#clients ) {
$node = $clients[$id];
generic_config($node,'Client',$id);
if ( ! defined $node->{glb_ip} ) { $node->glb_ip = '127.0.0.1' ; }
if ( ! defined $node->{glb_port} ) { $node->glb_port = '3307' ; }
}
# Preen the command config information
foreach my $id (0 .. $#commands ) {
$node = $commands[$id];
generic_config($node,'Command',$id);
}
#--------------------------------
if ($problem == 1) {
print "Cannot continue due to configuration problems.\n";
die;
}
sub generic_config {
my $node = shift;
my $type = shift;
my $id = shift;
print "Genric config for node id=$id type=$type\n";
$node->{id} = $id;
$node->{type} = $type;
if ( ! defined $node->{hostname} && ! defined $node->{instance_name} ) {
print "$type $id has no hostname nor instance_name!\n";
$problem = 1;
} else {
if ( $node->{instance_name} eq $this_instance_name ) {
print "Instance identified by $this_instance_name\n";
$this_node = $node;
} else {
print "this instance is $this_instance_name and node is $node->{instance_name}\n";
if ( $this_instance_name eq '' && $node->{hostname} eq $this_hostname ) {
print "Instance identified by host name $this_hostname\n";
$this_node = $node;
}
}
}
if ( ! defined $node->{minder_ip} ) {
print "$type $id has no minder_ip address\n";
$problem = 1;
}
if ( ! defined $node->{minder_port} ) { $node->{minder_port} = 3309; }
if ( ! defined $node->{segment} ) { $node->{segment} = 0; }
$segments[ $node->{segment} ] ++;
}
sub create_Clients {
foreach my $id (0 .. $#clients ) {
$node = $clients[$id];
Correspondent::create($node);
}
}
sub create_Servers {
foreach my $id (0 .. $#servers ) {
$node = $servers[$id];
Correspondent::create($node);
}
}
sub create_Arbitrators {
foreach my $id (0 .. $#arbitrators ) {
$node = $arbitrators[$id];
Correspondent::create($node);
}
}
sub node { return $this_node; }
sub server_count { return $#servers + 1; }
sub client_count { return $#clients + 1; }
sub arbitrator_count { return $#arbitrators + 1; }
sub nodeByGaleraIp {
# my $klass = shift;
my $IP = shift;
print "Looking for $IP\n";
return $nodesByGaleraIp->{$IP} ;
}
sub nodeByTypeId {
my $type = shift;
my $id = shift;
my $ip = shift;
my $port = shift;
if ( $type eq 'Server' ) { $node = $servers[$id]; }
if ( $type eq 'Arbitrator' ) { $node = $arbitrators[$id]; }
if ( $type eq 'Client' ) { $node = $clients[$id]; }
if ( $type eq 'Command' ) {
# The command nodes are not kept in config.
# So we just make this up on the spot.
$node = { type=>'Command', id=>$id, minder_ip=>$ip, minder_port=>$port};
}
return $node;
}
1;