… | |
… | |
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 | |
23 | package cf; |
23 | package cf; |
24 | |
24 | |
25 | use 5.10.0; |
25 | use common::sense; |
26 | use utf8; |
|
|
27 | use strict qw(vars subs); |
|
|
28 | |
26 | |
29 | use Symbol; |
27 | use Symbol; |
30 | use List::Util; |
28 | use List::Util; |
31 | use Socket; |
29 | use Socket; |
32 | use EV; |
30 | use EV; |
… | |
… | |
518 | =cut |
516 | =cut |
519 | |
517 | |
520 | sub sync_job(&) { |
518 | sub sync_job(&) { |
521 | my ($job) = @_; |
519 | my ($job) = @_; |
522 | |
520 | |
523 | if ($Coro::current == $Coro::main) { |
521 | if (in_main) { |
524 | my $time = AE::time; |
522 | my $time = AE::time; |
525 | |
523 | |
526 | # this is the main coro, too bad, we have to block |
524 | # this is the main coro, too bad, we have to block |
527 | # till the operation succeeds, freezing the server :/ |
525 | # till the operation succeeds, freezing the server :/ |
528 | |
526 | |
529 | LOG llevError, Carp::longmess "sync job";#d# |
527 | #LOG llevError, Carp::longmess "sync job";#d# |
530 | |
528 | |
531 | my $freeze_guard = freeze_mainloop; |
529 | my $freeze_guard = freeze_mainloop; |
532 | |
530 | |
533 | my $busy = 1; |
531 | my $busy = 1; |
534 | my @res; |
532 | my @res; |
… | |
… | |
657 | within each server. |
655 | within each server. |
658 | |
656 | |
659 | =cut |
657 | =cut |
660 | |
658 | |
661 | sub db_table($) { |
659 | sub db_table($) { |
|
|
660 | cf::error "db_get called from main context" |
|
|
661 | if $Coro::current == $Coro::main; |
|
|
662 | |
662 | my ($name) = @_; |
663 | my ($name) = @_; |
663 | my $db = BDB::db_create $DB_ENV; |
664 | my $db = BDB::db_create $DB_ENV; |
664 | |
665 | |
665 | eval { |
666 | eval { |
666 | $db->set_flags (BDB::CHKSUM); |
667 | $db->set_flags (BDB::CHKSUM); |
… | |
… | |
676 | } |
677 | } |
677 | |
678 | |
678 | our $DB; |
679 | our $DB; |
679 | |
680 | |
680 | sub db_init { |
681 | sub db_init { |
681 | cf::sync_job { |
|
|
682 | $DB ||= db_table "db"; |
682 | $DB ||= db_table "db"; |
683 | }; |
|
|
684 | } |
683 | } |
685 | |
684 | |
686 | sub db_get($$) { |
685 | sub db_get($$) { |
687 | my $key = "$_[0]/$_[1]"; |
686 | my $key = "$_[0]/$_[1]"; |
688 | |
687 | |
689 | cf::sync_job { |
688 | cf::error "db_get called from main context" |
|
|
689 | if $Coro::current == $Coro::main; |
|
|
690 | |
690 | BDB::db_get $DB, undef, $key, my $data; |
691 | BDB::db_get $DB, undef, $key, my $data; |
691 | |
692 | |
692 | $! ? () |
693 | $! ? () |
693 | : $data |
694 | : $data |
694 | } |
|
|
695 | } |
695 | } |
696 | |
696 | |
697 | sub db_put($$$) { |
697 | sub db_put($$$) { |
698 | BDB::dbreq_pri 4; |
698 | BDB::dbreq_pri 4; |
699 | BDB::db_put $DB, undef, "$_[0]/$_[1]", $_[2], 0, sub { }; |
699 | BDB::db_put $DB, undef, "$_[0]/$_[1]", $_[2], 0, sub { }; |
… | |
… | |
1421 | |
1421 | |
1422 | $ext{meta} = { map { (split /=/, $_, 2)[0, 1] } split /\s+/, $1 } |
1422 | $ext{meta} = { map { (split /=/, $_, 2)[0, 1] } split /\s+/, $1 } |
1423 | if $source =~ /\A#!.*?perl.*?#\s*(.*)$/m; |
1423 | if $source =~ /\A#!.*?perl.*?#\s*(.*)$/m; |
1424 | |
1424 | |
1425 | $ext{source} = |
1425 | $ext{source} = |
1426 | "package $pkg; use 5.10.0; use strict 'vars', 'subs'; use utf8;\n" |
1426 | "package $pkg; use common::sense;\n" |
1427 | . "#line 1 \"$path\"\n{\n" |
1427 | . "#line 1 \"$path\"\n{\n" |
1428 | . $source |
1428 | . $source |
1429 | . "\n};\n1"; |
1429 | . "\n};\n1"; |
1430 | |
1430 | |
1431 | $todo{$base} = \%ext; |
1431 | $todo{$base} = \%ext; |
… | |
… | |
1449 | |
1449 | |
1450 | my $active = eval $v->{source}; |
1450 | my $active = eval $v->{source}; |
1451 | |
1451 | |
1452 | if (length $@) { |
1452 | if (length $@) { |
1453 | error "$v->{path}: $@\n"; |
1453 | error "$v->{path}: $@\n"; |
|
|
1454 | undef $@; # work around perl 5.10.0 utf-8 caching bug |
1454 | |
1455 | |
1455 | cf::cleanup "mandatory extension '$k' failed to load, exiting." |
1456 | cf::cleanup "mandatory extension '$k' failed to load, exiting." |
1456 | if exists $v->{meta}{mandatory}; |
1457 | if exists $v->{meta}{mandatory}; |
1457 | |
1458 | |
1458 | warn "$v->{base}: optional extension cannot be loaded, skipping.\n"; |
1459 | warn "$v->{base}: optional extension cannot be loaded, skipping.\n"; |
… | |
… | |
1846 | |
1847 | |
1847 | sub register { |
1848 | sub register { |
1848 | my (undef, $regex, $prio) = @_; |
1849 | my (undef, $regex, $prio) = @_; |
1849 | my $pkg = caller; |
1850 | my $pkg = caller; |
1850 | |
1851 | |
1851 | no strict; |
|
|
1852 | push @{"$pkg\::ISA"}, __PACKAGE__; |
1852 | push @{"$pkg\::ISA"}, __PACKAGE__; |
1853 | |
1853 | |
1854 | $EXT_MAP{$pkg} = [$prio, qr<$regex>]; |
1854 | $EXT_MAP{$pkg} = [$prio, qr<$regex>]; |
1855 | } |
1855 | } |
1856 | |
1856 | |
… | |
… | |
2190 | } |
2190 | } |
2191 | |
2191 | |
2192 | sub find_sync { |
2192 | sub find_sync { |
2193 | my ($path, $origin) = @_; |
2193 | my ($path, $origin) = @_; |
2194 | |
2194 | |
2195 | cf::sync_job { find $path, $origin } |
2195 | return cf::LOG cf::llevError | cf::logBacktrace, "do_find_sync" |
|
|
2196 | if $Coro::current == $Coro::main; |
|
|
2197 | |
|
|
2198 | find $path, $origin |
2196 | } |
2199 | } |
2197 | |
2200 | |
2198 | sub do_load_sync { |
2201 | sub do_load_sync { |
2199 | my ($map) = @_; |
2202 | my ($map) = @_; |
2200 | |
2203 | |
2201 | cf::LOG cf::llevDebug | cf::logBacktrace, "do_load_sync" |
2204 | return cf::LOG cf::llevError | cf::logBacktrace, "do_load_sync" |
2202 | if $Coro::current == $Coro::main; |
2205 | if $Coro::current == $Coro::main; |
2203 | |
2206 | |
2204 | cf::sync_job { $map->load }; |
2207 | $map->load; |
2205 | } |
2208 | } |
2206 | |
2209 | |
2207 | our %MAP_PREFETCH; |
2210 | our %MAP_PREFETCH; |
2208 | our $MAP_PREFETCHER = undef; |
2211 | our $MAP_PREFETCHER = undef; |
2209 | |
2212 | |
… | |
… | |
3268 | decrease split destroy change_exp value msg lore send_msg)], |
3271 | decrease split destroy change_exp value msg lore send_msg)], |
3269 | ["cf::object::player" => qw(player)], |
3272 | ["cf::object::player" => qw(player)], |
3270 | ["cf::player" => qw(peaceful send_msg)], |
3273 | ["cf::player" => qw(peaceful send_msg)], |
3271 | ["cf::map" => qw(trigger)], |
3274 | ["cf::map" => qw(trigger)], |
3272 | ) { |
3275 | ) { |
3273 | no strict 'refs'; |
|
|
3274 | my ($pkg, @funs) = @$_; |
3276 | my ($pkg, @funs) = @$_; |
3275 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
3277 | *{"safe::$pkg\::$_"} = $safe_hole->wrap (\&{"$pkg\::$_"}) |
3276 | for @funs; |
3278 | for @funs; |
3277 | } |
3279 | } |
3278 | |
3280 | |
… | |
… | |
3747 | sub emergency_save() { |
3749 | sub emergency_save() { |
3748 | my $freeze_guard = cf::freeze_mainloop; |
3750 | my $freeze_guard = cf::freeze_mainloop; |
3749 | |
3751 | |
3750 | info "emergency_perl_save: enter\n"; |
3752 | info "emergency_perl_save: enter\n"; |
3751 | |
3753 | |
|
|
3754 | # this is a trade-off: we want to be very quick here, so |
|
|
3755 | # save all maps without fsync, and later call a global sync |
|
|
3756 | # (which in turn might be very very slow) |
|
|
3757 | local $USE_FSYNC = 0; |
|
|
3758 | |
3752 | cf::sync_job { |
3759 | cf::sync_job { |
3753 | # this is a trade-off: we want to be very quick here, so |
3760 | 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 | |
3761 | |
3758 | # use a peculiar iteration method to avoid tripping on perl |
3762 | # use a peculiar iteration method to avoid tripping on perl |
3759 | # refcount bugs in for. also avoids problems with players |
3763 | # refcount bugs in for. also avoids problems with players |
3760 | # and maps saved/destroyed asynchronously. |
3764 | # and maps saved/destroyed asynchronously. |
3761 | info "emergency_perl_save: begin player save\n"; |
3765 | info "emergency_perl_save: begin player save\n"; |
… | |
… | |
3765 | delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt |
3769 | delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt |
3766 | $pl->save; |
3770 | $pl->save; |
3767 | } |
3771 | } |
3768 | info "emergency_perl_save: end player save\n"; |
3772 | info "emergency_perl_save: end player save\n"; |
3769 | |
3773 | |
|
|
3774 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3775 | |
3770 | info "emergency_perl_save: begin map save\n"; |
3776 | info "emergency_perl_save: begin map save\n"; |
3771 | for my $path (keys %cf::MAP) { |
3777 | for my $path (keys %cf::MAP) { |
3772 | my $map = $cf::MAP{$path} or next; |
3778 | my $map = $cf::MAP{$path} or next; |
3773 | $map->valid or next; |
3779 | $map->valid or next; |
3774 | $map->save; |
3780 | $map->save; |
3775 | } |
3781 | } |
3776 | info "emergency_perl_save: end map save\n"; |
3782 | info "emergency_perl_save: end map save\n"; |
3777 | |
3783 | |
|
|
3784 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3785 | |
3778 | info "emergency_perl_save: begin database checkpoint\n"; |
3786 | info "emergency_perl_save: begin database checkpoint\n"; |
3779 | BDB::db_env_txn_checkpoint $DB_ENV; |
3787 | BDB::db_env_txn_checkpoint $DB_ENV; |
3780 | info "emergency_perl_save: end database checkpoint\n"; |
3788 | info "emergency_perl_save: end database checkpoint\n"; |
3781 | |
3789 | |
3782 | info "emergency_perl_save: begin write uuid\n"; |
3790 | info "emergency_perl_save: begin write uuid\n"; |
3783 | write_uuid_sync 1; |
3791 | write_uuid_sync 1; |
3784 | info "emergency_perl_save: end write uuid\n"; |
3792 | info "emergency_perl_save: end write uuid\n"; |
3785 | }; |
|
|
3786 | |
3793 | |
|
|
3794 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3795 | |
|
|
3796 | trace "emergency_perl_save: syncing database to disk"; |
|
|
3797 | BDB::db_env_txn_checkpoint $DB_ENV; |
|
|
3798 | |
3787 | info "emergency_perl_save: starting sync()\n"; |
3799 | info "emergency_perl_save: starting sync\n"; |
3788 | IO::AIO::aio_sync sub { |
3800 | IO::AIO::aio_sync sub { |
3789 | info "emergency_perl_save: finished sync()\n"; |
3801 | info "emergency_perl_save: finished sync\n"; |
|
|
3802 | }; |
|
|
3803 | |
|
|
3804 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3805 | |
|
|
3806 | trace "emergency_perl_save: flushing outstanding aio requests"; |
|
|
3807 | while (IO::AIO::nreqs || BDB::nreqs) { |
|
|
3808 | Coro::EV::timer_once 0.01; # let the sync_job do it's thing |
|
|
3809 | } |
|
|
3810 | |
|
|
3811 | cf::write_runtime_sync; # external watchdog should not bark |
3790 | }; |
3812 | }; |
3791 | |
3813 | |
3792 | info "emergency_perl_save: leave\n"; |
3814 | info "emergency_perl_save: leave\n"; |
3793 | } |
3815 | } |
3794 | |
3816 | |
3795 | sub post_cleanup { |
3817 | sub post_cleanup { |
3796 | my ($make_core) = @_; |
3818 | my ($make_core) = @_; |
|
|
3819 | |
|
|
3820 | IO::AIO::flush; |
3797 | |
3821 | |
3798 | error Carp::longmess "post_cleanup backtrace" |
3822 | error Carp::longmess "post_cleanup backtrace" |
3799 | if $make_core; |
3823 | if $make_core; |
3800 | |
3824 | |
3801 | my $fh = pidfile; |
3825 | my $fh = pidfile; |
… | |
… | |
3842 | info "reloading..."; |
3866 | info "reloading..."; |
3843 | |
3867 | |
3844 | trace "entering sync_job"; |
3868 | trace "entering sync_job"; |
3845 | |
3869 | |
3846 | cf::sync_job { |
3870 | cf::sync_job { |
3847 | cf::write_runtime_sync; # external watchdog should not bark |
|
|
3848 | cf::emergency_save; |
3871 | 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 | |
3872 | |
3861 | trace "cancelling all extension coros"; |
3873 | trace "cancelling all extension coros"; |
3862 | $_->cancel for values %EXT_CORO; |
3874 | $_->cancel for values %EXT_CORO; |
3863 | %EXT_CORO = (); |
3875 | %EXT_CORO = (); |
3864 | |
3876 | |
… | |
… | |
4056 | $DB_ENV->set_flags (&BDB::LOG_AUTOREMOVE ) if BDB::VERSION v0, v4.7; |
4068 | $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; |
4069 | $DB_ENV->log_set_config (&BDB::LOG_AUTO_REMOVE) if BDB::VERSION v4.7; |
4058 | $DB_ENV->set_timeout (30, BDB::SET_TXN_TIMEOUT); |
4070 | $DB_ENV->set_timeout (30, BDB::SET_TXN_TIMEOUT); |
4059 | $DB_ENV->set_timeout (30, BDB::SET_LOCK_TIMEOUT); |
4071 | $DB_ENV->set_timeout (30, BDB::SET_LOCK_TIMEOUT); |
4060 | |
4072 | |
4061 | (Coro::async { # async to keep sync_job from complaining |
|
|
4062 | cf::sync_job { |
4073 | cf::sync_job { |
4063 | eval { |
4074 | eval { |
4064 | BDB::db_env_open |
4075 | BDB::db_env_open |
4065 | $DB_ENV, |
4076 | $DB_ENV, |
4066 | $BDBDIR, |
4077 | $BDBDIR, |
4067 | BDB::INIT_LOCK | BDB::INIT_LOG | BDB::INIT_MPOOL | BDB::INIT_TXN |
4078 | BDB::INIT_LOCK | BDB::INIT_LOG | BDB::INIT_MPOOL | BDB::INIT_TXN |
4068 | | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE, |
4079 | | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE, |
4069 | 0666; |
4080 | 0666; |
4070 | |
4081 | |
4071 | cf::cleanup "db_env_open($BDBDIR): $!" if $!; |
4082 | cf::cleanup "db_env_open($BDBDIR): $!" if $!; |
4072 | }; |
|
|
4073 | |
|
|
4074 | cf::cleanup "db_env_open(db): $@" if $@; |
|
|
4075 | }; |
4083 | }; |
4076 | })->join; |
4084 | |
|
|
4085 | cf::cleanup "db_env_open(db): $@" if $@; |
|
|
4086 | }; |
4077 | } |
4087 | } |
4078 | |
4088 | |
4079 | $BDB_DEADLOCK_WATCHER = EV::periodic 0, 3, 0, sub { |
4089 | $BDB_DEADLOCK_WATCHER = EV::periodic 0, 3, 0, sub { |
4080 | BDB::db_env_lock_detect $DB_ENV, 0, BDB::LOCK_DEFAULT, 0, sub { }; |
4090 | BDB::db_env_lock_detect $DB_ENV, 0, BDB::LOCK_DEFAULT, 0, sub { }; |
4081 | }; |
4091 | }; |