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.181 by root, Fri Jan 19 15:29:52 2007 UTC vs.
Revision 1.189 by root, Sun Jan 21 21:28:27 2007 UTC

349 my $runtime = cf::localdir . "/runtime"; 349 my $runtime = cf::localdir . "/runtime";
350 350
351 my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644 351 my $fh = aio_open "$runtime~", O_WRONLY | O_CREAT, 0644
352 or return; 352 or return;
353 353
354 my $value = $cf::RUNTIME + 90 + 10;
354 my $value = $cf::RUNTIME + 1 + 10; # 10 is the runtime save interval, for a monotonic clock 355 # 10 is the runtime save interval, for a monotonic clock
356 # 60 allows for the watchdog to kill the server.
357
355 (aio_write $fh, 0, (length $value), $value, 0) <= 0 358 (aio_write $fh, 0, (length $value), $value, 0) <= 0
356 and return; 359 and return;
357 360
358 aio_fsync $fh 361 aio_fsync $fh
359 and return; 362 and return;
1137 $rmp->{map_layout_style}, $rmp->{treasureoptions}, $rmp->{symmetry_used}, 1140 $rmp->{map_layout_style}, $rmp->{treasureoptions}, $rmp->{symmetry_used},
1138 (cf::region::find $rmp->{region}), $rmp->{custom} 1141 (cf::region::find $rmp->{region}), $rmp->{custom}
1139 ) 1142 )
1140} 1143}
1141 1144
1145=item cf::map->register ($regex, $prio)
1146
1147Register a handler for the map path matching the given regex at the
1148givne priority (higher is better, built-in handlers have priority 0, the
1149default).
1150
1151=cut
1152
1142sub register { 1153sub register {
1143 my (undef, $regex) = @_; 1154 my (undef, $regex, $prio) = @_;
1144 my $pkg = caller; 1155 my $pkg = caller;
1145 1156
1146 no strict; 1157 no strict;
1147 push @{"$pkg\::ISA"}, __PACKAGE__; 1158 push @{"$pkg\::ISA"}, __PACKAGE__;
1148 1159
1149 $EXT_MAP{$pkg} = qr<$regex>; 1160 $EXT_MAP{$pkg} = [$prio, qr<$regex>];
1150} 1161}
1151 1162
1152# also paths starting with '/' 1163# also paths starting with '/'
1153$EXT_MAP{"cf::map"} = qr{^(?=/)}; 1164$EXT_MAP{"cf::map"} = [0, qr{^(?=/)}];
1154 1165
1155sub thawer_merge { 1166sub thawer_merge {
1156 my ($self, $merge) = @_; 1167 my ($self, $merge) = @_;
1157 1168
1158 # we have to keep some variables in memory intact 1169 # we have to keep some variables in memory intact
1201 1212
1202 return $path if UNIVERSAL::isa $path, "cf::map"; # already a map object 1213 return $path if UNIVERSAL::isa $path, "cf::map"; # already a map object
1203 1214
1204 $path = normalise $path, $base; 1215 $path = normalise $path, $base;
1205 1216
1206 for my $pkg (keys %EXT_MAP) { 1217 for my $pkg (sort { $EXT_MAP{$b}[0] <=> $EXT_MAP{$a}[0] } keys %EXT_MAP) {
1207 if ($path =~ $EXT_MAP{$pkg}) { 1218 if ($path =~ $EXT_MAP{$pkg}[1]) {
1208 my $self = bless cf::map::new, $pkg; 1219 my $self = bless cf::map::new, $pkg;
1209 $self->{path} = $path; $self->path ($path); 1220 $self->{path} = $path; $self->path ($path);
1210 $self->init; # pass $1 etc. 1221 $self->init; # pass $1 etc.
1211 return $self; 1222 return $self;
1212 } 1223 }
1286 $self->{load_path} = $path; 1297 $self->{load_path} = $path;
1287 1298
1288 1 1299 1
1289} 1300}
1290 1301
1291sub load_orig { 1302sub load_header_orig {
1292 my ($self) = @_; 1303 my ($self) = @_;
1293 1304
1294 $self->load_header_from ($self->load_path) 1305 $self->load_header_from ($self->load_path)
1295} 1306}
1296 1307
1297sub load_temp { 1308sub load_header_temp {
1298 my ($self) = @_; 1309 my ($self) = @_;
1299 1310
1300 $self->load_header_from ($self->save_path) 1311 $self->load_header_from ($self->save_path)
1312}
1313
1314sub prepare_temp {
1315 my ($self) = @_;
1316
1317 $self->last_access ((delete $self->{last_access})
1318 || $cf::RUNTIME); #d#
1319 # safety
1320 $self->{instantiate_time} = $cf::RUNTIME
1321 if $self->{instantiate_time} > $cf::RUNTIME;
1322}
1323
1324sub prepare_orig {
1325 my ($self) = @_;
1326
1327 $self->{load_original} = 1;
1328 $self->{instantiate_time} = $cf::RUNTIME;
1329 $self->last_access ($cf::RUNTIME);
1330 $self->instantiate;
1301} 1331}
1302 1332
1303sub load_header { 1333sub load_header {
1304 my ($self) = @_; 1334 my ($self) = @_;
1305 1335
1306 if ($self->load_temp) { 1336 if ($self->load_header_temp) {
1307 $self->last_access ((delete $self->{last_access}) 1337 $self->prepare_temp;
1308 || $cf::RUNTIME); #d#
1309 # safety
1310 $self->{instantiate_time} = $cf::RUNTIME
1311 if $self->{instantiate_time} > $cf::RUNTIME;
1312 } else { 1338 } else {
1313 $self->load_orig 1339 $self->load_header_orig
1314 or return; 1340 or return;
1315 1341 $self->prepare_orig;
1316 $self->{load_original} = 1;
1317 $self->{instantiate_time} = $cf::RUNTIME;
1318 $self->last_access ($cf::RUNTIME);
1319 $self->instantiate;
1320 } 1342 }
1321 1343
1322 1 1344 1
1323} 1345}
1324 1346
1339 1361
1340 $map->load_header 1362 $map->load_header
1341 or return; 1363 or return;
1342 1364
1343 if ($map->should_reset) { 1365 if ($map->should_reset) {
1366 # doing this can freeze the server in a sync job, obviously
1344 $cf::WAIT_FOR_TICK->wait; 1367 #$cf::WAIT_FOR_TICK->wait;
1345 $map->reset; 1368 $map->reset;
1346 undef $guard; 1369 undef $guard;
1347 $map = find $path 1370 $map = find $path
1348 or return; 1371 or return;
1349 } 1372 }
1350 1373
1351 $cf::MAP{$path} = $map 1374 $cf::MAP{$path} = $map
1352 } 1375 }
1353} 1376}
1354 1377
1378sub pre_load { }
1379sub post_load { }
1380
1355sub load { 1381sub load {
1356 my ($self) = @_; 1382 my ($self) = @_;
1357 1383
1358 my $path = $self->{path}; 1384 my $path = $self->{path};
1359 my $guard = cf::lock_acquire "map_load:$path"; 1385 my $guard = cf::lock_acquire "map_load:$path";
1361 return if $self->in_memory != cf::MAP_SWAPPED; 1387 return if $self->in_memory != cf::MAP_SWAPPED;
1362 1388
1363 $self->in_memory (cf::MAP_LOADING); 1389 $self->in_memory (cf::MAP_LOADING);
1364 1390
1365 $self->alloc; 1391 $self->alloc;
1392
1393 $self->pre_load;
1394
1366 $self->_load_objects ($self->{load_path}, 1) 1395 $self->_load_objects ($self->{load_path}, 1)
1367 or return; 1396 or return;
1368 1397
1369 $self->set_object_flag (cf::FLAG_OBJ_ORIGINAL, 1) 1398 $self->set_object_flag (cf::FLAG_OBJ_ORIGINAL, 1)
1370 if delete $self->{load_original}; 1399 if delete $self->{load_original};
1392 unless $self->difficulty; 1421 unless $self->difficulty;
1393 Coro::cede; 1422 Coro::cede;
1394 $self->activate; 1423 $self->activate;
1395 } 1424 }
1396 1425
1426 $self->post_load;
1427
1397 $self->in_memory (cf::MAP_IN_MEMORY); 1428 $self->in_memory (cf::MAP_IN_MEMORY);
1398} 1429}
1399 1430
1400sub customise_for { 1431sub customise_for {
1401 my ($self, $ob) = @_; 1432 my ($self, $ob) = @_;
1441 1472
1442 cf::sync_job { $map->load }; 1473 cf::sync_job { $map->load };
1443} 1474}
1444 1475
1445our %MAP_PREFETCH; 1476our %MAP_PREFETCH;
1446our $MAP_PREFETCHER = Coro::async { 1477our $MAP_PREFETCHER = undef;
1447 while () {
1448 for my $path (keys %MAP_PREFETCH) {
1449 my $map = find $path
1450 or next;
1451 $map->load;
1452
1453 delete $MAP_PREFETCH{$path};
1454 }
1455 Coro::schedule;
1456 }
1457};
1458 1478
1459sub find_async { 1479sub find_async {
1460 my ($path, $origin) = @_; 1480 my ($path, $origin) = @_;
1461 1481
1462 $path = normalise $path, $origin && $origin->{path}; 1482 $path = normalise $path, $origin && $origin->{path};
1463 1483
1464 if (my $map = $cf::MAP{$path}) { 1484 if (my $map = $cf::MAP{$path}) {
1465 return $map if $map->in_memory == cf::MAP_IN_MEMORY; 1485 return $map if $map->in_memory == cf::MAP_IN_MEMORY;
1466 } 1486 }
1467 1487
1468 $MAP_PREFETCH{$path} = 0; 1488 undef $MAP_PREFETCH{$path};
1489 $MAP_PREFETCHER ||= cf::async {
1490 while (%MAP_PREFETCH) {
1491 for my $path (keys %MAP_PREFETCH) {
1492 my $map = find $path
1493 or next;
1494 $map->load;
1495
1496 delete $MAP_PREFETCH{$path};
1497 }
1498 }
1499 undef $MAP_PREFETCHER;
1500 };
1469 $MAP_PREFETCHER->ready; 1501 $MAP_PREFETCHER->prio (6);
1470 1502
1471 () 1503 ()
1472} 1504}
1473 1505
1474sub save { 1506sub save {
1815 1847
1816 my $rmp = { # defaults 1848 my $rmp = { # defaults
1817 xsize => (cf::rndm 15, 40), 1849 xsize => (cf::rndm 15, 40),
1818 ysize => (cf::rndm 15, 40), 1850 ysize => (cf::rndm 15, 40),
1819 symmetry => (cf::rndm 1, cf::SYMMETRY_XY), 1851 symmetry => (cf::rndm 1, cf::SYMMETRY_XY),
1820 #layout => string 1852 #layout => string,
1821 }; 1853 };
1822 1854
1823 for (split /\n/, $spec) { 1855 for (split /\n/, $spec) {
1824 my ($k, $v) = split /\s+/, $_, 2; 1856 my ($k, $v) = split /\s+/, $_, 2;
1825 1857
2174 sub db_sync() { 2206 sub db_sync() {
2175 db_save if $dirty; 2207 db_save if $dirty;
2176 undef $dirty; 2208 undef $dirty;
2177 } 2209 }
2178 2210
2179 my $idle = Event->idle (min => $TICK * 2.8, max => 10, repeat => 0, data => WF_AUTOCANCEL, cb => sub { 2211 my $idle = Event->idle (
2180 db_sync; 2212 reentrant => 0,
2213 min => 10,
2214 max => 20,
2215 repeat => 0,
2216 data => WF_AUTOCANCEL,
2217 cb => \&db_sync,
2181 }); 2218 );
2182 2219
2183 sub db_dirty() { 2220 sub db_dirty() {
2184 $dirty = 1; 2221 $dirty = 1;
2185 $idle->start; 2222 $idle->start;
2186 } 2223 }
2242 }; 2279 };
2243 2280
2244 cfg_load; 2281 cfg_load;
2245 db_load; 2282 db_load;
2246 load_extensions; 2283 load_extensions;
2284
2285 $TICK_WATCHER->start;
2247 Event::loop; 2286 Event::loop;
2248} 2287}
2249 2288
2250############################################################################# 2289#############################################################################
2251# initialisation and cleanup 2290# initialisation and cleanup
2252 2291
2253# install some emergency cleanup handlers 2292# install some emergency cleanup handlers
2254BEGIN { 2293BEGIN {
2255 for my $signal (qw(INT HUP TERM)) { 2294 for my $signal (qw(INT HUP TERM)) {
2256 Event->signal ( 2295 Event->signal (
2296 reentrant => 0,
2257 data => WF_AUTOCANCEL, 2297 data => WF_AUTOCANCEL,
2258 signal => $signal, 2298 signal => $signal,
2259 cb => sub { 2299 cb => sub {
2260 cf::cleanup "SIG$signal"; 2300 cf::cleanup "SIG$signal";
2261 }, 2301 },
2262 ); 2302 );
2263 } 2303 }
2264} 2304}
2293} 2333}
2294 2334
2295sub reload() { 2335sub reload() {
2296 # can/must only be called in main 2336 # can/must only be called in main
2297 if ($Coro::current != $Coro::main) { 2337 if ($Coro::current != $Coro::main) {
2298 warn "can only reload from main coroutine\n"; 2338 warn "can only reload from main coroutine";
2299 return; 2339 return;
2300 } 2340 }
2301 2341
2302 warn "reloading..."; 2342 warn "reloading...";
2303 2343
2304 warn "freezing server"; 2344 warn "cancelling server ticker";
2305 my $guard = freeze_mainloop; 2345 $TICK_WATCHER->cancel;
2346
2306 cf::emergency_save; 2347 cf::emergency_save;
2307
2308 warn "sync database to disk";
2309 cf::db_sync;
2310 IO::AIO::flush;
2311 2348
2312 eval { 2349 eval {
2313 # if anything goes wrong in here, we should simply crash as we already saved 2350 # if anything goes wrong in here, we should simply crash as we already saved
2314 2351
2352 warn "syncing database to disk";
2353 cf::db_sync;
2354
2315 warn "cancel all watchers"; 2355 warn "cancelling all WF_AUTOCANCEL watchers";
2316 for (Event::all_watchers) { 2356 for (Event::all_watchers) {
2317 $_->cancel if $_->data & WF_AUTOCANCEL; 2357 $_->cancel if $_->data & WF_AUTOCANCEL;
2318 } 2358 }
2319 2359
2360 warn "flushing outstanding aio requests";
2361 for (;;) {
2362 IO::AIO::flush;
2363 Coro::cede;
2364 last unless IO::AIO::nreqs;
2365 warn "iterate...";
2366 }
2367
2320 warn "cancel all extension coros"; 2368 warn "cancelling all extension coros";
2321 $_->cancel for values %EXT_CORO; 2369 $_->cancel for values %EXT_CORO;
2322 %EXT_CORO = (); 2370 %EXT_CORO = ();
2323 2371
2324 warn "remove commands"; 2372 warn "removing commands";
2325 %COMMAND = (); 2373 %COMMAND = ();
2326 2374
2327 warn "remove ext commands"; 2375 warn "removing ext commands";
2328 %EXTCMD = (); 2376 %EXTCMD = ();
2329 2377
2330 warn "unload/nuke all extensions"; 2378 warn "unloading/nuking all extensions";
2331 for my $pkg (@EXTS) { 2379 for my $pkg (@EXTS) {
2332 warn "... unloading $pkg"; 2380 warn "... unloading $pkg";
2333 2381
2334 if (my $cb = $pkg->can ("unload")) { 2382 if (my $cb = $pkg->can ("unload")) {
2335 eval { 2383 eval {
2340 2388
2341 warn "... nuking $pkg"; 2389 warn "... nuking $pkg";
2342 Symbol::delete_package $pkg; 2390 Symbol::delete_package $pkg;
2343 } 2391 }
2344 2392
2345 warn "unload all perl modules loaded from $LIBDIR"; 2393 warn "unloading all perl modules loaded from $LIBDIR";
2346 while (my ($k, $v) = each %INC) { 2394 while (my ($k, $v) = each %INC) {
2347 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; 2395 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/;
2348 2396
2349 warn "removing <$k>"; 2397 warn "... unloading $k";
2350 delete $INC{$k}; 2398 delete $INC{$k};
2351 2399
2352 $k =~ s/\.pm$//; 2400 $k =~ s/\.pm$//;
2353 $k =~ s/\//::/g; 2401 $k =~ s/\//::/g;
2354 2402
2357 } 2405 }
2358 2406
2359 Symbol::delete_package $k; 2407 Symbol::delete_package $k;
2360 } 2408 }
2361 2409
2362 warn "get rid of safe::, as good as possible"; 2410 warn "getting rid of safe::, as good as possible";
2363 Symbol::delete_package "safe::$_" 2411 Symbol::delete_package "safe::$_"
2364 for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); 2412 for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region);
2365 2413
2366 warn "unload cf.pm \"a bit\""; 2414 warn "unloading cf.pm \"a bit\"";
2367 delete $INC{"cf.pm"}; 2415 delete $INC{"cf.pm"};
2368 2416
2369 # don't, removes xs symbols, too, 2417 # don't, removes xs symbols, too,
2370 # and global variables created in xs 2418 # and global variables created in xs
2371 #Symbol::delete_package __PACKAGE__; 2419 #Symbol::delete_package __PACKAGE__;
2372 2420
2421 warn "unload completed, starting to reload now";
2422
2373 warn "reloading cf.pm"; 2423 warn "reloading cf.pm";
2374 require cf; 2424 require cf;
2375 cf::_connect_to_perl; # nominally unnecessary, but cannot hurt 2425 cf::_connect_to_perl; # nominally unnecessary, but cannot hurt
2376 2426
2377 warn "load config and database again"; 2427 warn "loading config and database again";
2378 cf::cfg_load; 2428 cf::cfg_load;
2379 cf::db_load; 2429 cf::db_load;
2380 2430
2381 warn "load extensions"; 2431 warn "loading extensions";
2382 cf::load_extensions; 2432 cf::load_extensions;
2383 2433
2384 warn "reattach attachments to objects/players"; 2434 warn "reattaching attachments to objects/players";
2385 _global_reattach; 2435 _global_reattach;
2386 warn "reattach attachments to maps"; 2436 warn "reattaching attachments to maps";
2387 reattach $_ for values %MAP; 2437 reattach $_ for values %MAP;
2438
2439 warn "restarting server ticker";
2440
2441 $TICK_WATCHER->start;
2388 }; 2442 };
2389 2443
2390 if ($@) { 2444 if ($@) {
2391 warn $@; 2445 warn $@;
2392 warn "error while reloading, exiting."; 2446 warn "error while reloading, exiting.";
2405 $who->message ("reloading server."); 2459 $who->message ("reloading server.");
2406 2460
2407 # doing reload synchronously and two reloads happen back-to-back, 2461 # doing reload synchronously and two reloads happen back-to-back,
2408 # coro crashes during coro_state_free->destroy here. 2462 # coro crashes during coro_state_free->destroy here.
2409 2463
2410 $RELOAD_WATCHER ||= Event->timer (after => 0, data => WF_AUTOCANCEL, cb => sub { 2464 $RELOAD_WATCHER ||= Event->timer (
2465 reentrant => 0,
2466 after => 0,
2467 data => WF_AUTOCANCEL,
2468 cb => sub {
2411 reload; 2469 reload;
2412 undef $RELOAD_WATCHER; 2470 undef $RELOAD_WATCHER;
2471 },
2413 }); 2472 );
2414 } 2473 }
2415}; 2474};
2416 2475
2417unshift @INC, $LIBDIR; 2476unshift @INC, $LIBDIR;
2477
2478my $bug_warning = 0;
2418 2479
2419$TICK_WATCHER = Event->timer ( 2480$TICK_WATCHER = Event->timer (
2420 reentrant => 0, 2481 reentrant => 0,
2482 parked => 1,
2421 prio => 0, 2483 prio => 0,
2422 at => $NEXT_TICK || $TICK, 2484 at => $NEXT_TICK || $TICK,
2423 data => WF_AUTOCANCEL, 2485 data => WF_AUTOCANCEL,
2424 cb => sub { 2486 cb => sub {
2487 if ($Coro::current != $Coro::main) {
2488 Carp::cluck "major BUG: server tick called outside of main coro, skipping it"
2489 unless ++$bug_warning > 10;
2490 return;
2491 }
2492
2425 $NOW = Event::time; 2493 $NOW = Event::time;
2426 2494
2427 cf::server_tick; # one server iteration 2495 cf::server_tick; # one server iteration
2428 $RUNTIME += $TICK; 2496 $RUNTIME += $TICK;
2429 $NEXT_TICK += $TICK; 2497 $NEXT_TICK += $TICK;
2439 }, 2507 },
2440); 2508);
2441 2509
2442IO::AIO::max_poll_time $TICK * 0.2; 2510IO::AIO::max_poll_time $TICK * 0.2;
2443 2511
2512undef $Coro::AIO::WATCHER;
2444$AIO_POLL_WATCHER = Event->io ( 2513$AIO_POLL_WATCHER = Event->io (
2514 reentrant => 0,
2445 fd => IO::AIO::poll_fileno, 2515 fd => IO::AIO::poll_fileno,
2446 poll => 'r', 2516 poll => 'r',
2447 prio => 5, 2517 prio => 6,
2448 data => WF_AUTOCANCEL, 2518 data => WF_AUTOCANCEL,
2449 cb => \&IO::AIO::poll_cb, 2519 cb => \&IO::AIO::poll_cb,
2450); 2520);
2451 2521
2452$WRITE_RUNTIME_WATCHER = Event->timer ( 2522$WRITE_RUNTIME_WATCHER = Event->timer (
2523 reentrant => 0,
2453 data => WF_AUTOCANCEL, 2524 data => WF_AUTOCANCEL,
2454 after => 0, 2525 after => 1,
2455 interval => 10, 2526 interval => 10,
2456 cb => sub { 2527 prio => 6, # keep it lowest so it acts like a watchdog
2457 (Coro::unblock_sub { 2528 cb => Coro::unblock_sub {
2458 write_runtime 2529 write_runtime
2459 or warn "ERROR: unable to write runtime file: $!"; 2530 or warn "ERROR: unable to write runtime file: $!";
2460 })->();
2461 }, 2531 },
2462); 2532);
2463 2533
2464END { cf::emergency_save } 2534END { cf::emergency_save }
2465 2535

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines