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.545 by root, Thu May 6 22:35:41 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 = ();
124our $BDB_DEADLOCK_WATCHER; 124our $BDB_DEADLOCK_WATCHER;
125our $BDB_CHECKPOINT_WATCHER; 125our $BDB_CHECKPOINT_WATCHER;
126our $BDB_TRICKLE_WATCHER; 126our $BDB_TRICKLE_WATCHER;
127our $DB_ENV; 127our $DB_ENV;
128 128
129our @EXTRA_MODULES = qw(pod match mapscript); 129our @EXTRA_MODULES = qw(pod match mapscript incloader);
130 130
131our %CFG; 131our %CFG;
132 132
133our $UPTIME; $UPTIME ||= time; 133our $UPTIME; $UPTIME ||= time;
134our $RUNTIME; 134our $RUNTIME;
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
1868} 1869}
1869 1870
1870sub normalise { 1871sub normalise {
1871 my ($path, $base) = @_; 1872 my ($path, $base) = @_;
1872 1873
1873 $path = "$path"; # make sure its a string 1874 $path = "$path"; # make sure it's a string
1874 1875
1875 $path =~ s/\.map$//; 1876 $path =~ s/\.map$//;
1876 1877
1877 # map plan: 1878 # map plan:
1878 # 1879 #
1917 $self->init; # pass $1 etc. 1918 $self->init; # pass $1 etc.
1918 return $self; 1919 return $self;
1919 } 1920 }
1920 } 1921 }
1921 1922
1922 Carp::cluck "unable to resolve path '$path' (base '$base')."; 1923 Carp::cluck "unable to resolve path '$path' (base '$base')";
1923 () 1924 ()
1924} 1925}
1925 1926
1926sub init { 1927sub init {
1927 my ($self) = @_; 1928 my ($self) = @_;
2049} 2050}
2050 2051
2051sub find; 2052sub find;
2052sub find { 2053sub find {
2053 my ($path, $origin) = @_; 2054 my ($path, $origin) = @_;
2055
2056 cf::cede_to_tick;
2054 2057
2055 $path = normalise $path, $origin && $origin->path; 2058 $path = normalise $path, $origin && $origin->path;
2056 2059
2057 my $guard1 = cf::lock_acquire "map_data:$path";#d#remove 2060 my $guard1 = cf::lock_acquire "map_data:$path";#d#remove
2058 my $guard2 = cf::lock_acquire "map_find:$path"; 2061 my $guard2 = cf::lock_acquire "map_find:$path";
2190} 2193}
2191 2194
2192sub find_sync { 2195sub find_sync {
2193 my ($path, $origin) = @_; 2196 my ($path, $origin) = @_;
2194 2197
2195 cf::sync_job { find $path, $origin } 2198 return cf::LOG cf::llevError | cf::logBacktrace, "do_find_sync"
2199 if $Coro::current == $Coro::main;
2200
2201 find $path, $origin
2196} 2202}
2197 2203
2198sub do_load_sync { 2204sub do_load_sync {
2199 my ($map) = @_; 2205 my ($map) = @_;
2200 2206
2201 cf::LOG cf::llevDebug | cf::logBacktrace, "do_load_sync" 2207 return cf::LOG cf::llevError | cf::logBacktrace, "do_load_sync"
2202 if $Coro::current == $Coro::main; 2208 if $Coro::current == $Coro::main;
2203 2209
2204 cf::sync_job { $map->load }; 2210 $map->load;
2205} 2211}
2206 2212
2207our %MAP_PREFETCH; 2213our %MAP_PREFETCH;
2208our $MAP_PREFETCHER = undef; 2214our $MAP_PREFETCHER = undef;
2209 2215
3268 decrease split destroy change_exp value msg lore send_msg)], 3274 decrease split destroy change_exp value msg lore send_msg)],
3269 ["cf::object::player" => qw(player)], 3275 ["cf::object::player" => qw(player)],
3270 ["cf::player" => qw(peaceful send_msg)], 3276 ["cf::player" => qw(peaceful send_msg)],
3271 ["cf::map" => qw(trigger)], 3277 ["cf::map" => qw(trigger)],
3272) { 3278) {
3273 no strict 'refs';
3274 my ($pkg, @funs) = @$_; 3279 my ($pkg, @funs) = @$_;
3275 *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) 3280 *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"})
3276 for @funs; 3281 for @funs;
3277} 3282}
3278 3283
3520} 3525}
3521 3526
3522sub reload_resources { 3527sub reload_resources {
3523 trace "reloading resource files...\n"; 3528 trace "reloading resource files...\n";
3524 3529
3530 reload_exp_table;
3531 reload_materials;
3525 reload_facedata; 3532 reload_facedata;
3526 reload_sound; 3533 reload_sound;
3527 reload_archetypes; 3534 reload_archetypes;
3528 reload_regions; 3535 reload_regions;
3529 reload_treasures; 3536 reload_treasures;
3586 3593
3587sub main { 3594sub main {
3588 cf::init_globals; # initialise logging 3595 cf::init_globals; # initialise logging
3589 3596
3590 LOG llevInfo, "Welcome to Deliantra, v" . VERSION; 3597 LOG llevInfo, "Welcome to Deliantra, v" . VERSION;
3591 LOG llevInfo, "Copyright (C) 2005-2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team."; 3598 LOG llevInfo, "Copyright (C) 2005-2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team.";
3592 LOG llevInfo, "Copyright (C) 1994 Mark Wedel."; 3599 LOG llevInfo, "Copyright (C) 1994 Mark Wedel.";
3593 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen."; 3600 LOG llevInfo, "Copyright (C) 1992 Frank Tore Johansen.";
3594 3601
3595 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority 3602 $Coro::current->prio (Coro::PRIO_MAX); # give the main loop max. priority
3596 3603
3604 }; 3611 };
3605 3612
3606 evthread_start IO::AIO::poll_fileno; 3613 evthread_start IO::AIO::poll_fileno;
3607 3614
3608 cf::sync_job { 3615 cf::sync_job {
3609 cf::init_experience; 3616 cf::incloader::init ();
3617
3610 cf::init_anim; 3618 cf::init_anim;
3611 cf::init_attackmess; 3619 cf::init_attackmess;
3612 cf::init_dynamic; 3620 cf::init_dynamic;
3613 3621
3614 cf::load_settings; 3622 cf::load_settings;
3615 cf::load_materials;
3616 3623
3617 reload_resources; 3624 reload_resources;
3618 reload_config; 3625 reload_config;
3619 db_init; 3626 db_init;
3620 3627
3638 }; 3645 };
3639 3646
3640 cf::object::thawer::errors_are_fatal 0; 3647 cf::object::thawer::errors_are_fatal 0;
3641 info "parse errors in files are no longer fatal from this point on.\n"; 3648 info "parse errors in files are no longer fatal from this point on.\n";
3642 3649
3650 my $free_main; $free_main = EV::idle sub {
3651 undef $free_main;
3652 undef &main; # free gobs of memory :)
3653 };
3654
3643 main_loop; 3655 goto &main_loop;
3644} 3656}
3645 3657
3646############################################################################# 3658#############################################################################
3647# initialisation and cleanup 3659# initialisation and cleanup
3648 3660
3747sub emergency_save() { 3759sub emergency_save() {
3748 my $freeze_guard = cf::freeze_mainloop; 3760 my $freeze_guard = cf::freeze_mainloop;
3749 3761
3750 info "emergency_perl_save: enter\n"; 3762 info "emergency_perl_save: enter\n";
3751 3763
3764 # this is a trade-off: we want to be very quick here, so
3765 # save all maps without fsync, and later call a global sync
3766 # (which in turn might be very very slow)
3767 local $USE_FSYNC = 0;
3768
3752 cf::sync_job { 3769 cf::sync_job {
3753 # this is a trade-off: we want to be very quick here, so 3770 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 3771
3758 # use a peculiar iteration method to avoid tripping on perl 3772 # use a peculiar iteration method to avoid tripping on perl
3759 # refcount bugs in for. also avoids problems with players 3773 # refcount bugs in for. also avoids problems with players
3760 # and maps saved/destroyed asynchronously. 3774 # and maps saved/destroyed asynchronously.
3761 info "emergency_perl_save: begin player save\n"; 3775 info "emergency_perl_save: begin player save\n";
3765 delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt 3779 delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt
3766 $pl->save; 3780 $pl->save;
3767 } 3781 }
3768 info "emergency_perl_save: end player save\n"; 3782 info "emergency_perl_save: end player save\n";
3769 3783
3784 cf::write_runtime_sync; # external watchdog should not bark
3785
3770 info "emergency_perl_save: begin map save\n"; 3786 info "emergency_perl_save: begin map save\n";
3771 for my $path (keys %cf::MAP) { 3787 for my $path (keys %cf::MAP) {
3772 my $map = $cf::MAP{$path} or next; 3788 my $map = $cf::MAP{$path} or next;
3773 $map->valid or next; 3789 $map->valid or next;
3774 $map->save; 3790 $map->save;
3775 } 3791 }
3776 info "emergency_perl_save: end map save\n"; 3792 info "emergency_perl_save: end map save\n";
3777 3793
3794 cf::write_runtime_sync; # external watchdog should not bark
3795
3778 info "emergency_perl_save: begin database checkpoint\n"; 3796 info "emergency_perl_save: begin database checkpoint\n";
3779 BDB::db_env_txn_checkpoint $DB_ENV; 3797 BDB::db_env_txn_checkpoint $DB_ENV;
3780 info "emergency_perl_save: end database checkpoint\n"; 3798 info "emergency_perl_save: end database checkpoint\n";
3781 3799
3782 info "emergency_perl_save: begin write uuid\n"; 3800 info "emergency_perl_save: begin write uuid\n";
3783 write_uuid_sync 1; 3801 write_uuid_sync 1;
3784 info "emergency_perl_save: end write uuid\n"; 3802 info "emergency_perl_save: end write uuid\n";
3785 };
3786 3803
3804 cf::write_runtime_sync; # external watchdog should not bark
3805
3806 trace "emergency_perl_save: syncing database to disk";
3807 BDB::db_env_txn_checkpoint $DB_ENV;
3808
3787 info "emergency_perl_save: starting sync()\n"; 3809 info "emergency_perl_save: starting sync\n";
3788 IO::AIO::aio_sync sub { 3810 IO::AIO::aio_sync sub {
3789 info "emergency_perl_save: finished sync()\n"; 3811 info "emergency_perl_save: finished sync\n";
3812 };
3813
3814 cf::write_runtime_sync; # external watchdog should not bark
3815
3816 trace "emergency_perl_save: flushing outstanding aio requests";
3817 while (IO::AIO::nreqs || BDB::nreqs) {
3818 Coro::EV::timer_once 0.01; # let the sync_job do it's thing
3819 }
3820
3821 cf::write_runtime_sync; # external watchdog should not bark
3790 }; 3822 };
3791 3823
3792 info "emergency_perl_save: leave\n"; 3824 info "emergency_perl_save: leave\n";
3793} 3825}
3794 3826
3795sub post_cleanup { 3827sub post_cleanup {
3796 my ($make_core) = @_; 3828 my ($make_core) = @_;
3829
3830 IO::AIO::flush;
3797 3831
3798 error Carp::longmess "post_cleanup backtrace" 3832 error Carp::longmess "post_cleanup backtrace"
3799 if $make_core; 3833 if $make_core;
3800 3834
3801 my $fh = pidfile; 3835 my $fh = pidfile;
3827 } 3861 }
3828} 3862}
3829 3863
3830sub do_reload_perl() { 3864sub do_reload_perl() {
3831 # can/must only be called in main 3865 # can/must only be called in main
3832 if (in_main) { 3866 unless (in_main) {
3833 error "can only reload from main coroutine"; 3867 error "can only reload from main coroutine";
3834 return; 3868 return;
3835 } 3869 }
3836 3870
3837 return if $RELOAD++; 3871 return if $RELOAD++;
3838 3872
3839 my $t1 = AE::time; 3873 my $t1 = AE::time;
3840 3874
3841 while ($RELOAD) { 3875 while ($RELOAD) {
3876 cf::get_slot 0.1, -1, "reload_perl";
3842 info "reloading..."; 3877 info "reloading...";
3843 3878
3844 trace "entering sync_job"; 3879 trace "entering sync_job";
3845 3880
3846 cf::sync_job { 3881 cf::sync_job {
3847 cf::write_runtime_sync; # external watchdog should not bark
3848 cf::emergency_save; 3882 #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 3883
3861 trace "cancelling all extension coros"; 3884 trace "cancelling all extension coros";
3862 $_->cancel for values %EXT_CORO; 3885 $_->cancel for values %EXT_CORO;
3863 %EXT_CORO = (); 3886 %EXT_CORO = ();
3864 3887
3979 reload_perl; 4002 reload_perl;
3980 }; 4003 };
3981 } 4004 }
3982}; 4005};
3983 4006
3984unshift @INC, $LIBDIR; 4007#############################################################################
3985 4008
3986my $bug_warning = 0; 4009my $bug_warning = 0;
3987 4010
3988our @WAIT_FOR_TICK; 4011our @WAIT_FOR_TICK;
3989our @WAIT_FOR_TICK_BEGIN; 4012our @WAIT_FOR_TICK_BEGIN;
4056 $DB_ENV->set_flags (&BDB::LOG_AUTOREMOVE ) if BDB::VERSION v0, v4.7; 4079 $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; 4080 $DB_ENV->log_set_config (&BDB::LOG_AUTO_REMOVE) if BDB::VERSION v4.7;
4058 $DB_ENV->set_timeout (30, BDB::SET_TXN_TIMEOUT); 4081 $DB_ENV->set_timeout (30, BDB::SET_TXN_TIMEOUT);
4059 $DB_ENV->set_timeout (30, BDB::SET_LOCK_TIMEOUT); 4082 $DB_ENV->set_timeout (30, BDB::SET_LOCK_TIMEOUT);
4060 4083
4061 (Coro::async { # async to keep sync_job from complaining
4062 cf::sync_job { 4084 cf::sync_job {
4063 eval { 4085 eval {
4064 BDB::db_env_open 4086 BDB::db_env_open
4065 $DB_ENV, 4087 $DB_ENV,
4066 $BDBDIR, 4088 $BDBDIR,
4067 BDB::INIT_LOCK | BDB::INIT_LOG | BDB::INIT_MPOOL | BDB::INIT_TXN 4089 BDB::INIT_LOCK | BDB::INIT_LOG | BDB::INIT_MPOOL | BDB::INIT_TXN
4068 | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE, 4090 | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE,
4069 0666; 4091 0666;
4070 4092
4071 cf::cleanup "db_env_open($BDBDIR): $!" if $!; 4093 cf::cleanup "db_env_open($BDBDIR): $!" if $!;
4072 };
4073
4074 cf::cleanup "db_env_open(db): $@" if $@;
4075 }; 4094 };
4076 })->join; 4095
4096 cf::cleanup "db_env_open(db): $@" if $@;
4097 };
4077 } 4098 }
4078 4099
4079 $BDB_DEADLOCK_WATCHER = EV::periodic 0, 3, 0, sub { 4100 $BDB_DEADLOCK_WATCHER = EV::periodic 0, 3, 0, sub {
4080 BDB::db_env_lock_detect $DB_ENV, 0, BDB::LOCK_DEFAULT, 0, sub { }; 4101 BDB::db_env_lock_detect $DB_ENV, 0, BDB::LOCK_DEFAULT, 0, sub { };
4081 }; 4102 };

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines