Author: timbo
Date: Tue May  1 05:19:34 2007
New Revision: 9480

Modified:
   dbi/trunk/Changes
   dbi/trunk/lib/DBI/Gofer/Execute.pm
   dbi/trunk/t/86gofer_fail.t

Log:
Make first value in DBI_GOFER_RANDOM_FAIL be a percentage rather than 1-in-N 
value.


Modified: dbi/trunk/Changes
==============================================================================
--- dbi/trunk/Changes   (original)
+++ dbi/trunk/Changes   Tue May  1 05:19:34 2007
@@ -78,6 +78,7 @@
     Added way to generate random 1-in-N failures for methods.
     Added automatic retry-on-error mechanism to gofer transport base class.
     Added tests to show automatic retry mechanism works a treat!
+    Added go_retry_hook callback hook so apps can fine-tune retry behaviour.
     Added header to request and response packets for sanity checking
       and to enable version skew between client and server.
     Added forced_single_resultset, max_cached_sth_per_dbh and 
max_cached_dbh_per_drh

Modified: dbi/trunk/lib/DBI/Gofer/Execute.pm
==============================================================================
--- dbi/trunk/lib/DBI/Gofer/Execute.pm  (original)
+++ dbi/trunk/lib/DBI/Gofer/Execute.pm  Tue May  1 05:19:34 2007
@@ -501,12 +501,12 @@
 
 sub _install_rand_fail_callbacks {
     my ($self, $dbh, $dbi_gofer_random_fail) = @_;
-    my ($rand_fail_freq, @rand_fail_methods) = split /,/, 
$dbi_gofer_random_fail;
+    my ($rand_fail_pct, @rand_fail_methods) = split /,/, 
$dbi_gofer_random_fail;
     @rand_fail_methods = qw(do prepare) if [EMAIL PROTECTED]; # only works for 
dbh methods
-    if ($rand_fail_freq) {
+    if ($rand_fail_pct) {
         warn "DBI_GOFER_RANDOM_FAIL set to '$ENV{DBI_GOFER_RANDOM_FAIL}' "
             ."so random failures will be generated! "
-            ."(approx 1-in-$rand_fail_freq calls for methods: 
@rand_fail_methods)\n";
+            ."(approx $rand_fail_pct% of calls to methods: 
@rand_fail_methods)\n";
         my $callbacks = $dbh->{Callbacks} || {};
         my $prev      = $dbh->{private_gofer_rand_fail_callbacks} || {};
         for my $method (@rand_fail_methods) {
@@ -514,7 +514,7 @@
                 warn "Callback for $method method already installed so 
DBI_GOFER_RANDOM_FAIL callback not installed\n";
                 next;
             }
-            $callbacks->{$method} = $self->_mk_rand_fail_sub($rand_fail_freq, 
$method);
+            $callbacks->{$method} = $self->_mk_rand_fail_sub($rand_fail_pct, 
$method);
         }
         $dbh->{Callbacks} = $callbacks;
         $dbh->{private_gofer_rand_fail_callbacks} = $callbacks;
@@ -522,12 +522,13 @@
 }
 
 sub _mk_rand_fail_sub {
-    my ($self, $rand_fail_freq, $method) = @_;
+    my ($self, $rand_fail_pct, $method) = @_;
+    my $rand_fail_ratio = $rand_fail_pct/100;
     # $method may be "*"
     return sub {
         my $rand = rand();
-        #warn sprintf "DBI_GOFER_RANDOM_FAIL($rand_fail_freq) %f - %f\n", 
$rand, 1/$rand_fail_freq;
-        return if $rand > 1/$rand_fail_freq;
+        #warn sprintf "DBI_GOFER_RANDOM_FAIL($rand_fail_pct) %f - %f\n", 
$rand, 1/$rand_fail_pct;
+        return if $rand >= $rand_fail_ratio;
         undef $_; # tell DBI to not call the method
         return $_[0]->set_err(1, "fake error induced by DBI_GOFER_RANDOM_FAIL 
env var");
     }

Modified: dbi/trunk/t/86gofer_fail.t
==============================================================================
--- dbi/trunk/t/86gofer_fail.t  (original)
+++ dbi/trunk/t/86gofer_fail.t  Tue May  1 05:19:34 2007
@@ -38,7 +38,7 @@
 
 # --- 100% failure rate
 
-$ENV{DBI_GOFER_RANDOM_FAIL} = "1,do"; # total failure (almost)
+$ENV{DBI_GOFER_RANDOM_FAIL} = "100,do"; # total failure (almost)
 my $dbh_100 = 
DBI->connect("dbi:Gofer:transport=null;policy=rush;dsn=dbi:ExampleP:", 0, 0, {
     RaiseError => 1, PrintError => 0,
 });
@@ -58,7 +58,7 @@
 
 # --- 50% failure rate, with no retries
 
-$ENV{DBI_GOFER_RANDOM_FAIL} = "2,do"; # 50% failure (almost)
+$ENV{DBI_GOFER_RANDOM_FAIL} = "50,do"; # 50% failure (almost)
 ok my $dbh_50r0 = dbi_connect("policy=rush;retry_limit=0");
 $fails = precentage_exceptions(200, sub { $dbh_50r0->do("set foo=1") });
 print "target approx 50% random failures, got $fails%\n";
@@ -66,7 +66,7 @@
 
 # --- 50% failure rate, with many retries (should yield low failure rate)
 
-$ENV{DBI_GOFER_RANDOM_FAIL} = "2,do"; # 50% failure (almost)
+$ENV{DBI_GOFER_RANDOM_FAIL} = "50,do"; # 50% failure (almost)
 ok my $dbh_50r5 = dbi_connect("policy=rush;retry_limit=5");
 $fails = precentage_exceptions(200, sub { $dbh_50r5->do("set foo=1") });
 print "target approx 5% random failures, got $fails%\n";
@@ -81,7 +81,7 @@
 
 # --- 50% failure rate, test is_idempotent
 
-$ENV{DBI_GOFER_RANDOM_FAIL} = "2,do";   # 50%
+$ENV{DBI_GOFER_RANDOM_FAIL} = "50,do";   # 50%
 
 # test go_retry_hook and that ReadOnly => 1 retries a non-idempotent statement
 ok my $dbh_50r1ro = dbi_connect("policy=rush;retry_limit=1", {

Reply via email to