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