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