From 4b60d609c05d334d29a72c6dbbe301a66f518089 Mon Sep 17 00:00:00 2001 From: Timo Sirainen Date: Fri, 3 Apr 2020 11:06:15 +0300 Subject: [PATCH] lib-index: mailbox_cache_purge*() - Add reason string Include the reason in the mail_cache_purge_* events also. --- src/doveadm/doveadm-mail-mailbox-cache.c | 3 +- src/lib-index/mail-cache-decisions.c | 2 +- src/lib-index/mail-cache-purge.c | 35 ++++++++++++++---------- src/lib-index/mail-cache-transaction.c | 9 +++--- src/lib-index/mail-cache.h | 5 ++-- src/lib-index/mail-index-sync.c | 3 +- src/lib-storage/index/index-rebuild.c | 2 +- 7 files changed, 34 insertions(+), 25 deletions(-) diff --git a/src/doveadm/doveadm-mail-mailbox-cache.c b/src/doveadm/doveadm-mail-mailbox-cache.c index 01326ad36d..eb396b9b40 100644 --- a/src/doveadm/doveadm-mail-mailbox-cache.c +++ b/src/doveadm/doveadm-mail-mailbox-cache.c @@ -328,7 +328,8 @@ static void cmd_mailbox_cache_remove_init(struct doveadm_mail_cmd_context *_ctx, static int cmd_mailbox_cache_purge_run_box(struct mailbox_cache_cmd_context *ctx, struct mailbox *box) { - if (mail_cache_purge(box->cache, (uint32_t)-1) < 0) { + if (mail_cache_purge(box->cache, (uint32_t)-1, + "doveadm mailbox cache purge") < 0) { mailbox_set_index_error(box); doveadm_mail_failed_mailbox(&ctx->ctx, box); return -1; diff --git a/src/lib-index/mail-cache-decisions.c b/src/lib-index/mail-cache-decisions.c index 79f07eea4e..1a0083b3b8 100644 --- a/src/lib-index/mail-cache-decisions.c +++ b/src/lib-index/mail-cache-decisions.c @@ -201,5 +201,5 @@ int mail_cache_decisions_copy(struct mail_cache *src, struct mail_cache *dst) that the fields are updated even if the cache was already created and no purging was done. */ dst->field_header_write_pending = TRUE; - return mail_cache_purge(dst, 0); + return mail_cache_purge(dst, 0, "copy cache decisions"); } diff --git a/src/lib-index/mail-cache-purge.c b/src/lib-index/mail-cache-purge.c index 7d2c864eda..49fef81ed4 100644 --- a/src/lib-index/mail-cache-purge.c +++ b/src/lib-index/mail-cache-purge.c @@ -166,7 +166,6 @@ mail_cache_purge_get_fields(struct mail_cache_copy_context *ctx, struct event_passthrough *e = mail_cache_decision_changed_event( cache, ctx->event, i)-> - add_str("reason", "purge")-> add_str("old_decision", "yes")-> add_str("new_decision", "temp"); e_debug(e->event(), "Purge changes field %s " @@ -183,8 +182,8 @@ mail_cache_purge_get_fields(struct mail_cache_copy_context *ctx, static int mail_cache_copy(struct mail_cache *cache, struct mail_index_transaction *trans, - struct event *event, int fd, uint32_t *file_seq_r, - uoff_t *file_size_r, uint32_t *max_uid_r, + struct event *event, int fd, const char *reason, + uint32_t *file_seq_r, uoff_t *file_size_r, uint32_t *max_uid_r, ARRAY_TYPE(uint32_t) *ext_offsets) { struct mail_cache_copy_context ctx; @@ -200,6 +199,8 @@ mail_cache_copy(struct mail_cache *cache, struct mail_index_transaction *trans, unsigned int i, used_fields_count, orig_fields_count, record_count; time_t max_drop_time; + i_assert(reason != NULL); + *max_uid_r = 0; /* get the latest info on fields */ @@ -218,9 +219,10 @@ mail_cache_copy(struct mail_cache *cache, struct mail_index_transaction *trans, hdr.file_seq = get_next_file_seq(cache); o_stream_nsend(output, &hdr, sizeof(hdr)); + event_add_str(event, "reason", reason); event_add_int(event, "file_seq", hdr.file_seq); event_set_name(event, "mail_cache_purge_started"); - e_debug(event, "Purging (new file_seq=%u)", hdr.file_seq); + e_debug(event, "Purging (new file_seq=%u): %s", hdr.file_seq, reason); i_zero(&ctx); ctx.cache = cache; @@ -368,7 +370,8 @@ mail_cache_copy(struct mail_cache *cache, struct mail_index_transaction *trans, static int mail_cache_purge_write(struct mail_cache *cache, struct mail_index_transaction *trans, - int fd, const char *temp_path, bool *unlock) + int fd, const char *temp_path, const char * + reason, bool *unlock) { struct event *event; struct stat st; @@ -392,8 +395,8 @@ mail_cache_purge_write(struct mail_cache *cache, event_add_int(event, "prev_file_size", prev_file_size); event_add_int(event, "prev_deleted_records", prev_deleted_records); - if (mail_cache_copy(cache, trans, event, fd, &file_seq, &file_size, - &max_uid, &ext_offsets) < 0) + if (mail_cache_copy(cache, trans, event, fd, reason, + &file_seq, &file_size, &max_uid, &ext_offsets) < 0) return -1; if (fstat(fd, &st) < 0) { @@ -481,7 +484,7 @@ mail_cache_purge_has_file_changed(struct mail_cache *cache, static int mail_cache_purge_locked(struct mail_cache *cache, uint32_t purge_file_seq, struct mail_index_transaction *trans, - bool *unlock) + const char *reason, bool *unlock) { const char *temp_path; int fd, ret; @@ -513,7 +516,7 @@ static int mail_cache_purge_locked(struct mail_cache *cache, fd = mail_index_create_tmp_file(cache->index, cache->filepath, &temp_path); if (fd == -1) return -1; - if (mail_cache_purge_write(cache, trans, fd, temp_path, unlock) < 0) { + if (mail_cache_purge_write(cache, trans, fd, temp_path, reason, unlock) < 0) { i_close_fd(&fd); i_unlink(temp_path); return -1; @@ -533,7 +536,7 @@ static int mail_cache_purge_locked(struct mail_cache *cache, static int mail_cache_purge_full(struct mail_cache *cache, struct mail_index_transaction *trans, - uint32_t purge_file_seq) + uint32_t purge_file_seq, const char *reason) { bool unlock = FALSE; int ret; @@ -570,7 +573,7 @@ mail_cache_purge_full(struct mail_cache *cache, unlock = TRUE; } cache->purging = TRUE; - ret = mail_cache_purge_locked(cache, purge_file_seq, trans, &unlock); + ret = mail_cache_purge_locked(cache, purge_file_seq, trans, reason, &unlock); cache->purging = FALSE; if (unlock) mail_cache_unlock(cache); @@ -585,12 +588,13 @@ mail_cache_purge_full(struct mail_cache *cache, int mail_cache_purge_with_trans(struct mail_cache *cache, struct mail_index_transaction *trans, - uint32_t purge_file_seq) + uint32_t purge_file_seq, const char *reason) { - return mail_cache_purge_full(cache, trans, purge_file_seq); + return mail_cache_purge_full(cache, trans, purge_file_seq, reason); } -int mail_cache_purge(struct mail_cache *cache, uint32_t purge_file_seq) +int mail_cache_purge(struct mail_cache *cache, uint32_t purge_file_seq, + const char *reason) { struct mail_index_view *view; struct mail_index_transaction *trans; @@ -615,7 +619,8 @@ int mail_cache_purge(struct mail_cache *cache, uint32_t purge_file_seq) MAIL_INDEX_TRANSACTION_FLAG_EXTERNAL); if (ret < 0) ; - else if ((ret = mail_cache_purge_full(cache, trans, purge_file_seq)) < 0) + else if ((ret = mail_cache_purge_full(cache, trans, purge_file_seq, + reason)) < 0) mail_index_transaction_rollback(&trans); else { if (mail_index_transaction_commit(&trans) < 0) diff --git a/src/lib-index/mail-cache-transaction.c b/src/lib-index/mail-cache-transaction.c index 21378adc4e..c6f2e0680a 100644 --- a/src/lib-index/mail-cache-transaction.c +++ b/src/lib-index/mail-cache-transaction.c @@ -201,7 +201,8 @@ bool mail_cache_transactions_have_changes(struct mail_cache *cache) } static int -mail_cache_transaction_purge(struct mail_cache_transaction_ctx *ctx) +mail_cache_transaction_purge(struct mail_cache_transaction_ctx *ctx, + const char *reason) { struct mail_cache *cache = ctx->cache; @@ -210,7 +211,7 @@ mail_cache_transaction_purge(struct mail_cache_transaction_ctx *ctx) uint32_t purge_file_seq = MAIL_CACHE_IS_UNUSABLE(cache) ? 0 : cache->hdr->file_seq; - int ret = mail_cache_purge(cache, purge_file_seq); + int ret = mail_cache_purge(cache, purge_file_seq, reason); /* already written cache records must be forgotten, but records in memory can still be written to the new cache file */ mail_cache_transaction_forget_flushed(ctx, TRUE); @@ -229,7 +230,7 @@ static int mail_cache_transaction_lock(struct mail_cache_transaction_ctx *ctx) return -1; if (!ctx->tried_purging) { - if (mail_cache_transaction_purge(ctx) < 0) + if (mail_cache_transaction_purge(ctx, "creating cache") < 0) return -1; return mail_cache_transaction_lock(ctx); } else { @@ -245,7 +246,7 @@ static int mail_cache_transaction_lock(struct mail_cache_transaction_ctx *ctx) if (cache->hdr->continued_record_count > 0 || cache->hdr->deleted_record_count > 0) { mail_cache_unlock(cache); - (void)mail_cache_transaction_purge(ctx); + (void)mail_cache_transaction_purge(ctx, "cache is too large"); return mail_cache_transaction_lock(ctx); } } diff --git a/src/lib-index/mail-cache.h b/src/lib-index/mail-cache.h index 2bce3eb3a7..3f6803e60a 100644 --- a/src/lib-index/mail-cache.h +++ b/src/lib-index/mail-cache.h @@ -76,8 +76,9 @@ bool mail_cache_need_purge(struct mail_cache *cache); done always regardless of file_seq. */ int mail_cache_purge_with_trans(struct mail_cache *cache, struct mail_index_transaction *trans, - uint32_t purge_file_seq); -int mail_cache_purge(struct mail_cache *cache, uint32_t purge_file_seq); + uint32_t purge_file_seq, const char *reason); +int mail_cache_purge(struct mail_cache *cache, uint32_t purge_file_seq, + const char *reason); /* Returns TRUE if there is at least something in the cache. */ bool mail_cache_exists(struct mail_cache *cache); /* Open and read cache header. Returns 1 if ok, 0 if cache doesn't exist or it diff --git a/src/lib-index/mail-index-sync.c b/src/lib-index/mail-index-sync.c index c1510cc8b7..0e17ba1e24 100644 --- a/src/lib-index/mail-index-sync.c +++ b/src/lib-index/mail-index-sync.c @@ -942,7 +942,8 @@ int mail_index_sync_commit(struct mail_index_sync_ctx **_ctx) if (ret == 0 && mail_cache_need_purge(index->cache) && !mail_cache_transactions_have_changes(index->cache)) { if (mail_cache_purge(index->cache, - index->cache->need_purge_file_seq) < 0) { + index->cache->need_purge_file_seq, + "syncing") < 0) { /* can't really do anything if it fails */ } /* Make sure the newly committed cache record offsets are diff --git a/src/lib-storage/index/index-rebuild.c b/src/lib-storage/index/index-rebuild.c index 17a5e43a7a..b6cac3d226 100644 --- a/src/lib-storage/index/index-rebuild.c +++ b/src/lib-storage/index/index-rebuild.c @@ -240,7 +240,7 @@ void index_index_rebuild_deinit(struct index_rebuild_context **_ctx, /* initialize cache file with the old field decisions */ (void)mail_cache_purge_with_trans(ctx->box->cache, ctx->trans, - (uint32_t)-1); + (uint32_t)-1, "rebuilding index"); index_rebuild_header(ctx, cb); index_rebuild_box_name_header(ctx); if (ctx->backup_index != NULL) { -- 2.47.3