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.538 by root, Tue May 4 21:45:43 2010 UTC

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;
1449 1451
1450 my $active = eval $v->{source}; 1452 my $active = eval $v->{source};
1451 1453
1452 if (length $@) { 1454 if (length $@) {
1453 error "$v->{path}: $@\n"; 1455 error "$v->{path}: $@\n";
1456 undef $@; # work around perl 5.10.0 utf-8 caching bug
1454 1457
1455 cf::cleanup "mandatory extension '$k' failed to load, exiting." 1458 cf::cleanup "mandatory extension '$k' failed to load, exiting."
1456 if exists $v->{meta}{mandatory}; 1459 if exists $v->{meta}{mandatory};
1457 1460
1458 warn "$v->{base}: optional extension cannot be loaded, skipping.\n"; 1461 warn "$v->{base}: optional extension cannot be loaded, skipping.\n";
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
3747sub emergency_save() { 3753sub emergency_save() {
3748 my $freeze_guard = cf::freeze_mainloop; 3754 my $freeze_guard = cf::freeze_mainloop;
3749 3755
3750 info "emergency_perl_save: enter\n"; 3756 info "emergency_perl_save: enter\n";
3751 3757
3758 # this is a trade-off: we want to be very quick here, so
3759 # save all maps without fsync, and later call a global sync
3760 # (which in turn might be very very slow)
3761 local $USE_FSYNC = 0;
3762
3752 cf::sync_job { 3763 cf::sync_job {
3753 # this is a trade-off: we want to be very quick here, so 3764 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 3765
3758 # use a peculiar iteration method to avoid tripping on perl 3766 # use a peculiar iteration method to avoid tripping on perl
3759 # refcount bugs in for. also avoids problems with players 3767 # refcount bugs in for. also avoids problems with players
3760 # and maps saved/destroyed asynchronously. 3768 # and maps saved/destroyed asynchronously.
3761 info "emergency_perl_save: begin player save\n"; 3769 info "emergency_perl_save: begin player save\n";
3765 delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt 3773 delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt
3766 $pl->save; 3774 $pl->save;
3767 } 3775 }
3768 info "emergency_perl_save: end player save\n"; 3776 info "emergency_perl_save: end player save\n";
3769 3777
3778 cf::write_runtime_sync; # external watchdog should not bark
3779
3770 info "emergency_perl_save: begin map save\n"; 3780 info "emergency_perl_save: begin map save\n";
3771 for my $path (keys %cf::MAP) { 3781 for my $path (keys %cf::MAP) {
3772 my $map = $cf::MAP{$path} or next; 3782 my $map = $cf::MAP{$path} or next;
3773 $map->valid or next; 3783 $map->valid or next;
3774 $map->save; 3784 $map->save;
3775 } 3785 }
3776 info "emergency_perl_save: end map save\n"; 3786 info "emergency_perl_save: end map save\n";
3777 3787
3788 cf::write_runtime_sync; # external watchdog should not bark
3789
3778 info "emergency_perl_save: begin database checkpoint\n"; 3790 info "emergency_perl_save: begin database checkpoint\n";
3779 BDB::db_env_txn_checkpoint $DB_ENV; 3791 BDB::db_env_txn_checkpoint $DB_ENV;
3780 info "emergency_perl_save: end database checkpoint\n"; 3792 info "emergency_perl_save: end database checkpoint\n";
3781 3793
3782 info "emergency_perl_save: begin write uuid\n"; 3794 info "emergency_perl_save: begin write uuid\n";
3783 write_uuid_sync 1; 3795 write_uuid_sync 1;
3784 info "emergency_perl_save: end write uuid\n"; 3796 info "emergency_perl_save: end write uuid\n";
3785 };
3786 3797
3798 cf::write_runtime_sync; # external watchdog should not bark
3799
3800 trace "emergency_perl_save: syncing database to disk";
3801 BDB::db_env_txn_checkpoint $DB_ENV;
3802
3787 info "emergency_perl_save: starting sync()\n"; 3803 info "emergency_perl_save: starting sync\n";
3788 IO::AIO::aio_sync sub { 3804 IO::AIO::aio_sync sub {
3789 info "emergency_perl_save: finished sync()\n"; 3805 info "emergency_perl_save: finished sync\n";
3806 };
3807
3808 cf::write_runtime_sync; # external watchdog should not bark
3809
3810 trace "emergency_perl_save: flushing outstanding aio requests";
3811 while (IO::AIO::nreqs || BDB::nreqs) {
3812 Coro::EV::timer_once 0.01; # let the sync_job do it's thing
3813 }
3814
3815 cf::write_runtime_sync; # external watchdog should not bark
3790 }; 3816 };
3791 3817
3792 info "emergency_perl_save: leave\n"; 3818 info "emergency_perl_save: leave\n";
3793} 3819}
3794 3820
3795sub post_cleanup { 3821sub post_cleanup {
3796 my ($make_core) = @_; 3822 my ($make_core) = @_;
3823
3824 IO::AIO::flush;
3797 3825
3798 error Carp::longmess "post_cleanup backtrace" 3826 error Carp::longmess "post_cleanup backtrace"
3799 if $make_core; 3827 if $make_core;
3800 3828
3801 my $fh = pidfile; 3829 my $fh = pidfile;
3842 info "reloading..."; 3870 info "reloading...";
3843 3871
3844 trace "entering sync_job"; 3872 trace "entering sync_job";
3845 3873
3846 cf::sync_job { 3874 cf::sync_job {
3847 cf::write_runtime_sync; # external watchdog should not bark
3848 cf::emergency_save; 3875 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 3876
3861 trace "cancelling all extension coros"; 3877 trace "cancelling all extension coros";
3862 $_->cancel for values %EXT_CORO; 3878 $_->cancel for values %EXT_CORO;
3863 %EXT_CORO = (); 3879 %EXT_CORO = ();
3864 3880
4056 $DB_ENV->set_flags (&BDB::LOG_AUTOREMOVE ) if BDB::VERSION v0, v4.7; 4072 $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; 4073 $DB_ENV->log_set_config (&BDB::LOG_AUTO_REMOVE) if BDB::VERSION v4.7;
4058 $DB_ENV->set_timeout (30, BDB::SET_TXN_TIMEOUT); 4074 $DB_ENV->set_timeout (30, BDB::SET_TXN_TIMEOUT);
4059 $DB_ENV->set_timeout (30, BDB::SET_LOCK_TIMEOUT); 4075 $DB_ENV->set_timeout (30, BDB::SET_LOCK_TIMEOUT);
4060 4076
4061 (Coro::async { # async to keep sync_job from complaining
4062 cf::sync_job { 4077 cf::sync_job {
4063 eval { 4078 eval {
4064 BDB::db_env_open 4079 BDB::db_env_open
4065 $DB_ENV, 4080 $DB_ENV,
4066 $BDBDIR, 4081 $BDBDIR,
4067 BDB::INIT_LOCK | BDB::INIT_LOG | BDB::INIT_MPOOL | BDB::INIT_TXN 4082 BDB::INIT_LOCK | BDB::INIT_LOG | BDB::INIT_MPOOL | BDB::INIT_TXN
4068 | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE, 4083 | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE,
4069 0666; 4084 0666;
4070 4085
4071 cf::cleanup "db_env_open($BDBDIR): $!" if $!; 4086 cf::cleanup "db_env_open($BDBDIR): $!" if $!;
4072 };
4073
4074 cf::cleanup "db_env_open(db): $@" if $@;
4075 }; 4087 };
4076 })->join; 4088
4089 cf::cleanup "db_env_open(db): $@" if $@;
4090 };
4077 } 4091 }
4078 4092
4079 $BDB_DEADLOCK_WATCHER = EV::periodic 0, 3, 0, sub { 4093 $BDB_DEADLOCK_WATCHER = EV::periodic 0, 3, 0, sub {
4080 BDB::db_env_lock_detect $DB_ENV, 0, BDB::LOCK_DEFAULT, 0, sub { }; 4094 BDB::db_env_lock_detect $DB_ENV, 0, BDB::LOCK_DEFAULT, 0, sub { };
4081 }; 4095 };

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines