From 0ae024b814a67b56e54d142739754de68abac7ab Mon Sep 17 00:00:00 2001 From: Greg Sabino Mullane Date: Fri, 14 Dec 2012 00:21:05 -0500 Subject: [PATCH] Better version of self-excluding trigger magic via MCP and KID registration. --- Bucardo.pm | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/Bucardo.pm b/Bucardo.pm index 0fde56b5e..0cd53c570 100644 --- a/Bucardo.pm +++ b/Bucardo.pm @@ -627,10 +627,9 @@ sub start_mcp { $self->reload_mcp(); }; - ## We need KIDs to tell us their PID so we can [de]register them + ## We need KIDs to tell us their PID so we can deregister them $self->{kidpid} = {}; - $self->db_listen($masterdbh, 'kid_pid_start', '', 1); - $self->db_listen($masterdbh, 'kid_pid_stop', '', 1); + $self->db_listen($x->{dbh}, 'kid_pid_stop', '', 1); ## Let any listeners know we have gotten this far $self->db_notify($masterdbh, 'started', 1); @@ -820,6 +819,13 @@ sub mcp_main { next if $x->{dbtype} ne 'postgres'; + ## Start listening for KIDs if we have not done so already + if (! exists $self->{kidpid}{$dbname}) { + $self->{kidpid}{$dbname} = 1; + $self->db_listen($x->{dbh}, 'kid_pid_start', '', 1); + $x->{dbh}->commit(); + } + my $nlist = $self->db_get_notices($x->{dbh}); $x->{dbh}->rollback(); for my $name (keys %{ $nlist } ) { @@ -1041,12 +1047,16 @@ sub mcp_main { ## A kid reporting in. We just store the PID elsif ('kid_pid_start') { - $self->{kidpid}{$npid} = 1; + for my $lpid (keys %{ $notice->{$name}{pid} }) { + $self->{kidpid}{$lpid} = 1; + } } ## A kid leaving. We remove the stored PID. elsif ('kid_pid_stop') { - delete $self->{kidpid}{$npid}; + for my $lpid (keys %{ $notice->{$name}{pid} }) { + delete $self->{kidpid}{$lpid}; + } } ## Should not happen, but let's at least log it @@ -2121,9 +2131,6 @@ sub start_kid { ## Setup mapping so we can report in the log which things came from this backend $self->{pidmap}{$self->{master_backend}} = 'Bucardo DB'; - ## Register ourself with the MCP - $self->db_notify($maindbh, 'kid_pid_start', 1); - ## SQL to enter a new database in the dbrun table $SQL = q{ INSERT INTO bucardo.dbrun(sync,dbname,pgpid) @@ -2368,6 +2375,10 @@ sub start_kid { ## This overwrites the items we inherited from the controller ($x->{backend}, $x->{dbh}) = $self->connect_database($dbname); $self->glog(qq{Database "$dbname" backend PID: $x->{backend}}, LOG_VERBOSE); + + ## Register ourself with the MCP + $self->db_notify($x->{dbh}, 'kid_pid_start', 1); + } ## Set the maximum length of the $dbname.$S.$T string. -- 2.39.5