ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/lib/cf.pm
(Generate patch)

Comparing deliantra/server/lib/cf.pm (file contents):
Revision 1.533 by root, Thu Apr 29 07:59:17 2010 UTC vs.
Revision 1.542 by root, Wed May 5 09:05:03 2010 UTC

20# The authors can be reached via e-mail to <support@deliantra.net> 20# The authors can be reached via e-mail to <support@deliantra.net>
21# 21#
22 22
23package cf; 23package cf;
24 24
25use 5.10.0; 25use common::sense;
26use utf8;
27use strict qw(vars subs);
28 26
29use Symbol; 27use Symbol;
30use List::Util; 28use List::Util;
31use Socket; 29use Socket;
32use EV; 30use EV;
78# strictly for debugging 76# strictly for debugging
79$SIG{QUIT} = sub { Carp::cluck "SIGQUIT" }; 77$SIG{QUIT} = sub { Carp::cluck "SIGQUIT" };
80 78
81sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload 79sub WF_AUTOCANCEL () { 1 } # automatically cancel this watcher on reload
82 80
81our @ORIG_INC;
82
83our %COMMAND = (); 83our %COMMAND = ();
84our %COMMAND_TIME = (); 84our %COMMAND_TIME = ();
85 85
86our @EXTS = (); # list of extension package names 86our @EXTS = (); # list of extension package names
87our %EXTCMD = (); 87our %EXTCMD = ();
518=cut 518=cut
519 519
520sub sync_job(&) { 520sub sync_job(&) {
521 my ($job) = @_; 521 my ($job) = @_;
522 522
523 if ($Coro::current == $Coro::main) { 523 if (in_main) {
524 my $time = AE::time; 524 my $time = AE::time;
525 525
526 # this is the main coro, too bad, we have to block 526 # this is the main coro, too bad, we have to block
527 # till the operation succeeds, freezing the server :/ 527 # till the operation succeeds, freezing the server :/
528 528
529 LOG llevError, Carp::longmess "sync job";#d# 529 #LOG llevError, Carp::longmess "sync job";#d#
530 530
531 my $freeze_guard = freeze_mainloop; 531 my $freeze_guard = freeze_mainloop;
532 532
533 my $busy = 1; 533 my $busy = 1;
534 my @res; 534 my @res;
657within each server. 657within each server.
658 658
659=cut 659=cut
660 660
661sub db_table($) { 661sub db_table($) {
662 cf::error "db_get called from main context"
663 if $Coro::current == $Coro::main;
664
662 my ($name) = @_; 665 my ($name) = @_;
663 my $db = BDB::db_create $DB_ENV; 666 my $db = BDB::db_create $DB_ENV;
664 667
665 eval { 668 eval {
666 $db->set_flags (BDB::CHKSUM); 669 $db->set_flags (BDB::CHKSUM);
676} 679}
677 680
678our $DB; 681our $DB;
679 682
680sub db_init { 683sub db_init {
681 cf::sync_job {
682 $DB ||= db_table "db"; 684 $DB ||= db_table "db";
683 };
684} 685}
685 686
686sub db_get($$) { 687sub db_get($$) {
687 my $key = "$_[0]/$_[1]"; 688 my $key = "$_[0]/$_[1]";
688 689
689 cf::sync_job { 690 cf::error "db_get called from main context"
691 if $Coro::current == $Coro::main;
692
690 BDB::db_get $DB, undef, $key, my $data; 693 BDB::db_get $DB, undef, $key, my $data;
691 694
692 $! ? () 695 $! ? ()
693 : $data 696 : $data
694 }
695} 697}
696 698
697sub db_put($$$) { 699sub db_put($$$) {
698 BDB::dbreq_pri 4; 700 BDB::dbreq_pri 4;
699 BDB::db_put $DB, undef, "$_[0]/$_[1]", $_[2], 0, sub { }; 701 BDB::db_put $DB, undef, "$_[0]/$_[1]", $_[2], 0, sub { };
1421 1423
1422 $ext{meta} = { map { (split /=/, $_, 2)[0, 1] } split /\s+/, $1 } 1424 $ext{meta} = { map { (split /=/, $_, 2)[0, 1] } split /\s+/, $1 }
1423 if $source =~ /\A#!.*?perl.*?#\s*(.*)$/m; 1425 if $source =~ /\A#!.*?perl.*?#\s*(.*)$/m;
1424 1426
1425 $ext{source} = 1427 $ext{source} =
1426 "package $pkg; use 5.10.0; use strict 'vars', 'subs'; use utf8;\n" 1428 "package $pkg; use common::sense;\n"
1427 . "#line 1 \"$path\"\n{\n" 1429 . "#line 1 \"$path\"\n{\n"
1428 . $source 1430 . $source
1429 . "\n};\n1"; 1431 . "\n};\n1";
1430 1432
1431 $todo{$base} = \%ext; 1433 $todo{$base} = \%ext;
1846 1848
1847sub register { 1849sub register {
1848 my (undef, $regex, $prio) = @_; 1850 my (undef, $regex, $prio) = @_;
1849 my $pkg = caller; 1851 my $pkg = caller;
1850 1852
1851 no strict;
1852 push @{"$pkg\::ISA"}, __PACKAGE__; 1853 push @{"$pkg\::ISA"}, __PACKAGE__;
1853 1854
1854 $EXT_MAP{$pkg} = [$prio, qr<$regex>]; 1855 $EXT_MAP{$pkg} = [$prio, qr<$regex>];
1855} 1856}
1856 1857
2190} 2191}
2191 2192
2192sub find_sync { 2193sub find_sync {
2193 my ($path, $origin) = @_; 2194 my ($path, $origin) = @_;
2194 2195
2195 cf::sync_job { find $path, $origin } 2196 return cf::LOG cf::llevError | cf::logBacktrace, "do_find_sync"
2197 if $Coro::current == $Coro::main;
2198
2199 find $path, $origin
2196} 2200}
2197 2201
2198sub do_load_sync { 2202sub do_load_sync {
2199 my ($map) = @_; 2203 my ($map) = @_;
2200 2204
2201 cf::LOG cf::llevDebug | cf::logBacktrace, "do_load_sync" 2205 return cf::LOG cf::llevError | cf::logBacktrace, "do_load_sync"
2202 if $Coro::current == $Coro::main; 2206 if $Coro::current == $Coro::main;
2203 2207
2204 cf::sync_job { $map->load }; 2208 $map->load;
2205} 2209}
2206 2210
2207our %MAP_PREFETCH; 2211our %MAP_PREFETCH;
2208our $MAP_PREFETCHER = undef; 2212our $MAP_PREFETCHER = undef;
2209 2213
3268 decrease split destroy change_exp value msg lore send_msg)], 3272 decrease split destroy change_exp value msg lore send_msg)],
3269 ["cf::object::player" => qw(player)], 3273 ["cf::object::player" => qw(player)],
3270 ["cf::player" => qw(peaceful send_msg)], 3274 ["cf::player" => qw(peaceful send_msg)],
3271 ["cf::map" => qw(trigger)], 3275 ["cf::map" => qw(trigger)],
3272) { 3276) {
3273 no strict 'refs';
3274 my ($pkg, @funs) = @$_; 3277 my ($pkg, @funs) = @$_;
3275 *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) 3278 *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"})
3276 for @funs; 3279 for @funs;
3277} 3280}
3278 3281
3354=cut 3357=cut
3355 3358
3356############################################################################# 3359#############################################################################
3357# the server's init and main functions 3360# the server's init and main functions
3358 3361
3362# async inc loader. yay.
3363sub inc_loader {
3364 my $mod = $_[1];
3365
3366 if (in_main && !tick_inhibit) {
3367 Carp::cluck "ERROR: attempted synchronous perl module load ($mod)";
3368 } else {
3369 debug "loading perl module $mod\n";
3370 }
3371
3372 # 1. find real file
3373 for my $dir (@ORIG_INC) {
3374 ref $dir and next;
3375 0 <= Coro::AIO::aio_load "$dir/$mod", my $data
3376 or next;
3377
3378 $data = "#line 1 $dir/$mod\n$data";
3379
3380 open my $fh, "<", \$data or die;
3381
3382 return $fh;
3383 }
3384
3385 ()
3386}
3387
3388sub init_inc {
3389 # save original @INC
3390 @ORIG_INC = ($LIBDIR, @INC) unless @ORIG_INC;
3391
3392 # make sure we can do scalar-opens
3393 open my $dummy, "<", \my $dummy2;
3394
3395 # execute some stuff so perl load's some of the core modules
3396 /Ü/ =~ /ü/i;
3397 eval { &Storable::nstore_fd };
3398
3399 @INC = (\&inc_loader, @ORIG_INC); # @ORIG_INC is needed for DynaLoader, AutoLoad etc.
3400
3401 debug "module loading will be asynchronous from this point on.";
3402}
3403
3359sub load_facedata($) { 3404sub load_facedata($) {
3360 my ($path) = @_; 3405 my ($path) = @_;
3361 3406
3362 # HACK to clear player env face cache, we need some signal framework 3407 # HACK to clear player env face cache, we need some signal framework
3363 # for this (global event?) 3408 # for this (global event?)
3586 3631
3587sub main { 3632sub main {
3588 cf::init_globals; # initialise logging 3633 cf::init_globals; # initialise logging
3589 3634
3590 LOG llevInfo, "Welcome to Deliantra, v" . VERSION; 3635 LOG llevInfo, "Welcome to Deliantra, v" . VERSION;
3591 LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; 3636 LOG llevInfo, "Copyright (C) 2005-2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team.";
3592 LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; 3637 LOG llevInfo, "Copyright (C) 1994 Mark Wedel.";
3593 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; 3638 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen.";
3594 3639
3595 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority 3640 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority
3596 3641
3604 }; 3649 };
3605 3650
3606 evthread_start IO::AIO::poll_fileno; 3651 evthread_start IO::AIO::poll_fileno;
3607 3652
3608 cf::sync_job { 3653 cf::sync_job {
3654 init_inc;
3655
3609 cf::init_experience; 3656 cf::init_experience;
3610 cf::init_anim; 3657 cf::init_anim;
3611 cf::init_attackmess; 3658 cf::init_attackmess;
3612 cf::init_dynamic; 3659 cf::init_dynamic;
3613 3660
3638 }; 3685 };
3639 3686
3640 cf::object::thawer::errors_are_fatal 0; 3687 cf::object::thawer::errors_are_fatal 0;
3641 info "parse errors in files are no longer fatal from this point on.\n"; 3688 info "parse errors in files are no longer fatal from this point on.\n";
3642 3689
3690 my $free_main; $free_main = EV::idle sub {
3691 undef $free_main;
3692 undef &main; # free gobs of memory :)
3693 };
3694
3643 main_loop; 3695 goto &main_loop;
3644} 3696}
3645 3697
3646############################################################################# 3698#############################################################################
3647# initialisation and cleanup 3699# initialisation and cleanup
3648 3700
3747sub emergency_save() { 3799sub emergency_save() {
3748 my $freeze_guard = cf::freeze_mainloop; 3800 my $freeze_guard = cf::freeze_mainloop;
3749 3801
3750 info "emergency_perl_save: enter\n"; 3802 info "emergency_perl_save: enter\n";
3751 3803
3804 # this is a trade-off: we want to be very quick here, so
3805 # save all maps without fsync, and later call a global sync
3806 # (which in turn might be very very slow)
3807 local $USE_FSYNC = 0;
3808
3752 cf::sync_job { 3809 cf::sync_job {
3753 # this is a trade-off: we want to be very quick here, so 3810 cf::write_runtime_sync; # external watchdog should not bark
3754 # save all maps without fsync, and later call a global sync
3755 # (which in turn might be very very slow)
3756 local $USE_FSYNC = 0;
3757 3811
3758 # use a peculiar iteration method to avoid tripping on perl 3812 # use a peculiar iteration method to avoid tripping on perl
3759 # refcount bugs in for. also avoids problems with players 3813 # refcount bugs in for. also avoids problems with players
3760 # and maps saved/destroyed asynchronously. 3814 # and maps saved/destroyed asynchronously.
3761 info "emergency_perl_save: begin player save\n"; 3815 info "emergency_perl_save: begin player save\n";
3765 delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt 3819 delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt
3766 $pl->save; 3820 $pl->save;
3767 } 3821 }
3768 info "emergency_perl_save: end player save\n"; 3822 info "emergency_perl_save: end player save\n";
3769 3823
3824 cf::write_runtime_sync; # external watchdog should not bark
3825
3770 info "emergency_perl_save: begin map save\n"; 3826 info "emergency_perl_save: begin map save\n";
3771 for my $path (keys %cf::MAP) { 3827 for my $path (keys %cf::MAP) {
3772 my $map = $cf::MAP{$path} or next; 3828 my $map = $cf::MAP{$path} or next;
3773 $map->valid or next; 3829 $map->valid or next;
3774 $map->save; 3830 $map->save;
3775 } 3831 }
3776 info "emergency_perl_save: end map save\n"; 3832 info "emergency_perl_save: end map save\n";
3777 3833
3834 cf::write_runtime_sync; # external watchdog should not bark
3835
3778 info "emergency_perl_save: begin database checkpoint\n"; 3836 info "emergency_perl_save: begin database checkpoint\n";
3779 BDB::db_env_txn_checkpoint $DB_ENV; 3837 BDB::db_env_txn_checkpoint $DB_ENV;
3780 info "emergency_perl_save: end database checkpoint\n"; 3838 info "emergency_perl_save: end database checkpoint\n";
3781 3839
3782 info "emergency_perl_save: begin write uuid\n"; 3840 info "emergency_perl_save: begin write uuid\n";
3783 write_uuid_sync 1; 3841 write_uuid_sync 1;
3784 info "emergency_perl_save: end write uuid\n"; 3842 info "emergency_perl_save: end write uuid\n";
3785 };
3786 3843
3844 cf::write_runtime_sync; # external watchdog should not bark
3845
3846 trace "emergency_perl_save: syncing database to disk";
3847 BDB::db_env_txn_checkpoint $DB_ENV;
3848
3787 info "emergency_perl_save: starting sync()\n"; 3849 info "emergency_perl_save: starting sync\n";
3788 IO::AIO::aio_sync sub { 3850 IO::AIO::aio_sync sub {
3789 info "emergency_perl_save: finished sync()\n"; 3851 info "emergency_perl_save: finished sync\n";
3852 };
3853
3854 cf::write_runtime_sync; # external watchdog should not bark
3855
3856 trace "emergency_perl_save: flushing outstanding aio requests";
3857 while (IO::AIO::nreqs || BDB::nreqs) {
3858 Coro::EV::timer_once 0.01; # let the sync_job do it's thing
3859 }
3860
3861 cf::write_runtime_sync; # external watchdog should not bark
3790 }; 3862 };
3791 3863
3792 info "emergency_perl_save: leave\n"; 3864 info "emergency_perl_save: leave\n";
3793} 3865}
3794 3866
3795sub post_cleanup { 3867sub post_cleanup {
3796 my ($make_core) = @_; 3868 my ($make_core) = @_;
3869
3870 IO::AIO::flush;
3797 3871
3798 error Carp::longmess "post_cleanup backtrace" 3872 error Carp::longmess "post_cleanup backtrace"
3799 if $make_core; 3873 if $make_core;
3800 3874
3801 my $fh = pidfile; 3875 my $fh = pidfile;
3842 info "reloading..."; 3916 info "reloading...";
3843 3917
3844 trace "entering sync_job"; 3918 trace "entering sync_job";
3845 3919
3846 cf::sync_job { 3920 cf::sync_job {
3847 cf::write_runtime_sync; # external watchdog should not bark
3848 cf::emergency_save; 3921 cf::emergency_save;
3849 cf::write_runtime_sync; # external watchdog should not bark
3850
3851 trace "syncing database to disk";
3852 BDB::db_env_txn_checkpoint $DB_ENV;
3853
3854 # if anything goes wrong in here, we should simply crash as we already saved
3855
3856 trace "flushing outstanding aio requests";
3857 while (IO::AIO::nreqs || BDB::nreqs) {
3858 Coro::EV::timer_once 0.01; # let the sync_job do it's thing
3859 }
3860 3922
3861 trace "cancelling all extension coros"; 3923 trace "cancelling all extension coros";
3862 $_->cancel for values %EXT_CORO; 3924 $_->cancel for values %EXT_CORO;
3863 %EXT_CORO = (); 3925 %EXT_CORO = ();
3864 3926
3979 reload_perl; 4041 reload_perl;
3980 }; 4042 };
3981 } 4043 }
3982}; 4044};
3983 4045
3984unshift @INC, $LIBDIR; 4046#############################################################################
3985 4047
3986my $bug_warning = 0; 4048my $bug_warning = 0;
3987 4049
3988our @WAIT_FOR_TICK; 4050our @WAIT_FOR_TICK;
3989our @WAIT_FOR_TICK_BEGIN; 4051our @WAIT_FOR_TICK_BEGIN;
4056 $DB_ENV->set_flags (&BDB::LOG_AUTOREMOVE ) if BDB::VERSION v0, v4.7; 4118 $DB_ENV->set_flags (&BDB::LOG_AUTOREMOVE ) if BDB::VERSION v0, v4.7;
4057 $DB_ENV->log_set_config (&BDB::LOG_AUTO_REMOVE) if BDB::VERSION v4.7; 4119 $DB_ENV->log_set_config (&BDB::LOG_AUTO_REMOVE) if BDB::VERSION v4.7;
4058 $DB_ENV->set_timeout (30, BDB::SET_TXN_TIMEOUT); 4120 $DB_ENV->set_timeout (30, BDB::SET_TXN_TIMEOUT);
4059 $DB_ENV->set_timeout (30, BDB::SET_LOCK_TIMEOUT); 4121 $DB_ENV->set_timeout (30, BDB::SET_LOCK_TIMEOUT);
4060 4122
4061 (Coro::async { # async to keep sync_job from complaining
4062 cf::sync_job { 4123 cf::sync_job {
4063 eval { 4124 eval {
4064 BDB::db_env_open 4125 BDB::db_env_open
4065 $DB_ENV, 4126 $DB_ENV,
4066 $BDBDIR, 4127 $BDBDIR,
4067 BDB::INIT_LOCK | BDB::INIT_LOG | BDB::INIT_MPOOL | BDB::INIT_TXN 4128 BDB::INIT_LOCK | BDB::INIT_LOG | BDB::INIT_MPOOL | BDB::INIT_TXN
4068 | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE, 4129 | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE,
4069 0666; 4130 0666;
4070 4131
4071 cf::cleanup "db_env_open($BDBDIR): $!" if $!; 4132 cf::cleanup "db_env_open($BDBDIR): $!" if $!;
4072 };
4073
4074 cf::cleanup "db_env_open(db): $@" if $@;
4075 }; 4133 };
4076 })->join; 4134
4135 cf::cleanup "db_env_open(db): $@" if $@;
4136 };
4077 } 4137 }
4078 4138
4079 $BDB_DEADLOCK_WATCHER = EV::periodic 0, 3, 0, sub { 4139 $BDB_DEADLOCK_WATCHER = EV::periodic 0, 3, 0, sub {
4080 BDB::db_env_lock_detect $DB_ENV, 0, BDB::LOCK_DEFAULT, 0, sub { }; 4140 BDB::db_env_lock_detect $DB_ENV, 0, BDB::LOCK_DEFAULT, 0, sub { };
4081 }; 4141 };

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines