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.355 by root, Sun Sep 2 12:45:44 2007 UTC vs.
Revision 1.363 by root, Mon Sep 10 17:24:36 2007 UTC

80our $NEXT_TICK; 80our $NEXT_TICK;
81our $NOW; 81our $NOW;
82our $USE_FSYNC = 1; # use fsync to write maps - default off 82our $USE_FSYNC = 1; # use fsync to write maps - default off
83 83
84our $BDB_POLL_WATCHER; 84our $BDB_POLL_WATCHER;
85our $BDB_CHECKPOINT_WATCHER;
86our $BDB_TRICKLE_WATCHER;
85our $DB_ENV; 87our $DB_ENV;
86 88
87our %CFG; 89our %CFG;
88 90
89our $UPTIME; $UPTIME ||= time; 91our $UPTIME; $UPTIME ||= time;
90our $RUNTIME; 92our $RUNTIME;
91 93
92our %PLAYER; # all users 94our (%PLAYER, %PLAYER_LOADING); # all users
93our %MAP; # all maps 95our (%MAP, %MAP_LOADING ); # all maps
94our $LINK_MAP; # the special {link} map, which is always available 96our $LINK_MAP; # the special {link} map, which is always available
95 97
96# used to convert map paths into valid unix filenames by replacing / by ∕ 98# used to convert map paths into valid unix filenames by replacing / by ∕
97our $PATH_SEP = "∕"; # U+2215, chosen purely for visual reasons 99our $PATH_SEP = "∕"; # U+2215, chosen purely for visual reasons
98 100
187 $msg .= "\n" 189 $msg .= "\n"
188 unless $msg =~ /\n$/; 190 unless $msg =~ /\n$/;
189 191
190 $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge; 192 $msg =~ s/([\x00-\x08\x0b-\x1f])/sprintf "\\x%02x", ord $1/ge;
191 193
192 utf8::encode $msg;
193 LOG llevError, $msg; 194 LOG llevError, $msg;
194 }; 195 };
195} 196}
196 197
197@safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable'; 198@safe::cf::global::ISA = @cf::global::ISA = 'cf::attachable';
285Return true if the lock is currently active, i.e. somebody has locked it. 286Return true if the lock is currently active, i.e. somebody has locked it.
286 287
287=cut 288=cut
288 289
289our %LOCK; 290our %LOCK;
291our %LOCKER;#d#
290 292
291sub lock_wait($) { 293sub lock_wait($) {
292 my ($key) = @_; 294 my ($key) = @_;
295
296 if ($LOCKER{$key} == $Coro::current) {#d#
297 Carp::cluck "lock_wait($key) for already-acquired lock";#d#
298 return;#d#
299 }#d#
293 300
294 # wait for lock, if any 301 # wait for lock, if any
295 while ($LOCK{$key}) { 302 while ($LOCK{$key}) {
296 push @{ $LOCK{$key} }, $Coro::current; 303 push @{ $LOCK{$key} }, $Coro::current;
297 Coro::schedule; 304 Coro::schedule;
303 310
304 # wait, to be sure we are not locked 311 # wait, to be sure we are not locked
305 lock_wait $key; 312 lock_wait $key;
306 313
307 $LOCK{$key} = []; 314 $LOCK{$key} = [];
315 $LOCKER{$key} = $Coro::current;#d#
308 316
309 Coro::guard { 317 Coro::guard {
318 delete $LOCKER{$key};#d#
310 # wake up all waiters, to be on the safe side 319 # wake up all waiters, to be on the safe side
311 $_->ready for @{ delete $LOCK{$key} }; 320 $_->ready for @{ delete $LOCK{$key} };
312 } 321 }
313} 322}
314 323
500=item cf::db_put $family => $key => $value 509=item cf::db_put $family => $key => $value
501 510
502Stores the given C<$value> in the family. It can currently store binary 511Stores the given C<$value> in the family. It can currently store binary
503data only (use Compress::LZF::sfreeze_cr/sthaw to convert to/from binary). 512data only (use Compress::LZF::sfreeze_cr/sthaw to convert to/from binary).
504 513
514=item $db = cf::db_table "name"
515
516Create and/or open a new database table. The string must not be "db" and must be unique
517within each server.
518
505=cut 519=cut
520
521sub db_table($) {
522 my ($name) = @_;
523 my $db = BDB::db_create $DB_ENV;
524
525 eval {
526 $db->set_flags (BDB::CHKSUM);
527
528 utf8::encode $name;
529 BDB::db_open $db, undef, $name, undef, BDB::BTREE,
530 BDB::CREATE | BDB::AUTO_COMMIT, 0666;
531 cf::cleanup "db_open(db): $!" if $!;
532 };
533 cf::cleanup "db_open(db): $@" if $@;
534
535 $db
536}
506 537
507our $DB; 538our $DB;
508 539
509sub db_init { 540sub db_init {
510 unless ($DB) {
511 $DB = BDB::db_create $DB_ENV;
512
513 cf::sync_job { 541 cf::sync_job {
514 eval { 542 $DB ||= db_table "db";
515 $DB->set_flags (BDB::CHKSUM);
516
517 BDB::db_open $DB, undef, "db", undef, BDB::BTREE,
518 BDB::CREATE | BDB::AUTO_COMMIT, 0666;
519 cf::cleanup "db_open(db): $!" if $!;
520 };
521 cf::cleanup "db_open(db): $@" if $@;
522 };
523 } 543 };
524} 544}
525 545
526sub db_get($$) { 546sub db_get($$) {
527 my $key = "$_[0]/$_[1]"; 547 my $key = "$_[0]/$_[1]";
528 548
1005); 1025);
1006 1026
1007sub object_freezer_save { 1027sub object_freezer_save {
1008 my ($filename, $rdata, $objs) = @_; 1028 my ($filename, $rdata, $objs) = @_;
1009 1029
1030 my $guard = cf::lock_acquire "io";
1031
1010 sync_job { 1032 sync_job {
1011 if (length $$rdata) { 1033 if (length $$rdata) {
1034 utf8::decode (my $decname = $filename);
1012 warn sprintf "saving %s (%d,%d)\n", 1035 warn sprintf "saving %s (%d,%d)\n",
1013 $filename, length $$rdata, scalar @$objs; 1036 $decname, length $$rdata, scalar @$objs;
1014 1037
1015 if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) { 1038 if (my $fh = aio_open "$filename~", O_WRONLY | O_CREAT, 0600) {
1016 chmod SAVE_MODE, $fh; 1039 chmod SAVE_MODE, $fh;
1017 aio_write $fh, 0, (length $$rdata), $$rdata, 0; 1040 aio_write $fh, 0, (length $$rdata), $$rdata, 0;
1018 aio_fsync $fh if $cf::USE_FSYNC; 1041 aio_fsync $fh if $cf::USE_FSYNC;
1037 } 1060 }
1038 } else { 1061 } else {
1039 aio_unlink $filename; 1062 aio_unlink $filename;
1040 aio_unlink "$filename.pst"; 1063 aio_unlink "$filename.pst";
1041 } 1064 }
1042 } 1065 };
1066
1067 undef $guard;
1043} 1068}
1044 1069
1045sub object_freezer_as_string { 1070sub object_freezer_as_string {
1046 my ($rdata, $objs) = @_; 1071 my ($rdata, $objs) = @_;
1047 1072
1052 1077
1053sub object_thawer_load { 1078sub object_thawer_load {
1054 my ($filename) = @_; 1079 my ($filename) = @_;
1055 1080
1056 my ($data, $av); 1081 my ($data, $av);
1082
1083 my $guard = cf::lock_acquire "io";
1057 1084
1058 (aio_load $filename, $data) >= 0 1085 (aio_load $filename, $data) >= 0
1059 or return; 1086 or return;
1060 1087
1061 unless (aio_stat "$filename.pst") { 1088 unless (aio_stat "$filename.pst") {
1062 (aio_load "$filename.pst", $av) >= 0 1089 (aio_load "$filename.pst", $av) >= 0
1063 or return; 1090 or return;
1091
1092 undef $guard;
1064 $av = eval { (Storable::thaw $av)->{objs} }; 1093 $av = eval { (Storable::thaw $av)->{objs} };
1065 } 1094 }
1066 1095
1096 utf8::decode (my $decname = $filename);
1067 warn sprintf "loading %s (%d)\n", 1097 warn sprintf "loading %s (%d,%d)\n",
1068 $filename, length $data, scalar @{$av || []}; 1098 $decname, length $data, scalar @{$av || []};
1099
1069 return ($data, $av); 1100 ($data, $av)
1070} 1101}
1071 1102
1072=head2 COMMAND CALLBACKS 1103=head2 COMMAND CALLBACKS
1073 1104
1074=over 4 1105=over 4
1260 1291
1261=head3 cf::player 1292=head3 cf::player
1262 1293
1263=over 4 1294=over 4
1264 1295
1296=item cf::player::num_playing
1297
1298Returns the official number of playing players, as per the Crossfire metaserver rules.
1299
1300=cut
1301
1302sub num_playing {
1303 scalar grep
1304 $_->ob->map
1305 && !$_->hidden
1306 && !$_->ob->flag (cf::FLAG_WIZ),
1307 cf::player::list
1308}
1309
1265=item cf::player::find $login 1310=item cf::player::find $login
1266 1311
1267Returns the given player object, loading it if necessary (might block). 1312Returns the given player object, loading it if necessary (might block).
1268 1313
1269=cut 1314=cut
1304 aio_link +(playerdir $login) . "/$login.pl.pst", (playerdir $login) . "/playerdata.pst"; 1349 aio_link +(playerdir $login) . "/$login.pl.pst", (playerdir $login) . "/playerdata.pst";
1305 aio_link +(playerdir $login) . "/$login.pl" , (playerdir $login) . "/playerdata"; 1350 aio_link +(playerdir $login) . "/$login.pl" , (playerdir $login) . "/playerdata";
1306 aio_unlink +(playerdir $login) . "/$login.pl.pst"; 1351 aio_unlink +(playerdir $login) . "/$login.pl.pst";
1307 aio_unlink +(playerdir $login) . "/$login.pl"; 1352 aio_unlink +(playerdir $login) . "/$login.pl";
1308 1353
1309 my $pl = load_pl path $login 1354 my $f = new_from_file cf::object::thawer path $login
1310 or return; 1355 or return;
1356
1357 $f->next;
1358 my $pl = cf::player::load_pl $f
1359 or return;
1360 local $cf::PLAYER_LOADING{$login} = $pl;
1361 $f->resolve_delayed_derefs;
1311 $cf::PLAYER{$login} = $pl 1362 $cf::PLAYER{$login} = $pl
1312 } 1363 }
1313 } 1364 }
1314} 1365}
1315 1366
1755 1806
1756sub load_header_from($) { 1807sub load_header_from($) {
1757 my ($self, $path) = @_; 1808 my ($self, $path) = @_;
1758 1809
1759 utf8::encode $path; 1810 utf8::encode $path;
1760 #aio_open $path, O_RDONLY, 0 1811 my $f = new_from_file cf::object::thawer $path
1761 # or return;
1762
1763 $self->_load_header ($path)
1764 or return; 1812 or return;
1813
1814 $self->_load_header ($f)
1815 or return;
1816
1817 local $MAP_LOADING{$self->{path}} = $self;
1818 $f->resolve_delayed_derefs;
1765 1819
1766 $self->{load_path} = $path; 1820 $self->{load_path} = $path;
1767 1821
1768 1 1822 1
1769} 1823}
1823sub find { 1877sub find {
1824 my ($path, $origin) = @_; 1878 my ($path, $origin) = @_;
1825 1879
1826 $path = normalise $path, $origin && $origin->path; 1880 $path = normalise $path, $origin && $origin->path;
1827 1881
1882 cf::lock_wait "map_data:$path";#d#remove
1828 cf::lock_wait "map_find:$path"; 1883 cf::lock_wait "map_find:$path";
1829 1884
1830 $cf::MAP{$path} || do { 1885 $cf::MAP{$path} || do {
1831 my $guard = cf::lock_acquire "map_find:$path"; 1886 my $guard1 = cf::lock_acquire "map_find:$path";
1887 my $guard2 = cf::lock_acquire "map_data:$path"; # just for the fun of it
1888
1832 my $map = new_from_path cf::map $path 1889 my $map = new_from_path cf::map $path
1833 or return; 1890 or return;
1834 1891
1835 $map->{last_save} = $cf::RUNTIME; 1892 $map->{last_save} = $cf::RUNTIME;
1836 1893
1838 or return; 1895 or return;
1839 1896
1840 if ($map->should_reset) {#d#TODO# disabled, crashy (locking issue?) 1897 if ($map->should_reset) {#d#TODO# disabled, crashy (locking issue?)
1841 # doing this can freeze the server in a sync job, obviously 1898 # doing this can freeze the server in a sync job, obviously
1842 #$cf::WAIT_FOR_TICK->wait; 1899 #$cf::WAIT_FOR_TICK->wait;
1900 undef $guard1;
1901 undef $guard2;
1843 $map->reset; 1902 $map->reset;
1844 undef $guard;
1845 return find $path; 1903 return find $path;
1846 } 1904 }
1847 1905
1848 $cf::MAP{$path} = $map 1906 $cf::MAP{$path} = $map
1849 } 1907 }
1858 local $self->{deny_reset} = 1; # loading can take a long time 1916 local $self->{deny_reset} = 1; # loading can take a long time
1859 1917
1860 my $path = $self->{path}; 1918 my $path = $self->{path};
1861 1919
1862 { 1920 {
1863 my $guard1 = cf::lock_acquire "map_data:$path"; 1921 my $guard = cf::lock_acquire "map_data:$path";
1864 my $guard2 = cf::lock_acquire "map_load:$path";
1865 1922
1923 return unless $self->valid;
1866 return if $self->in_memory != cf::MAP_SWAPPED; 1924 return unless $self->in_memory == cf::MAP_SWAPPED;
1867 1925
1868 $self->in_memory (cf::MAP_LOADING); 1926 $self->in_memory (cf::MAP_LOADING);
1869 1927
1870 $self->alloc; 1928 $self->alloc;
1871 1929
1872 $self->pre_load; 1930 $self->pre_load;
1873 cf::cede_to_tick; 1931 cf::cede_to_tick;
1874 1932
1933 my $f = new_from_file cf::object::thawer $self->{load_path};
1934 $f->skip_block;
1875 $self->_load_objects ($self->{load_path}, 1) 1935 $self->_load_objects ($f)
1876 or return; 1936 or return;
1877 1937
1878 $self->set_object_flag (cf::FLAG_OBJ_ORIGINAL, 1) 1938 $self->set_object_flag (cf::FLAG_OBJ_ORIGINAL, 1)
1879 if delete $self->{load_original}; 1939 if delete $self->{load_original};
1880 1940
1881 if (my $uniq = $self->uniq_path) { 1941 if (my $uniq = $self->uniq_path) {
1882 utf8::encode $uniq; 1942 utf8::encode $uniq;
1883 if (aio_open $uniq, O_RDONLY, 0) { 1943 unless (aio_stat $uniq) {
1944 if (my $f = new_from_file cf::object::thawer $uniq) {
1884 $self->clear_unique_items; 1945 $self->clear_unique_items;
1885 $self->_load_objects ($uniq, 0); 1946 $self->_load_objects ($f);
1947 $f->resolve_delayed_derefs;
1948 }
1886 } 1949 }
1887 } 1950 }
1951
1952 $f->resolve_delayed_derefs;
1888 1953
1889 cf::cede_to_tick; 1954 cf::cede_to_tick;
1890 # now do the right thing for maps 1955 # now do the right thing for maps
1891 $self->link_multipart_objects; 1956 $self->link_multipart_objects;
1892 $self->difficulty ($self->estimate_difficulty) 1957 $self->difficulty ($self->estimate_difficulty)
2036 2101
2037 return if $self->players; 2102 return if $self->players;
2038 return if $self->in_memory != cf::MAP_IN_MEMORY; 2103 return if $self->in_memory != cf::MAP_IN_MEMORY;
2039 return if $self->{deny_save}; 2104 return if $self->{deny_save};
2040 2105
2106 $self->in_memory (cf::MAP_SWAPPED);
2107
2108 $self->deactivate;
2109 $_->clear_links_to ($self) for values %cf::MAP;
2041 $self->clear; 2110 $self->clear;
2042 $self->in_memory (cf::MAP_SWAPPED);
2043} 2111}
2044 2112
2045sub reset_at { 2113sub reset_at {
2046 my ($self) = @_; 2114 my ($self) = @_;
2047 2115
2079 if $uniq; 2147 if $uniq;
2080 } 2148 }
2081 2149
2082 delete $cf::MAP{$self->path}; 2150 delete $cf::MAP{$self->path};
2083 2151
2152 $self->deactivate;
2153 $_->clear_links_to ($self) for values %cf::MAP;
2084 $self->clear; 2154 $self->clear;
2085
2086 $_->clear_links_to ($self) for values %cf::MAP;
2087 2155
2088 $self->unlink_save; 2156 $self->unlink_save;
2089 $self->destroy; 2157 $self->destroy;
2090} 2158}
2091 2159
2190 map { $_, inv_recursive_ $_->inv } @_ 2258 map { $_, inv_recursive_ $_->inv } @_
2191} 2259}
2192 2260
2193sub inv_recursive { 2261sub inv_recursive {
2194 inv_recursive_ inv $_[0] 2262 inv_recursive_ inv $_[0]
2263}
2264
2265=item $ref = $ob->ref
2266
2267creates and returns a persistent reference to an objetc that can be stored as a string.
2268
2269=item $ob = cf::object::deref ($refstring)
2270
2271returns the objetc referenced by refstring. may return undef when it cnanot find the object,
2272even if the object actually exists. May block.
2273
2274=cut
2275
2276sub deref {
2277 my ($ref) = @_;
2278
2279 # temporary compatibility#TODO#remove
2280 $ref =~ s{^<}{player/<};
2281
2282 if ($ref =~ m{^player\/(<1\.\d+>)/(.*)$}) {
2283 my ($uuid, $name) = ($1, $2);
2284 my $pl = $cf::PLAYER_LOADING{$name} || cf::player::find $name
2285 or return;
2286 $pl->ob->uuid eq $uuid
2287 or return;
2288
2289 $pl->ob
2290 } else {
2291 warn "$ref: cannot resolve object reference\n";
2292 undef
2293 }
2195} 2294}
2196 2295
2197package cf; 2296package cf;
2198 2297
2199=back 2298=back
3374 or die; 3473 or die;
3375 3474
3376 $map->width (50); 3475 $map->width (50);
3377 $map->height (50); 3476 $map->height (50);
3378 $map->alloc; 3477 $map->alloc;
3379 $map->_load_objects ("/tmp/x.map", 1); 3478 $map->_load_objects ("/tmp/x.map", 1); #TODO: does not work
3380 my $t = Event::time - $t; 3479 my $t = Event::time - $t;
3381 3480
3382 #next unless $t < 0.0013;#d# 3481 #next unless $t < 0.0013;#d#
3383 if ($t < $min) { 3482 if ($t < $min) {
3384 $min = $t; 3483 $min = $t;
3427 3526
3428 }, 3527 },
3429); 3528);
3430 3529
3431{ 3530{
3531 BDB::min_parallel 8;
3432 BDB::max_poll_time $TICK * 0.1; 3532 BDB::max_poll_time $TICK * 0.1;
3433 $BDB_POLL_WATCHER = Event->io ( 3533 $BDB_POLL_WATCHER = Event->io (
3434 reentrant => 0, 3534 reentrant => 0,
3435 fd => BDB::poll_fileno, 3535 fd => BDB::poll_fileno,
3436 poll => 'r', 3536 poll => 'r',
3437 prio => 0, 3537 prio => 0,
3438 data => WF_AUTOCANCEL, 3538 data => WF_AUTOCANCEL,
3439 cb => \&BDB::poll_cb, 3539 cb => \&BDB::poll_cb,
3440 ); 3540 );
3441 BDB::min_parallel 8;
3442 3541
3443 BDB::set_sync_prepare { 3542 BDB::set_sync_prepare {
3444 my $status; 3543 my $status;
3445 my $current = $Coro::current; 3544 my $current = $Coro::current;
3446 ( 3545 (
3467 | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE, 3566 | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE,
3468 0666; 3567 0666;
3469 3568
3470 cf::cleanup "db_env_open($BDBDIR): $!" if $!; 3569 cf::cleanup "db_env_open($BDBDIR): $!" if $!;
3471 3570
3472 $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC, 1); 3571 $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT | BDB::TXN_NOSYNC
3572 | BDB::LOG_AUTOREMOVE, 1);
3473 $DB_ENV->set_lk_detect; 3573 $DB_ENV->set_lk_detect;
3474 }; 3574 };
3475 3575
3476 cf::cleanup "db_env_open(db): $@" if $@; 3576 cf::cleanup "db_env_open(db): $@" if $@;
3477 }; 3577 };
3478 } 3578 }
3579
3580 $BDB_CHECKPOINT_WATCHER = Event->timer (
3581 after => 11,
3582 interval => 60,
3583 hard => 1,
3584 prio => 0,
3585 data => WF_AUTOCANCEL,
3586 cb => sub {
3587 BDB::db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { };
3588 },
3589 );
3590 $BDB_TRICKLE_WATCHER = Event->timer (
3591 after => 5,
3592 interval => 10,
3593 hard => 1,
3594 prio => 0,
3595 data => WF_AUTOCANCEL,
3596 cb => sub {
3597 BDB::db_env_memp_trickle $DB_ENV, 20, 0, sub { };
3598 },
3599 );
3479} 3600}
3480 3601
3481{ 3602{
3482 IO::AIO::min_parallel 8; 3603 IO::AIO::min_parallel 8;
3483 3604

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines