ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/DB.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/DB.pm (file contents):
Revision 1.31 by root, Thu Jan 10 23:02:19 2008 UTC vs.
Revision 1.44 by root, Sat Jan 10 20:56:34 2009 UTC

15package DC::DB; 15package DC::DB;
16 16
17use strict; 17use strict;
18use utf8; 18use utf8;
19 19
20use File::Path ();
20use Carp (); 21use Carp ();
21use Storable (); 22use Storable ();
23use AnyEvent::Util ();
22use Config; 24use Config;
23use BDB; 25use BDB;
26use Fcntl ();
24 27
25use DC; 28use DC;
26 29
27our $ODBDIR = "cfplus-" . BDB::VERSION . "-$Config{archname}"; 30our $ODBDIR = "cfplus-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}";
28our $DBDIR = "client-" . BDB::VERSION . "-$Config{archname}"; 31our $DBDIR = "client-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}";
29our $DB_HOME = "$Deliantra::VARDIR/$DBDIR"; 32our $DB_HOME = "$Deliantra::VARDIR/$DBDIR";
30 33
34sub FIRST_TILE_ID () { 64 }
35
36unless (-d $DB_HOME) {
31if (!-e $DB_HOME and -e "$Deliantra::VARDIR/$ODBDIR") { 37 if (-d "$Deliantra::VARDIR/$ODBDIR") {
32 rename "$Deliantra::VARDIR/$ODBDIR", $DB_HOME; 38 rename "$Deliantra::VARDIR/$ODBDIR", $DB_HOME;
33 print STDERR "INFO: moved old database from $Deliantra::VARDIR/$ODBDIR to $DB_HOME\n"; 39 print STDERR "INFO: moved old database from $Deliantra::VARDIR/$ODBDIR to $DB_HOME\n";
34}
35
36if (!-e $DB_HOME and -e "$Deliantra::OLDDIR/$ODBDIR") { 40 } elsif (-d "$Deliantra::OLDDIR/$ODBDIR") {
37 rename "$Deliantra::OLDDIR/$DBDIR", $DB_HOME; 41 rename "$Deliantra::OLDDIR/$DBDIR", $DB_HOME;
38 print STDERR "INFO: moved old database from $Deliantra::OLDDIR/$ODBDIR to $DB_HOME\n"; 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 }
39} 47}
40 48
41BDB::max_poll_time 0.03; 49BDB::max_poll_time 0.03;
50BDB::max_parallel 1;
42 51
43our $DB_ENV; 52our $DB_ENV;
53our $DB_ENV_FH;
44our $DB_STATE; 54our $DB_STATE;
45our %DB_TABLE; 55our %DB_TABLE;
56our $TILE_SEQ;
46 57
58sub all_databases {
59 opendir my $fh, $DB_HOME
60 or return;
61
62 grep !/^(?:\.|log\.|_)/, readdir $fh
63}
64
65sub try_verify_env($) {
66 my ($env) = @_;
67
68 open my $lock, "+>$DB_HOME/__lock"
69 or die "__lock: $!";
70
71 flock $lock, &Fcntl::LOCK_EX
72 or die "flock: $!";
73
74 # we look at the __db.register env file that has been created by now
75 # and check for the number of registered processes - if there is
76 # only one, we verify all databases, otherwise we skip this
77 # we MUST NOT close the filehandle as longa swe keep the env open, as
78 # this destroys the record locks on it.
79 open $DB_ENV_FH, "<$DB_HOME/__db.register"
80 or die "__db.register: $!";
81
82 # __db.register contains one record per process, with X signifying
83 # empty records (of course, this is completely private to bdb...)
84 my $count = grep /^[^X]/, <$DB_ENV_FH>;
85
86 if ($count == 1) {
87 # if any databases are corrupted, we simply delete all of them
88
89 for (all_databases) {
90 my $dbh = db_create $env
91 or last;
92
93 # a failed verify will panic the environment, which is fine with us
94 db_verify $dbh, "$DB_HOME/$_";
95
96 return if $!; # nuke database and recreate if verification failure
97 }
98
99 }
100
101 # close probably cleans those up, but we also want to run on windows,
102 # so better be safe.
103 flock $lock, &Fcntl::LOCK_UN
104 or die "funlock: $!";
105
106 1
107}
108
47sub open_db { 109sub try_open_db {
48 mkdir $DB_HOME, 0777; 110 File::Path::mkpath [$DB_HOME];
49 111
112 undef $DB_ENV;
113 undef $DB_ENV_FH;
114
50 $DB_ENV = db_env_create; 115 my $env = db_env_create;
51 116
52 $DB_ENV->set_errfile (\*STDERR); 117 $env->set_errfile (\*STDERR);
53 $DB_ENV->set_msgfile (\*STDERR); 118 $env->set_msgfile (\*STDERR);
54 $DB_ENV->set_verbose (-1, 1); 119 $env->set_verbose (-1, 1);
55 120
56 $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::LOG_AUTOREMOVE | BDB::TXN_WRITE_NOSYNC); 121 $env->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT);
122 $env->set_flags (&BDB::LOG_AUTOREMOVE ) if BDB::VERSION v0, v4.7;
123 $env->log_set_config (&BDB::LOG_AUTO_REMOVE) if BDB::VERSION v4.7;
124
125 $env->set_timeout (3, BDB::SET_TXN_TIMEOUT);
126 $env->set_timeout (3, BDB::SET_LOCK_TIMEOUT);
127
57 $DB_ENV->set_cachesize (0, 2048 * 1024, 0); 128 $env->set_cachesize (0, 2048 * 1024, 0);
58 129
59 db_env_open $DB_ENV, $DB_HOME, 130 db_env_open $env, $DB_HOME,
60 BDB::CREATE | BDB::REGISTER | BDB::RECOVER | BDB::INIT_MPOOL | BDB::INIT_LOCK | BDB::INIT_TXN, 131 BDB::CREATE | BDB::REGISTER | BDB::RECOVER | BDB::INIT_MPOOL | BDB::INIT_LOCK | BDB::INIT_TXN,
61 0666; 132 0666;
62 133
63 $! and die "cannot open database environment $DB_HOME: " . BDB::strerror; 134 $! and die "cannot open database environment $DB_HOME: " . BDB::strerror;
135
136 # now we go through the registered processes, if there is only one, we verify all files
137 # to make sure windows didn'T corrupt them (as windows does....)
138 try_verify_env $env
139 or die "database environment failed verification";
140
141 $DB_ENV = $env;
64 142
65 1 143 1
66} 144}
67 145
68sub table($) { 146sub table($) {
69 $DB_TABLE{$_[0]} ||= do { 147 $DB_TABLE{$_[0]} ||= do {
70 my ($table) = @_; 148 my ($table) = @_;
71 149
72 $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge; 150 $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge;
73 151
152 $DB_ENV#d#
153 or return ::clienterror ("trying to create table $_[0] with empty db_env $DB_ENV" => 1);#d#
154
74 my $db = db_create $DB_ENV; 155 my $db = db_create $DB_ENV;
75 $db->set_flags (BDB::CHKSUM); 156 $db->set_flags (BDB::CHKSUM);
76 157
77 db_open $db, undef, $table, undef, BDB::BTREE, 158 db_open $db, undef, $table, undef, BDB::BTREE,
78 BDB::AUTO_COMMIT | BDB::CREATE | BDB::READ_UNCOMMITTED, 0666; 159 BDB::AUTO_COMMIT | BDB::CREATE | BDB::READ_UNCOMMITTED, 0666;
83 } 164 }
84} 165}
85 166
86############################################################################# 167#############################################################################
87 168
88unless (eval { open_db }) { 169our $WATCHER;
89 warn "$@";#d# 170our $SYNC;
90 eval { File::Path::rmtree $DB_HOME };
91 open_db;
92}
93
94our $WATCHER = EV::io BDB::poll_fileno, EV::READ, \&BDB::poll_cb;
95
96our $SYNC = EV::timer_ns 0, 60, sub {
97 $_[0]->stop;
98 db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { };
99};
100
101our $tilemap; 171our $facemap;
102 172
103sub exists($$$) { 173sub exists($$$) {
104 my ($db, $key, $cb) = @_; 174 my ($db, $key, $cb) = @_;
105 175
106 my $data; 176 my $data;
148 my ($name, $cb) = @_; 218 my ($name, $cb) = @_;
149 219
150 my $table = table "facemap"; 220 my $table = table "facemap";
151 my $id; 221 my $id;
152 222
153 db_get $table, undef, $name, $id, 0; 223 db_get $table, undef, $name => $id, 0;
154 return $cb->($id) unless $!; 224 $! or return $cb->($id);
155 225
156 for (1..100) { 226 unless ($TILE_SEQ) {
157 my $txn = $DB_ENV->txn_begin; 227 $TILE_SEQ = $table->sequence;
158 db_get $table, $txn, id => $id, 0; 228 $TILE_SEQ->initial_value (FIRST_TILE_ID);
229 $TILE_SEQ->set_cachesize (0);
230 db_sequence_open $TILE_SEQ, undef, "id", BDB::CREATE;
231 }
159 232
160 $id = 64 if $id < 64; 233 db_sequence_get $TILE_SEQ, undef, 1, my $id;
161 234
162 ++$id; 235 die "unable to allocate tile id: $!"
163 236 if $!;
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 } 237
238 db_put $table, undef, $name => $id, 0;
239 $cb->($id);
173 240
174 die "maximum number of transaction retries reached - database problems?";
175} 241}
176 242
177sub get_tile_id_sync($) { 243sub get_tile_id_sync($) {
178 my ($name) = @_; 244 my ($name) = @_;
179 245
180 $tilemap->{$name} ||= do { 246 $facemap->{$name} ||= do {
181 my $id; 247 my $id;
182 do_get_tile_id $name, sub { 248 do_get_tile_id $name, sub {
183 $id = $_[0]; 249 $id = $_[0];
184 }; 250 };
185 BDB::flush; 251 BDB::flush;
219sub logprint($$$) { 285sub logprint($$$) {
220 DC::DB::Server::req (logprint => @_); 286 DC::DB::Server::req (logprint => @_);
221} 287}
222 288
223############################################################################# 289#############################################################################
224
225# fetch the full face table first
226unless ($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 290
238package DC::DB::Server; 291package DC::DB::Server;
239 292
240use strict; 293use strict;
241 294
364 417
365 print { $LOG_FH{$path} } "$ts $line\n" 418 print { $LOG_FH{$path} } "$ts $line\n"
366} 419}
367 420
368sub run { 421sub run {
369 ($FH, my $fh) = DC::socketpipe; 422 ($FH, my $fh) = AnyEvent::Util::portable_socketpair
423 or die "unable to create database socketpair: $!";
370 424
371 my $oldfh = select $FH; $| = 1; select $oldfh; 425 my $oldfh = select $FH; $| = 1; select $oldfh;
372 my $oldfh = select $fh; $| = 1; select $oldfh; 426 my $oldfh = select $fh; $| = 1; select $oldfh;
373 427
374 my $pid = fork; 428 my $pid = fork;
421 475
422sub stop { 476sub stop {
423 close $FH; 477 close $FH;
424} 478}
425 479
480package DC::DB;
481
482sub nuke_db {
483 undef $DB_ENV;
484 undef $DB_ENV_FH;
485
486 File::Path::mkpath [$DB_HOME];
487 eval { File::Path::rmtree $DB_HOME };
488}
489
490sub open_db {
491 unless (eval { try_open_db }) {
492 warn "$@";#d#
493 eval { nuke_db };
494 try_open_db;
495 }
496
497 # fetch the full face table first
498 unless ($facemap) {
499 do_table facemap => sub {
500 $facemap = $_[0];
501 delete $facemap->{id};
502 my %maptile = reverse %$facemap;#d#
503 if ((scalar keys %$facemap) != (scalar keys %maptile)) {#d#
504 $facemap = { };#d#
505 DC::error "FATAL: facemap is not a 1:1 mapping, please report this and delete your $DB_HOME directory!\n";#d#
506 }#d#
507 };
508 }
509
510 $WATCHER = EV::io BDB::poll_fileno, EV::READ, \&BDB::poll_cb;
511 $SYNC = EV::timer_ns 0, 60, sub {
512 $_[0]->stop;
513 db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { };
514 };
515}
516
517END {
518 db_env_txn_checkpoint $DB_ENV, 0, 0, 0
519 if $DB_ENV;
520
521 undef $TILE_SEQ;
522 %DB_TABLE = ();
523 undef $DB_ENV;
524}
525
4261; 5261;
427 527
428=back 528=back
429 529
430=head1 AUTHOR 530=head1 AUTHOR

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines