OSDN Git Service

Attached is a patch that provides *VERY* limited support for multiple
authorBruce Momjian <bruce@momjian.us>
Wed, 25 Jun 2003 01:17:44 +0000 (01:17 +0000)
committerBruce Momjian <bruce@momjian.us>
Wed, 25 Jun 2003 01:17:44 +0000 (01:17 +0000)
slave
servers.  I haven't tested it very well, so use at your own risk (and I
recommend against using it in production).

Basically, I have a central database server that has 4 summary tables
inside
it replicated to a remote slave (these database tables are for my mail
server
authentication, so these are replicated to another server tuned for many
connections, and so I don't have postgres connections opened straight to
my
back-end database server).

Unfortunately, I also wanted to implement a replication database server
for
hot-backups.  I realized, too late, that the replication process is
pretty
greedy and will try to replicate all tables marked as a
"MasterAddTable".

To make a long story, I made a patch to RServ.pm and Replicate that
allows you
to specify, on the command line, a list of tables that you want to
replicate...it'll ignore all others.

I haven't finished, since this has to be integrated with CleanLog for
instance, but this should (and does) suffice for the moment.

I have yet to test it with two slaves, but at least my mail server
replication
database now works (it was failing every time it tried to replicate, for
a
variety of reasons).

Anyone have any suggestions on how to improve on this?  (or, if someone
more
familiar with this code wants to take the ball and run with it, you're
welcome to).

--
Michael A Nachbaur <mike@nachbaur.com>

contrib/rserv/RServ.pm
contrib/rserv/Replicate.in

index de0f037..7b6716d 100644 (file)
@@ -19,7 +19,7 @@ my %Stables = ();
 
 sub PrepareSnapshot
 {
-       my ($conn, $outf, $server) = @_; # (@_[0], @_[1], @_[2]);
+       my ($conn, $outf, $server, $onlytables) = @_; # (@_[0], @_[1], @_[2]);
 
        my $result = $conn->exec("BEGIN");
        if ($result->resultStatus ne PGRES_COMMAND_OK)
@@ -52,6 +52,10 @@ sub PrepareSnapshot
        while (@row = $result->fetchrow)
        {
        #       printf "$row[0], $row[1], $row[2]\n";
+               if (ref($onlytables) eq 'HASH') {
+                       next unless (exists $onlytables->{$row[1]});
+                       $onlytables->{$row[1]} = $row[0] unless ($onlytables->{$row[1]});
+               }
                push @{$Mtables{$row[0]}}, $row[1], $row[2];
        }
 
@@ -232,7 +236,7 @@ sub GetSYNCID
 
 sub CleanLog
 {
-       my ($conn, $howold) = @_; # (@_[0], @_[1]);
+       my ($conn, $howold, $onlytables) = @_; # (@_[0], @_[1]);
 
        my $result = $conn->exec("BEGIN");
        if ($result->resultStatus ne PGRES_COMMAND_OK)
@@ -274,6 +278,11 @@ sub CleanLog
        my $alist = join(',', keys %active);
        my $sinfo = "logid < $maxid";
        $sinfo .= " and logid not in ($alist)" if $alist ne '';
+       #if (ref($onlytables) eq 'HASH') {
+       #       foreach my $onlytable (keys %{$onlytables}) {
+       #               $sinfo
+       #       }
+       #}
        
        $sql = "delete from _RSERV_LOG_ where " . 
                "logtime < now() - '$howold second'::interval and $sinfo";
@@ -302,7 +311,7 @@ sub CleanLog
 
 sub ApplySnapshot
 {
-       my ($conn, $inpf) = @_; # (@_[0], @_[1]);
+       my ($conn, $inpf, $onlytables) = @_; # (@_[0], @_[1]);
 
        my $result = $conn->exec("BEGIN");
        if ($result->resultStatus ne PGRES_COMMAND_OK)
@@ -336,6 +345,10 @@ sub ApplySnapshot
        while (@row = $result->fetchrow)
        {
        #       printf "        %s      %s\n", $row[1], $row[0];
+               if (ref($onlytables) eq 'HASH') {
+                       next unless (exists $onlytables->{$row[1]});
+                       $onlytables->{$row[1]} = $row[0] unless ($onlytables->{$row[1]});
+               }
                push @{$Stables{$row[1]}}, $row[0], $row[2], $row[3];
        }
 
index bd7c089..28abc8d 100644 (file)
@@ -32,6 +32,7 @@ if (defined($opt_help) || (scalar(@ARGV) < 2)) {
 
 my $master = $ARGV[0] || "master";
 my $slave = $ARGV[1] || "slave";
+my $tables = $#ARGV < 2 ? undef : { map {($_, undef)} @ARGV[2..$#ARGV] };
 my $server = 0;
 
 my $minfo = "dbname=$master";
@@ -56,7 +57,7 @@ SyncSync($mconn, $sconn);
 my $outf = new IO::File;
 open $outf, ">$snapshot";
 print "\n>>>>>>>>>>>>> Prepare Snapshot\n\n" if ($verbose);
-$res = PrepareSnapshot($mconn, $outf, $server);
+$res = PrepareSnapshot($mconn, $outf, $server, $tables);
 close $outf;
 die "\n>>>>>>>>>>>>> ERROR\n" if $res < 0;
 if ($res == 0)
@@ -68,7 +69,7 @@ if ($res == 0)
 my $inpf = new IO::File;
 open $inpf, "<$snapshot";
 print "\n>>>>>>>>>>>>> Apply Snapshot\n\n" if ($verbose);
-$res = ApplySnapshot($sconn, $inpf);
+$res = ApplySnapshot($sconn, $inpf, $tables);
 close $inpf;
 die "\n>>>>>>>>>>>>> ERROR\n" if $res < 0;