… | |
… | |
32 | use Opcode; |
32 | use Opcode; |
33 | use Safe; |
33 | use Safe; |
34 | use Safe::Hole; |
34 | use Safe::Hole; |
35 | use Storable (); |
35 | use Storable (); |
36 | |
36 | |
|
|
37 | use Guard (); |
37 | use Coro (); |
38 | use Coro (); |
38 | use Coro::State; |
39 | use Coro::State; |
39 | use Coro::Handle; |
40 | use Coro::Handle; |
40 | use Coro::EV; |
41 | use Coro::EV; |
41 | use Coro::AnyEvent; |
42 | use Coro::AnyEvent; |
42 | use Coro::Timer; |
43 | use Coro::Timer; |
43 | use Coro::Signal; |
44 | use Coro::Signal; |
44 | use Coro::Semaphore; |
45 | use Coro::Semaphore; |
|
|
46 | use Coro::SemaphoreSet; |
45 | use Coro::AnyEvent; |
47 | use Coro::AnyEvent; |
46 | use Coro::AIO; |
48 | use Coro::AIO; |
47 | use Coro::BDB 1.6; |
49 | use Coro::BDB 1.6; |
48 | use Coro::Storable; |
50 | use Coro::Storable; |
49 | use Coro::Util (); |
51 | use Coro::Util (); |
… | |
… | |
130 | our $JITTER; # average jitter |
132 | our $JITTER; # average jitter |
131 | our $TICK_START; # for load detecting purposes |
133 | our $TICK_START; # for load detecting purposes |
132 | |
134 | |
133 | our @POST_INIT; |
135 | our @POST_INIT; |
134 | |
136 | |
|
|
137 | our $REATTACH_ON_RELOAD; # ste to true to force object reattach on reload (slow) |
|
|
138 | |
135 | binmode STDOUT; |
139 | binmode STDOUT; |
136 | binmode STDERR; |
140 | binmode STDERR; |
137 | |
141 | |
138 | # read virtual server time, if available |
142 | # read virtual server time, if available |
139 | unless ($RUNTIME || !-e $RUNTIMEFILE) { |
143 | unless ($RUNTIME || !-e $RUNTIMEFILE) { |
… | |
… | |
328 | Wait until the given lock is available. See cf::lock_acquire. |
332 | Wait until the given lock is available. See cf::lock_acquire. |
329 | |
333 | |
330 | =item my $lock = cf::lock_acquire $string |
334 | =item my $lock = cf::lock_acquire $string |
331 | |
335 | |
332 | Wait until the given lock is available and then acquires it and returns |
336 | Wait until the given lock is available and then acquires it and returns |
333 | a Coro::guard object. If the guard object gets destroyed (goes out of scope, |
337 | a L<Guard> object. If the guard object gets destroyed (goes out of scope, |
334 | for example when the coroutine gets canceled), the lock is automatically |
338 | for example when the coroutine gets canceled), the lock is automatically |
335 | returned. |
339 | returned. |
336 | |
340 | |
337 | Locks are *not* recursive, locking from the same coro twice results in a |
341 | Locks are *not* recursive, locking from the same coro twice results in a |
338 | deadlocked coro. |
342 | deadlocked coro. |
… | |
… | |
344 | |
348 | |
345 | Return true if the lock is currently active, i.e. somebody has locked it. |
349 | Return true if the lock is currently active, i.e. somebody has locked it. |
346 | |
350 | |
347 | =cut |
351 | =cut |
348 | |
352 | |
349 | our %LOCK; |
353 | our $LOCKS = new Coro::SemaphoreSet; |
350 | our %LOCKER;#d# |
|
|
351 | |
354 | |
352 | sub lock_wait($) { |
355 | sub lock_wait($) { |
353 | my ($key) = @_; |
356 | $LOCKS->wait ($_[0]); |
354 | |
|
|
355 | if ($LOCKER{$key} == $Coro::current) {#d# |
|
|
356 | Carp::cluck "lock_wait($key) for already-acquired lock";#d# |
|
|
357 | return;#d# |
|
|
358 | }#d# |
|
|
359 | |
|
|
360 | # wait for lock, if any |
|
|
361 | while ($LOCK{$key}) { |
|
|
362 | #local $Coro::current->{desc} = "$Coro::current->{desc} <waiting for lock $key>"; |
|
|
363 | push @{ $LOCK{$key} }, $Coro::current; |
|
|
364 | Coro::schedule; |
|
|
365 | } |
|
|
366 | } |
357 | } |
367 | |
358 | |
368 | sub lock_acquire($) { |
359 | sub lock_acquire($) { |
369 | my ($key) = @_; |
360 | $LOCKS->guard ($_[0]) |
370 | |
|
|
371 | # wait, to be sure we are not locked |
|
|
372 | lock_wait $key; |
|
|
373 | |
|
|
374 | $LOCK{$key} = []; |
|
|
375 | $LOCKER{$key} = $Coro::current;#d# |
|
|
376 | |
|
|
377 | Coro::guard { |
|
|
378 | delete $LOCKER{$key};#d# |
|
|
379 | # wake up all waiters, to be on the safe side |
|
|
380 | $_->ready for @{ delete $LOCK{$key} }; |
|
|
381 | } |
|
|
382 | } |
361 | } |
383 | |
362 | |
384 | sub lock_active($) { |
363 | sub lock_active($) { |
385 | my ($key) = @_; |
364 | $LOCKS->count ($_[0]) < 1 |
386 | |
|
|
387 | ! ! $LOCK{$key} |
|
|
388 | } |
365 | } |
389 | |
366 | |
390 | sub freeze_mainloop { |
367 | sub freeze_mainloop { |
391 | tick_inhibit_inc; |
368 | tick_inhibit_inc; |
392 | |
369 | |
393 | Coro::guard \&tick_inhibit_dec; |
370 | &Guard::guard (\&tick_inhibit_dec); |
394 | } |
371 | } |
395 | |
372 | |
396 | =item cf::periodic $interval, $cb |
373 | =item cf::periodic $interval, $cb |
397 | |
374 | |
398 | Like EV::periodic, but randomly selects a starting point so that the actions |
375 | Like EV::periodic, but randomly selects a starting point so that the actions |
… | |
… | |
1187 | } else { |
1164 | } else { |
1188 | aio_unlink "$filename.pst"; |
1165 | aio_unlink "$filename.pst"; |
1189 | } |
1166 | } |
1190 | |
1167 | |
1191 | aio_rename "$filename~", $filename; |
1168 | aio_rename "$filename~", $filename; |
|
|
1169 | |
|
|
1170 | $filename =~ s%/[^/]+$%%; |
|
|
1171 | aio_pathsync $filename if $cf::USE_FSYNC; |
1192 | } else { |
1172 | } else { |
1193 | warn "FATAL: $filename~: $!\n"; |
1173 | warn "unable to save objects: $filename~: $!\n"; |
1194 | } |
1174 | } |
1195 | } else { |
1175 | } else { |
1196 | aio_unlink $filename; |
1176 | aio_unlink $filename; |
1197 | aio_unlink "$filename.pst"; |
1177 | aio_unlink "$filename.pst"; |
1198 | } |
1178 | } |
… | |
… | |
1289 | my ($name, $cb) = @_; |
1269 | my ($name, $cb) = @_; |
1290 | |
1270 | |
1291 | $EXTICMD{$name} = $cb; |
1271 | $EXTICMD{$name} = $cb; |
1292 | } |
1272 | } |
1293 | |
1273 | |
|
|
1274 | use File::Glob (); |
|
|
1275 | |
1294 | cf::player->attach ( |
1276 | cf::player->attach ( |
1295 | on_command => sub { |
1277 | on_command => sub { |
1296 | my ($pl, $name, $params) = @_; |
1278 | my ($pl, $name, $params) = @_; |
1297 | |
1279 | |
1298 | my $cb = $COMMAND{$name} |
1280 | my $cb = $COMMAND{$name} |
… | |
… | |
1329 | } |
1311 | } |
1330 | |
1312 | |
1331 | cf::override; |
1313 | cf::override; |
1332 | }, |
1314 | }, |
1333 | ); |
1315 | ); |
|
|
1316 | |
|
|
1317 | # "readahead" all extensions |
|
|
1318 | sub cache_extensions { |
|
|
1319 | my $grp = IO::AIO::aio_group; |
|
|
1320 | |
|
|
1321 | add $grp IO::AIO::aio_readdir $LIBDIR, sub { |
|
|
1322 | for (grep /\.ext$/, @{$_[0]}) { |
|
|
1323 | add $grp IO::AIO::aio_load "$LIBDIR/$_", my $data; |
|
|
1324 | } |
|
|
1325 | }; |
|
|
1326 | |
|
|
1327 | $grp |
|
|
1328 | } |
1334 | |
1329 | |
1335 | sub load_extensions { |
1330 | sub load_extensions { |
1336 | cf::sync_job { |
1331 | cf::sync_job { |
1337 | my %todo; |
1332 | my %todo; |
1338 | |
1333 | |
… | |
… | |
1967 | sub find { |
1962 | sub find { |
1968 | my ($path, $origin) = @_; |
1963 | my ($path, $origin) = @_; |
1969 | |
1964 | |
1970 | $path = normalise $path, $origin && $origin->path; |
1965 | $path = normalise $path, $origin && $origin->path; |
1971 | |
1966 | |
1972 | cf::lock_wait "map_data:$path";#d#remove |
1967 | my $guard1 = cf::lock_acquire "map_data:$path";#d#remove |
1973 | cf::lock_wait "map_find:$path"; |
1968 | my $guard2 = cf::lock_acquire "map_find:$path"; |
1974 | |
1969 | |
1975 | $cf::MAP{$path} || do { |
1970 | $cf::MAP{$path} || do { |
1976 | my $guard1 = cf::lock_acquire "map_data:$path"; # just for the fun of it |
|
|
1977 | my $guard2 = cf::lock_acquire "map_find:$path"; |
|
|
1978 | |
|
|
1979 | my $map = new_from_path cf::map $path |
1971 | my $map = new_from_path cf::map $path |
1980 | or return; |
1972 | or return; |
1981 | |
1973 | |
1982 | $map->{last_save} = $cf::RUNTIME; |
1974 | $map->{last_save} = $cf::RUNTIME; |
1983 | |
1975 | |
… | |
… | |
3518 | my $fh = aio_open "$uuid~", O_WRONLY | O_CREAT, 0644 |
3510 | my $fh = aio_open "$uuid~", O_WRONLY | O_CREAT, 0644 |
3519 | or return; |
3511 | or return; |
3520 | |
3512 | |
3521 | my $value = uuid_seq uuid_cur; |
3513 | my $value = uuid_seq uuid_cur; |
3522 | |
3514 | |
3523 | if ($value < 1024) { |
3515 | unless ($value) { |
3524 | warn "cowardly refusing to write low uuid value ($value)!\n"; |
3516 | warn "cowardly refusing to write zero uuid value!\n"; |
3525 | return; |
3517 | return; |
3526 | } |
3518 | } |
3527 | |
3519 | |
3528 | my $value = uuid_str $value + $uuid_skip; |
3520 | my $value = uuid_str $value + $uuid_skip; |
3529 | $uuid_skip = 0; |
3521 | $uuid_skip = 0; |
… | |
… | |
3555 | } |
3547 | } |
3556 | |
3548 | |
3557 | sub emergency_save() { |
3549 | sub emergency_save() { |
3558 | my $freeze_guard = cf::freeze_mainloop; |
3550 | my $freeze_guard = cf::freeze_mainloop; |
3559 | |
3551 | |
3560 | warn "enter emergency perl save\n"; |
3552 | warn "emergency_perl_save: enter\n"; |
3561 | |
3553 | |
3562 | cf::sync_job { |
3554 | cf::sync_job { |
|
|
3555 | # this is a trade-off: we want to be very quick here, so |
|
|
3556 | # save all maps without fsync, and later call a global sync |
|
|
3557 | # (which in turn might be very very slow) |
|
|
3558 | local $USE_FSYNC = 0; |
|
|
3559 | |
3563 | # use a peculiar iteration method to avoid tripping on perl |
3560 | # use a peculiar iteration method to avoid tripping on perl |
3564 | # refcount bugs in for. also avoids problems with players |
3561 | # refcount bugs in for. also avoids problems with players |
3565 | # and maps saved/destroyed asynchronously. |
3562 | # and maps saved/destroyed asynchronously. |
3566 | warn "begin emergency player save\n"; |
3563 | warn "emergency_perl_save: begin player save\n"; |
3567 | for my $login (keys %cf::PLAYER) { |
3564 | for my $login (keys %cf::PLAYER) { |
3568 | my $pl = $cf::PLAYER{$login} or next; |
3565 | my $pl = $cf::PLAYER{$login} or next; |
3569 | $pl->valid or next; |
3566 | $pl->valid or next; |
3570 | delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt |
3567 | delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt |
3571 | $pl->save; |
3568 | $pl->save; |
3572 | } |
3569 | } |
3573 | warn "end emergency player save\n"; |
3570 | warn "emergency_perl_save: end player save\n"; |
3574 | |
3571 | |
3575 | warn "begin emergency map save\n"; |
3572 | warn "emergency_perl_save: begin map save\n"; |
3576 | for my $path (keys %cf::MAP) { |
3573 | for my $path (keys %cf::MAP) { |
3577 | my $map = $cf::MAP{$path} or next; |
3574 | my $map = $cf::MAP{$path} or next; |
3578 | $map->valid or next; |
3575 | $map->valid or next; |
3579 | $map->save; |
3576 | $map->save; |
3580 | } |
3577 | } |
3581 | warn "end emergency map save\n"; |
3578 | warn "emergency_perl_save: end map save\n"; |
3582 | |
3579 | |
3583 | warn "begin emergency database checkpoint\n"; |
3580 | warn "emergency_perl_save: begin database checkpoint\n"; |
3584 | BDB::db_env_txn_checkpoint $DB_ENV; |
3581 | BDB::db_env_txn_checkpoint $DB_ENV; |
3585 | warn "end emergency database checkpoint\n"; |
3582 | warn "emergency_perl_save: end database checkpoint\n"; |
3586 | |
3583 | |
3587 | warn "begin write uuid\n"; |
3584 | warn "emergency_perl_save: begin write uuid\n"; |
3588 | write_uuid_sync 1; |
3585 | write_uuid_sync 1; |
3589 | warn "end write uuid\n"; |
3586 | warn "emergency_perl_save: end write uuid\n"; |
3590 | }; |
3587 | }; |
3591 | |
3588 | |
|
|
3589 | warn "emergency_perl_save: starting sync()\n"; |
|
|
3590 | IO::AIO::aio_sync sub { |
|
|
3591 | warn "emergency_perl_save: finished sync()\n"; |
|
|
3592 | }; |
|
|
3593 | |
3592 | warn "leave emergency perl save\n"; |
3594 | warn "emergency_perl_save: leave\n"; |
3593 | } |
3595 | } |
3594 | |
3596 | |
3595 | sub post_cleanup { |
3597 | sub post_cleanup { |
3596 | my ($make_core) = @_; |
3598 | my ($make_core) = @_; |
3597 | |
3599 | |
… | |
… | |
3634 | warn "can only reload from main coroutine"; |
3636 | warn "can only reload from main coroutine"; |
3635 | return; |
3637 | return; |
3636 | } |
3638 | } |
3637 | |
3639 | |
3638 | return if $RELOAD++; |
3640 | return if $RELOAD++; |
|
|
3641 | |
|
|
3642 | my $t1 = EV::time; |
3639 | |
3643 | |
3640 | while ($RELOAD) { |
3644 | while ($RELOAD) { |
3641 | warn "reloading..."; |
3645 | warn "reloading..."; |
3642 | |
3646 | |
3643 | warn "entering sync_job"; |
3647 | warn "entering sync_job"; |
… | |
… | |
3722 | cf::reload_config; |
3726 | cf::reload_config; |
3723 | |
3727 | |
3724 | warn "loading extensions"; |
3728 | warn "loading extensions"; |
3725 | cf::load_extensions; |
3729 | cf::load_extensions; |
3726 | |
3730 | |
|
|
3731 | if ($REATTACH_ON_RELOAD) { |
3727 | warn "reattaching attachments to objects/players"; |
3732 | warn "reattaching attachments to objects/players"; |
3728 | _global_reattach; # objects, sockets |
3733 | _global_reattach; # objects, sockets |
3729 | warn "reattaching attachments to maps"; |
3734 | warn "reattaching attachments to maps"; |
3730 | reattach $_ for values %MAP; |
3735 | reattach $_ for values %MAP; |
3731 | warn "reattaching attachments to players"; |
3736 | warn "reattaching attachments to players"; |
3732 | reattach $_ for values %PLAYER; |
3737 | reattach $_ for values %PLAYER; |
|
|
3738 | } |
3733 | |
3739 | |
3734 | warn "running post_load"; |
3740 | warn "running post_init jobs"; |
3735 | (pop @POST_INIT)->(1) while @POST_INIT; |
3741 | (pop @POST_INIT)->(1) while @POST_INIT; |
3736 | |
3742 | |
3737 | warn "leaving sync_job"; |
3743 | warn "leaving sync_job"; |
3738 | |
3744 | |
3739 | 1 |
3745 | 1 |
… | |
… | |
3743 | }; |
3749 | }; |
3744 | |
3750 | |
3745 | warn "reloaded"; |
3751 | warn "reloaded"; |
3746 | --$RELOAD; |
3752 | --$RELOAD; |
3747 | } |
3753 | } |
|
|
3754 | |
|
|
3755 | $t1 = EV::time - $t1; |
|
|
3756 | warn "reload completed in ${t1}s\n"; |
3748 | }; |
3757 | }; |
3749 | |
3758 | |
3750 | our $RELOAD_WATCHER; # used only during reload |
3759 | our $RELOAD_WATCHER; # used only during reload |
3751 | |
3760 | |
3752 | sub reload_perl() { |
3761 | sub reload_perl() { |
3753 | # doing reload synchronously and two reloads happen back-to-back, |
3762 | # doing reload synchronously and two reloads happen back-to-back, |
3754 | # coro crashes during coro_state_free->destroy here. |
3763 | # coro crashes during coro_state_free->destroy here. |
3755 | |
3764 | |
|
|
3765 | $RELOAD_WATCHER ||= cf::async { |
|
|
3766 | Coro::AIO::aio_wait cache_extensions; |
|
|
3767 | |
3756 | $RELOAD_WATCHER ||= EV::timer $TICK * 1.5, 0, sub { |
3768 | $RELOAD_WATCHER = EV::timer $TICK * 1.5, 0, sub { |
3757 | do_reload_perl; |
3769 | do_reload_perl; |
3758 | undef $RELOAD_WATCHER; |
3770 | undef $RELOAD_WATCHER; |
|
|
3771 | }; |
3759 | }; |
3772 | }; |
3760 | } |
3773 | } |
3761 | |
3774 | |
3762 | register_command "reload" => sub { |
3775 | register_command "reload" => sub { |
3763 | my ($who, $arg) = @_; |
3776 | my ($who, $arg) = @_; |