--- BayesStore.pm.post-793 2003-03-21 15:09:00.000000000 -0800 +++ BayesStore.pm.new 2003-03-21 15:10:42.000000000 -0800 @@ -19,7 +19,7 @@ @DBNAMES @DB_EXTENSIONS $NSPAM_MAGIC_TOKEN $NHAM_MAGIC_TOKEN $LAST_EXPIRE_MAGIC_TOKEN $NTOKENS_MAGIC_TOKEN $OLDEST_TOKEN_AGE_MAGIC_TOKEN - $SCANCOUNT_BASE_MAGIC_TOKEN + $SCANCOUNT_BASE_MAGIC_TOKEN $NOW_REBUILDING_MAGIC_TOKEN }; @ISA = qw(); @@ -66,6 +66,7 @@ $LAST_EXPIRE_MAGIC_TOKEN = '**LASTEXPIRE'; $NTOKENS_MAGIC_TOKEN = '**NTOKENS'; $SCANCOUNT_BASE_MAGIC_TOKEN = '**SCANBASE'; +$NOW_REBUILDING_MAGIC_TOKEN = '**NOWREBUILDING'; use constant MAX_SIZE_FOR_SCAN_COUNT_FILE => 5000; @@ -316,6 +317,7 @@ || $tok eq $LAST_EXPIRE_MAGIC_TOKEN || $tok eq $NTOKENS_MAGIC_TOKEN || $tok eq $OLDEST_TOKEN_AGE_MAGIC_TOKEN + || $tok eq $NOW_REBUILDING_MAGIC_TOKEN || $tok eq $SCANCOUNT_BASE_MAGIC_TOKEN); my ($ts, $th, $atime) = $self->tok_get ($tok); @@ -341,8 +343,10 @@ } } - if ($showdots && (($kept + $deleted) % 1000) == 0) { - print STDERR "."; + if ((($kept + $deleted) % 1000) == 0) { + # update "now expiring" token. + $self->{db_toks}->{$NOW_REBUILDING_MAGIC_TOKEN} = time(); + if ($showdots) { print STDERR "."; } } } @@ -374,6 +378,9 @@ $new_toks{$NHAM_MAGIC_TOKEN} = $self->{db_toks}->{$NHAM_MAGIC_TOKEN}; $new_toks{$NTOKENS_MAGIC_TOKEN} = $kept + $reprieved; + # ensure this is NOT set in the new db + delete $new_toks{$NOW_REBUILDING_MAGIC_TOKEN}; + # now untie so we can do renames untie %{$self->{db_toks}}; untie %new_toks; @@ -391,7 +398,6 @@ # ok, once that's done we can re-tie. Call untie_db() first so # we unlock correctly etc. first $self->untie_db(); - $self->tie_db_writable(); my $done = time(); @@ -427,6 +433,16 @@ if (!$ntoks || $ntoks =~ /\D/) { $ntoks = $self->{expiry_min_db_size} + 1; } if ($ntoks <= $self->{expiry_min_db_size}) { + dbg ("bayes: expiry not needed, db too small ($ntoks)"); + return 0; + } + + # is the database currently being expired, or did an expiry attempt + # fail for some reason in the last 300 seconds? + my $timenow = time(); + my $amexping = $self->{db_toks}->{$NOW_REBUILDING_MAGIC_TOKEN}; + if (defined ($amexping) && $timenow - $amexping < 300) { # TODO: conf item? + dbg ("bayes: expiry already in progress ($amexping)"); return 0; } @@ -441,6 +457,7 @@ return 1; } + dbg ("bayes: expiry not needed, happened recently ($last, now=$now)"); 0; } @@ -583,6 +600,12 @@ my $showdots = $opts->{showdots}; my $retirepath = $path.".old"; + # Set this now. Unset it at end of expire_old_tokens_trapped(). + # NOTE: we do NOT unset it on error. If something went + # wrong, give the server 300 secs to recover in case it was + # a system-level problem, or caused by load. + $self->{db_toks}->{$NOW_REBUILDING_MAGIC_TOKEN} = $started; + # now read the retired journal if (!open (JOURNAL, "<$path")) { warn "bayes: cannot open read $path\n"; @@ -613,9 +636,11 @@ warn "Bayes journal: gibberish entry found: $_"; } -# if ($showdots && ($count % 1000) == 0) { -# print STDERR "."; -# } + if (($total_count % 1000) == 0) { + # update "now expiring" token. + $self->{db_toks}->{$NOW_REBUILDING_MAGIC_TOKEN} = time(); +# if ($showdots) { print STDERR "."; } + } } close JOURNAL; @@ -625,8 +650,10 @@ while( my($k,$v) = each %tokens ) { $self->tok_touch_token ($v, $k); - if ($showdots && (++$count % 1000) == 0) { - print STDERR "."; + if ((++$count % 1000) == 0) { + # update "now expiring" token. + $self->{db_toks}->{$NOW_REBUILDING_MAGIC_TOKEN} = time(); + if ($showdots) { print STDERR "."; } } }