Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: BerkeleyDB blowing up on multiple, concurrent processes

by m0r0n (Initiate)
on Jun 14, 2006 at 19:58 UTC ( [id://555343]=note: print w/replies, xml ) Need Help??


in reply to BerkeleyDB blowing up on multiple, concurrent processes

Figured out what was wrong. The $env and $db vars need to be child-process-local or the locking doesn't work right. Here's the updated, working script code:
#!/usr/bin/env perl # vim:set ts=4 sw=4 ai: use strict; use warnings; use Carp; use POSIX ":sys_wait_h"; use Data::Dumper; use lib '/junk/lib'; use BerkeleyDB; use BerkeleyDB::Hash; use Time::HiRes qw(gettimeofday tv_interval); use constant RUNS => 1000000; use constant DBPATH => '/junk/test.db'; use constant NUM_CHILDREN => 10; $|++; # print backtrace info sub bt { croak "DIED ($.)\n"; } # generate random integer sub rand_num { 4278190080 + int(rand(1048576)); } # read an entry from a BDB sub read_db { my ($db, $key) = @_; my $val; $db->db_get($key, $val); return (defined($val)) ? eval($val) : {}; } # increment an entry in a BDB, inserting if not there sub write_db { my ($db, $key, $stuff) = @_; my $val = read_db($db, $key); $val->{timestamp} = time; $val->{events}++; $val->{stuff}++ if ($stuff); my $str = Dumper(\$val) or croak "failed to serialize row data: $! +\n"; return $db->db_put($key, $str); } # fork off a worker process that will mutate the BDB independently of # the others sub spawn_child { my $pid = fork; croak "Could not fork: '$!'\n" unless (defined($pid)); return unless ($pid == 0); my $env = new BerkeleyDB::Env( -Cachesize => 4 * 1024 * 1024, -Flags => (DB_CREATE() | DB_INIT_CDB() | DB_INIT_MPOOL())) or croak "Failed to create DB env: '$!' ($BerkeleyDB::Error)\n +"; my $db = new BerkeleyDB::Hash( -Filename => DBPATH, -Env => $env, -Flags => DB_CREATE()) or croak "Failed to open DBPATH: '$!' ($BerkeleyDB::Error)\n"; # child process starts here my ($start, $finish, $key, $ip, $rv, $val, $reads, $writes, $delet +es); # perform a number of operations using BDB transactions $start = [ gettimeofday ]; for (1..RUNS) { $key = int(rand(101)); $ip = rand_num(); if ($key <= 49) { # do random update/insert $rv = write_db($db, $ip, ($key % 2 == 0)); croak "db_put failed: $rv ($! / $BerkeleyDB::Error)\n" unless ($rv == 0); $reads++; $writes++; } elsif ($key <= 98) { # do random read read_db($db, $ip); $reads++; } else { # do random delete $db->db_del($key); $deletes++; } } $finish = [ gettimeofday ]; # print summary info for timing my $sum = ($reads || 0) + ($writes || 0) + ($deletes || 0); my $elapsed = tv_interval($start, $finish); printf "$$: %d reads, %d writes, %d deletes (%d total) in %0.2f se +conds: %0.2f ops/sec\n", ($reads || 0), ($writes || 0), ($deletes || 0), $sum, $elapsed, $sum / $elapsed; exit 0; } # install signal handlers $SIG{__WARN__} = \&bt; # spawn concurrent DB mutators and just wait for them to finish for (1..NUM_CHILDREN) { spawn_child(); } for (1..NUM_CHILDREN) { my $pid = wait; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://555343]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-23 06:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found