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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines