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