1 |
=head1 NAME |
2 |
|
3 |
DC::DB - async. database and filesystem access for deliantra |
4 |
|
5 |
=head1 SYNOPSIS |
6 |
|
7 |
use DC::DB; |
8 |
|
9 |
=head1 DESCRIPTION |
10 |
|
11 |
=over 4 |
12 |
|
13 |
=cut |
14 |
|
15 |
package DC::DB; |
16 |
|
17 |
use strict; |
18 |
use utf8; |
19 |
|
20 |
use File::Path (); |
21 |
use Carp (); |
22 |
use Storable (); |
23 |
use AnyEvent::Util (); |
24 |
use Config; |
25 |
use BDB; |
26 |
|
27 |
use DC; |
28 |
|
29 |
our $ODBDIR = "cfplus-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}"; |
30 |
our $DBDIR = "client-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}"; |
31 |
our $DB_HOME = "$Deliantra::VARDIR/$DBDIR"; |
32 |
|
33 |
sub FIRST_TILE_ID () { 64 } |
34 |
|
35 |
unless (-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 |
|
48 |
BDB::max_poll_time 0.03; |
49 |
BDB::max_parallel 1; |
50 |
|
51 |
our $DB_ENV; |
52 |
our $DB_STATE; |
53 |
our %DB_TABLE; |
54 |
our $TILE_SEQ; |
55 |
|
56 |
sub try_open_db { |
57 |
File::Path::mkpath [$DB_HOME]; |
58 |
|
59 |
my $env = db_env_create; |
60 |
|
61 |
$env->set_errfile (\*STDERR); |
62 |
$env->set_msgfile (\*STDERR); |
63 |
$env->set_verbose (-1, 1); |
64 |
|
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 |
|
72 |
$env->set_cachesize (0, 2048 * 1024, 0); |
73 |
|
74 |
db_env_open $env, $DB_HOME, |
75 |
BDB::CREATE | BDB::REGISTER | BDB::RECOVER | BDB::INIT_MPOOL | BDB::INIT_LOCK | BDB::INIT_TXN, |
76 |
0666; |
77 |
|
78 |
$! and die "cannot open database environment $DB_HOME: " . BDB::strerror; |
79 |
|
80 |
$DB_ENV = $env; |
81 |
|
82 |
1 |
83 |
} |
84 |
|
85 |
sub table($) { |
86 |
$DB_TABLE{$_[0]} ||= do { |
87 |
my ($table) = @_; |
88 |
|
89 |
$table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge; |
90 |
|
91 |
$DB_ENV#d# |
92 |
or return ::clienterror ("trying to create table $_[0] with empty db_env $DB_ENV" => 1);#d# |
93 |
|
94 |
my $db = db_create $DB_ENV; |
95 |
$db->set_flags (BDB::CHKSUM); |
96 |
|
97 |
db_open $db, undef, $table, undef, BDB::BTREE, |
98 |
BDB::AUTO_COMMIT | BDB::CREATE | BDB::READ_UNCOMMITTED, 0666; |
99 |
|
100 |
$! and "unable to open/create database table $_[0]: ". BDB::strerror; |
101 |
|
102 |
$db |
103 |
} |
104 |
} |
105 |
|
106 |
############################################################################# |
107 |
|
108 |
our $WATCHER; |
109 |
our $SYNC; |
110 |
our $facemap; |
111 |
|
112 |
sub exists($$$) { |
113 |
my ($db, $key, $cb) = @_; |
114 |
|
115 |
my $data; |
116 |
db_get table $db, undef, $key, $data, 0, sub { |
117 |
$cb->($! ? () : length $data); |
118 |
}; |
119 |
} |
120 |
|
121 |
sub get($$$) { |
122 |
my ($db, $key, $cb) = @_; |
123 |
|
124 |
my $data; |
125 |
db_get table $db, undef, $key, $data, 0, sub { |
126 |
$cb->($! ? () : $data); |
127 |
}; |
128 |
} |
129 |
|
130 |
sub put($$$$) { |
131 |
my ($db, $key, $data, $cb) = @_; |
132 |
|
133 |
db_put table $db, undef, $key, $data, 0, sub { |
134 |
$cb->($!); |
135 |
$SYNC->again unless $SYNC->is_active; |
136 |
}; |
137 |
} |
138 |
|
139 |
sub do_table { |
140 |
my ($db, $cb) = @_; |
141 |
|
142 |
$db = table $db; |
143 |
|
144 |
my $cursor = $db->cursor; |
145 |
my %kv; |
146 |
|
147 |
for (;;) { |
148 |
db_c_get $cursor, my $k, my $v, BDB::NEXT; |
149 |
last if $!; |
150 |
$kv{$k} = $v; |
151 |
} |
152 |
|
153 |
$cb->(\%kv); |
154 |
} |
155 |
|
156 |
sub do_get_tile_id { |
157 |
my ($name, $cb) = @_; |
158 |
|
159 |
my $table = table "facemap"; |
160 |
my $id; |
161 |
|
162 |
db_get $table, undef, $name => $id, 0; |
163 |
$! or return $cb->($id); |
164 |
|
165 |
unless ($TILE_SEQ) { |
166 |
$TILE_SEQ = $table->sequence; |
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 |
} |
171 |
|
172 |
db_sequence_get $TILE_SEQ, undef, 1, my $id; |
173 |
|
174 |
die "unable to allocate tile id: $!" |
175 |
if $!; |
176 |
|
177 |
db_put $table, undef, $name => $id, 0; |
178 |
$cb->($id); |
179 |
|
180 |
} |
181 |
|
182 |
sub get_tile_id_sync($) { |
183 |
my ($name) = @_; |
184 |
|
185 |
$facemap->{$name} ||= do { |
186 |
my $id; |
187 |
do_get_tile_id $name, sub { |
188 |
$id = $_[0]; |
189 |
}; |
190 |
BDB::flush; |
191 |
$id |
192 |
} |
193 |
} |
194 |
|
195 |
############################################################################# |
196 |
|
197 |
sub path_of_res($) { |
198 |
utf8::downgrade $_[0]; # bug in unpack "H*" |
199 |
"$DB_HOME/res-data-" . unpack "H*", $_[0] |
200 |
} |
201 |
|
202 |
sub sync { |
203 |
# for debugging |
204 |
#DC::DB::Server::req (sync => sub { }); |
205 |
DC::DB::Server::sync (); |
206 |
} |
207 |
|
208 |
sub unlink($$) { |
209 |
DC::DB::Server::req (unlink => @_); |
210 |
} |
211 |
|
212 |
sub read_file($$) { |
213 |
DC::DB::Server::req (read_file => @_); |
214 |
} |
215 |
|
216 |
sub write_file($$$) { |
217 |
DC::DB::Server::req (write_file => @_); |
218 |
} |
219 |
|
220 |
sub prefetch_file($$$) { |
221 |
DC::DB::Server::req (prefetch_file => @_); |
222 |
} |
223 |
|
224 |
sub logprint($$$) { |
225 |
DC::DB::Server::req (logprint => @_); |
226 |
} |
227 |
|
228 |
############################################################################# |
229 |
|
230 |
package DC::DB::Server; |
231 |
|
232 |
use strict; |
233 |
|
234 |
use EV (); |
235 |
use Fcntl; |
236 |
|
237 |
our %CB; |
238 |
our $FH; |
239 |
our $ID = "aaa0"; |
240 |
our ($fh_r_watcher, $fh_w_watcher); |
241 |
our $sync_timer; |
242 |
our $write_buf; |
243 |
our $read_buf; |
244 |
|
245 |
sub fh_write { |
246 |
my $len = syswrite $FH, $write_buf; |
247 |
|
248 |
substr $write_buf, 0, $len, ""; |
249 |
|
250 |
$fh_w_watcher->stop |
251 |
unless length $write_buf; |
252 |
} |
253 |
|
254 |
sub fh_read { |
255 |
my $status = sysread $FH, $read_buf, 16384, length $read_buf; |
256 |
|
257 |
die "FATAL: database process died\n" |
258 |
if $status == 0 && defined $status; |
259 |
|
260 |
while () { |
261 |
return if 4 > length $read_buf; |
262 |
my $len = unpack "N", $read_buf; |
263 |
|
264 |
return if $len + 4 > length $read_buf; |
265 |
|
266 |
substr $read_buf, 0, 4, ""; |
267 |
my $res = Storable::thaw substr $read_buf, 0, $len, ""; |
268 |
|
269 |
my ($id, @args) = @$res; |
270 |
(delete $CB{$id})->(@args); |
271 |
} |
272 |
} |
273 |
|
274 |
sub sync { |
275 |
# biggest mess evarr |
276 |
my $fds; (vec $fds, fileno $FH, 1) = 1; |
277 |
|
278 |
while (1 < scalar keys %CB) { |
279 |
my $r = $fds; |
280 |
my $w = length $write_buf ? $fds : undef; |
281 |
select $r, $w, undef, undef; |
282 |
|
283 |
fh_write if vec $w, fileno $FH, 1; |
284 |
fh_read if vec $r, fileno $FH, 1; |
285 |
} |
286 |
} |
287 |
|
288 |
sub req { |
289 |
my ($type, @args) = @_; |
290 |
my $cb = pop @args; |
291 |
|
292 |
my $id = ++$ID; |
293 |
$write_buf .= pack "N/a*", Storable::freeze [$id, $type, @args]; |
294 |
$CB{$id} = $cb; |
295 |
|
296 |
$fh_w_watcher->start; |
297 |
} |
298 |
|
299 |
sub do_unlink { |
300 |
unlink $_[0]; |
301 |
} |
302 |
|
303 |
sub do_read_file { |
304 |
my ($path) = @_; |
305 |
|
306 |
utf8::downgrade $path; |
307 |
open my $fh, "<:raw", $path |
308 |
or return; |
309 |
sysread $fh, my $buf, -s $fh; |
310 |
|
311 |
$buf |
312 |
} |
313 |
|
314 |
sub do_write_file { |
315 |
my ($path, $data) = @_; |
316 |
|
317 |
utf8::downgrade $path; |
318 |
utf8::downgrade $data; |
319 |
open my $fh, ">:raw", $path |
320 |
or return; |
321 |
syswrite $fh, $data; |
322 |
close $fh; |
323 |
|
324 |
1 |
325 |
} |
326 |
|
327 |
sub do_prefetch_file { |
328 |
my ($path, $size) = @_; |
329 |
|
330 |
utf8::downgrade $path; |
331 |
open my $fh, "<:raw", $path |
332 |
or return; |
333 |
sysread $fh, my $buf, $size; |
334 |
|
335 |
1 |
336 |
} |
337 |
|
338 |
our %LOG_FH; |
339 |
|
340 |
sub do_logprint { |
341 |
my ($path, $line) = @_; |
342 |
|
343 |
$LOG_FH{$path} ||= do { |
344 |
open my $fh, ">>:utf8", $path |
345 |
or warn "Couldn't open logfile $path: $!"; |
346 |
|
347 |
$fh->autoflush (1); |
348 |
|
349 |
$fh |
350 |
}; |
351 |
|
352 |
my ($sec, $min, $hour, $mday, $mon, $year) = localtime time; |
353 |
|
354 |
my $ts = sprintf "%04d-%02d-%02d %02d:%02d:%02d", |
355 |
$year + 1900, $mon + 1, $mday, $hour, $min, $sec; |
356 |
|
357 |
print { $LOG_FH{$path} } "$ts $line\n" |
358 |
} |
359 |
|
360 |
sub run { |
361 |
($FH, my $fh) = AnyEvent::Util::portable_socketpair |
362 |
or die "unable to create database socketpair: $!"; |
363 |
|
364 |
my $oldfh = select $FH; $| = 1; select $oldfh; |
365 |
my $oldfh = select $fh; $| = 1; select $oldfh; |
366 |
|
367 |
my $pid = fork; |
368 |
|
369 |
if (defined $pid && !$pid) { |
370 |
local $SIG{QUIT} = "IGNORE"; |
371 |
local $SIG{__DIE__}; |
372 |
local $SIG{__WARN__}; |
373 |
eval { |
374 |
close $FH; |
375 |
|
376 |
while () { |
377 |
4 == read $fh, my $len, 4 |
378 |
or last; |
379 |
$len = unpack "N", $len; |
380 |
$len == read $fh, my $req, $len |
381 |
or die "unexpected eof while reading request"; |
382 |
|
383 |
$req = Storable::thaw $req; |
384 |
|
385 |
my ($id, $type, @args) = @$req; |
386 |
my $cb = DC::DB::Server->can ("do_$type") |
387 |
or die "$type: unknown database request type\n"; |
388 |
my $res = pack "N/a*", Storable::freeze [$id, $cb->(@args)]; |
389 |
(syswrite $fh, $res) == length $res |
390 |
or die "DB::write: $!"; |
391 |
} |
392 |
}; |
393 |
|
394 |
my $error = $@; |
395 |
|
396 |
eval { |
397 |
Storable::store_fd [die => $error], $fh; |
398 |
}; |
399 |
|
400 |
warn $error |
401 |
if $error; |
402 |
|
403 |
DC::_exit 0; |
404 |
} |
405 |
|
406 |
close $fh; |
407 |
DC::fh_nonblocking $FH, 1; |
408 |
|
409 |
$CB{die} = sub { die shift }; |
410 |
|
411 |
$fh_r_watcher = EV::io $FH, EV::READ , \&fh_read; |
412 |
$fh_w_watcher = EV::io $FH, EV::WRITE, \&fh_write; |
413 |
} |
414 |
|
415 |
sub stop { |
416 |
close $FH; |
417 |
} |
418 |
|
419 |
package DC::DB; |
420 |
|
421 |
sub nuke_db { |
422 |
File::Path::mkpath [$DB_HOME]; |
423 |
eval { File::Path::rmtree $DB_HOME }; |
424 |
} |
425 |
|
426 |
sub 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 |
|
453 |
END { |
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 |
|
462 |
1; |
463 |
|
464 |
=back |
465 |
|
466 |
=head1 AUTHOR |
467 |
|
468 |
Marc Lehmann <schmorp@schmorp.de> |
469 |
http://home.schmorp.de/ |
470 |
|
471 |
=cut |
472 |
|