Implementing pidfile to identify the main process of slony instance:
authorLuiz K. Matsumura <luiz.matsumura@gmail.com>
Wed, 7 Nov 2012 00:38:51 +0000 (22:38 -0200)
committerLuiz K. Matsumura <luiz.matsumura@gmail.com>
Thu, 8 Nov 2012 22:03:26 +0000 (20:03 -0200)
slon_tools.conf-sample - add configuration variables
  . $PIDFILE_DIR : directory where the pid files will be created (default: /var/run/slony1)
  . $PIDFILE_PREFIX : prefix to pidfiles of node instances (default: cluster name)

slon-tools.pm:
 . start_slon : changed to init slon daemon with -p pidfile and added a sleep time
    to give time to slon daemon initialize and create the pid file

 . get_pid: changed to read the pid file to get the pid number

slon_kill.pl:
  . change kill method of slon daemon to get_pid then use kill 15 instead 9 to
    slon daemon remove the pid file properly

slon_watchdog2.pl:
  . Add sleep time before first query status to avoid the watchdog kill slon_daemon
    just immediatelly they started
  . Do not try to kill slon proccess with 9.

tools/altperl/slon-tools.pm
tools/altperl/slon_kill.pl
tools/altperl/slon_tools.conf-sample
tools/altperl/slon_watchdog2.pl

index 33ddc05150fdc4b20ba95dd21139fbd8785431d6..2ee4e79587a5cac88f2ddd91e25e49a62ca563e6 100644 (file)
@@ -129,24 +129,24 @@ sub get_pid {
   $node =~ /^(?:node)?(\d+)$/;
   my $nodenum = $1;
   my $pid;
-  my $tpid;
-  my $command;
   my ($dsn, $config) = ($DSN[$nodenum], $CONFIG[$nodenum]);
   #  print "Searching for PID for $dbname on port $dbport\n";
-  if ($config) {
-    my $config_regexp = quotemeta( $config );
-    $command =  ps_args() . "| egrep \"[s]lon -f $config_regexp\" | awk '{print \$2}' | sort -n | head -1";
-  } else {
-    $dsn = quotemeta($dsn);
-    $command =  ps_args() . "| egrep \"[s]lon .* $CLUSTER_NAME \" | egrep \"$dsn\" | awk '{print \$2}' | sort -n | head -1";
+
+  $PIDFILE_DIR ||= '/var/run/slony1';
+  $PIDFILE_PREFIX ||= $CLUSTER_NAME;
+
+  my $pidfile;
+  $pidfile = "$PIDFILE_DIR/$PIDFILE_PREFIX" . "_node$nodenum.pid";
+
+  open my $in, '<' , $pidfile or return '';
+
+  while( <$in> ) {
+    $pid = $_;
   }
+
   #print "Command:\n$command\n";
-  open(PSOUT, "$command|");
-  while ($tpid = <PSOUT>) {
-    chomp $tpid;
-    $pid = $tpid;
-  }
-  close(PSOUT);
+  chomp $pid;
+
   return $pid;
 }
 
@@ -156,23 +156,33 @@ sub start_slon {
   $SYNC_CHECK_INTERVAL ||= 1000;
   $DEBUGLEVEL ||= 0;
   $LOG_NAME_SUFFIX ||= '%Y-%m-%d';
+  $PIDFILE_DIR ||= '/var/run/slony1';
+  $PIDFILE_PREFIX ||= $CLUSTER_NAME;
+
+  # system("mkdir -p $PIDFILE_DIR" );
   system("mkdir -p $LOGDIR/node$nodenum");
-  my $cmd;
+
+  my $cmd,$pidfile;
+
+  $pidfile = "$PIDFILE_DIR/$PIDFILE_PREFIX" . "_node$nodenum.pid";
+
   if ($config) {
-     $cmd = "@@SLONBINDIR@@/slon -f $config ";
+     $cmd = "@@SLONBINDIR@@/slon -p $pidfile -f $config ";
   } else {
-     $cmd = "@@SLONBINDIR@@/slon -s $SYNC_CHECK_INTERVAL -d$DEBUGLEVEL $opts $CLUSTER_NAME '$dsn' ";
+     $cmd = "@@SLONBINDIR@@/slon -p $pidfile -s $SYNC_CHECK_INTERVAL -d$DEBUGLEVEL $opts $CLUSTER_NAME '$dsn' ";
   }
   my $logfilesuffix = POSIX::strftime( "$LOG_NAME_SUFFIX",localtime );
   chomp $logfilesuffix;
 
   if ($APACHE_ROTATOR) {
-    $cmd .= "2>&1 | $APACHE_ROTATOR \"$LOGDIR/node$nodenum/" .  $dbname . "_$logfilesuffix.log\" 10M &";
+    $cmd .= "2>&1 | $APACHE_ROTATOR \"$LOGDIR/node$nodenum/" . $dbname . "-$logfilesuffix.log\" 10M &";
   } else {
     $cmd .= "> $LOGDIR/node$nodenum/$dbname-$logfilesuffix.log 2>&1 &";
   }
   print "Invoke slon for node $nodenum - $cmd\n";
   system ($cmd);
+  # give time to slon daemon start and create pid file
+  sleep 3;
 }
 
 
index 0710512bb068ad2dea07837e0a07d94c1189e2ab..85914421f1e7917dd6abd9ab5916cd4882d267da 100644 (file)
@@ -75,21 +75,6 @@ unless ($WATCHDOG_ONLY) {
     }
 }
 
-sub shut_off_processes($$) {
-    my ( $watchdog_suffix , $nodenum ) = @_;
-
-    while ($pid = <PSOUT>) {
-       chomp $pid;
-       if (!($pid)) {
-           print "No slon$watchdog_suffix is running for the cluster $CLUSTER_NAME, node $nodenum!\n";
-       } else {
-           $found="y";
-           kill 9, $pid;
-           print "slon$watchdog_suffix for cluster $CLUSTER_NAME node $nodenum killed - PID [$pid]\n";
-       }
-    }
-}
-
 sub kill_watchdog($) {
   my ($nodenum) = @_;
 
@@ -99,24 +84,31 @@ sub kill_watchdog($) {
 
   #print "Command:\n$command\n";
   open(PSOUT, "$command|");
-  shut_off_processes('_watchdog',$nodenum);
+
+  while ($pid = <PSOUT>) {
+      chomp $pid;
+      if (!($pid)) {
+          print "No slon_watchdog is running for the cluster $CLUSTER_NAME, node $nodenum!\n";
+      } else {
+          $found="y";
+          kill 9, $pid;
+          print "slon_watchdog for cluster $CLUSTER_NAME node $nodenum killed - PID [$pid]\n";
+      }
+  }
   close(PSOUT);
 }
 
 sub kill_slon_node($) {
   my ($nodenum) = @_;
 
-  my $command;
-  my ($dsn, $config) = ($DSN[$nodenum], $CONFIG[$nodenum]);
-  if ($config) {
-    my $config_regexp = quotemeta( $config );
-    $command =  ps_args() . "| egrep \"[s]lon -f $config_regexp\" | awk '{print \$2}' | sort -n";
+  my $pid = get_pid($nodenum);
+
+  #print "Command:\n$command\n";
+  if (!($pid)) {
+    print "No slon is running for the cluster $CLUSTER_NAME, node $nodenum!\n";
   } else {
-    $dsn = quotemeta($dsn);
-    $command =  ps_args() . "| egrep \"[s]lon .* $CLUSTER_NAME \" | egrep \"$dsn\" | awk '{print \$2}' | sort -n";
+    $found="y";
+    kill 15, $pid;
+    print "slon for cluster $CLUSTER_NAME node $nodenum killed - PID [$pid]\n";
   }
-  #print "Command:\n$command\n";
-  open(PSOUT, "$command|");
-  shut_off_processes("",$nodenum);
-  close(PSOUT);
 }
index 5b023767b5d739e315e8b2b69170d57dc21d9999..ca7249aeadd59c034582f334b322da25a47cfbfe 100644 (file)
@@ -20,6 +20,16 @@ if ($ENV{"SLONYNODES"}) {
     # contain Slony-related data.
     $CLUSTER_NAME = 'replication';
 
+    # The directory where Slony store PID files.  This
+    # directory will need to be writable by the user that invokes
+    # Slony.
+    $PIDFILE_DIR = /var/run/slony1
+
+    # PID file prefix. Files will be generated as $PIDFILE_PREFIX_node#
+    # in PIDFILE_DIR
+    # If not defined, default to $CLUSTER_NAME
+    # $PIDFILE_PREFIX =
+
     # The directory where Slony should record log messages.  This
     # directory will need to be writable by the user that invokes
     # Slony.
index 6fe42949aa0d24a9dbce0bc7a4f8c61835f2b8a7..5f40c806da6cf378330dc8836e3a6cde95187a4d 100644 (file)
@@ -45,6 +45,11 @@ my $logfile = "$LOGDIR/slon-watchdog.log";
 
 log_to_file( $logfile , "Invoking watchdog for $CLUSTER_NAME node $nodenum, sleep time = $sleep +/- " . int($sleep/2) . " seconds");
 
+# When slon daemon is just started, may not have time to start syncronization
+# and the watchdog will kill the process with no mercy.
+# So sleep to give time to slony try to do their job.
+sleep $sleep;
+
 while (1) {
   my $res = query_slony_status($nodenum);    # See where the node stands
   my $eventsOK;
@@ -109,7 +114,8 @@ while (1) {
       sleep 3;
       kill 15, $pid;
       sleep 3;
-      kill 9, $pid;
+      # if killed with 9 the pid file isn´t deleted and the service don´t restart
+      # kill 9, $pid;
     }
     log_to_file($logfile,"restart slon for $CLUSTER_NAME node $nodenum");
     start_slon($nodenum);