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.24 by root, Tue Dec 11 02:42:38 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 $DB_HOME = "$Crossfire::VARDIR/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}";
31our $DB_HOME = "$Deliantra::VARDIR/$DBDIR";
32
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") {
40 rename "$Deliantra::OLDDIR/$DBDIR", $DB_HOME;
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 }
46}
47
48BDB::max_poll_time 0.03;
49BDB::max_parallel 1;
28 50
29our $DB_ENV; 51our $DB_ENV;
30our $DB_STATE; 52our $DB_STATE;
31our %DB_TABLE; 53our %DB_TABLE;
54our $TILE_SEQ;
32 55
33sub open_db { 56sub try_open_db {
34 mkdir $DB_HOME, 0777; 57 File::Path::mkpath [$DB_HOME];
35 58
36 $DB_ENV = db_env_create; 59 my $env = db_env_create;
37 60
38 $DB_ENV->set_errfile (\*STDERR); 61 $env->set_errfile (\*STDERR);
39 $DB_ENV->set_msgfile (\*STDERR); 62 $env->set_msgfile (\*STDERR);
40 $DB_ENV->set_verbose (-1, 1); 63 $env->set_verbose (-1, 1);
41 64
42 $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
43 $DB_ENV->set_cachesize (0, 2048 * 1024, 0); 72 $env->set_cachesize (0, 2048 * 1024, 0);
44 73
45 db_env_open $DB_ENV, $DB_HOME, 74 db_env_open $env, $DB_HOME,
46 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,
47 0666; 76 0666;
48 77
49 $! 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;
50 81
51 1 82 1
52} 83}
53 84
54sub table($) { 85sub table($) {
55 $DB_TABLE{$_[0]} ||= do { 86 $DB_TABLE{$_[0]} ||= do {
56 my ($table) = @_; 87 my ($table) = @_;
57 88
58 $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge; 89 $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge;
59 90
91 $DB_ENV#d#
92 or return ::clienterror ("trying to create table $_[0] with empty db_env $DB_ENV" => 1);#d#
93
60 my $db = db_create $DB_ENV; 94 my $db = db_create $DB_ENV;
61 $db->set_flags (BDB::CHKSUM); 95 $db->set_flags (BDB::CHKSUM);
62 96
63 db_open $db, undef, $table, undef, BDB::BTREE, 97 db_open $db, undef, $table, undef, BDB::BTREE,
64 BDB::AUTO_COMMIT | BDB::CREATE | BDB::READ_UNCOMMITTED, 0666; 98 BDB::AUTO_COMMIT | BDB::CREATE | BDB::READ_UNCOMMITTED, 0666;
69 } 103 }
70} 104}
71 105
72############################################################################# 106#############################################################################
73 107
74unless (eval { open_db }) { 108our $WATCHER;
75 warn "$@";#d# 109our $SYNC;
76 eval { File::Path::rmtree $DB_HOME };
77 open_db;
78}
79
80our $WATCHER = EV::io BDB::poll_fileno, EV::READ, \&BDB::poll_cb;
81
82our $SYNC = EV::timer_ns 0, 60, sub {
83 $_[0]->stop;
84 db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { };
85};
86
87our $tilemap; 110our $facemap;
88 111
89sub exists($$$) { 112sub exists($$$) {
90 my ($db, $key, $cb) = @_; 113 my ($db, $key, $cb) = @_;
91 114
92 my $data; 115 my $data;
134 my ($name, $cb) = @_; 157 my ($name, $cb) = @_;
135 158
136 my $table = table "facemap"; 159 my $table = table "facemap";
137 my $id; 160 my $id;
138 161
139 db_get $table, undef, $name, $id, 0; 162 db_get $table, undef, $name => $id, 0;
140 return $cb->($id) unless $!; 163 $! or return $cb->($id);
141 164
142 for (1..100) { 165 unless ($TILE_SEQ) {
143 my $txn = $DB_ENV->txn_begin; 166 $TILE_SEQ = $table->sequence;
144 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 }
145 171
146 $id = 64 if $id < 64; 172 db_sequence_get $TILE_SEQ, undef, 1, my $id;
147 173
148 ++$id; 174 die "unable to allocate tile id: $!"
149 175 if $!;
150 db_put $table, $txn, id => $id, 0;
151 db_txn_finish $txn;
152
153 $SYNC->again unless $SYNC->is_active;
154
155 return $cb->($id) unless $!;
156
157 select undef, undef, undef, 0.01 * rand;
158 } 176
177 db_put $table, undef, $name => $id, 0;
178 $cb->($id);
159 179
160 die "maximum number of transaction retries reached - database problems?";
161} 180}
162 181
163sub get_tile_id_sync($) { 182sub get_tile_id_sync($) {
164 my ($name) = @_; 183 my ($name) = @_;
165 184
166 # fetch the full face table first
167 unless ($tilemap) {
168 do_table facemap => sub {
169 $tilemap = $_[0];
170 delete $tilemap->{id};
171 my %maptile = reverse %$tilemap;#d#
172 if ((scalar keys %$tilemap) != (scalar keys %maptile)) {#d#
173 $tilemap = { };#d#
174 CFPlus::error "FATAL: facemap is not a 1:1 mapping, please report this and delete your $DB_HOME directory!\n";#d#
175 }#d#
176 };
177 BDB::flush;
178 }
179
180 $tilemap->{$name} ||= do { 185 $facemap->{$name} ||= do {
181 my $id; 186 my $id;
182 do_get_tile_id $name, sub { 187 do_get_tile_id $name, sub {
183 $id = $_[0]; 188 $id = $_[0];
184 }; 189 };
185 BDB::flush; 190 BDB::flush;
194 "$DB_HOME/res-data-" . unpack "H*", $_[0] 199 "$DB_HOME/res-data-" . unpack "H*", $_[0]
195} 200}
196 201
197sub sync { 202sub sync {
198 # for debugging 203 # for debugging
199 #CFPlus::DB::Server::req (sync => sub { }); 204 #DC::DB::Server::req (sync => sub { });
200 CFPlus::DB::Server::sync (); 205 DC::DB::Server::sync ();
201} 206}
202 207
203sub unlink($$) { 208sub unlink($$) {
204 CFPlus::DB::Server::req (unlink => @_); 209 DC::DB::Server::req (unlink => @_);
205} 210}
206 211
207sub read_file($$) { 212sub read_file($$) {
208 CFPlus::DB::Server::req (read_file => @_); 213 DC::DB::Server::req (read_file => @_);
209} 214}
210 215
211sub write_file($$$) { 216sub write_file($$$) {
212 CFPlus::DB::Server::req (write_file => @_); 217 DC::DB::Server::req (write_file => @_);
213} 218}
214 219
215sub prefetch_file($$$) { 220sub prefetch_file($$$) {
216 CFPlus::DB::Server::req (prefetch_file => @_); 221 DC::DB::Server::req (prefetch_file => @_);
217} 222}
218 223
219sub logprint($$$) { 224sub logprint($$$) {
220 CFPlus::DB::Server::req (logprint => @_); 225 DC::DB::Server::req (logprint => @_);
221} 226}
222 227
228#############################################################################
229
223package CFPlus::DB::Server; 230package DC::DB::Server;
224 231
225use strict; 232use strict;
226 233
227use EV (); 234use EV ();
228use Fcntl; 235use Fcntl;
349 356
350 print { $LOG_FH{$path} } "$ts $line\n" 357 print { $LOG_FH{$path} } "$ts $line\n"
351} 358}
352 359
353sub run { 360sub run {
354 ($FH, my $fh) = CFPlus::socketpipe; 361 ($FH, my $fh) = AnyEvent::Util::portable_socketpair
362 or die "unable to create database socketpair: $!";
355 363
356 my $oldfh = select $FH; $| = 1; select $oldfh; 364 my $oldfh = select $FH; $| = 1; select $oldfh;
357 my $oldfh = select $fh; $| = 1; select $oldfh; 365 my $oldfh = select $fh; $| = 1; select $oldfh;
358 366
359 my $pid = fork; 367 my $pid = fork;
360 368
361 if (defined $pid && !$pid) { 369 if (defined $pid && !$pid) {
362 local $SIG{QUIT}; 370 local $SIG{QUIT} = "IGNORE";
363 local $SIG{__DIE__}; 371 local $SIG{__DIE__};
364 local $SIG{__WARN__}; 372 local $SIG{__WARN__};
365 eval { 373 eval {
366 close $FH; 374 close $FH;
367 375
373 or die "unexpected eof while reading request"; 381 or die "unexpected eof while reading request";
374 382
375 $req = Storable::thaw $req; 383 $req = Storable::thaw $req;
376 384
377 my ($id, $type, @args) = @$req; 385 my ($id, $type, @args) = @$req;
378 my $cb = CFPlus::DB::Server->can ("do_$type") 386 my $cb = DC::DB::Server->can ("do_$type")
379 or die "$type: unknown database request type\n"; 387 or die "$type: unknown database request type\n";
380 my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)]; 388 my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)];
381 (syswrite $fh, $res) == length $res 389 (syswrite $fh, $res) == length $res
382 or die "DB::write: $!"; 390 or die "DB::write: $!";
383 } 391 }
387 395
388 eval { 396 eval {
389 Storable::store_fd [die => $error], $fh; 397 Storable::store_fd [die => $error], $fh;
390 }; 398 };
391 399
392 warn $error; 400 warn $error
401 if $error;
393 402
394 CFPlus::_exit 0; 403 DC::_exit 0;
395 } 404 }
396 405
397 close $fh; 406 close $fh;
398 CFPlus::fh_nonblocking $FH, 1; 407 DC::fh_nonblocking $FH, 1;
399 408
400 $CB{die} = sub { die shift }; 409 $CB{die} = sub { die shift };
401 410
402 $fh_r_watcher = EV::io $FH, EV::READ , \&fh_read; 411 $fh_r_watcher = EV::io $FH, EV::READ , \&fh_read;
403 $fh_w_watcher = EV::io $FH, EV::WRITE, \&fh_write; 412 $fh_w_watcher = EV::io $FH, EV::WRITE, \&fh_write;
405 414
406sub stop { 415sub stop {
407 close $FH; 416 close $FH;
408} 417}
409 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
4101; 4621;
411 463
412=back 464=back
413 465
414=head1 AUTHOR 466=head1 AUTHOR

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines