On 02/12/2010 06:57 PM, Jonathan Swartz wrote:
> I need to guarantee that only one process at a time enters a subroutine
> foo() for a particular argument.
>
> That is, if one process is in a call to foo(1), another call to foo(1)
> will block, but a call to foo(2) could proceed.
>
> This needs to be guaranteed across multiple servers, as the calls to
> foo() manipulate multiple shared objects in the database.
>
> Even though foo() isn't directly associated with one database table (and
> thus I can't rely on database transactions directly), I figured I could
I am probably missing something here... I don't see how the lack of a
1-1 function-table association precludes the use of database transactions.
Maybe something like [back of envelope sketch code]:
while (!$done && ++$count < $max_tries) {
eval {
foo($foo_param);
}; if (my $e = $@) {
$dbh->rollback();
next if $e =~ /$serialization_error_re/;
die $e;
}
++$done;
}
confess "Busy signal will not abate" if !$done;
sub foo {
$dbh->begin_work();
$dbh->do("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE");
$dbh->prepare(q{
SELECT columns...
FROM obj_table1 NATURAL JOIN obj_table2 NATURAL JOIN...
WHERE things > "Good"
});
$dbh->execute();
do foo stuff with objects from select & update database tables.
....
$dbh->commit();
}
> sub foo {
> my ($id) = @_;
>
> my $hash = $id % 1024;
> my $dbh = DBI->connect(..., AutoCommit => 0);
> $dbh->prepare("update mutexes set id = ? where id = ?", $hash, $hash);
Depending on the DBD / DBD Driver options, you might need to do a
$dhb->execute(). Some of the DBDs might not always talk to the database
when you do the $dbh->prepare().
> sense, if there's a CPAN module that already does this (couldn't find
A quick search of CPAN turned up this one:
http://search.cpan.org/~rjbs/DBIx-Locker-0.100111/lib/DBIx/Locker.pm
And here is a module that looks to do something similar, but instead of
bothering your database, you point it at your mcached cluster. However,
from the test reports, it looks like some work might be required to beat
it into shape ?
http://search.cpan.org/~zmij/Cache-Memcached-Semaphore-0.3/Semaphore.pm
-r