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.532 by root, Thu Apr 29 07:32:34 2010 UTC vs.
Revision 1.536 by root, Thu Apr 29 08:26:38 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 { };
2190} 2192}
2191 2193
2192sub find_sync { 2194sub find_sync {
2193 my ($path, $origin) = @_; 2195 my ($path, $origin) = @_;
2194 2196
2195 cf::sync_job { find $path, $origin } 2197 return cf::LOG cf::llevError | cf::logBacktrace, "do_find_sync"
2198 if $Coro::current == $Coro::main;
2199
2200 find $path, $origin
2196} 2201}
2197 2202
2198sub do_load_sync { 2203sub do_load_sync {
2199 my ($map) = @_; 2204 my ($map) = @_;
2200 2205
2201 cf::LOG cf::llevDebug | cf::logBacktrace, "do_load_sync" 2206 return cf::LOG cf::llevError | cf::logBacktrace, "do_load_sync"
2202 if $Coro::current == $Coro::main; 2207 if $Coro::current == $Coro::main;
2203 2208
2204 cf::sync_job { $map->load }; 2209 $map->load;
2205} 2210}
2206 2211
2207our %MAP_PREFETCH; 2212our %MAP_PREFETCH;
2208our $MAP_PREFETCHER = undef; 2213our $MAP_PREFETCHER = undef;
2209 2214
3747sub emergency_save() { 3752sub emergency_save() {
3748 my $freeze_guard = cf::freeze_mainloop; 3753 my $freeze_guard = cf::freeze_mainloop;
3749 3754
3750 info "emergency_perl_save: enter\n"; 3755 info "emergency_perl_save: enter\n";
3751 3756
3757 # this is a trade-off: we want to be very quick here, so
3758 # save all maps without fsync, and later call a global sync
3759 # (which in turn might be very very slow)
3760 local $USE_FSYNC = 0;
3761
3752 cf::sync_job { 3762 cf::sync_job {
3753 # this is a trade-off: we want to be very quick here, so 3763 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 3764
3758 # use a peculiar iteration method to avoid tripping on perl 3765 # use a peculiar iteration method to avoid tripping on perl
3759 # refcount bugs in for. also avoids problems with players 3766 # refcount bugs in for. also avoids problems with players
3760 # and maps saved/destroyed asynchronously. 3767 # and maps saved/destroyed asynchronously.
3761 info "emergency_perl_save: begin player save\n"; 3768 info "emergency_perl_save: begin player save\n";
3765 delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt 3772 delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt
3766 $pl->save; 3773 $pl->save;
3767 } 3774 }
3768 info "emergency_perl_save: end player save\n"; 3775 info "emergency_perl_save: end player save\n";
3769 3776
3777 cf::write_runtime_sync; # external watchdog should not bark
3778
3770 info "emergency_perl_save: begin map save\n"; 3779 info "emergency_perl_save: begin map save\n";
3771 for my $path (keys %cf::MAP) { 3780 for my $path (keys %cf::MAP) {
3772 my $map = $cf::MAP{$path} or next; 3781 my $map = $cf::MAP{$path} or next;
3773 $map->valid or next; 3782 $map->valid or next;
3774 $map->save; 3783 $map->save;
3775 } 3784 }
3776 info "emergency_perl_save: end map save\n"; 3785 info "emergency_perl_save: end map save\n";
3777 3786
3787 cf::write_runtime_sync; # external watchdog should not bark
3788
3778 info "emergency_perl_save: begin database checkpoint\n"; 3789 info "emergency_perl_save: begin database checkpoint\n";
3779 BDB::db_env_txn_checkpoint $DB_ENV; 3790 BDB::db_env_txn_checkpoint $DB_ENV;
3780 info "emergency_perl_save: end database checkpoint\n"; 3791 info "emergency_perl_save: end database checkpoint\n";
3781 3792
3782 info "emergency_perl_save: begin write uuid\n"; 3793 info "emergency_perl_save: begin write uuid\n";
3783 write_uuid_sync 1; 3794 write_uuid_sync 1;
3784 info "emergency_perl_save: end write uuid\n"; 3795 info "emergency_perl_save: end write uuid\n";
3785 };
3786 3796
3797 cf::write_runtime_sync; # external watchdog should not bark
3798
3799 trace "emergency_perl_save: syncing database to disk";
3800 BDB::db_env_txn_checkpoint $DB_ENV;
3801
3787 info "emergency_perl_save: starting sync()\n"; 3802 info "emergency_perl_save: starting sync\n";
3788 IO::AIO::aio_sync sub { 3803 IO::AIO::aio_sync sub {
3789 info "emergency_perl_save: finished sync()\n"; 3804 info "emergency_perl_save: finished sync\n";
3805 };
3806
3807 cf::write_runtime_sync; # external watchdog should not bark
3808
3809 trace "emergency_perl_save: flushing outstanding aio requests";
3810 while (IO::AIO::nreqs || BDB::nreqs) {
3811 Coro::EV::timer_once 0.01; # let the sync_job do it's thing
3812 }
3813
3814 cf::write_runtime_sync; # external watchdog should not bark
3790 }; 3815 };
3791 3816
3792 info "emergency_perl_save: leave\n"; 3817 info "emergency_perl_save: leave\n";
3793} 3818}
3794 3819
3795sub post_cleanup { 3820sub post_cleanup {
3796 my ($make_core) = @_; 3821 my ($make_core) = @_;
3822
3823 IO::AIO::flush;
3824
3825 IO::AIO::flush;
3797 3826
3798 error Carp::longmess "post_cleanup backtrace" 3827 error Carp::longmess "post_cleanup backtrace"
3799 if $make_core; 3828 if $make_core;
3800 3829
3801 my $fh = pidfile; 3830 my $fh = pidfile;
3842 info "reloading..."; 3871 info "reloading...";
3843 3872
3844 trace "entering sync_job"; 3873 trace "entering sync_job";
3845 3874
3846 cf::sync_job { 3875 cf::sync_job {
3847 cf::write_runtime_sync; # external watchdog should not bark
3848 cf::emergency_save; 3876 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 3877
3861 trace "cancelling all extension coros"; 3878 trace "cancelling all extension coros";
3862 $_->cancel for values %EXT_CORO; 3879 $_->cancel for values %EXT_CORO;
3863 %EXT_CORO = (); 3880 %EXT_CORO = ();
3864 3881

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines