From 9f5bf703f91598672ac9864569ab5fa35e77b68a Mon Sep 17 00:00:00 2001 From: "yamaguchi.toru" Date: Tue, 7 Jun 2011 21:08:21 +0900 Subject: [PATCH] Use setup() method internally. Add pod. --- Makefile.PL | 2 +- lib/MySQL/SustainableQuery.pm | 84 ++++++++++++++++++- lib/MySQL/SustainableQuery/Log.pm | 18 +++- lib/MySQL/SustainableQuery/Strategy.pm | 6 +- .../Strategy/BalancedReplication.pm | 18 ++++ lib/MySQL/SustainableQuery/Strategy/ByLoad.pm | 14 ++++ t/run.t | 1 - t/setup_log.t | 1 - t/setup_strategy.t | 2 - .../balanced_replication/wait_correction.t | 1 - t/strategy/by_load/wait_correction.t | 1 - 11 files changed, 136 insertions(+), 12 deletions(-) diff --git a/Makefile.PL b/Makefile.PL index 95bb4b6..4211d56 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -8,7 +8,7 @@ requires 'DBI'; requires 'POSIX'; requires 'Time::HiRes'; -tests 't/*.t'; +tests_recursive 't'; author_tests 'xt'; test_requires 'DBD::Mock'; diff --git a/lib/MySQL/SustainableQuery.pm b/lib/MySQL/SustainableQuery.pm index ed0d876..0077352 100644 --- a/lib/MySQL/SustainableQuery.pm +++ b/lib/MySQL/SustainableQuery.pm @@ -32,13 +32,15 @@ sub new { %$args, ); - bless $args => $class; + my $self = bless $args => $class; + $self->setup; } sub setup { my $self = shift; $self->setup_log; $self->setup_strategy; + $self; } sub setup_log { @@ -117,15 +119,91 @@ __END__ =head1 NAME -MySQL::SustainableQuery - +MySQL::SustainableQuery - Execute query sustainably by strategy =head1 SYNOPSIS + use DBI; use MySQL::SustainableQuery; + my $dbh = DBI->connect( ... ); + + my $query = MySQL::SustainableQuery->new( + exec_query => sub { + my ( $q, $i ) = @_; + return $dbh->do('DELETE FROM large_table ORDER BY id ASC LIMIT 100'); + }, + terminate_condition => sub { + my ( $q, $rv, $i, $ts ) = @_; + $rv < 100 ? 1 : 0; + } + ); + + my $rs = $query->run; + printf("execute count: %d; total times: %.02f sec\n", $rs->{executed}, $rs->{time_total}); + =head1 DESCRIPTION -MySQL::SustainableQuery is +MySQL::SustainableQuery executes query to care load time or replication behind times or other factor. + +=head2 new( %args ) + +The details of args is below. + +=over + +=item wait_interval + +Base interval time (seconds). + +=item check_strategy_interval + +The interval count calling strategy's wait_correction() method. + +=item strategy + +=over + +=item class + +Specify strategy class name. When the strategy class name is beggining of 'MySQL::SustainableQuery::Strategy::', +you can omit it likes 'ByLoad' or 'BalancedReplication'. + +=item args + +Arguments passed to strategy modules's new() method. + +=back + +=item log + +Specify logger object or code reference. + +=item exec_query + +Specify code rederence to execute query. + +=item terminate_condition + +Specify code rederence to judge which it can terminate or not. + +=back + +=head2 run() + +Execute query. + +=head2 setup() + +Internal uses. + +=head2 setup_log() + +Internal uses. + +=head2 setup_strategy() + +Internal uses. =head1 AUTHOR diff --git a/lib/MySQL/SustainableQuery/Log.pm b/lib/MySQL/SustainableQuery/Log.pm index f48b8b9..0d23eff 100644 --- a/lib/MySQL/SustainableQuery/Log.pm +++ b/lib/MySQL/SustainableQuery/Log.pm @@ -42,7 +42,7 @@ sub new { $logger ||= sub { my ( $level, @messages ) = @_; for ( @messages ) { - printf("[%s %s] %s\n", $level, POSIX::strftime("%Y-%m-%d %H:%M:%S", localtime), $_); + print STDERR sprintf("[%s %s] %s\n", $level, POSIX::strftime("%Y-%m-%d %H:%M:%S", localtime), $_); }; }; @@ -83,6 +83,22 @@ MySQL::SustainableQuery::Log - Logger for MySQL::SustainableQuery =head1 METHODS +=head2 new( $logger ) + +=head2 debug( @msg ) + +=head2 info( @msg ) + +=head2 notice( @msg ) + +=head2 warning( @msg ) + +=head2 error( @msg ) + +=head2 alert( @msg ) + +=head2 emergency( @msg ) + =head1 AUTHOR Toru Yamaguchi Ezigorou@dena.jp diff --git a/lib/MySQL/SustainableQuery/Strategy.pm b/lib/MySQL/SustainableQuery/Strategy.pm index 79b0ab0..eaf2f80 100644 --- a/lib/MySQL/SustainableQuery/Strategy.pm +++ b/lib/MySQL/SustainableQuery/Strategy.pm @@ -19,7 +19,7 @@ __END__ =head1 NAME -MySQL::SustainableQuery::Strategy - write short description for MySQL::SustainableQuery::Strategy +MySQL::SustainableQuery::Strategy - Strategy base class =head1 SYNOPSIS @@ -29,6 +29,10 @@ MySQL::SustainableQuery::Strategy - write short description for MySQL::Sustainab =head1 METHODS +=head2 new( %args ) + +=head2 wait_correction( $query, $time_sum, $executed_count ) + =head1 AUTHOR Toru Yamaguchi Ezigorou@dena.jp diff --git a/lib/MySQL/SustainableQuery/Strategy/BalancedReplication.pm b/lib/MySQL/SustainableQuery/Strategy/BalancedReplication.pm index 7d5c635..c4b1afd 100644 --- a/lib/MySQL/SustainableQuery/Strategy/BalancedReplication.pm +++ b/lib/MySQL/SustainableQuery/Strategy/BalancedReplication.pm @@ -66,6 +66,24 @@ MySQL::SustainableQuery::Strategy::BalancedReplication - write short description =head1 METHODS +=head1 METHODS + +=head2 new( %args ) + +=over + +=item dbh + +L object + +=item capable_behind_seconds + +=item on_error_scale_factor + +=back + +=head2 wait_correction( $query, $time_sum, $executed_count ) + =head1 AUTHOR Toru Yamaguchi Ezigorou@dena.jp diff --git a/lib/MySQL/SustainableQuery/Strategy/ByLoad.pm b/lib/MySQL/SustainableQuery/Strategy/ByLoad.pm index 76eaf08..af7c15f 100644 --- a/lib/MySQL/SustainableQuery/Strategy/ByLoad.pm +++ b/lib/MySQL/SustainableQuery/Strategy/ByLoad.pm @@ -40,6 +40,20 @@ MySQL::SustainableQuery::Strategy::ByLoad - write short description for MySQL::S =head1 METHODS +=head1 METHODS + +=head2 new( %args ) + +=over + +=item load + +The ratio of executed time. + +=back + +=head2 wait_correction( $query, $time_sum, $executed_count ) + =head1 AUTHOR Toru Yamaguchi Ezigorou@dena.jp diff --git a/t/run.t b/t/run.t index b6bdd76..b261aca 100644 --- a/t/run.t +++ b/t/run.t @@ -9,7 +9,6 @@ use MySQL::SustainableQuery; sub create_query { my $query = MySQL::SustainableQuery->new(@_); - $query->setup; $query; } diff --git a/t/setup_log.t b/t/setup_log.t index 3ff5b55..158fa5e 100755 --- a/t/setup_log.t +++ b/t/setup_log.t @@ -8,7 +8,6 @@ use MySQL::SustainableQuery; subtest 'setup_log() default' => sub { my $query = MySQL::SustainableQuery->new; lives_and { - $query->setup_log; isa_ok( $query->log, 'MySQL::SustainableQuery::Log' ); }; }; diff --git a/t/setup_strategy.t b/t/setup_strategy.t index 3f53fbd..e9d918b 100755 --- a/t/setup_strategy.t +++ b/t/setup_strategy.t @@ -14,7 +14,6 @@ subtest 'Setup MySQL::SustainableQuery::Strategy::Byload' => sub { }); lives_and { - $query->setup_strategy; isa_ok( $query->strategy, 'MySQL::SustainableQuery::Strategy::ByLoad' ); can_ok( $query->strategy, 'wait_correction' ); } 'setup_strategy() lives ok'; @@ -29,7 +28,6 @@ subtest 'Setup MySQL::SustainableQuery::Strategy::BalancedReplication' => sub { }); lives_and { - $query->setup_strategy; isa_ok( $query->strategy, 'MySQL::SustainableQuery::Strategy::BalancedReplication' ); can_ok( $query->strategy, 'wait_correction' ); } 'setup_strategy() lives ok'; diff --git a/t/strategy/balanced_replication/wait_correction.t b/t/strategy/balanced_replication/wait_correction.t index 4a1777f..5846462 100644 --- a/t/strategy/balanced_replication/wait_correction.t +++ b/t/strategy/balanced_replication/wait_correction.t @@ -9,7 +9,6 @@ use MySQL::SustainableQuery; sub create_query_and_strategy { my $query = MySQL::SustainableQuery->new(@_); - $query->setup; my $strategy = $query->strategy; return ( $query, $strategy ); diff --git a/t/strategy/by_load/wait_correction.t b/t/strategy/by_load/wait_correction.t index 042056a..8cf4b38 100644 --- a/t/strategy/by_load/wait_correction.t +++ b/t/strategy/by_load/wait_correction.t @@ -7,7 +7,6 @@ use MySQL::SustainableQuery; sub create_query_and_strategy { my $query = MySQL::SustainableQuery->new(@_); - $query->setup; my $strategy = $query->strategy; return ( $query, $strategy );