ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/DB.pm
Revision: 1.42
Committed: Sun Jan 4 10:22:19 2009 UTC (15 years, 5 months ago) by root
Branch: MAIN
Changes since 1.41: +3 -1 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 =head1 NAME
2    
3 root 1.31 DC::DB - async. database and filesystem access for deliantra
4 root 1.1
5     =head1 SYNOPSIS
6    
7 root 1.29 use DC::DB;
8 root 1.1
9     =head1 DESCRIPTION
10    
11     =over 4
12    
13     =cut
14    
15 root 1.29 package DC::DB;
16 root 1.1
17     use strict;
18     use utf8;
19    
20 root 1.35 use File::Path ();
21 root 1.1 use Carp ();
22 root 1.5 use Storable ();
23 root 1.42 use AnyEvent::Util ();
24 root 1.9 use Config;
25 root 1.24 use BDB;
26 root 1.1
27 root 1.29 use DC;
28 root 1.3
29 root 1.38 our $ODBDIR = "cfplus-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}";
30     our $DBDIR = "client-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}";
31 root 1.27 our $DB_HOME = "$Deliantra::VARDIR/$DBDIR";
32    
33 root 1.40 sub FIRST_TILE_ID () { 64 }
34    
35 root 1.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 root 1.27 }
47 root 1.9
48 root 1.30 BDB::max_poll_time 0.03;
49 root 1.32 BDB::max_parallel 1;
50 root 1.30
51 root 1.24 our $DB_ENV;
52     our $DB_STATE;
53     our %DB_TABLE;
54 root 1.35 our $TILE_SEQ;
55 root 1.24
56 root 1.33 sub try_open_db {
57 root 1.35 File::Path::mkpath [$DB_HOME];
58 root 1.24
59 root 1.36 my $env = db_env_create;
60 root 1.24
61 root 1.36 $env->set_errfile (\*STDERR);
62     $env->set_msgfile (\*STDERR);
63     $env->set_verbose (-1, 1);
64 root 1.24
65 root 1.39 $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 root 1.36 $env->set_cachesize (0, 2048 * 1024, 0);
73 root 1.24
74 root 1.36 db_env_open $env, $DB_HOME,
75 root 1.24 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 root 1.36 $DB_ENV = $env;
81    
82 root 1.24 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 root 1.41 $DB_ENV#d#
92     or return ::clienterror ("trying to create table $_[0] with empty db_env $DB_ENV" => 1);#d#
93    
94 root 1.24 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 root 1.9 }
105    
106 root 1.24 #############################################################################
107    
108 root 1.33 our $WATCHER;
109     our $SYNC;
110 root 1.34 our $facemap;
111 root 1.24
112 root 1.9 sub exists($$$) {
113 root 1.24 my ($db, $key, $cb) = @_;
114    
115     my $data;
116     db_get table $db, undef, $key, $data, 0, sub {
117     $cb->($! ? () : length $data);
118     };
119 root 1.9 }
120    
121 root 1.1 sub get($$$) {
122 root 1.24 my ($db, $key, $cb) = @_;
123    
124     my $data;
125     db_get table $db, undef, $key, $data, 0, sub {
126     $cb->($! ? () : $data);
127     };
128 root 1.1 }
129    
130     sub put($$$$) {
131 root 1.24 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 root 1.1 }
138    
139 root 1.24 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 root 1.9 }
155    
156 root 1.24 sub do_get_tile_id {
157     my ($name, $cb) = @_;
158    
159     my $table = table "facemap";
160     my $id;
161    
162 root 1.35 db_get $table, undef, $name => $id, 0;
163 root 1.34 $! or return $cb->($id);
164 root 1.24
165 root 1.35 unless ($TILE_SEQ) {
166     $TILE_SEQ = $table->sequence;
167 root 1.40 $TILE_SEQ->initial_value (FIRST_TILE_ID);
168 root 1.35 $TILE_SEQ->set_cachesize (0);
169     db_sequence_open $TILE_SEQ, undef, "id", BDB::CREATE;
170     }
171 root 1.24
172 root 1.35 db_sequence_get $TILE_SEQ, undef, 1, my $id;
173 root 1.24
174 root 1.35 die "unable to allocate tile id: $!"
175     if $!;
176    
177     db_put $table, undef, $name => $id, 0;
178     $cb->($id);
179 root 1.24
180 root 1.15 }
181    
182 root 1.1 sub get_tile_id_sync($) {
183 root 1.20 my ($name) = @_;
184 root 1.1
185 root 1.34 $facemap->{$name} ||= do {
186 root 1.1 my $id;
187 root 1.24 do_get_tile_id $name, sub {
188     $id = $_[0];
189     };
190     BDB::flush;
191 root 1.1 $id
192     }
193     }
194    
195 root 1.24 #############################################################################
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 root 1.29 #DC::DB::Server::req (sync => sub { });
205     DC::DB::Server::sync ();
206 root 1.24 }
207 root 1.1
208 root 1.24 sub unlink($$) {
209 root 1.29 DC::DB::Server::req (unlink => @_);
210 root 1.24 }
211 root 1.1
212 root 1.24 sub read_file($$) {
213 root 1.29 DC::DB::Server::req (read_file => @_);
214 root 1.24 }
215 root 1.1
216 root 1.24 sub write_file($$$) {
217 root 1.29 DC::DB::Server::req (write_file => @_);
218 root 1.24 }
219 root 1.1
220 root 1.24 sub prefetch_file($$$) {
221 root 1.29 DC::DB::Server::req (prefetch_file => @_);
222 root 1.24 }
223 root 1.1
224 root 1.24 sub logprint($$$) {
225 root 1.29 DC::DB::Server::req (logprint => @_);
226 root 1.1 }
227    
228 root 1.30 #############################################################################
229    
230 root 1.29 package DC::DB::Server;
231 root 1.1
232 root 1.24 use strict;
233 root 1.1
234 root 1.24 use EV ();
235     use Fcntl;
236 root 1.1
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 root 1.14 $fh_w_watcher->stop
251 root 1.1 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 root 1.14 $fh_w_watcher->start;
297 root 1.1 }
298    
299 root 1.9 sub do_unlink {
300     unlink $_[0];
301     }
302    
303 root 1.16 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 root 1.9 sub do_write_file {
315 root 1.16 my ($path, $data) = @_;
316 root 1.9
317 root 1.16 utf8::downgrade $path;
318 root 1.9 utf8::downgrade $data;
319 root 1.16 open my $fh, ">:raw", $path
320 root 1.9 or return;
321 root 1.16 syswrite $fh, $data;
322 root 1.9 close $fh;
323    
324     1
325     }
326    
327 root 1.11 sub do_prefetch_file {
328 root 1.16 my ($path, $size) = @_;
329 root 1.11
330 root 1.16 utf8::downgrade $path;
331     open my $fh, "<:raw", $path
332 root 1.11 or return;
333     sysread $fh, my $buf, $size;
334    
335     1
336     }
337    
338 root 1.15 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 root 1.1 sub run {
361 root 1.42 ($FH, my $fh) = AnyEvent::Util::portable_socketpair
362     or die "unable to create database socketpair: $!";
363 root 1.1
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 root 1.30 local $SIG{QUIT} = "IGNORE";
371 root 1.1 local $SIG{__DIE__};
372 root 1.15 local $SIG{__WARN__};
373 root 1.1 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 root 1.29 my $cb = DC::DB::Server->can ("do_$type")
387 root 1.1 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 root 1.21 or die "DB::write: $!";
391 root 1.1 }
392     };
393    
394     my $error = $@;
395    
396     eval {
397     Storable::store_fd [die => $error], $fh;
398     };
399    
400 root 1.25 warn $error
401     if $error;
402 root 1.24
403 root 1.29 DC::_exit 0;
404 root 1.1 }
405    
406     close $fh;
407 root 1.29 DC::fh_nonblocking $FH, 1;
408 root 1.1
409 root 1.2 $CB{die} = sub { die shift };
410 root 1.1
411 root 1.21 $fh_r_watcher = EV::io $FH, EV::READ , \&fh_read;
412     $fh_w_watcher = EV::io $FH, EV::WRITE, \&fh_write;
413 root 1.1 }
414    
415 root 1.6 sub stop {
416     close $FH;
417     }
418    
419 root 1.33 package DC::DB;
420    
421 root 1.35 sub nuke_db {
422     File::Path::mkpath [$DB_HOME];
423     eval { File::Path::rmtree $DB_HOME };
424     }
425    
426 root 1.33 sub open_db {
427     unless (eval { try_open_db }) {
428     warn "$@";#d#
429 root 1.35 eval { nuke_db };
430 root 1.33 try_open_db;
431     }
432    
433     # fetch the full face table first
434 root 1.34 unless ($facemap) {
435 root 1.33 do_table facemap => sub {
436 root 1.34 $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 root 1.33 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 root 1.37 db_env_txn_checkpoint $DB_ENV, 0, 0, 0
455     if $DB_ENV;
456    
457 root 1.35 undef $TILE_SEQ;
458 root 1.33 %DB_TABLE = ();
459     undef $DB_ENV;
460     }
461    
462 root 1.1 1;
463    
464     =back
465    
466     =head1 AUTHOR
467    
468     Marc Lehmann <schmorp@schmorp.de>
469     http://home.schmorp.de/
470    
471     =cut
472