~ubuntu-branches/ubuntu/maverick/slony1/maverick

« back to all changes in this revision

Viewing changes to tools/altperl/slonik_init_cluster.pl

  • Committer: Bazaar Package Importer
  • Author(s): Peter Eisentraut
  • Date: 2006-11-17 17:19:48 UTC
  • mfrom: (1.1.2 upstream) (2.1.1 etch)
  • Revision ID: james.westby@ubuntu.com-20061117171948-jd89seng1dcff6rc
Tags: 1.2.1-1
* New upstream release
  - Updated no-client-log.patch
  - Obsoleted patches: hardcoded-paths.patch
* Updated watch file
* Reenabled Perl tools
* Added missing final newline to slon.conf-sample
* Updated test script

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!@@PERL@@
2
 
# $Id: slonik_init_cluster.pl,v 1.1 2005/05/31 16:11:05 cbbrowne Exp $
 
2
# $Id: slonik_init_cluster.pl,v 1.1.4.4 2006/11/07 16:08:20 cbbrowne Exp $
3
3
# Author: Christopher Browne
4
4
# Copyright 2004 Afilias Canada
5
5
 
30
30
require '@@PGLIBDIR@@/slon-tools.pm';
31
31
require $CONFIG_FILE;
32
32
 
33
 
my $FILE="/tmp/init-cluster.$$";
34
 
 
35
33
# 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";
41
38
 
42
39
# STORE NODE
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";
48
45
  }
49
46
}
50
 
print SLONIK "  echo 'Set up replication nodes';\n";
 
47
$slonik .=  "  echo 'Set up replication nodes';\n";
51
48
 
52
49
# STORE PATH
53
 
print SLONIK "\n# STORE PATH\n";
54
 
 
55
 
my @COST;
56
 
my @VIA;
57
 
my @PATH;
58
 
generate_listen_paths();
59
 
 
60
 
print SLONIK "  echo 'Next: configure paths for each node/origin';\n";
 
50
$slonik .=  "\n# STORE PATH\n";
 
51
 
 
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";
71
63
      }
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";
75
67
      }
76
68
    }
77
69
  }
78
70
}
79
71
 
80
 
# STORE LISTEN
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";
88
 
    }
89
 
  }
90
 
}
91
 
print SLONIK "  echo 'Replication nodes prepared';\n";
92
 
print SLONIK "  echo 'Please start a slon replication daemon for each node';\n";
93
 
close SLONIK;
94
 
run_slonik_script($FILE);
95
 
report_on_paths();
96
 
 
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;
102
 
    }
103
 
  }
104
 
 
105
 
  # Initialize paths between parents and children, and based on them,
106
 
  # generate initial seeding of listener paths, @VIA
107
 
 
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
119
 
          # Parent to child
120
 
          $COST[$node1][$node2] = 1;
121
 
          $VIA[$node1][$node2] = $node1;
122
 
 
123
 
          # Child to parent
124
 
          $COST[$node2][$node1] = 1;
125
 
          $VIA[$node2][$node1] = $node2;
126
 
        }
127
 
      }
128
 
    }
129
 
  }
130
 
 
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
142
 
  # further.
143
 
  $didwork = "yes";
144
 
  while ($didwork eq "yes") {
145
 
    $didwork = "no";
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) {
156
 
                $didwork = "yes";
157
 
                                # So we go via node 2
158
 
                $VIA[$node3][$node1] = $node2;
159
 
                $COST[$node3][$node1] = $newcost;
160
 
 
161
 
                $VIA[$node1][$node3] = $node2;
162
 
                $COST[$node1][$node3] = $newcost;
163
 
              }
164
 
            }
165
 
          }
166
 
        }
167
 
      }
168
 
    }
169
 
  }
170
 
}
171
 
 
172
 
sub report_on_paths {
173
 
  print "# Configuration Summary:\n";
174
 
  print "#\n";
175
 
  print "# COST\n";
176
 
  print "#      ";
177
 
  foreach my $node2 (@NODES) {
178
 
    printf "| %3d ", $node2;
179
 
  }
180
 
  print "|\n# ";
181
 
  print ("-----+" x (scalar(@NODES) + 1));
182
 
  print "\n";
183
 
  foreach my $node1 (@NODES) {
184
 
    printf "#  %3d ", $node1;
185
 
    foreach my $node2 (@NODES) {
186
 
      if ($COST[$node2][$node1] == $infinity) {
187
 
        printf "| inf ";
188
 
      } else {
189
 
        printf "|%4d ", $COST[$node2][$node1];
190
 
      }
191
 
    }
192
 
    print "|\n";
193
 
  }
194
 
  print "# \n";
195
 
  print "# VIA\n";
196
 
  print "#      ";
197
 
  foreach my $node2 (@NODES) {
198
 
    printf "| %3d ", $node2;
199
 
  }
200
 
  print "|\n# ";
201
 
  print ("-----+" x (scalar(@NODES) + 1));
202
 
  print "\n";
203
 
  foreach my $node1 (@NODES) {
204
 
    printf "#  %3d ", $node1;
205
 
    foreach my $node2 (@NODES) {
206
 
      printf "|%4d ", $VIA[$node2][$node1];
207
 
    }
208
 
    print "|\n";
209
 
  }
210
 
 
211
 
  print "# \n";
212
 
  print "# PATHS\n";
213
 
  print "#      ";
214
 
  foreach my $node2 (@NODES) {
215
 
    printf "| %3d ", $node2;
216
 
  }
217
 
  print "|\n# ";
218
 
  print ("-----+" x (scalar(@NODES) + 1));
219
 
  print "\n";
220
 
  foreach my $node1 (@NODES) {
221
 
    printf "#  %3d ", $node1;
222
 
    foreach my $node2 (@NODES) {
223
 
      printf "|%4d ", $PATH[$node2][$node1];
224
 
    }
225
 
    print "|\n";
226
 
  }
227
 
  print "\n";
228
 
}
 
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');