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.46 by root, Fri Jul 29 08:35:35 2011 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 common::sense;
18use utf8;
19 18
19use File::Path ();
20use Carp (); 20use Carp ();
21use Storable (); 21use Storable ();
22use AnyEvent::Util ();
22use Config; 23use Config;
23use BDB; 24use BDB;
25use Fcntl ();
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;
52our $DB_ENV_FH;
36our $DB_STATE; 53our $DB_STATE;
37our %DB_TABLE; 54our %DB_TABLE;
55our $TILE_SEQ;
38 56
57sub all_databases {
58 opendir my $fh, $DB_HOME
59 or return;
60
61 grep !/^(?:\.|log\.|_)/, readdir $fh
62}
63
64sub try_verify_env($) {
65 my ($env) = @_;
66
67 open my $lock, "+>$DB_HOME/__lock"
68 or die "__lock: $!";
69
70 flock $lock, &Fcntl::LOCK_EX
71 or die "flock: $!";
72
73 # we look at the __db.register env file that has been created by now
74 # and check for the number of registered processes - if there is
75 # only one, we verify all databases, otherwise we skip this
76 # we MUST NOT close the filehandle as longa swe keep the env open, as
77 # this destroys the record locks on it.
78 open $DB_ENV_FH, "<$DB_HOME/__db.register"
79 or die "__db.register: $!";
80
81 # __db.register contains one record per process, with X signifying
82 # empty records (of course, this is completely private to bdb...)
83 my $count = grep /^[^X]/, <$DB_ENV_FH>;
84
85 if ($count == 1) {
86 # if any databases are corrupted, we simply delete all of them
87
88 for (all_databases) {
89 my $dbh = db_create $env
90 or last;
91
92 # a failed verify will panic the environment, which is fine with us
93 db_verify $dbh, "$DB_HOME/$_";
94
95 return if $!; # nuke database and recreate if verification failure
96 }
97
98 }
99
100 # close probably cleans those up, but we also want to run on windows,
101 # so better be safe.
102 flock $lock, &Fcntl::LOCK_UN
103 or die "funlock: $!";
104
105 1
106}
107
39sub open_db { 108sub try_open_db {
40 mkdir $DB_HOME, 0777; 109 File::Path::mkpath [$DB_HOME];
41 110
111 undef $DB_ENV;
112 undef $DB_ENV_FH;
113
42 $DB_ENV = db_env_create; 114 my $env = db_env_create;
43 115
44 $DB_ENV->set_errfile (\*STDERR); 116 $env->set_errfile (\*STDERR);
45 $DB_ENV->set_msgfile (\*STDERR); 117 $env->set_msgfile (\*STDERR);
46 $DB_ENV->set_verbose (-1, 1); 118 $env->set_verbose (-1, 1);
47 119
48 $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::LOG_AUTOREMOVE | BDB::TXN_WRITE_NOSYNC); 120 $env->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT);
121 $env->set_flags (&BDB::LOG_AUTOREMOVE ) if BDB::VERSION v0, v4.7;
122 $env->log_set_config (&BDB::LOG_AUTO_REMOVE) if BDB::VERSION v4.7;
123
124 $env->set_timeout (3, BDB::SET_TXN_TIMEOUT);
125 $env->set_timeout (3, BDB::SET_LOCK_TIMEOUT);
126
49 $DB_ENV->set_cachesize (0, 2048 * 1024, 0); 127 $env->set_cachesize (0, 2048 * 1024, 0);
50 128
51 db_env_open $DB_ENV, $DB_HOME, 129 db_env_open $env, $DB_HOME,
52 BDB::CREATE | BDB::REGISTER | BDB::RECOVER | BDB::INIT_MPOOL | BDB::INIT_LOCK | BDB::INIT_TXN, 130 BDB::CREATE | BDB::REGISTER | BDB::RECOVER | BDB::INIT_MPOOL | BDB::INIT_LOCK | BDB::INIT_TXN,
53 0666; 131 0666;
54 132
55 $! and die "cannot open database environment $DB_HOME: " . BDB::strerror; 133 $! and die "cannot open database environment $DB_HOME: " . BDB::strerror;
134
135 # now we go through the registered processes, if there is only one, we verify all files
136 # to make sure windows didn't corrupt them (as windows does....)
137 try_verify_env $env
138 or die "database environment failed verification";
139
140 $DB_ENV = $env;
56 141
57 1 142 1
58} 143}
59 144
60sub table($) { 145sub table($) {
61 $DB_TABLE{$_[0]} ||= do { 146 $DB_TABLE{$_[0]} ||= do {
62 my ($table) = @_; 147 my ($table) = @_;
63 148
64 $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge; 149 $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge;
65 150
151 $DB_ENV#d#
152 or return ::clienterror ("trying to create table $_[0] with empty db_env $DB_ENV" => 1);#d#
153
66 my $db = db_create $DB_ENV; 154 my $db = db_create $DB_ENV;
67 $db->set_flags (BDB::CHKSUM); 155 $db->set_flags (BDB::CHKSUM);
68 156
69 db_open $db, undef, $table, undef, BDB::BTREE, 157 db_open $db, undef, $table, undef, BDB::BTREE,
70 BDB::AUTO_COMMIT | BDB::CREATE | BDB::READ_UNCOMMITTED, 0666; 158 BDB::AUTO_COMMIT | BDB::CREATE | BDB::READ_UNCOMMITTED, 0666;
75 } 163 }
76} 164}
77 165
78############################################################################# 166#############################################################################
79 167
80unless (eval { open_db }) { 168our $WATCHER;
81 warn "$@";#d# 169our $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; 170our $facemap;
94 171
95sub exists($$$) { 172sub exists($$$) {
96 my ($db, $key, $cb) = @_; 173 my ($db, $key, $cb) = @_;
97 174
98 my $data; 175 my $data;
140 my ($name, $cb) = @_; 217 my ($name, $cb) = @_;
141 218
142 my $table = table "facemap"; 219 my $table = table "facemap";
143 my $id; 220 my $id;
144 221
145 db_get $table, undef, $name, $id, 0; 222 db_get $table, undef, $name => $id, 0;
146 return $cb->($id) unless $!; 223 $! or return $cb->($id);
147 224
148 for (1..100) { 225 unless ($TILE_SEQ) {
149 my $txn = $DB_ENV->txn_begin; 226 $TILE_SEQ = $table->sequence;
150 db_get $table, $txn, id => $id, 0; 227 $TILE_SEQ->initial_value (FIRST_TILE_ID);
228 $TILE_SEQ->set_cachesize (0);
229 db_sequence_open $TILE_SEQ, undef, "id", BDB::CREATE;
230 }
151 231
152 $id = 64 if $id < 64; 232 db_sequence_get $TILE_SEQ, undef, 1, my $id;
153 233
154 ++$id; 234 die "unable to allocate tile id: $!"
155 235 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 } 236
237 db_put $table, undef, $name => $id, 0;
238 $cb->($id);
165 239
166 die "maximum number of transaction retries reached - database problems?";
167} 240}
168 241
169sub get_tile_id_sync($) { 242sub get_tile_id_sync($) {
170 my ($name) = @_; 243 my ($name) = @_;
171 244
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 { 245 $facemap->{$name} ||= do {
187 my $id; 246 my $id;
188 do_get_tile_id $name, sub { 247 do_get_tile_id $name, sub {
189 $id = $_[0]; 248 $id = $_[0];
190 }; 249 };
191 BDB::flush; 250 BDB::flush;
200 "$DB_HOME/res-data-" . unpack "H*", $_[0] 259 "$DB_HOME/res-data-" . unpack "H*", $_[0]
201} 260}
202 261
203sub sync { 262sub sync {
204 # for debugging 263 # for debugging
205 #CFPlus::DB::Server::req (sync => sub { }); 264 #DC::DB::Server::req (sync => sub { });
206 CFPlus::DB::Server::sync (); 265 DC::DB::Server::sync ();
207} 266}
208 267
209sub unlink($$) { 268sub unlink($$) {
210 CFPlus::DB::Server::req (unlink => @_); 269 DC::DB::Server::req (unlink => @_);
211} 270}
212 271
213sub read_file($$) { 272sub read_file($$) {
214 CFPlus::DB::Server::req (read_file => @_); 273 DC::DB::Server::req (read_file => @_);
215} 274}
216 275
217sub write_file($$$) { 276sub write_file($$$) {
218 CFPlus::DB::Server::req (write_file => @_); 277 DC::DB::Server::req (write_file => @_);
219} 278}
220 279
221sub prefetch_file($$$) { 280sub prefetch_file($$$) {
222 CFPlus::DB::Server::req (prefetch_file => @_); 281 DC::DB::Server::req (prefetch_file => @_);
223} 282}
224 283
225sub logprint($$$) { 284sub logprint($$$) {
226 CFPlus::DB::Server::req (logprint => @_); 285 DC::DB::Server::req (logprint => @_);
227} 286}
228 287
288#############################################################################
289
229package CFPlus::DB::Server; 290package DC::DB::Server;
230 291
231use strict; 292use common::sense;
232 293
233use EV (); 294use EV ();
234use Fcntl; 295use Fcntl;
235 296
236our %CB; 297our %CB;
355 416
356 print { $LOG_FH{$path} } "$ts $line\n" 417 print { $LOG_FH{$path} } "$ts $line\n"
357} 418}
358 419
359sub run { 420sub run {
360 ($FH, my $fh) = CFPlus::socketpipe; 421 ($FH, my $fh) = AnyEvent::Util::portable_socketpair
422 or die "unable to create database socketpair: $!";
361 423
362 my $oldfh = select $FH; $| = 1; select $oldfh; 424 my $oldfh = select $FH; $| = 1; select $oldfh;
363 my $oldfh = select $fh; $| = 1; select $oldfh; 425 my $oldfh = select $fh; $| = 1; select $oldfh;
364 426
365 my $pid = fork; 427 my $pid = fork;
366 428
367 if (defined $pid && !$pid) { 429 if (defined $pid && !$pid) {
368 local $SIG{QUIT}; 430 local $SIG{QUIT} = "IGNORE";
369 local $SIG{__DIE__}; 431 local $SIG{__DIE__};
370 local $SIG{__WARN__}; 432 local $SIG{__WARN__};
371 eval { 433 eval {
372 close $FH; 434 close $FH;
373 435
379 or die "unexpected eof while reading request"; 441 or die "unexpected eof while reading request";
380 442
381 $req = Storable::thaw $req; 443 $req = Storable::thaw $req;
382 444
383 my ($id, $type, @args) = @$req; 445 my ($id, $type, @args) = @$req;
384 my $cb = CFPlus::DB::Server->can ("do_$type") 446 my $cb = DC::DB::Server->can ("do_$type")
385 or die "$type: unknown database request type\n"; 447 or die "$type: unknown database request type\n";
386 my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)]; 448 my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)];
387 (syswrite $fh, $res) == length $res 449 (syswrite $fh, $res) == length $res
388 or die "DB::write: $!"; 450 or die "DB::write: $!";
389 } 451 }
396 }; 458 };
397 459
398 warn $error 460 warn $error
399 if $error; 461 if $error;
400 462
401 CFPlus::_exit 0; 463 DC::_exit 0;
402 } 464 }
403 465
404 close $fh; 466 close $fh;
405 CFPlus::fh_nonblocking $FH, 1; 467 DC::fh_nonblocking $FH, 1;
406 468
407 $CB{die} = sub { die shift }; 469 $CB{die} = sub { die shift };
408 470
409 $fh_r_watcher = EV::io $FH, EV::READ , \&fh_read; 471 $fh_r_watcher = EV::io $FH, EV::READ , \&fh_read;
410 $fh_w_watcher = EV::io $FH, EV::WRITE, \&fh_write; 472 $fh_w_watcher = EV::io $FH, EV::WRITE, \&fh_write;
412 474
413sub stop { 475sub stop {
414 close $FH; 476 close $FH;
415} 477}
416 478
479package DC::DB;
480
481sub nuke_db {
482 undef $DB_ENV;
483 undef $DB_ENV_FH;
484
485 File::Path::mkpath [$DB_HOME];
486 eval { File::Path::rmtree $DB_HOME };
487}
488
489sub open_db {
490 unless (eval { try_open_db }) {
491 warn "$@";#d#
492 eval { nuke_db };
493 try_open_db;
494 }
495
496 # fetch the full face table first
497 unless ($facemap) {
498 do_table facemap => sub {
499 $facemap = $_[0];
500 delete $facemap->{id};
501 my %maptile = reverse %$facemap;#d#
502 if ((scalar keys %$facemap) != (scalar keys %maptile)) {#d#
503 $facemap = { };#d#
504 DC::error "FATAL: facemap is not a 1:1 mapping, please report this and delete your $DB_HOME directory!\n";#d#
505 }#d#
506 };
507 }
508
509 $WATCHER = EV::io BDB::poll_fileno, EV::READ, \&BDB::poll_cb;
510 $SYNC = EV::timer_ns 0, 60, sub {
511 $_[0]->stop;
512 db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { };
513 };
514}
515
516END {
517 db_env_txn_checkpoint $DB_ENV, 0, 0, 0
518 if $DB_ENV;
519
520 undef $TILE_SEQ;
521 %DB_TABLE = ();
522 undef $DB_ENV;
523}
524
4171; 5251;
418 526
419=back 527=back
420 528
421=head1 AUTHOR 529=head1 AUTHOR

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines