30
30
require '@@PGLIBDIR@@/slon-tools.pm';
31
31
require $CONFIG_FILE;
33
my $FILE="/tmp/init-cluster.$$";
36
open(SLONIK, ">", $FILE);
37
print SLONIK "\n# INIT CLUSTER\n";
38
print SLONIK genheader();
34
$slonik .= "\n# INIT CLUSTER\n";
35
$slonik .= genheader();
39
36
my ($dbname, $dbhost) = ($DBNAME[$MASTERNODE], $HOST[$MASTERNODE]);
40
print SLONIK " init cluster (id = $MASTERNODE, comment = 'Node $MASTERNODE - $dbname\@$dbhost');\n";
37
$slonik .= " init cluster (id = $MASTERNODE, comment = 'Node $MASTERNODE - $dbname\@$dbhost');\n";
43
print SLONIK "\n# STORE NODE\n";
40
$slonik .= "\n# STORE NODE\n";
44
41
foreach my $node (@NODES) {
45
42
if ($node != $MASTERNODE) { # skip the master node; it's already initialized!
46
43
my ($dbname, $dbhost) = ($DBNAME[$node], $HOST[$node]);
47
print SLONIK " store node (id = $node, event node = $MASTERNODE, comment = 'Node $node - $dbname\@$dbhost');\n";
44
$slonik .= " store node (id = $node, event node = $MASTERNODE, comment = 'Node $node - $dbname\@$dbhost');\n";
50
print SLONIK " echo 'Set up replication nodes';\n";
47
$slonik .= " echo 'Set up replication nodes';\n";
53
print SLONIK "\n# STORE PATH\n";
58
generate_listen_paths();
60
print SLONIK " echo 'Next: configure paths for each node/origin';\n";
50
$slonik .= "\n# STORE PATH\n";
52
$slonik .= " echo 'Next: configure paths for each node/origin';\n";
61
53
foreach my $nodea (@NODES) {
62
54
my $dsna = $DSN[$nodea];
63
55
foreach my $nodeb (@NODES) {
66
58
my $providerba = $VIA[$nodea][$nodeb];
67
59
my $providerab = $VIA[$nodeb][$nodea];
68
60
if (!$printed[$nodea][$nodeb] and $providerab == $nodea) {
69
print SLONIK " store path (server = $nodea, client = $nodeb, conninfo = '$dsna');\n";
61
$slonik .= " store path (server = $nodea, client = $nodeb, conninfo = '$dsna');\n";
70
62
$printed[$nodea][$nodeb] = "done";
72
64
if (!$printed[$nodeb][$nodea] and $providerba == $nodea) {
73
print SLONIK " store path (server = $nodeb, client = $nodea, conninfo = '$dsnb');\n";
65
$slonik .= " store path (server = $nodeb, client = $nodea, conninfo = '$dsnb');\n";
74
66
$printed[$nodeb][$nodea] = "done";
81
print SLONIK "\n# STORE LISTEN\n";
82
foreach my $origin (@NODES) {
83
my $dsna = $DSN[$origin];
84
foreach my $receiver (@NODES) {
85
if ($origin != $receiver) {
86
my $provider = $VIA[$origin][$receiver];
87
print SLONIK " store listen (origin = $origin, receiver = $receiver, provider = $provider);\n";
91
print SLONIK " echo 'Replication nodes prepared';\n";
92
print SLONIK " echo 'Please start a slon replication daemon for each node';\n";
94
run_slonik_script($FILE);
97
sub generate_listen_paths {
98
my $infinity = 10000000; # Initial costs are all infinite
99
foreach my $node1 (@NODES) {
100
foreach my $node2 (@NODES) {
101
$COST[$node1][$node2] = $infinity;
105
# Initialize paths between parents and children, and based on them,
106
# generate initial seeding of listener paths, @VIA
108
foreach my $node1 (@NODES) {
109
$COST[$node1][$node1] = 0;
110
$VIA[$node1][$node1] = 0;
111
foreach my $node2 (@NODES) {
112
if ($node2 != $node1) {
113
if ((not ($PARENT[$node1] or $PARENT[$node2])) or
114
($PARENT[$node1] and $PARENT[$node1] == $node2) or
115
($PARENT[$node2] and $PARENT[$node2] == $node1)) {
116
$PATH[$node1][$node2] = 1;
117
$PATH[$node2][$node1] = 1;
118
# Set up a cost 1 path between them
120
$COST[$node1][$node2] = 1;
121
$VIA[$node1][$node2] = $node1;
124
$COST[$node2][$node1] = 1;
125
$VIA[$node2][$node1] = $node2;
131
# Now, update the listener paths...
132
# 4 level nested iteration:
133
# 1 while not done, do
134
# 2 for each node, node1
135
# 3 for each node, node2, where node2 <> node1, where we don't
136
# yet have a listener path
137
# 4 for each node node3 (<> node1 or node2),
138
# consider introducing the listener path:
139
# node1 to node2 then node2 to node3
140
# In concept, it's an O(n^4) algorithm; since the number of nodes, n,
141
# is not likely to get particularly large, it's not worth tuning
144
while ($didwork eq "yes") {
146
foreach my $node1 (@NODES) {
147
foreach my $node3 (@NODES) {
148
if (($VIA[$node3][$node1] == 0) && ($node3 != $node1)) {
149
foreach my $node2 (@NODES) {
150
if ($PATH[$node1][$node2] && ($VIA[$node2][$node3] != 0) && ($node2 != $node3) && ($node2 != $node1)) {
151
# Consider introducing a path from n1 to n2 then n2 to n3
152
# as a cheaper alternative to going direct from n1 to n3
153
my $oldcost = $COST[$node3][$node1];
154
my $newcost = $COST[$node1][$node2] + $COST[$node2][$node3];
155
if ($newcost < $oldcost) {
157
# So we go via node 2
158
$VIA[$node3][$node1] = $node2;
159
$COST[$node3][$node1] = $newcost;
161
$VIA[$node1][$node3] = $node2;
162
$COST[$node1][$node3] = $newcost;
172
sub report_on_paths {
173
print "# Configuration Summary:\n";
177
foreach my $node2 (@NODES) {
178
printf "| %3d ", $node2;
181
print ("-----+" x (scalar(@NODES) + 1));
183
foreach my $node1 (@NODES) {
184
printf "# %3d ", $node1;
185
foreach my $node2 (@NODES) {
186
if ($COST[$node2][$node1] == $infinity) {
189
printf "|%4d ", $COST[$node2][$node1];
197
foreach my $node2 (@NODES) {
198
printf "| %3d ", $node2;
201
print ("-----+" x (scalar(@NODES) + 1));
203
foreach my $node1 (@NODES) {
204
printf "# %3d ", $node1;
205
foreach my $node2 (@NODES) {
206
printf "|%4d ", $VIA[$node2][$node1];
214
foreach my $node2 (@NODES) {
215
printf "| %3d ", $node2;
218
print ("-----+" x (scalar(@NODES) + 1));
220
foreach my $node1 (@NODES) {
221
printf "# %3d ", $node1;
222
foreach my $node2 (@NODES) {
223
printf "|%4d ", $PATH[$node2][$node1];
72
$slonik .= " echo 'Replication nodes prepared';\n";
73
$slonik .= " echo 'Please start a slon replication daemon for each node';\n";
74
run_slonik_script($slonik, 'INIT CLUSTER');