ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/DB.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/DB.pm (file contents):
Revision 1.27 by root, Wed Dec 26 18:20:46 2007 UTC vs.
Revision 1.42 by root, Sun Jan 4 10:22:19 2009 UTC

1=head1 NAME 1=head1 NAME
2 2
3CFPlus::DB - async. database and filesystem access for cfplus 3DC::DB - async. database and filesystem access for deliantra
4 4
5=head1 SYNOPSIS 5=head1 SYNOPSIS
6 6
7 use CFPlus::DB; 7 use DC::DB;
8 8
9=head1 DESCRIPTION 9=head1 DESCRIPTION
10 10
11=over 4 11=over 4
12 12
13=cut 13=cut
14 14
15package CFPlus::DB; 15package DC::DB;
16 16
17use strict; 17use strict;
18use utf8; 18use utf8;
19 19
20use File::Path ();
20use Carp (); 21use Carp ();
21use Storable (); 22use Storable ();
23use AnyEvent::Util ();
22use Config; 24use Config;
23use BDB; 25use BDB;
24 26
25use CFPlus; 27use DC;
26 28
27our $DBDIR = "cfplus-" . BDB::VERSION . "-$Config{archname}"; 29our $ODBDIR = "cfplus-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}";
30our $DBDIR = "client-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}";
28our $DB_HOME = "$Deliantra::VARDIR/$DBDIR"; 31our $DB_HOME = "$Deliantra::VARDIR/$DBDIR";
29 32
30if (!-e $DB_HOME and -e "$Deliantra::OLDDIR/$DBDIR") { 33sub FIRST_TILE_ID () { 64 }
34
35unless (-d $DB_HOME) {
36 if (-d "$Deliantra::VARDIR/$ODBDIR") {
37 rename "$Deliantra::VARDIR/$ODBDIR", $DB_HOME;
38 print STDERR "INFO: moved old database from $Deliantra::VARDIR/$ODBDIR to $DB_HOME\n";
39 } elsif (-d "$Deliantra::OLDDIR/$ODBDIR") {
31 rename "$Deliantra::OLDDIR/$DBDIR", $DB_HOME; 40 rename "$Deliantra::OLDDIR/$DBDIR", $DB_HOME;
32 print STDERR "INFO: moved old database from $Deliantra::OLDDIR/$DBDIR to $DB_HOME\n"; 41 print STDERR "INFO: moved old database from $Deliantra::OLDDIR/$ODBDIR to $DB_HOME\n";
42 } else {
43 File::Path::mkpath [$DB_HOME]
44 or die "unable to create database directory $DB_HOME: $!";
45 }
33} 46}
47
48BDB::max_poll_time 0.03;
49BDB::max_parallel 1;
34 50
35our $DB_ENV; 51our $DB_ENV;
36our $DB_STATE; 52our $DB_STATE;
37our %DB_TABLE; 53our %DB_TABLE;
54our $TILE_SEQ;
38 55
39sub open_db { 56sub try_open_db {
40 mkdir $DB_HOME, 0777; 57 File::Path::mkpath [$DB_HOME];
41 58
42 $DB_ENV = db_env_create; 59 my $env = db_env_create;
43 60
44 $DB_ENV->set_errfile (\*STDERR); 61 $env->set_errfile (\*STDERR);
45 $DB_ENV->set_msgfile (\*STDERR); 62 $env->set_msgfile (\*STDERR);
46 $DB_ENV->set_verbose (-1, 1); 63 $env->set_verbose (-1, 1);
47 64
48 $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::LOG_AUTOREMOVE | BDB::TXN_WRITE_NOSYNC); 65 $env->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT);
66 $env->set_flags (&BDB::LOG_AUTOREMOVE ) if BDB::VERSION v0, v4.7;
67 $env->log_set_config (&BDB::LOG_AUTO_REMOVE) if BDB::VERSION v4.7;
68
69 $env->set_timeout (3, BDB::SET_TXN_TIMEOUT);
70 $env->set_timeout (3, BDB::SET_LOCK_TIMEOUT);
71
49 $DB_ENV->set_cachesize (0, 2048 * 1024, 0); 72 $env->set_cachesize (0, 2048 * 1024, 0);
50 73
51 db_env_open $DB_ENV, $DB_HOME, 74 db_env_open $env, $DB_HOME,
52 BDB::CREATE | BDB::REGISTER | BDB::RECOVER | BDB::INIT_MPOOL | BDB::INIT_LOCK | BDB::INIT_TXN, 75 BDB::CREATE | BDB::REGISTER | BDB::RECOVER | BDB::INIT_MPOOL | BDB::INIT_LOCK | BDB::INIT_TXN,
53 0666; 76 0666;
54 77
55 $! and die "cannot open database environment $DB_HOME: " . BDB::strerror; 78 $! and die "cannot open database environment $DB_HOME: " . BDB::strerror;
79
80 $DB_ENV = $env;
56 81
57 1 82 1
58} 83}
59 84
60sub table($) { 85sub table($) {
61 $DB_TABLE{$_[0]} ||= do { 86 $DB_TABLE{$_[0]} ||= do {
62 my ($table) = @_; 87 my ($table) = @_;
63 88
64 $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge; 89 $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge;
65 90
91 $DB_ENV#d#
92 or return ::clienterror ("trying to create table $_[0] with empty db_env $DB_ENV" => 1);#d#
93
66 my $db = db_create $DB_ENV; 94 my $db = db_create $DB_ENV;
67 $db->set_flags (BDB::CHKSUM); 95 $db->set_flags (BDB::CHKSUM);
68 96
69 db_open $db, undef, $table, undef, BDB::BTREE, 97 db_open $db, undef, $table, undef, BDB::BTREE,
70 BDB::AUTO_COMMIT | BDB::CREATE | BDB::READ_UNCOMMITTED, 0666; 98 BDB::AUTO_COMMIT | BDB::CREATE | BDB::READ_UNCOMMITTED, 0666;
75 } 103 }
76} 104}
77 105
78############################################################################# 106#############################################################################
79 107
80unless (eval { open_db }) { 108our $WATCHER;
81 warn "$@";#d# 109our $SYNC;
82 eval { File::Path::rmtree $DB_HOME };
83 open_db;
84}
85
86our $WATCHER = EV::io BDB::poll_fileno, EV::READ, \&BDB::poll_cb;
87
88our $SYNC = EV::timer_ns 0, 60, sub {
89 $_[0]->stop;
90 db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { };
91};
92
93our $tilemap; 110our $facemap;
94 111
95sub exists($$$) { 112sub exists($$$) {
96 my ($db, $key, $cb) = @_; 113 my ($db, $key, $cb) = @_;
97 114
98 my $data; 115 my $data;
140 my ($name, $cb) = @_; 157 my ($name, $cb) = @_;
141 158
142 my $table = table "facemap"; 159 my $table = table "facemap";
143 my $id; 160 my $id;
144 161
145 db_get $table, undef, $name, $id, 0; 162 db_get $table, undef, $name => $id, 0;
146 return $cb->($id) unless $!; 163 $! or return $cb->($id);
147 164
148 for (1..100) { 165 unless ($TILE_SEQ) {
149 my $txn = $DB_ENV->txn_begin; 166 $TILE_SEQ = $table->sequence;
150 db_get $table, $txn, id => $id, 0; 167 $TILE_SEQ->initial_value (FIRST_TILE_ID);
168 $TILE_SEQ->set_cachesize (0);
169 db_sequence_open $TILE_SEQ, undef, "id", BDB::CREATE;
170 }
151 171
152 $id = 64 if $id < 64; 172 db_sequence_get $TILE_SEQ, undef, 1, my $id;
153 173
154 ++$id; 174 die "unable to allocate tile id: $!"
155 175 if $!;
156 db_put $table, $txn, id => $id, 0;
157 db_txn_finish $txn;
158
159 $SYNC->again unless $SYNC->is_active;
160
161 return $cb->($id) unless $!;
162
163 select undef, undef, undef, 0.01 * rand;
164 } 176
177 db_put $table, undef, $name => $id, 0;
178 $cb->($id);
165 179
166 die "maximum number of transaction retries reached - database problems?";
167} 180}
168 181
169sub get_tile_id_sync($) { 182sub get_tile_id_sync($) {
170 my ($name) = @_; 183 my ($name) = @_;
171 184
172 # fetch the full face table first
173 unless ($tilemap) {
174 do_table facemap => sub {
175 $tilemap = $_[0];
176 delete $tilemap->{id};
177 my %maptile = reverse %$tilemap;#d#
178 if ((scalar keys %$tilemap) != (scalar keys %maptile)) {#d#
179 $tilemap = { };#d#
180 CFPlus::error "FATAL: facemap is not a 1:1 mapping, please report this and delete your $DB_HOME directory!\n";#d#
181 }#d#
182 };
183 BDB::flush;
184 }
185
186 $tilemap->{$name} ||= do { 185 $facemap->{$name} ||= do {
187 my $id; 186 my $id;
188 do_get_tile_id $name, sub { 187 do_get_tile_id $name, sub {
189 $id = $_[0]; 188 $id = $_[0];
190 }; 189 };
191 BDB::flush; 190 BDB::flush;
200 "$DB_HOME/res-data-" . unpack "H*", $_[0] 199 "$DB_HOME/res-data-" . unpack "H*", $_[0]
201} 200}
202 201
203sub sync { 202sub sync {
204 # for debugging 203 # for debugging
205 #CFPlus::DB::Server::req (sync => sub { }); 204 #DC::DB::Server::req (sync => sub { });
206 CFPlus::DB::Server::sync (); 205 DC::DB::Server::sync ();
207} 206}
208 207
209sub unlink($$) { 208sub unlink($$) {
210 CFPlus::DB::Server::req (unlink => @_); 209 DC::DB::Server::req (unlink => @_);
211} 210}
212 211
213sub read_file($$) { 212sub read_file($$) {
214 CFPlus::DB::Server::req (read_file => @_); 213 DC::DB::Server::req (read_file => @_);
215} 214}
216 215
217sub write_file($$$) { 216sub write_file($$$) {
218 CFPlus::DB::Server::req (write_file => @_); 217 DC::DB::Server::req (write_file => @_);
219} 218}
220 219
221sub prefetch_file($$$) { 220sub prefetch_file($$$) {
222 CFPlus::DB::Server::req (prefetch_file => @_); 221 DC::DB::Server::req (prefetch_file => @_);
223} 222}
224 223
225sub logprint($$$) { 224sub logprint($$$) {
226 CFPlus::DB::Server::req (logprint => @_); 225 DC::DB::Server::req (logprint => @_);
227} 226}
228 227
228#############################################################################
229
229package CFPlus::DB::Server; 230package DC::DB::Server;
230 231
231use strict; 232use strict;
232 233
233use EV (); 234use EV ();
234use Fcntl; 235use Fcntl;
355 356
356 print { $LOG_FH{$path} } "$ts $line\n" 357 print { $LOG_FH{$path} } "$ts $line\n"
357} 358}
358 359
359sub run { 360sub run {
360 ($FH, my $fh) = CFPlus::socketpipe; 361 ($FH, my $fh) = AnyEvent::Util::portable_socketpair
362 or die "unable to create database socketpair: $!";
361 363
362 my $oldfh = select $FH; $| = 1; select $oldfh; 364 my $oldfh = select $FH; $| = 1; select $oldfh;
363 my $oldfh = select $fh; $| = 1; select $oldfh; 365 my $oldfh = select $fh; $| = 1; select $oldfh;
364 366
365 my $pid = fork; 367 my $pid = fork;
366 368
367 if (defined $pid && !$pid) { 369 if (defined $pid && !$pid) {
368 local $SIG{QUIT}; 370 local $SIG{QUIT} = "IGNORE";
369 local $SIG{__DIE__}; 371 local $SIG{__DIE__};
370 local $SIG{__WARN__}; 372 local $SIG{__WARN__};
371 eval { 373 eval {
372 close $FH; 374 close $FH;
373 375
379 or die "unexpected eof while reading request"; 381 or die "unexpected eof while reading request";
380 382
381 $req = Storable::thaw $req; 383 $req = Storable::thaw $req;
382 384
383 my ($id, $type, @args) = @$req; 385 my ($id, $type, @args) = @$req;
384 my $cb = CFPlus::DB::Server->can ("do_$type") 386 my $cb = DC::DB::Server->can ("do_$type")
385 or die "$type: unknown database request type\n"; 387 or die "$type: unknown database request type\n";
386 my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)]; 388 my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)];
387 (syswrite $fh, $res) == length $res 389 (syswrite $fh, $res) == length $res
388 or die "DB::write: $!"; 390 or die "DB::write: $!";
389 } 391 }
396 }; 398 };
397 399
398 warn $error 400 warn $error
399 if $error; 401 if $error;
400 402
401 CFPlus::_exit 0; 403 DC::_exit 0;
402 } 404 }
403 405
404 close $fh; 406 close $fh;
405 CFPlus::fh_nonblocking $FH, 1; 407 DC::fh_nonblocking $FH, 1;
406 408
407 $CB{die} = sub { die shift }; 409 $CB{die} = sub { die shift };
408 410
409 $fh_r_watcher = EV::io $FH, EV::READ , \&fh_read; 411 $fh_r_watcher = EV::io $FH, EV::READ , \&fh_read;
410 $fh_w_watcher = EV::io $FH, EV::WRITE, \&fh_write; 412 $fh_w_watcher = EV::io $FH, EV::WRITE, \&fh_write;
412 414
413sub stop { 415sub stop {
414 close $FH; 416 close $FH;
415} 417}
416 418
419package DC::DB;
420
421sub nuke_db {
422 File::Path::mkpath [$DB_HOME];
423 eval { File::Path::rmtree $DB_HOME };
424}
425
426sub open_db {
427 unless (eval { try_open_db }) {
428 warn "$@";#d#
429 eval { nuke_db };
430 try_open_db;
431 }
432
433 # fetch the full face table first
434 unless ($facemap) {
435 do_table facemap => sub {
436 $facemap = $_[0];
437 delete $facemap->{id};
438 my %maptile = reverse %$facemap;#d#
439 if ((scalar keys %$facemap) != (scalar keys %maptile)) {#d#
440 $facemap = { };#d#
441 DC::error "FATAL: facemap is not a 1:1 mapping, please report this and delete your $DB_HOME directory!\n";#d#
442 }#d#
443 };
444 }
445
446 $WATCHER = EV::io BDB::poll_fileno, EV::READ, \&BDB::poll_cb;
447 $SYNC = EV::timer_ns 0, 60, sub {
448 $_[0]->stop;
449 db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { };
450 };
451}
452
453END {
454 db_env_txn_checkpoint $DB_ENV, 0, 0, 0
455 if $DB_ENV;
456
457 undef $TILE_SEQ;
458 %DB_TABLE = ();
459 undef $DB_ENV;
460}
461
4171; 4621;
418 463
419=back 464=back
420 465
421=head1 AUTHOR 466=head1 AUTHOR

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines