comparison src/doveadm/dsync/dsync-mailbox.c @ 22715:20415dd0b85a

dsync: Add per-mailbox sync lock that is always used. Both importing and exporting gets the lock before they even sync the mailbox. The lock is kept until the import/export finishes. This guarantees that no matter how dsync is run, two dsyncs can't be working on the same mailbox at the same time. This lock is in addition to the optional per-user lock enabled by the -l parameter. If the -l parameter is used, the same lock timeout is used for the per-mailbox lock. Otherwise 30s timeout is used. This should help to avoid email duplication when replication is enabled for public namespaces, and maybe in some other rare situations as well.
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Thu, 28 Dec 2017 14:10:23 +0200
parents cb108f786fb4
children
comparison
equal deleted inserted replaced
22714:a34e1e7232f1 22715:20415dd0b85a
1 /* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */ 1 /* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */
2 2
3 #include "lib.h" 3 #include "lib.h"
4 #include "istream.h" 4 #include "istream.h"
5 #include "mail-storage-private.h"
6 #include "dsync-brain-private.h"
5 #include "dsync-mailbox.h" 7 #include "dsync-mailbox.h"
6 8
7 void dsync_mailbox_attribute_dup(pool_t pool, 9 void dsync_mailbox_attribute_dup(pool_t pool,
8 const struct dsync_mailbox_attribute *src, 10 const struct dsync_mailbox_attribute *src,
9 struct dsync_mailbox_attribute *dest_r) 11 struct dsync_mailbox_attribute *dest_r)
18 20
19 dest_r->deleted = src->deleted; 21 dest_r->deleted = src->deleted;
20 dest_r->last_change = src->last_change; 22 dest_r->last_change = src->last_change;
21 dest_r->modseq = src->modseq; 23 dest_r->modseq = src->modseq;
22 } 24 }
25
26 int dsync_mailbox_lock(struct dsync_brain *brain, struct mailbox *box,
27 struct file_lock **lock_r)
28 {
29 const char *path, *error;
30 int ret;
31
32 /* Make sure the mailbox is open - locking requires it */
33 if (mailbox_open(box) < 0) {
34 i_error("Can't open mailbox %s: %s", mailbox_get_vname(box),
35 mailbox_get_last_internal_error(box, &brain->mail_error));
36 return -1;
37 }
38
39 ret = mailbox_get_path_to(box, MAILBOX_LIST_PATH_TYPE_INDEX, &path);
40 if (ret < 0) {
41 i_error("Can't get mailbox %s path: %s", mailbox_get_vname(box),
42 mailbox_get_last_internal_error(box, &brain->mail_error));
43 return -1;
44 }
45 if (ret == 0) {
46 /* No index files - don't do any locking. In theory we still
47 could, but this lock is mainly meant to prevent replication
48 problems, and replication wouldn't work without indexes. */
49 *lock_r = NULL;
50 return 0;
51 }
52
53 if (mailbox_lock_file_create(box, DSYNC_MAILBOX_LOCK_FILENAME,
54 brain->mailbox_lock_timeout_secs,
55 lock_r, &error) <= 0) {
56 i_error("Failed to lock mailbox %s for dsyncing: %s",
57 box->vname, error);
58 return -1;
59 }
60 return 0;
61 }