| 1 | # $Id: /mirror/coderepos/lang/perl/Mvalve/trunk/lib/Mvalve/Base.pm 72443 2008-09-08T14:21:42.664054Z daisuke $ | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package Mvalve::Base; | ||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Mvalve; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Mvalve::QueueSet; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Mvalve::Logger; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Mvalve::Types; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Time::HiRes; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | with 'MooseX::KeyedMutex'; | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | has 'logger' => ( | ||||||||||||||||||||||||||||
| 15 | is => 'rw', | ||||||||||||||||||||||||||||
| 16 | does => 'Mvalve::Logger', | ||||||||||||||||||||||||||||
| 17 | coerce => 1 | ||||||||||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | has 'queue' => ( | ||||||||||||||||||||||||||||
| 21 | is => 'rw', | ||||||||||||||||||||||||||||
| 22 | does => 'Mvalve::Queue', | ||||||||||||||||||||||||||||
| 23 | required => 1, | ||||||||||||||||||||||||||||
| 24 | coerce => 1, | ||||||||||||||||||||||||||||
| 25 | handles => { | ||||||||||||||||||||||||||||
| 26 | map { ( "q_$_" => $_ ) } | ||||||||||||||||||||||||||||
| 27 | qw(next fetch insert clear) | ||||||||||||||||||||||||||||
| 28 | }, | ||||||||||||||||||||||||||||
| 29 | ); | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | { | ||||||||||||||||||||||||||||
| 32 | my $default = sub { | ||||||||||||||||||||||||||||
| 33 | my $class = shift; | ||||||||||||||||||||||||||||
| 34 | return sub { | ||||||||||||||||||||||||||||
| 35 | Class::MOP::load_class($class); | ||||||||||||||||||||||||||||
| 36 | $class->new; | ||||||||||||||||||||||||||||
| 37 | }; | ||||||||||||||||||||||||||||
| 38 | }; | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | has 'queue_set' => ( | ||||||||||||||||||||||||||||
| 41 | is => 'rw', | ||||||||||||||||||||||||||||
| 42 | isa => 'Mvalve::QueueSet', | ||||||||||||||||||||||||||||
| 43 | required => 1, | ||||||||||||||||||||||||||||
| 44 | default => $default->( 'Mvalve::QueueSet' ) | ||||||||||||||||||||||||||||
| 45 | ); | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | has 'state' => ( | ||||||||||||||||||||||||||||
| 48 | is => 'rw', | ||||||||||||||||||||||||||||
| 49 | does => 'Mvalve::State', | ||||||||||||||||||||||||||||
| 50 | coerce => 1, | ||||||||||||||||||||||||||||
| 51 | required => 1, | ||||||||||||||||||||||||||||
| 52 | default => $default->( 'Mvalve::State::Memory' ), | ||||||||||||||||||||||||||||
| 53 | handles => { | ||||||||||||||||||||||||||||
| 54 | map { ("state_$_" => $_) } qw(get set remove incr decr) | ||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | ); | ||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | __PACKAGE__->meta->make_immutable; | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 1 | ▶ | no Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | ▶ | sub log { | ||||||||||||||||||||||||||
|
Subroutine
log: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 64 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 65 | 0 | my $logger = $self->logger ; | |||||||||||||||||||||||||||
| 66 | 0 | ▶ | return () unless $logger; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | $logger->log(@_); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | ▶ | sub clear_all { | ||||||||||||||||||||||||||
|
Subroutine
clear_all: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 72 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | foreach my $table ($self->queue_set->all_tables) { | |||||||||||||||||||||||||||
| 75 | 0 | $self->q_clear($table); | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | sub defer | ||||||||||||||||||||||||||||
| 80 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
defer: not called CC=15 SCAR=54.8
| |||||||||||||||||||||||||||||
| 81 | 0 | my( $self, %args ) = @_; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | my $message = $args{message}; | |||||||||||||||||||||||||||
| 84 | 0 | ▶ | my $interval = $args{interval} || 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
$args{'interval'} || 0
MC/DC0%0/1
$args{'interval'} || 0
$args{'interval'}
Truth table0%0/2
A
A= $args{'interval'}
| |||||||||||||||||||||||||||||
| 85 | my $duration = $args{duration} || | ||||||||||||||||||||||||||||
| 86 | 0 | ▶ | $message->header( &Mvalve::Const::DURATION_HEADER ) || | ||||||||||||||||||||||||||
|
Condition0%0/5
$args{'duration'} || $message->header(&Mvalve::Const::DURATION_HEADER) || 0
$args{"duration"} or $message->header(&Mvalve::Const::DURATION_HEADER)
MC/DC0%0/2
$args{'duration'} || $message->header(&Mvalve::Const::DURATION_HEADER) || 0
$args{"duration"}
$message->header(&Mvalve::Const::DURATION_HEADER)
Truth table0%0/3
A or B
A= $args{"duration"}B= $message->header(&Mvalve::Const::DURATION_HEADER)
| |||||||||||||||||||||||||||||
| 87 | 0; | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | my $factor = 100_000; | |||||||||||||||||||||||||||
| 90 | 0 | $interval *= $factor; | |||||||||||||||||||||||||||
| 91 | 0 | $duration *= $factor; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | ▶ | if ( ! Scalar::Util::blessed($message) || ! $message->isa( 'Mvalve::Message' ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
builtin::blessed($message) and $message->isa("Mvalve::Message")
MC/DC0%0/2
builtin::blessed($message) and $message->isa("Mvalve::Message")
builtin::blessed($message)
$message->isa("Mvalve::Message")
Truth table0%0/3
A and B
A= builtin::blessed($message)B= $message->isa("Mvalve::Message")
| |||||||||||||||||||||||||||||
| 94 | 0 | return () ; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | my $qs = $self->queue_set; | |||||||||||||||||||||||||||
| 98 | 0 | my $destination = $message->header( &Mvalve::Const::DESTINATION_HEADER ); | |||||||||||||||||||||||||||
| 99 | 0 | my $time_key = [ $destination, 'retry time' ]; | |||||||||||||||||||||||||||
| 100 | 0 | my $retry_key = [ $destination, 'retry' ]; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 0 | my $done = 0; | |||||||||||||||||||||||||||
| 103 | 0 | my $rv; | |||||||||||||||||||||||||||
| 104 | 0 | ▶ | while (! $done) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 0 | my $lock = $self->lock( join('.', @$time_key ) ); | |||||||||||||||||||||||||||
| 106 | 0 | ▶ | next unless $lock; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | $done = 1; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | my $now = Time::HiRes::time() * $factor; | |||||||||||||||||||||||||||
| 111 | 0 | ▶ | my $retry = int($self->state_get($time_key) || $now); | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->state_get($time_key) || $now
MC/DC0%0/2
$self->state_get($time_key) || $now
$self->state_get($time_key)
$now
Truth table0%0/3
A || B
A= $self->state_get($time_key)B= $now
| |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # we always prefer duration | ||||||||||||||||||||||||||||
| 114 | 0 | ▶ | my $offset = $duration || $interval; | ||||||||||||||||||||||||||
|
Condition0%0/3
$duration || $interval
MC/DC0%0/2
$duration || $interval
$duration
$interval
Truth table0%0/3
A || B
A= $durationB= $interval
| |||||||||||||||||||||||||||||
| 115 | 0 | my $myturn = 0; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | ▶ | if ($retry > $now) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 0 | $myturn = $retry; | |||||||||||||||||||||||||||
| 119 | } else { | ||||||||||||||||||||||||||||
| 120 | 0 | ▶ | if ( $retry + $offset >= $now ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 0 | $myturn = $retry + $offset; | |||||||||||||||||||||||||||
| 122 | } else { | ||||||||||||||||||||||||||||
| 123 | 0 | $myturn = $now; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | 0 | my $next = $myturn + $offset; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | $message->header( &Mvalve::Const::RETRY_HEADER, $myturn ); | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | ▶ | Mvalve::trace( "defer (retry = $retry)" ) if &Mvalve::Const::MVALVE_TRACE; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 0 | $rv = $self->q_insert( | |||||||||||||||||||||||||||
| 132 | table => $qs->choose_table('timed'), | ||||||||||||||||||||||||||||
| 133 | data => { | ||||||||||||||||||||||||||||
| 134 | destination => $destination, | ||||||||||||||||||||||||||||
| 135 | ready => $myturn, | ||||||||||||||||||||||||||||
| 136 | message => $message->serialize, | ||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | ); | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | ▶ | Mvalve::trace( "q_insert results in $rv" ) if &Mvalve::Const::MVALVE_TRACE; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 0 | ▶ | if ($rv) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 0 | $self->state_set($time_key, $next); | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | return $rv; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 1; | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | __END__ | ||||||||||||||||||||||||||||