Fix up some of the makedelta logic.
authorGreg Sabino Mullane <greg@endpoint.com>
Sat, 30 Nov 2013 16:39:34 +0000 (11:39 -0500)
committerGreg Sabino Mullane <greg@endpoint.com>
Sat, 30 Nov 2013 16:39:34 +0000 (11:39 -0500)
Bucardo.pm

index 7b4c336d343724fcb6fe0ebabd37f0eee7266ce2..eaf0e281d61f1c9b88439cbaeac65d0439997433 100644 (file)
@@ -2079,8 +2079,8 @@ sub start_kid {
         ## Can it be queried?
         $x->{does_append_only} = 0;
 
-        ## List of tables in this database that do makedelta
-        $x->{is_makedelta} = {};
+        ## List of tables in this database that need makedelta inserts
+        $x->{needs_makedelta} = {};
 
         ## Start clumping into groups and adjust the attributes
 
@@ -2561,7 +2561,7 @@ sub start_kid {
                     ## Set the per database/per table makedelta setting now
                     if (defined $g->{makedelta}) {
                         if ($g->{makedelta} eq 'on' or $g->{makedelta} =~ /\b$dbname\b/) {
-                            $x->{is_makedelta}{$S}{$T} = 1;
+                            $x->{needs_makedelta}{$S}{$T} = 1;
                             $self->glog("Set table $dbname.$S.$T to makedelta", LOG_NORMAL);
                         }
                     }
@@ -4174,38 +4174,15 @@ sub start_kid {
                     }
                 }
                 ## For each database that had delta changes, insert rows to bucardo_track
-                ## We also need to consider makedelta:
-                ## For all tables that are marked as makedelta, we need to ensure
-                ## that we call the SQL below for each dbs_source in which
-                ## the deltacount for *any* other source dbname is non-zero
                 for my $dbname (@dbs_source) {
+
                     $x = $sync->{db}{$dbname};
 
                     $x->{needs_track} = 0;
 
                     if ($deltacount{dbtable}{$dbname}{$S}{$T}) {
                         $x->{needs_track} = 1;
-                    }
-                    elsif (exists $x->{is_makedelta}{$S}{$T}) { ## XXX set this earlier!
-                        ## We know that this particular table in this database is makedelta
-                        ## See if any of the other sources had deltas
-                        ## If they did, then rows were inserted here, so we need a track update
-                        my $found = 0;
-                        for my $dbname2 (@dbs_source) {
-                            if ($deltacount{dbtable}{$dbname}{$S}{$T}) {
-                                $found = 1;
-                                last;
-                            }
-                        }
-                    }
-                }
-
-                ## Kick off the track or stage update asynchronously
-                for my $dbname (@dbs_source) {
-                    $x = $sync->{db}{$dbname};
-
-                    if ($x->{needs_track}) {
-                        ## This is async:
+                        ## Kick off the track or stage update asynchronously
                         if ($x->{trackstage}) {
                             $sth{stage}{$dbname}{$g}->execute();
                         }
@@ -4227,6 +4204,31 @@ sub start_kid {
                     }
                 }
 
+                ## Do makedelta here: no sense in doing it before all the changes above
+                ## We only need to call makedelta if there is a positive delta 
+                ## count for any other database
+                for my $dbname (@dbs_source) {
+
+                    $x = $sync->{db}{$dbname};
+
+                    if ($x->{needs_makedelta}{$S}{$T}) {
+                        my $found = 0;
+                        for my $dbname2 (@dbs_source) {
+                            next if $dbname eq $dbname2;
+                            if ($deltacount{dbtable}{$dbname}{$S}{$T}) {
+                                $found = 1;
+                                last;
+                            }
+                        }
+                        if ($found) {
+                            $self->glog("Setting makdelta for table $S.$T because others updated it", LOG_VERBOSE);
+                        }
+                        else {
+                            $x->{needs_makedelta}{$S}{$T} = 0;
+                        }
+                    }
+                }
+
             } ## end each goat
 
             ## Get sizing for the next printout
@@ -9060,7 +9062,8 @@ sub push_rows {
                 ##   normal action of a trigger and add a row to bucardo.track to indicate that
                 ##   it has already been replicated here.
                 my $dbinfo = $sync->{db}{ $t->{name} };
-                if (!$fullcopy and exists $dbinfo->{is_makedelta}{$S}{$T}) {
+                if (!$fullcopy and $dbinfo->{needs_makedelta}{$S}{$T}) {
+                    $self->glog("Using makedelta to populate delta and track tables", LOG_VERBOSE);
                     my ($cols, $vals);
                     if ($numpks == 1) {
                         $cols = "($pkcols)";