attempt more tuning of torrus-srvderive, RT#10574
[freeside.git] / FS / bin / freeside-torrus-srvderive
index d3ea419..19dd50e 100644 (file)
@@ -11,7 +11,8 @@ 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 = 12;
+our $search_timeout = 60*60; #30*60; # 15*60 5*60; #$torrus_srvderive->last_srv_date ? 5*60 : 15*60);
 our %kids;
 
 my $user = shift or die &usage;
@@ -59,9 +60,14 @@ my $sql = "
 
 my $orderlimit = "
     ORDER BY srv_date, srv_time
-    LIMIT 50
+    LIMIT 100
 ";
 
+if ( driver_name =~ /mysql/i ) {
+  dbh->do('SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED');
+  dbh->commit or die dbh->errstr;
+}
+
 our $kids = 0;
 
 #MAIN: while (1) {
@@ -75,6 +81,7 @@ while (1) {
     &reap_kids;
     if ( $kids >= $max_kids ) {
       sleep 5;
+      myexit() if sigterm() || sigint();
       redo;
     }
 
@@ -99,6 +106,12 @@ while (1) {
       my @serviceids = $torrus_srvderive->component_serviceids;
       exit unless @serviceids; #don't try to search for empty virtual ports
 
+      #nonlocking select statements; rows in this table never change
+      if ( driver_name =~ /mysql/i ) {
+        dbh->do('SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED');
+        dbh->commit or die dbh->errstr;
+      }
+
       my @in = ();
       for my $dir ('_IN', '_OUT') {
         push @in, map dbh->quote("$_$dir"), @serviceids;
@@ -144,7 +157,7 @@ while (1) {
 
       eval {
         my $h = set_sig_handler( 'ALRM', sub { die "_timeout\n"; } );
-        alarm(15*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);
       };
@@ -154,7 +167,7 @@ while (1) {
         #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';
+          if driver_name =~ /mysql/i;
         dbh->rollback; #or die dbh->errstr;
         #adminsuidsetup($user);
         #next SERVICEID; #MAIN;
@@ -163,7 +176,7 @@ while (1) {
         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 ) {
@@ -182,20 +195,27 @@ while (1) {
 
           my $sin = join(',', map dbh->quote("$_$dir"), @serviceids);
 
+          my $srv_date = time2str('%Y-%m-%d', $cur);
+
           my $sum = "
             SELECT COALESCE(SUM(value),0) FROM srvexport AS other
               WHERE other.serviceid IN ($sin)
+                AND other.srv_date = '$srv_date'
                 AND ABS( $cur - $other_date ) <= 60
           ";
+          my $ssth = dbh->prepare($sum) or die $DBI::errstr;
+          $ssth->execute or die $ssth->errstr; #better recovery?
+          my $value = $ssth->fetchrow_arrayref->[0];
 
           my $isql = "
-            INSERT INTO srvexport ( srv_date, srv_time, serviceid, value, intvl )
-              VALUES ( ?, ?, ?, ($sum), ? )
+            INSERT INTO srvexport (srv_date, srv_time, serviceid, value, intvl)
+              VALUES ( ?, ?, ?, ?, ? )
           ";
-          my @param = ( time2str('%Y-%m-%d', $cur), #srv_date
-                        time2str('%X', $cur),       #srv_time
-                        "$serviceid$dir",
-                        300, #intvl ... 
+          my @param = ( $srv_date,
+                        time2str('%X', $cur), #srv_time
+                        "$serviceid$dir",     #serviceid
+                        $value,
+                        300,                  #intvl ... 
                       );
           warn $isql. ' with param '. join(',',@param). "\n"
             if $DEBUG > 2;
@@ -236,7 +256,7 @@ while (1) {
 
   myexit() if sigterm() || sigint();
   warn "restarting main loop\n" if $DEBUG > 1;
-  sleep 60 unless $found;
+  #sleep 60 unless $found;
 }
 
 sub _shouldrun {