avoid deadlocks, RT#10574
[freeside.git] / FS / bin / freeside-torrus-srvderive
index f95029a..a02d23e 100644 (file)
@@ -6,12 +6,13 @@ use Sys::SigAction qw( set_sig_handler );
 use Date::Parse;
 use Date::Format;
 use FS::Daemon ':all'; #daemonize1 drop_root daemonize2 myexit logfile sig*
-use FS::UID qw( adminsuidsetup dbh driver_name );
+use FS::UID qw( adminsuidsetup forksuidsetup dbh driver_name );
 use FS::Record qw( qsearch str2time_sql str2time_sql_closing concat_sql );
 use FS::torrus_srvderive;
 
 our $DEBUG = 2;
-our $max_kids = 3;
+our $max_kids = 6;
+our $search_timeout = 30*60; # 15*60 5*60; #$torrus_srvderive->last_srv_date ? 5*60 : 15*60);
 our %kids;
 
 my $user = shift or die &usage;
@@ -59,20 +60,23 @@ my $sql = "
 
 my $orderlimit = "
     ORDER BY srv_date, srv_time
-    LIMIT 50
-";
+    LIMIT 100
+"; #50?
 
 our $kids = 0;
 
-MAIN: while (1) {
+#MAIN: while (1) {
+while (1) {
 
   my $found = 0;
 
-  SERVICEID: foreach my $torrus_srvderive ( qsearch('torrus_srvderive', {}) ) {
+  #SERVICEID: foreach my $torrus_srvderive ( qsearch('torrus_srvderive', {}) ) {
+  foreach my $torrus_srvderive ( qsearch('torrus_srvderive', {}) ) {
 
     &reap_kids;
     if ( $kids >= $max_kids ) {
-      sleep 10;
+      sleep 5;
+      myexit() if sigterm() || sigint();
       redo;
     }
 
@@ -95,7 +99,13 @@ MAIN: while (1) {
       my $serviceid = $torrus_srvderive->serviceid;
 
       my @serviceids = $torrus_srvderive->component_serviceids;
-      next unless @serviceids; #don't try to search for empty virtual ports
+      exit unless @serviceids; #don't try to search for empty virtual ports
+
+      #nonlocking select statements; rows in this table never change
+      if ( driver_name eq 'mysql' ) {
+        dbh->do('SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED');
+        dbh->commit or die dbh->errstr;
+      }
 
       my @in = ();
       for my $dir ('_IN', '_OUT') {
@@ -118,7 +128,7 @@ MAIN: while (1) {
           die $error if $error;
         } else {
           warn "no initial last_srv_date for $serviceid; skipping\n" if $DEBUG;
-          next;
+          exit;
         }
       }
 
@@ -142,24 +152,26 @@ MAIN: while (1) {
 
       eval {
         my $h = set_sig_handler( 'ALRM', sub { die "_timeout\n"; } );
-        alarm(10*60); #5*60); #$torrus_srvderive->last_srv_date ? 5*60 : 15*60);
+        alarm($search_timeout);
         $sth->execute($serviceid, $serviceid) or die $sth->errstr;
         alarm(0);
       };
       alarm(0);
       
       if ( $@ && $@ eq "_timeout\n" ) {
-        warn "search timed out; reconnecting and restarting\n";
+        #warn "search timed out; reconnecting and restarting\n";
+        warn "search timed out\n";
         dbh->clone()->do("KILL QUERY ". dbh->{"mysql_thread_id"})
           if driver_name eq 'mysql';
         dbh->rollback; #or die dbh->errstr;
-        adminsuidsetup($user);
-        next SERVICEID; #MAIN;
+        #adminsuidsetup($user);
+        #next SERVICEID; #MAIN;
+        exit;
       } elsif ( $@ ) {
         die $@;
       }
 
-      warn "search finished; checking results\n" if $DEBUG;
+      warn "search for $serviceid finished; checking results\n" if $DEBUG;
 
       my $prev = 0;
       while ( my $row = $sth->fetchrow_arrayref ) {
@@ -200,11 +212,13 @@ MAIN: while (1) {
 
           #stupid mysql deadlocks all the time on insert, so we need to recover
           unless ( $isth->execute(@param) ) {
-            warn "Error inserting data for $serviceid$dir (restarting): ".
-                 $isth->errstr;
+            #warn "Error inserting data for $serviceid$dir (restarting): ".
+            #     $isth->errstr;
+            warn "Error inserting data for $serviceid$dir: ". $isth->errstr;
             dbh->rollback; #or die dbh->errstr;
-            sleep 5;
-            next SERVICEID; #MAIN;
+            #sleep 5;
+            #next SERVICEID; #MAIN;
+            exit;
           }
                           
         }
@@ -230,7 +244,7 @@ MAIN: while (1) {
 
   myexit() if sigterm() || sigint();
   warn "restarting main loop\n" if $DEBUG > 1;
-  sleep 60 unless $found;
+  #sleep 60 unless $found;
 }
 
 sub _shouldrun {