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.530 by root, Wed Apr 28 21:05:33 2010 UTC vs.
Revision 1.535 by root, Thu Apr 29 08:21:28 2010 UTC

252 252
253=back 253=back
254 254
255=cut 255=cut
256 256
257sub error(@) { LOG llevError, join "", @_ }
258sub warn (@) { LOG llevWarn , join "", @_ }
259sub info (@) { LOG llevInfo , join "", @_ }
260sub debug(@) { LOG llevDebug, join "", @_ }
261sub trace(@) { LOG llevTrace, join "", @_ }
262
257$Coro::State::WARNHOOK = sub { 263$Coro::State::WARNHOOK = sub {
258 my $msg = join "", @_; 264 my $msg = join "", @_;
259 265
260 $msg .= "\n" 266 $msg .= "\n"
261 unless $msg =~ /\n$/; 267 unless $msg =~ /\n$/;
262 268
263 $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; 269 $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge;
264 270
265 LOG llevError, $msg; 271 LOG llevWarn, $msg;
266}; 272};
267 273
268$Coro::State::DIEHOOK = sub { 274$Coro::State::DIEHOOK = sub {
269 return unless $^S eq 0; # "eq", not "==" 275 return unless $^S eq 0; # "eq", not "=="
270 276
271 warn Carp::longmess $_[0]; 277 error Carp::longmess $_[0];
272 278
273 if (in_main) {#d# 279 if (in_main) {#d#
274 warn "DIEHOOK called in main context, Coro bug?\n";#d# 280 error "DIEHOOK called in main context, Coro bug?\n";#d#
275 return;#d# 281 return;#d#
276 }#d# 282 }#d#
277 283
278 # kill coroutine otherwise 284 # kill coroutine otherwise
279 Coro::terminate 285 Coro::terminate
512=cut 518=cut
513 519
514sub sync_job(&) { 520sub sync_job(&) {
515 my ($job) = @_; 521 my ($job) = @_;
516 522
517 if ($Coro::current == $Coro::main) { 523 if (in_main) {
518 my $time = AE::time; 524 my $time = AE::time;
519 525
520 # this is the main coro, too bad, we have to block 526 # this is the main coro, too bad, we have to block
521 # till the operation succeeds, freezing the server :/ 527 # till the operation succeeds, freezing the server :/
522 528
523 LOG llevError, Carp::longmess "sync job";#d# 529 #LOG llevError, Carp::longmess "sync job";#d#
524 530
525 my $freeze_guard = freeze_mainloop; 531 my $freeze_guard = freeze_mainloop;
526 532
527 my $busy = 1; 533 my $busy = 1;
528 my @res; 534 my @res;
529 535
530 (async { 536 (async {
531 $Coro::current->desc ("sync job coro"); 537 $Coro::current->desc ("sync job coro");
532 @res = eval { $job->() }; 538 @res = eval { $job->() };
533 warn $@ if $@; 539 error $@ if $@;
534 undef $busy; 540 undef $busy;
535 })->prio (Coro::PRIO_MAX); 541 })->prio (Coro::PRIO_MAX);
536 542
537 while ($busy) { 543 while ($busy) {
538 if (Coro::nready) { 544 if (Coro::nready) {
651within each server. 657within each server.
652 658
653=cut 659=cut
654 660
655sub db_table($) { 661sub db_table($) {
662 cf::error "db_get called from main context"
663 if $Coro::current == $Coro::main;
664
656 my ($name) = @_; 665 my ($name) = @_;
657 my $db = BDB::db_create $DB_ENV; 666 my $db = BDB::db_create $DB_ENV;
658 667
659 eval { 668 eval {
660 $db->set_flags (BDB::CHKSUM); 669 $db->set_flags (BDB::CHKSUM);
670} 679}
671 680
672our $DB; 681our $DB;
673 682
674sub db_init { 683sub db_init {
675 cf::sync_job {
676 $DB ||= db_table "db"; 684 $DB ||= db_table "db";
677 };
678} 685}
679 686
680sub db_get($$) { 687sub db_get($$) {
681 my $key = "$_[0]/$_[1]"; 688 my $key = "$_[0]/$_[1]";
682 689
683 cf::sync_job { 690 cf::error "db_get called from main context"
691 if $Coro::current == $Coro::main;
692
684 BDB::db_get $DB, undef, $key, my $data; 693 BDB::db_get $DB, undef, $key, my $data;
685 694
686 $! ? () 695 $! ? ()
687 : $data 696 : $data
688 }
689} 697}
690 698
691sub db_put($$$) { 699sub db_put($$$) {
692 BDB::dbreq_pri 4; 700 BDB::dbreq_pri 4;
693 BDB::db_put $DB, undef, "$_[0]/$_[1]", $_[2], 0, sub { }; 701 BDB::db_put $DB, undef, "$_[0]/$_[1]", $_[2], 0, sub { };
749 757
750 my $t1 = Time::HiRes::time; 758 my $t1 = Time::HiRes::time;
751 my $data = $process->(\@data); 759 my $data = $process->(\@data);
752 my $t2 = Time::HiRes::time; 760 my $t2 = Time::HiRes::time;
753 761
754 warn "cache: '$id' processed in ", $t2 - $t1, "s\n"; 762 info "cache: '$id' processed in ", $t2 - $t1, "s\n";
755 763
756 db_put cache => "$id/data", $data; 764 db_put cache => "$id/data", $data;
757 db_put cache => "$id/md5" , $md5; 765 db_put cache => "$id/md5" , $md5;
758 db_put cache => "$id/meta", $meta; 766 db_put cache => "$id/meta", $meta;
759 767
769 777
770=cut 778=cut
771 779
772sub datalog($@) { 780sub datalog($@) {
773 my ($type, %kv) = @_; 781 my ($type, %kv) = @_;
774 warn "DATALOG ", JSON::XS->new->ascii->encode ({ %kv, type => $type }); 782 info "DATALOG ", JSON::XS->new->ascii->encode ({ %kv, type => $type });
775} 783}
776 784
777=back 785=back
778 786
779=cut 787=cut
974 982
975 } elsif (exists $cb_id{$type}) { 983 } elsif (exists $cb_id{$type}) {
976 _attach_cb $registry, $cb_id{$type}, $prio, shift @arg; 984 _attach_cb $registry, $cb_id{$type}, $prio, shift @arg;
977 985
978 } elsif (ref $type) { 986 } elsif (ref $type) {
979 warn "attaching objects not supported, ignoring.\n"; 987 error "attaching objects not supported, ignoring.\n";
980 988
981 } else { 989 } else {
982 shift @arg; 990 shift @arg;
983 warn "attach argument '$type' not supported, ignoring.\n"; 991 error "attach argument '$type' not supported, ignoring.\n";
984 } 992 }
985 } 993 }
986} 994}
987 995
988sub _object_attach { 996sub _object_attach {
998 _attach $registry, $klass, @attach; 1006 _attach $registry, $klass, @attach;
999 } 1007 }
1000 1008
1001 $obj->{$name} = \%arg; 1009 $obj->{$name} = \%arg;
1002 } else { 1010 } else {
1003 warn "object uses attachment '$name' which is not available, postponing.\n"; 1011 info "object uses attachment '$name' which is not available, postponing.\n";
1004 } 1012 }
1005 1013
1006 $obj->{_attachment}{$name} = undef; 1014 $obj->{_attachment}{$name} = undef;
1007} 1015}
1008 1016
1067 1075
1068 for (@$callbacks) { 1076 for (@$callbacks) {
1069 eval { &{$_->[1]} }; 1077 eval { &{$_->[1]} };
1070 1078
1071 if ($@) { 1079 if ($@) {
1072 warn "$@";
1073 warn "... while processing $EVENT[$event][0](@_) event, skipping processing altogether.\n"; 1080 error "$@", "... while processing $EVENT[$event][0](@_) event, skipping processing altogether.\n";
1074 override; 1081 override;
1075 } 1082 }
1076 1083
1077 return 1 if $override; 1084 return 1 if $override;
1078 } 1085 }
1157 for (@$attach) { 1164 for (@$attach) {
1158 my ($klass, @attach) = @$_; 1165 my ($klass, @attach) = @$_;
1159 _attach $registry, $klass, @attach; 1166 _attach $registry, $klass, @attach;
1160 } 1167 }
1161 } else { 1168 } else {
1162 warn "object uses attachment '$name' that is not available, postponing.\n"; 1169 info "object uses attachment '$name' that is not available, postponing.\n";
1163 } 1170 }
1164 } 1171 }
1165} 1172}
1166 1173
1167cf::attachable->attach ( 1174cf::attachable->attach (
1194 my ($filename, $rdata, $objs) = @_; 1201 my ($filename, $rdata, $objs) = @_;
1195 1202
1196 sync_job { 1203 sync_job {
1197 if (length $$rdata) { 1204 if (length $$rdata) {
1198 utf8::decode (my $decname = $filename); 1205 utf8::decode (my $decname = $filename);
1199 warn sprintf "saving %s (%d,%d)\n", 1206 trace sprintf "saving %s (%d,%d)\n",
1200 $decname, length $$rdata, scalar @$objs 1207 $decname, length $$rdata, scalar @$objs
1201 if $VERBOSE_IO; 1208 if $VERBOSE_IO;
1202 1209
1203 if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { 1210 if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) {
1204 aio_chmod $fh, SAVE_MODE; 1211 aio_chmod $fh, SAVE_MODE;
1205 aio_write $fh, 0, (length $$rdata), $$rdata, 0; 1212 aio_write $fh, 0, (length $$rdata), $$rdata, 0;
1228 aio_rename "$filename~", $filename; 1235 aio_rename "$filename~", $filename;
1229 1236
1230 $filename =~ s%/[^/]+$%%; 1237 $filename =~ s%/[^/]+$%%;
1231 aio_pathsync $filename if $cf::USE_FSYNC; 1238 aio_pathsync $filename if $cf::USE_FSYNC;
1232 } else { 1239 } else {
1233 warn "unable to save objects: $filename~: $!\n"; 1240 error "unable to save objects: $filename~: $!\n";
1234 } 1241 }
1235 } else { 1242 } else {
1236 aio_unlink $filename; 1243 aio_unlink $filename;
1237 aio_unlink "$filename.pst"; 1244 aio_unlink "$filename.pst";
1238 } 1245 }
1262 my $st = eval { Coro::Storable::thaw $av }; 1269 my $st = eval { Coro::Storable::thaw $av };
1263 $av = $st->{objs}; 1270 $av = $st->{objs};
1264 } 1271 }
1265 1272
1266 utf8::decode (my $decname = $filename); 1273 utf8::decode (my $decname = $filename);
1267 warn sprintf "loading %s (%d,%d)\n", 1274 trace sprintf "loading %s (%d,%d)\n",
1268 $decname, length $data, scalar @{$av || []} 1275 $decname, length $data, scalar @{$av || []}
1269 if $VERBOSE_IO; 1276 if $VERBOSE_IO;
1270 1277
1271 ($data, $av) 1278 ($data, $av)
1272} 1279}
1273 1280
1366 1373
1367 $pl->ext_reply ($reply, @reply) 1374 $pl->ext_reply ($reply, @reply)
1368 if $reply; 1375 if $reply;
1369 1376
1370 } else { 1377 } else {
1371 warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; 1378 error "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n";
1372 } 1379 }
1373 1380
1374 cf::override; 1381 cf::override;
1375 }, 1382 },
1376); 1383);
1387 1394
1388 $grp 1395 $grp
1389} 1396}
1390 1397
1391sub load_extensions { 1398sub load_extensions {
1399 info "loading extensions...";
1400
1392 cf::sync_job { 1401 cf::sync_job {
1393 my %todo; 1402 my %todo;
1394 1403
1395 for my $path (<$LIBDIR/*.ext>) { 1404 for my $path (<$LIBDIR/*.ext>) {
1396 next unless -r $path; 1405 next unless -r $path;
1436 for (split /,\s*/, $v->{meta}{depends}) { 1445 for (split /,\s*/, $v->{meta}{depends}) {
1437 next ext 1446 next ext
1438 unless exists $done{$_}; 1447 unless exists $done{$_};
1439 } 1448 }
1440 1449
1441 warn "... pass $pass, loading '$k' into '$v->{pkg}'\n"; 1450 trace "... pass $pass, loading '$k' into '$v->{pkg}'\n";
1442 1451
1443 my $active = eval $v->{source}; 1452 my $active = eval $v->{source};
1444 1453
1445 if (length $@) { 1454 if (length $@) {
1446 warn "$v->{path}: $@\n"; 1455 error "$v->{path}: $@\n";
1447 1456
1448 cf::cleanup "mandatory extension '$k' failed to load, exiting." 1457 cf::cleanup "mandatory extension '$k' failed to load, exiting."
1449 if exists $v->{meta}{mandatory}; 1458 if exists $v->{meta}{mandatory};
1450 1459
1451 warn "$v->{base}: optional extension cannot be loaded, skipping.\n"; 1460 warn "$v->{base}: optional extension cannot be loaded, skipping.\n";
1453 } else { 1462 } else {
1454 $done{$k} = delete $todo{$k}; 1463 $done{$k} = delete $todo{$k};
1455 push @EXTS, $v->{pkg}; 1464 push @EXTS, $v->{pkg};
1456 $progress = 1; 1465 $progress = 1;
1457 1466
1458 warn "$v->{base}: extension inactive.\n" 1467 info "$v->{base}: extension inactive.\n"
1459 unless $active; 1468 unless $active;
1460 } 1469 }
1461 } 1470 }
1462 1471
1463 unless ($progress) { 1472 unless ($progress) {
2183} 2192}
2184 2193
2185sub find_sync { 2194sub find_sync {
2186 my ($path, $origin) = @_; 2195 my ($path, $origin) = @_;
2187 2196
2188 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
2189} 2201}
2190 2202
2191sub do_load_sync { 2203sub do_load_sync {
2192 my ($map) = @_; 2204 my ($map) = @_;
2193 2205
2194 cf::LOG cf::llevDebug | cf::logBacktrace, "do_load_sync" 2206 return cf::LOG cf::llevError | cf::logBacktrace, "do_load_sync"
2195 if $Coro::current == $Coro::main; 2207 if $Coro::current == $Coro::main;
2196 2208
2197 cf::sync_job { $map->load }; 2209 $map->load;
2198} 2210}
2199 2211
2200our %MAP_PREFETCH; 2212our %MAP_PREFETCH;
2201our $MAP_PREFETCHER = undef; 2213our $MAP_PREFETCHER = undef;
2202 2214
2314 2326
2315 my $lock = cf::lock_acquire "map_data:$self->{path}"; 2327 my $lock = cf::lock_acquire "map_data:$self->{path}";
2316 2328
2317 return if $self->players; 2329 return if $self->players;
2318 2330
2319 warn "resetting map ", $self->path, "\n"; 2331 cf::trace "resetting map ", $self->path, "\n";
2320 2332
2321 $self->in_memory (cf::MAP_SWAPPED); 2333 $self->in_memory (cf::MAP_SWAPPED);
2322 2334
2323 # need to save uniques path 2335 # need to save uniques path
2324 unless ($self->{deny_save}) { 2336 unless ($self->{deny_save}) {
2689 2701
2690sub cf::object::player::goto { 2702sub cf::object::player::goto {
2691 my ($self, $path, $x, $y, $check, $done) = @_; 2703 my ($self, $path, $x, $y, $check, $done) = @_;
2692 2704
2693 if ($self->{_link_recursion} >= $MAX_LINKS) { 2705 if ($self->{_link_recursion} >= $MAX_LINKS) {
2694 warn "FATAL: link recursion exceeded, ", $self->name, " goto $path $x $y, redirecting."; 2706 error "FATAL: link recursion exceeded, ", $self->name, " goto $path $x $y, redirecting.";
2695 $self->failmsg ("Something went wrong inside the server - please contact an administrator!"); 2707 $self->failmsg ("Something went wrong inside the server - please contact an administrator!");
2696 ($path, $x, $y) = @$EMERGENCY_POSITION; 2708 ($path, $x, $y) = @$EMERGENCY_POSITION;
2697 } 2709 }
2698 2710
2699 # do generation counting so two concurrent goto's will be executed in-order 2711 # do generation counting so two concurrent goto's will be executed in-order
2847 $self->message ("Something went wrong deep within the deliantra server. " 2859 $self->message ("Something went wrong deep within the deliantra server. "
2848 . "I'll try to bring you back to the map you were before. " 2860 . "I'll try to bring you back to the map you were before. "
2849 . "Please report this to the dungeon master!", 2861 . "Please report this to the dungeon master!",
2850 cf::NDI_UNIQUE | cf::NDI_RED); 2862 cf::NDI_UNIQUE | cf::NDI_RED);
2851 2863
2852 warn "ERROR in enter_exit: $@"; 2864 error "ERROR in enter_exit: $@";
2853 $self->leave_link; 2865 $self->leave_link;
2854 } 2866 }
2855 })->prio (1); 2867 })->prio (1);
2856} 2868}
2857 2869
3170 3182
3171 $ns->ext_reply ($reply, @reply) 3183 $ns->ext_reply ($reply, @reply)
3172 if $reply; 3184 if $reply;
3173 3185
3174 } else { 3186 } else {
3175 warn "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n"; 3187 error "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n";
3176 } 3188 }
3177 3189
3178 cf::override; 3190 cf::override;
3179 }, 3191 },
3180); 3192);
3310 local @cf::_safe_eval_args = values %vars; 3322 local @cf::_safe_eval_args = values %vars;
3311 @res = wantarray ? eval eval : scalar eval $eval; 3323 @res = wantarray ? eval eval : scalar eval $eval;
3312 } 3324 }
3313 3325
3314 if ($@) { 3326 if ($@) {
3315 warn "$@"; 3327 warn "$@",
3316 warn "while executing safe code '$code'\n"; 3328 "while executing safe code '$code'\n",
3317 warn "with arguments " . (join " ", %vars) . "\n"; 3329 "with arguments " . (join " ", %vars) . "\n";
3318 } 3330 }
3319 3331
3320 wantarray ? @res : $res[0] 3332 wantarray ? @res : $res[0]
3321} 3333}
3322 3334
3356 # for this (global event?) 3368 # for this (global event?)
3357 %ext::player_env::MUSIC_FACE_CACHE = (); 3369 %ext::player_env::MUSIC_FACE_CACHE = ();
3358 3370
3359 my $enc = JSON::XS->new->utf8->canonical->relaxed; 3371 my $enc = JSON::XS->new->utf8->canonical->relaxed;
3360 3372
3361 warn "loading facedata from $path\n"; 3373 trace "loading facedata from $path\n";
3362 3374
3363 my $facedata; 3375 my $facedata;
3364 0 < aio_load $path, $facedata 3376 0 < aio_load $path, $facedata
3365 or die "$path: $!"; 3377 or die "$path: $!";
3366 3378
3400 3412
3401 if (my $smooth = cf::face::find $info->{smooth}) { 3413 if (my $smooth = cf::face::find $info->{smooth}) {
3402 cf::face::set_smooth $idx, $smooth; 3414 cf::face::set_smooth $idx, $smooth;
3403 cf::face::set_smoothlevel $idx, $info->{smoothlevel}; 3415 cf::face::set_smoothlevel $idx, $info->{smoothlevel};
3404 } else { 3416 } else {
3405 warn "smooth face '$info->{smooth}' not found for face '$face'"; 3417 error "smooth face '$info->{smooth}' not found for face '$face'";
3406 } 3418 }
3407 3419
3408 cf::cede_to_tick; 3420 cf::cede_to_tick;
3409 } 3421 }
3410 } 3422 }
3488 load_resource_file "$DATADIR/treasures" 3500 load_resource_file "$DATADIR/treasures"
3489 or die "unable to load treasurelists\n"; 3501 or die "unable to load treasurelists\n";
3490} 3502}
3491 3503
3492sub reload_sound { 3504sub reload_sound {
3505 trace "loading sound config from $DATADIR/sound\n";
3506
3493 0 < Coro::AIO::aio_load "$DATADIR/sound", my $data 3507 0 < Coro::AIO::aio_load "$DATADIR/sound", my $data
3494 or die "$DATADIR/sound $!"; 3508 or die "$DATADIR/sound $!";
3495 3509
3496 my $soundconf = JSON::XS->new->utf8->relaxed->decode ($data); 3510 my $soundconf = JSON::XS->new->utf8->relaxed->decode ($data);
3497 3511
3509 cf::sound::set $k => $face; 3523 cf::sound::set $k => $face;
3510 } 3524 }
3511} 3525}
3512 3526
3513sub reload_resources { 3527sub reload_resources {
3514 warn "reloading resource files...\n"; 3528 trace "reloading resource files...\n";
3515 3529
3516 reload_facedata; 3530 reload_facedata;
3517 reload_sound; 3531 reload_sound;
3518 reload_archetypes; 3532 reload_archetypes;
3519 reload_regions; 3533 reload_regions;
3520 reload_treasures; 3534 reload_treasures;
3521 3535
3522 warn "finished reloading resource files\n"; 3536 trace "finished reloading resource files\n";
3523} 3537}
3524 3538
3525sub reload_config { 3539sub reload_config {
3526 warn "reloading config file...\n"; 3540 trace "reloading config file...\n";
3527 3541
3528 open my $fh, "<:utf8", "$CONFDIR/config" 3542 open my $fh, "<:utf8", "$CONFDIR/config"
3529 or return; 3543 or return;
3530 3544
3531 local $/; 3545 local $/;
3542 and die "WARNING: m(un)lockall failed: $!\n"; 3556 and die "WARNING: m(un)lockall failed: $!\n";
3543 }; 3557 };
3544 warn $@ if $@; 3558 warn $@ if $@;
3545 } 3559 }
3546 3560
3547 warn "finished reloading resource files\n"; 3561 trace "finished reloading resource files\n";
3548} 3562}
3549 3563
3550sub pidfile() { 3564sub pidfile() {
3551 sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT 3565 sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT
3552 or die "$PIDFILE: $!"; 3566 or die "$PIDFILE: $!";
3565 seek $fh, 0, 0; 3579 seek $fh, 0, 0;
3566 print $fh $$; 3580 print $fh $$;
3567} 3581}
3568 3582
3569sub main_loop { 3583sub main_loop {
3570 warn "EV::loop starting\n"; 3584 trace "EV::loop starting\n";
3571 if (1) { 3585 if (1) {
3572 EV::loop; 3586 EV::loop;
3573 } 3587 }
3574 warn "EV::loop returned\n"; 3588 trace "EV::loop returned\n";
3575 goto &main_loop unless $REALLY_UNLOOP; 3589 goto &main_loop unless $REALLY_UNLOOP;
3576} 3590}
3577 3591
3578sub main { 3592sub main {
3579 cf::init_globals; # initialise logging 3593 cf::init_globals; # initialise logging
3627 3641
3628 (pop @POST_INIT)->(0) while @POST_INIT; 3642 (pop @POST_INIT)->(0) while @POST_INIT;
3629 }; 3643 };
3630 3644
3631 cf::object::thawer::errors_are_fatal 0; 3645 cf::object::thawer::errors_are_fatal 0;
3632 warn "parse errors in files are no longer fatal from this point on.\n"; 3646 info "parse errors in files are no longer fatal from this point on.\n";
3633 3647
3634 main_loop; 3648 main_loop;
3635} 3649}
3636 3650
3637############################################################################# 3651#############################################################################
3678 or return; 3692 or return;
3679 3693
3680 aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE 3694 aio_rename "$RUNTIMEFILE~", $RUNTIMEFILE
3681 and return; 3695 and return;
3682 3696
3683 warn sprintf "runtime file written (%gs).\n", AE::time - $t0; 3697 trace sprintf "runtime file written (%gs).\n", AE::time - $t0;
3684 3698
3685 1 3699 1
3686} 3700}
3687 3701
3688our $uuid_lock; 3702our $uuid_lock;
3700 or return; 3714 or return;
3701 3715
3702 my $value = uuid_seq uuid_cur; 3716 my $value = uuid_seq uuid_cur;
3703 3717
3704 unless ($value) { 3718 unless ($value) {
3705 warn "cowardly refusing to write zero uuid value!\n"; 3719 info "cowardly refusing to write zero uuid value!\n";
3706 return; 3720 return;
3707 } 3721 }
3708 3722
3709 my $value = uuid_str $value + $uuid_skip; 3723 my $value = uuid_str $value + $uuid_skip;
3710 $uuid_skip = 0; 3724 $uuid_skip = 0;
3720 or return; 3734 or return;
3721 3735
3722 aio_rename "$uuid~", $uuid 3736 aio_rename "$uuid~", $uuid
3723 and return; 3737 and return;
3724 3738
3725 warn "uuid file written ($value).\n"; 3739 trace "uuid file written ($value).\n";
3726 3740
3727 1 3741 1
3728 3742
3729} 3743}
3730 3744
3736} 3750}
3737 3751
3738sub emergency_save() { 3752sub emergency_save() {
3739 my $freeze_guard = cf::freeze_mainloop; 3753 my $freeze_guard = cf::freeze_mainloop;
3740 3754
3741 warn "emergency_perl_save: enter\n"; 3755 info "emergency_perl_save: enter\n";
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;
3742 3761
3743 cf::sync_job { 3762 cf::sync_job {
3744 # this is a trade-off: we want to be very quick here, so 3763 cf::write_runtime_sync; # external watchdog should not bark
3745 # save all maps without fsync, and later call a global sync
3746 # (which in turn might be very very slow)
3747 local $USE_FSYNC = 0;
3748 3764
3749 # use a peculiar iteration method to avoid tripping on perl 3765 # use a peculiar iteration method to avoid tripping on perl
3750 # refcount bugs in for. also avoids problems with players 3766 # refcount bugs in for. also avoids problems with players
3751 # and maps saved/destroyed asynchronously. 3767 # and maps saved/destroyed asynchronously.
3752 warn "emergency_perl_save: begin player save\n"; 3768 info "emergency_perl_save: begin player save\n";
3753 for my $login (keys %cf::PLAYER) { 3769 for my $login (keys %cf::PLAYER) {
3754 my $pl = $cf::PLAYER{$login} or next; 3770 my $pl = $cf::PLAYER{$login} or next;
3755 $pl->valid or next; 3771 $pl->valid or next;
3756 delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt 3772 delete $pl->{unclean_save}; # not strictly necessary, but cannot hurt
3757 $pl->save; 3773 $pl->save;
3758 } 3774 }
3759 warn "emergency_perl_save: end player save\n"; 3775 info "emergency_perl_save: end player save\n";
3760 3776
3777 cf::write_runtime_sync; # external watchdog should not bark
3778
3761 warn "emergency_perl_save: begin map save\n"; 3779 info "emergency_perl_save: begin map save\n";
3762 for my $path (keys %cf::MAP) { 3780 for my $path (keys %cf::MAP) {
3763 my $map = $cf::MAP{$path} or next; 3781 my $map = $cf::MAP{$path} or next;
3764 $map->valid or next; 3782 $map->valid or next;
3765 $map->save; 3783 $map->save;
3766 } 3784 }
3767 warn "emergency_perl_save: end map save\n"; 3785 info "emergency_perl_save: end map save\n";
3768 3786
3787 cf::write_runtime_sync; # external watchdog should not bark
3788
3769 warn "emergency_perl_save: begin database checkpoint\n"; 3789 info "emergency_perl_save: begin database checkpoint\n";
3770 BDB::db_env_txn_checkpoint $DB_ENV; 3790 BDB::db_env_txn_checkpoint $DB_ENV;
3771 warn "emergency_perl_save: end database checkpoint\n"; 3791 info "emergency_perl_save: end database checkpoint\n";
3772 3792
3773 warn "emergency_perl_save: begin write uuid\n"; 3793 info "emergency_perl_save: begin write uuid\n";
3774 write_uuid_sync 1; 3794 write_uuid_sync 1;
3775 warn "emergency_perl_save: end write uuid\n"; 3795 info "emergency_perl_save: end write uuid\n";
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
3802 info "emergency_perl_save: starting sync()\n";
3803 IO::AIO::aio_sync sub {
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
3776 }; 3815 };
3777 3816
3778 warn "emergency_perl_save: starting sync()\n";
3779 IO::AIO::aio_sync sub {
3780 warn "emergency_perl_save: finished sync()\n";
3781 };
3782
3783 warn "emergency_perl_save: leave\n"; 3817 info "emergency_perl_save: leave\n";
3784} 3818}
3785 3819
3786sub post_cleanup { 3820sub post_cleanup {
3787 my ($make_core) = @_; 3821 my ($make_core) = @_;
3788 3822
3823 IO::AIO::flush;
3824
3825 IO::AIO::flush;
3826
3789 warn Carp::longmess "post_cleanup backtrace" 3827 error Carp::longmess "post_cleanup backtrace"
3790 if $make_core; 3828 if $make_core;
3791 3829
3792 my $fh = pidfile; 3830 my $fh = pidfile;
3793 unlink $PIDFILE if <$fh> == $$; 3831 unlink $PIDFILE if <$fh> == $$;
3794} 3832}
3819} 3857}
3820 3858
3821sub do_reload_perl() { 3859sub do_reload_perl() {
3822 # can/must only be called in main 3860 # can/must only be called in main
3823 if (in_main) { 3861 if (in_main) {
3824 warn "can only reload from main coroutine"; 3862 error "can only reload from main coroutine";
3825 return; 3863 return;
3826 } 3864 }
3827 3865
3828 return if $RELOAD++; 3866 return if $RELOAD++;
3829 3867
3830 my $t1 = AE::time; 3868 my $t1 = AE::time;
3831 3869
3832 while ($RELOAD) { 3870 while ($RELOAD) {
3833 warn "reloading..."; 3871 info "reloading...";
3834 3872
3835 warn "entering sync_job"; 3873 trace "entering sync_job";
3836 3874
3837 cf::sync_job { 3875 cf::sync_job {
3838 cf::write_runtime_sync; # external watchdog should not bark
3839 cf::emergency_save; 3876 cf::emergency_save;
3840 cf::write_runtime_sync; # external watchdog should not bark
3841 3877
3842 warn "syncing database to disk";
3843 BDB::db_env_txn_checkpoint $DB_ENV;
3844
3845 # if anything goes wrong in here, we should simply crash as we already saved
3846
3847 warn "flushing outstanding aio requests";
3848 while (IO::AIO::nreqs || BDB::nreqs) {
3849 Coro::EV::timer_once 0.01; # let the sync_job do it's thing
3850 }
3851
3852 warn "cancelling all extension coros"; 3878 trace "cancelling all extension coros";
3853 $_->cancel for values %EXT_CORO; 3879 $_->cancel for values %EXT_CORO;
3854 %EXT_CORO = (); 3880 %EXT_CORO = ();
3855 3881
3856 warn "removing commands"; 3882 trace "removing commands";
3857 %COMMAND = (); 3883 %COMMAND = ();
3858 3884
3859 warn "removing ext/exti commands"; 3885 trace "removing ext/exti commands";
3860 %EXTCMD = (); 3886 %EXTCMD = ();
3861 %EXTICMD = (); 3887 %EXTICMD = ();
3862 3888
3863 warn "unloading/nuking all extensions"; 3889 trace "unloading/nuking all extensions";
3864 for my $pkg (@EXTS) { 3890 for my $pkg (@EXTS) {
3865 warn "... unloading $pkg"; 3891 trace "... unloading $pkg";
3866 3892
3867 if (my $cb = $pkg->can ("unload")) { 3893 if (my $cb = $pkg->can ("unload")) {
3868 eval { 3894 eval {
3869 $cb->($pkg); 3895 $cb->($pkg);
3870 1 3896 1
3871 } or warn "$pkg unloaded, but with errors: $@"; 3897 } or error "$pkg unloaded, but with errors: $@";
3872 } 3898 }
3873 3899
3874 warn "... clearing $pkg"; 3900 trace "... clearing $pkg";
3875 clear_package $pkg; 3901 clear_package $pkg;
3876 } 3902 }
3877 3903
3878 warn "unloading all perl modules loaded from $LIBDIR"; 3904 trace "unloading all perl modules loaded from $LIBDIR";
3879 while (my ($k, $v) = each %INC) { 3905 while (my ($k, $v) = each %INC) {
3880 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; 3906 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/;
3881 3907
3882 warn "... unloading $k"; 3908 trace "... unloading $k";
3883 delete $INC{$k}; 3909 delete $INC{$k};
3884 3910
3885 $k =~ s/\.pm$//; 3911 $k =~ s/\.pm$//;
3886 $k =~ s/\//::/g; 3912 $k =~ s/\//::/g;
3887 3913
3890 } 3916 }
3891 3917
3892 clear_package $k; 3918 clear_package $k;
3893 } 3919 }
3894 3920
3895 warn "getting rid of safe::, as good as possible"; 3921 trace "getting rid of safe::, as good as possible";
3896 clear_package "safe::$_" 3922 clear_package "safe::$_"
3897 for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); 3923 for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region);
3898 3924
3899 warn "unloading cf.pm \"a bit\""; 3925 trace "unloading cf.pm \"a bit\"";
3900 delete $INC{"cf.pm"}; 3926 delete $INC{"cf.pm"};
3901 delete $INC{"cf/$_.pm"} for @EXTRA_MODULES; 3927 delete $INC{"cf/$_.pm"} for @EXTRA_MODULES;
3902 3928
3903 # don't, removes xs symbols, too, 3929 # don't, removes xs symbols, too,
3904 # and global variables created in xs 3930 # and global variables created in xs
3905 #clear_package __PACKAGE__; 3931 #clear_package __PACKAGE__;
3906 3932
3907 warn "unload completed, starting to reload now"; 3933 info "unload completed, starting to reload now";
3908 3934
3909 warn "reloading cf.pm"; 3935 trace "reloading cf.pm";
3910 require cf; 3936 require cf;
3911 cf::_connect_to_perl_1; 3937 cf::_connect_to_perl_1;
3912 3938
3913 warn "loading config and database again"; 3939 trace "loading config and database again";
3914 cf::reload_config; 3940 cf::reload_config;
3915 3941
3916 warn "loading extensions"; 3942 trace "loading extensions";
3917 cf::load_extensions; 3943 cf::load_extensions;
3918 3944
3919 if ($REATTACH_ON_RELOAD) { 3945 if ($REATTACH_ON_RELOAD) {
3920 warn "reattaching attachments to objects/players"; 3946 trace "reattaching attachments to objects/players";
3921 _global_reattach; # objects, sockets 3947 _global_reattach; # objects, sockets
3922 warn "reattaching attachments to maps"; 3948 trace "reattaching attachments to maps";
3923 reattach $_ for values %MAP; 3949 reattach $_ for values %MAP;
3924 warn "reattaching attachments to players"; 3950 trace "reattaching attachments to players";
3925 reattach $_ for values %PLAYER; 3951 reattach $_ for values %PLAYER;
3926 } 3952 }
3927 3953
3928 warn "running post_init jobs"; 3954 trace "running post_init jobs";
3929 (pop @POST_INIT)->(1) while @POST_INIT; 3955 (pop @POST_INIT)->(1) while @POST_INIT;
3930 3956
3931 warn "leaving sync_job"; 3957 trace "leaving sync_job";
3932 3958
3933 1 3959 1
3934 } or do { 3960 } or do {
3935 warn $@; 3961 error $@;
3936 cf::cleanup "error while reloading, exiting."; 3962 cf::cleanup "error while reloading, exiting.";
3937 }; 3963 };
3938 3964
3939 warn "reloaded"; 3965 info "reloaded";
3940 --$RELOAD; 3966 --$RELOAD;
3941 } 3967 }
3942 3968
3943 $t1 = AE::time - $t1; 3969 $t1 = AE::time - $t1;
3944 warn "reload completed in ${t1}s\n"; 3970 info "reload completed in ${t1}s\n";
3945}; 3971};
3946 3972
3947our $RELOAD_WATCHER; # used only during reload 3973our $RELOAD_WATCHER; # used only during reload
3948 3974
3949sub reload_perl() { 3975sub reload_perl() {
4009 if ($NOW >= $NEXT_RUNTIME_WRITE) { 4035 if ($NOW >= $NEXT_RUNTIME_WRITE) {
4010 $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.; 4036 $NEXT_RUNTIME_WRITE = List::Util::max $NEXT_RUNTIME_WRITE + 10, $NOW + 5.;
4011 Coro::async_pool { 4037 Coro::async_pool {
4012 $Coro::current->{desc} = "runtime saver"; 4038 $Coro::current->{desc} = "runtime saver";
4013 write_runtime_sync 4039 write_runtime_sync
4014 or warn "ERROR: unable to write runtime file: $!"; 4040 or error "ERROR: unable to write runtime file: $!";
4015 }; 4041 };
4016 } 4042 }
4017 4043
4018 if (my $sig = shift @WAIT_FOR_TICK_BEGIN) { 4044 if (my $sig = shift @WAIT_FOR_TICK_BEGIN) {
4019 $sig->send; 4045 $sig->send;
4027 4053
4028 if (0) { 4054 if (0) {
4029 if ($NEXT_TICK) { 4055 if ($NEXT_TICK) {
4030 my $jitter = $TICK_START - $NEXT_TICK; 4056 my $jitter = $TICK_START - $NEXT_TICK;
4031 $JITTER = $JITTER * 0.75 + $jitter * 0.25; 4057 $JITTER = $JITTER * 0.75 + $jitter * 0.25;
4032 warn "jitter $JITTER\n";#d# 4058 debug "jitter $JITTER\n";#d#
4033 } 4059 }
4034 } 4060 }
4035} 4061}
4036 4062
4037{ 4063{

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines