--- deliantra/server/lib/cf.pm 2007/01/26 20:59:57 1.195 +++ deliantra/server/lib/cf.pm 2007/02/11 22:42:09 1.207 @@ -17,6 +17,7 @@ use Coro::Semaphore; use Coro::AIO; +use BDB (); use Data::Dumper; use Digest::MD5; use Fcntl; @@ -50,6 +51,10 @@ our $WRITE_RUNTIME_WATCHER; our $NEXT_TICK; our $NOW; +our $USE_FSYNC = 1; # use fsync to write maps - default off + +our $BDB_POLL_WATCHER; +our $DB_ENV; our %CFG; @@ -60,6 +65,7 @@ our %MAP; # all maps our $LINK_MAP; # the special {link} map, which is always available our $RANDOM_MAPS = cf::localdir . "/random"; +our $BDB_ENV_DIR = cf::localdir . "/db"; our $WAIT_FOR_TICK; $WAIT_FOR_TICK ||= new Coro::Signal; our $WAIT_FOR_TICK_ONE; $WAIT_FOR_TICK_ONE ||= new Coro::Signal; @@ -82,9 +88,12 @@ mkdir cf::localdir . "/" . cf::tmpdir; mkdir cf::localdir . "/" . cf::uniquedir; mkdir $RANDOM_MAPS; +mkdir $BDB_ENV_DIR; our $EMERGENCY_POSITION; +sub cf::map::normalise; + ############################################################################# =head2 GLOBAL VARIABLES @@ -358,6 +367,7 @@ (aio_write $fh, 0, (length $value), $value, 0) <= 0 and return; + # always fsync - this file is important aio_fsync $fh and return; @@ -752,7 +762,7 @@ if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { chmod SAVE_MODE, $fh; aio_write $fh, 0, (length $$rdata), $$rdata, 0; - aio_fsync $fh; + aio_fsync $fh if $cf::USE_FSYNC; close $fh; if (@$objs) { @@ -760,7 +770,7 @@ chmod SAVE_MODE, $fh; my $data = Storable::nfreeze { version => 1, objs => $objs }; aio_write $fh, 0, (length $data), $data, 0; - aio_fsync $fh; + aio_fsync $fh if $cf::USE_FSYNC; close $fh; aio_rename "$filename.pst~", "$filename.pst"; } @@ -776,6 +786,12 @@ aio_unlink $filename; aio_unlink "$filename.pst"; } + + #d##TODO# nuke non .map-files if exist + if ($filename =~ s/\.map$//) { + aio_unlink $filename; + aio_unlink "$filename.pst"; + } } } @@ -792,6 +808,9 @@ my ($data, $av); + #d#TODO remove .map if file does not exist + aio_stat $filename and $filename =~ s/\.map$//; + (aio_load $filename, $data) >= 0 or return; @@ -1070,6 +1089,8 @@ sub maps($) { my ($pl) = @_; + $pl = ref $pl ? $pl->ob->name : $pl; + my $files = aio_readdir playerdir $pl or return; @@ -1080,8 +1101,7 @@ next if /\.(?:pl|pst)$/; next unless /^$PATH_SEP/o; - s/\.map$//; - push @paths, "~" . $pl->ob->name . "/" . $_; + push @paths, cf::map::normalise "~$pl/$_"; } \@paths @@ -1180,6 +1200,8 @@ $path = "$path"; # make sure its a string + $path =~ s/\.map$//; + # map plan: # # /! non-realised random map exit (special hack!) @@ -1250,7 +1272,7 @@ sub load_path { my ($self) = @_; - sprintf "%s/%s/%s", cf::datadir, cf::mapdir, $self->{path} + sprintf "%s/%s/%s.map", cf::datadir, cf::mapdir, $self->{path} } # the temporary/swap location @@ -1258,7 +1280,7 @@ my ($self) = @_; (my $path = $_[0]{path}) =~ s/\//$PATH_SEP/g; - sprintf "%s/%s/%s", cf::localdir, cf::tmpdir, $path + sprintf "%s/%s/%s.map", cf::localdir, cf::tmpdir, $path } # the unique path, undef == no special unique path @@ -1284,14 +1306,19 @@ utf8::encode (my $save = $self->save_path); IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink $save; IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink "$save.pst"; + + #d#TODO remove .map and also nuke + $save =~ s/\.map// or return;#d# + IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink $save;#d# + IO::AIO::aioreq_pri 4; Coro::AIO::aio_unlink "$save.pst";#d# } sub load_header_from($) { my ($self, $path) = @_; utf8::encode $path; - aio_open $path, O_RDONLY, 0 - or return; + #aio_open $path, O_RDONLY, 0 + # or return; $self->_load_header ($path) or return; @@ -1382,6 +1409,8 @@ sub load { my ($self) = @_; + local $self->{deny_reset} = 1; # loading can take a long time + my $path = $self->{path}; my $guard = cf::lock_acquire "map_load:$path"; @@ -1628,8 +1657,7 @@ next if /\.pst$/; next unless /^$PATH_SEP/o; - s/\.map$//; - push @paths, $_; + push @paths, cf::map::normalise $_; } \@paths @@ -1733,7 +1761,7 @@ sub link_map { unless ($LINK_MAP) { $LINK_MAP = cf::map::find "{link}" - or do { warn "FATAL: unable to provide {link} map, exiting."; exit 1 }; + or cf::cleanup "FATAL: unable to provide {link} map, exiting."; $LINK_MAP->load; } @@ -1827,13 +1855,12 @@ $self->enter_link; (async { - my $map = cf::map::find $path; - $map = $map->customise_for ($self) if $map; - -# warn "entering ", $map->path, " at ($x, $y)\n" -# if $map; - - $map or $self->message ("The exit to '" . ($path->visible_name) . "' is closed", cf::NDI_UNIQUE | cf::NDI_RED); + my $map = eval { + my $map = cf::map::find $path; + $map = $map->customise_for ($self) if $map; + $map + } or + $self->message ("The exit to '$path' is closed", cf::NDI_UNIQUE | cf::NDI_RED); $self->leave_link ($map, $x, $y); })->prio (1); @@ -1874,7 +1901,7 @@ my $rmp = parse_random_map_params $exit->msg; if ($exit->map) { - $rmp->{region} = $exit->map->region_name; + $rmp->{region} = $exit->region->name; $rmp->{origin_map} = $exit->map->path; $rmp->{origin_x} = $exit->x; $rmp->{origin_y} = $exit->y; @@ -1986,6 +2013,7 @@ or return; # be conservative, not sure how that can happen, but we saw a crash here (shift @$queue)->[1]->($msg); + return unless $ns->valid; # temporary(?) workaround for callback destroying socket push @{ $ns->{query_queue} }, @$queue; @@ -2252,7 +2280,12 @@ } ############################################################################# -# the server's main() +# the server's init and main functions + +sub load_resources { + load_regions sprintf "%s/%s/regions", cf::datadir, cf::mapdir + or die "unable to load regions file\n";#d# +} sub cfg_load { open my $fh, "<:utf8", cf::confdir . "/config" @@ -2275,6 +2308,10 @@ } } +sub init { + load_resources; +} + sub main { # we must not ever block the main coroutine local $Coro::idle = sub { @@ -2443,6 +2480,9 @@ warn "reattaching attachments to maps"; reattach $_ for values %MAP; + warn "loading reloadable resources"; + load_resources; + warn "restarting server ticker"; $TICK_WATCHER->start; @@ -2520,17 +2560,66 @@ }, ); -IO::AIO::max_poll_time $TICK * 0.1; +{ + BDB::max_poll_time $TICK * 0.1; + $BDB_POLL_WATCHER = Event->io ( + reentrant => 0, + fd => BDB::poll_fileno, + poll => 'r', + prio => 0, + data => WF_AUTOCANCEL, + cb => \&BDB::poll_cb, + ); + BDB::min_parallel 8; + + BDB::set_sync_prepare { + my $status; + my $current = $Coro::current; + ( + sub { + $status = $!; + $current->ready; undef $current; + }, + sub { + Coro::schedule while defined $current; + $! = $status; + }, + ) + }; -undef $Coro::AIO::WATCHER; -$AIO_POLL_WATCHER = Event->io ( - reentrant => 0, - fd => IO::AIO::poll_fileno, - poll => 'r', - prio => 6, - data => WF_AUTOCANCEL, - cb => \&IO::AIO::poll_cb, -); + unless ($DB_ENV) { + $DB_ENV = BDB::db_env_create; + + cf::sync_job { + BDB::db_env_open + $DB_ENV, + $BDB_ENV_DIR, + BDB::INIT_LOCK | BDB::INIT_LOG | BDB::INIT_MPOOL | BDB::INIT_TXN + | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE, + 0666; + + cf::cleanup "$!" if $!; + + $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC, 1); + $DB_ENV->set_lk_detect; + }; + } +} + +{ + IO::AIO::min_parallel 8; + + undef $Coro::AIO::WATCHER; + IO::AIO::max_poll_time $TICK * 0.1; + $AIO_POLL_WATCHER = Event->io ( + reentrant => 0, + fd => IO::AIO::poll_fileno, + poll => 'r', + prio => 6, + data => WF_AUTOCANCEL, + cb => \&IO::AIO::poll_cb, + ); +} $WRITE_RUNTIME_WATCHER = Event->timer ( reentrant => 0,