… | |
… | |
80 | our $NEXT_TICK; |
80 | our $NEXT_TICK; |
81 | our $NOW; |
81 | our $NOW; |
82 | our $USE_FSYNC = 1; # use fsync to write maps - default off |
82 | our $USE_FSYNC = 1; # use fsync to write maps - default off |
83 | |
83 | |
84 | our $BDB_POLL_WATCHER; |
84 | our $BDB_POLL_WATCHER; |
|
|
85 | our $BDB_CHECKPOINT_WATCHER; |
|
|
86 | our $BDB_TRICKLE_WATCHER; |
85 | our $DB_ENV; |
87 | our $DB_ENV; |
86 | |
88 | |
87 | our %CFG; |
89 | our %CFG; |
88 | |
90 | |
89 | our $UPTIME; $UPTIME ||= time; |
91 | our $UPTIME; $UPTIME ||= time; |
… | |
… | |
507 | =item cf::db_put $family => $key => $value |
509 | =item cf::db_put $family => $key => $value |
508 | |
510 | |
509 | Stores the given C<$value> in the family. It can currently store binary |
511 | Stores the given C<$value> in the family. It can currently store binary |
510 | data only (use Compress::LZF::sfreeze_cr/sthaw to convert to/from binary). |
512 | data only (use Compress::LZF::sfreeze_cr/sthaw to convert to/from binary). |
511 | |
513 | |
|
|
514 | =item $db = cf::db_table "name" |
|
|
515 | |
|
|
516 | Create and/or open a new database table. The string must not be "db" and must be unique |
|
|
517 | within each server. |
|
|
518 | |
512 | =cut |
519 | =cut |
|
|
520 | |
|
|
521 | sub 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 | } |
513 | |
537 | |
514 | our $DB; |
538 | our $DB; |
515 | |
539 | |
516 | sub db_init { |
540 | sub db_init { |
517 | unless ($DB) { |
|
|
518 | $DB = BDB::db_create $DB_ENV; |
|
|
519 | |
|
|
520 | cf::sync_job { |
541 | cf::sync_job { |
521 | eval { |
542 | $DB ||= db_table "db"; |
522 | $DB->set_flags (BDB::CHKSUM); |
|
|
523 | |
|
|
524 | BDB::db_open $DB, undef, "db", undef, BDB::BTREE, |
|
|
525 | BDB::CREATE | BDB::AUTO_COMMIT, 0666; |
|
|
526 | cf::cleanup "db_open(db): $!" if $!; |
|
|
527 | }; |
|
|
528 | cf::cleanup "db_open(db): $@" if $@; |
|
|
529 | }; |
|
|
530 | } |
543 | }; |
531 | } |
544 | } |
532 | |
545 | |
533 | sub db_get($$) { |
546 | sub db_get($$) { |
534 | my $key = "$_[0]/$_[1]"; |
547 | my $key = "$_[0]/$_[1]"; |
535 | |
548 | |
… | |
… | |
3513 | |
3526 | |
3514 | }, |
3527 | }, |
3515 | ); |
3528 | ); |
3516 | |
3529 | |
3517 | { |
3530 | { |
|
|
3531 | BDB::min_parallel 8; |
3518 | BDB::max_poll_time $TICK * 0.1; |
3532 | BDB::max_poll_time $TICK * 0.1; |
3519 | $BDB_POLL_WATCHER = Event->io ( |
3533 | $BDB_POLL_WATCHER = Event->io ( |
3520 | reentrant => 0, |
3534 | reentrant => 0, |
3521 | fd => BDB::poll_fileno, |
3535 | fd => BDB::poll_fileno, |
3522 | poll => 'r', |
3536 | poll => 'r', |
3523 | prio => 0, |
3537 | prio => 0, |
3524 | data => WF_AUTOCANCEL, |
3538 | data => WF_AUTOCANCEL, |
3525 | cb => \&BDB::poll_cb, |
3539 | cb => \&BDB::poll_cb, |
3526 | ); |
3540 | ); |
3527 | BDB::min_parallel 8; |
|
|
3528 | |
3541 | |
3529 | BDB::set_sync_prepare { |
3542 | BDB::set_sync_prepare { |
3530 | my $status; |
3543 | my $status; |
3531 | my $current = $Coro::current; |
3544 | my $current = $Coro::current; |
3532 | ( |
3545 | ( |
… | |
… | |
3553 | | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE, |
3566 | | BDB::RECOVER | BDB::REGISTER | BDB::USE_ENVIRON | BDB::CREATE, |
3554 | 0666; |
3567 | 0666; |
3555 | |
3568 | |
3556 | cf::cleanup "db_env_open($BDBDIR): $!" if $!; |
3569 | cf::cleanup "db_env_open($BDBDIR): $!" if $!; |
3557 | |
3570 | |
3558 | $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); |
3559 | $DB_ENV->set_lk_detect; |
3573 | $DB_ENV->set_lk_detect; |
3560 | }; |
3574 | }; |
3561 | |
3575 | |
3562 | cf::cleanup "db_env_open(db): $@" if $@; |
3576 | cf::cleanup "db_env_open(db): $@" if $@; |
3563 | }; |
3577 | }; |
3564 | } |
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 | ); |
3565 | } |
3600 | } |
3566 | |
3601 | |
3567 | { |
3602 | { |
3568 | IO::AIO::min_parallel 8; |
3603 | IO::AIO::min_parallel 8; |
3569 | |
3604 | |