annotate src/doveadm/dsync/dsync-brain.c @ 16396:2d6a3035a6f7

dsync: Small code cleanup.
author Timo Sirainen <tss@iki.fi>
date Sun, 26 May 2013 21:20:47 +0300
parents 1a58d4bcb49c
children 4883a8e1db13
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15715
36ef72481934 Oops :) Update copyrights to 2013 without breaking all .c files.
Timo Sirainen <tss@iki.fi>
parents: 15714
diff changeset
1 /* Copyright (c) 2013 Dovecot authors, see the included COPYING file */
9559
34eef8a2716b dsync: Initial implementation.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
2
34eef8a2716b dsync: Initial implementation.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
3 #include "lib.h"
34eef8a2716b dsync: Initial implementation.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
4 #include "array.h"
34eef8a2716b dsync: Initial implementation.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
5 #include "hash.h"
15782
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
6 #include "hostpid.h"
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
7 #include "mail-namespace.h"
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
8 #include "dsync-mailbox-tree.h"
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
9 #include "dsync-ibc.h"
9559
34eef8a2716b dsync: Initial implementation.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
10 #include "dsync-brain-private.h"
34eef8a2716b dsync: Initial implementation.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
11
15782
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
12 #include <sys/stat.h>
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
13
15464
af3b86a16ac3 dsync: Added debugging and rawlogging support.
Timo Sirainen <tss@iki.fi>
parents: 15355
diff changeset
14 static const char *dsync_state_names[DSYNC_STATE_DONE+1] = {
af3b86a16ac3 dsync: Added debugging and rawlogging support.
Timo Sirainen <tss@iki.fi>
parents: 15355
diff changeset
15 "recv_handshake",
af3b86a16ac3 dsync: Added debugging and rawlogging support.
Timo Sirainen <tss@iki.fi>
parents: 15355
diff changeset
16 "send_last_common",
af3b86a16ac3 dsync: Added debugging and rawlogging support.
Timo Sirainen <tss@iki.fi>
parents: 15355
diff changeset
17 "recv_last_common",
af3b86a16ac3 dsync: Added debugging and rawlogging support.
Timo Sirainen <tss@iki.fi>
parents: 15355
diff changeset
18 "send_mailbox_tree",
af3b86a16ac3 dsync: Added debugging and rawlogging support.
Timo Sirainen <tss@iki.fi>
parents: 15355
diff changeset
19 "send_mailbox_tree_deletes",
af3b86a16ac3 dsync: Added debugging and rawlogging support.
Timo Sirainen <tss@iki.fi>
parents: 15355
diff changeset
20 "recv_mailbox_tree",
af3b86a16ac3 dsync: Added debugging and rawlogging support.
Timo Sirainen <tss@iki.fi>
parents: 15355
diff changeset
21 "recv_mailbox_tree_deletes",
af3b86a16ac3 dsync: Added debugging and rawlogging support.
Timo Sirainen <tss@iki.fi>
parents: 15355
diff changeset
22 "master_send_mailbox",
af3b86a16ac3 dsync: Added debugging and rawlogging support.
Timo Sirainen <tss@iki.fi>
parents: 15355
diff changeset
23 "slave_recv_mailbox",
af3b86a16ac3 dsync: Added debugging and rawlogging support.
Timo Sirainen <tss@iki.fi>
parents: 15355
diff changeset
24 "sync_mails",
af3b86a16ac3 dsync: Added debugging and rawlogging support.
Timo Sirainen <tss@iki.fi>
parents: 15355
diff changeset
25 "done"
af3b86a16ac3 dsync: Added debugging and rawlogging support.
Timo Sirainen <tss@iki.fi>
parents: 15355
diff changeset
26 };
af3b86a16ac3 dsync: Added debugging and rawlogging support.
Timo Sirainen <tss@iki.fi>
parents: 15355
diff changeset
27
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
28 static void dsync_brain_run_io(void *context)
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
29 {
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
30 struct dsync_brain *brain = context;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
31 bool changed, try_pending;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
32
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
33 if (dsync_ibc_has_failed(brain->ibc)) {
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
34 io_loop_stop(current_ioloop);
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
35 brain->failed = TRUE;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
36 return;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
37 }
11687
b37c85676f8e dsync: When -v parameter is given, show progress counter of saving new messages.
Timo Sirainen <tss@iki.fi>
parents: 11684
diff changeset
38
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
39 try_pending = TRUE;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
40 do {
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
41 if (!dsync_brain_run(brain, &changed)) {
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
42 io_loop_stop(current_ioloop);
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
43 break;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
44 }
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
45 if (changed)
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
46 try_pending = TRUE;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
47 else if (try_pending) {
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
48 if (dsync_ibc_has_pending_data(brain->ibc))
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
49 changed = TRUE;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
50 try_pending = FALSE;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
51 }
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
52 } while (changed);
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
53 }
11727
42dfcf9c896b dsync backup: Fail if it looks like backup is running in wrong direction.
Timo Sirainen <tss@iki.fi>
parents: 11688
diff changeset
54
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
55 static struct dsync_brain *
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
56 dsync_brain_common_init(struct mail_user *user, struct dsync_ibc *ibc)
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
57 {
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
58 struct dsync_brain *brain;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
59 pool_t pool;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
60
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
61 pool = pool_alloconly_create("dsync brain", 10240);
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
62 brain = p_new(pool, struct dsync_brain, 1);
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
63 brain->pool = pool;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
64 brain->user = user;
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
65 brain->ibc = ibc;
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
66 brain->sync_type = DSYNC_BRAIN_SYNC_TYPE_UNKNOWN;
15782
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
67 brain->lock_fd = -1;
15232
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
68 hash_table_create(&brain->mailbox_states, pool, 0,
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
69 guid_128_hash, guid_128_cmp);
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
70 p_array_init(&brain->remote_mailbox_states, pool, 64);
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
71 return brain;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
72 }
9559
34eef8a2716b dsync: Initial implementation.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
73
15231
454d0563927d doveadm: "backup" command is working again.
Timo Sirainen <tss@iki.fi>
parents: 15037
diff changeset
74 static void
454d0563927d doveadm: "backup" command is working again.
Timo Sirainen <tss@iki.fi>
parents: 15037
diff changeset
75 dsync_brain_set_flags(struct dsync_brain *brain, enum dsync_brain_flags flags)
454d0563927d doveadm: "backup" command is working again.
Timo Sirainen <tss@iki.fi>
parents: 15037
diff changeset
76 {
15744
b4e2b3b54f0a dsync: Renamed "guid_requests" to "mail_requests"
Timo Sirainen <tss@iki.fi>
parents: 15715
diff changeset
77 brain->mail_requests =
b4e2b3b54f0a dsync: Renamed "guid_requests" to "mail_requests"
Timo Sirainen <tss@iki.fi>
parents: 15715
diff changeset
78 (flags & DSYNC_BRAIN_FLAG_SEND_MAIL_REQUESTS) != 0;
15231
454d0563927d doveadm: "backup" command is working again.
Timo Sirainen <tss@iki.fi>
parents: 15037
diff changeset
79 brain->backup_send = (flags & DSYNC_BRAIN_FLAG_BACKUP_SEND) != 0;
454d0563927d doveadm: "backup" command is working again.
Timo Sirainen <tss@iki.fi>
parents: 15037
diff changeset
80 brain->backup_recv = (flags & DSYNC_BRAIN_FLAG_BACKUP_RECV) != 0;
15464
af3b86a16ac3 dsync: Added debugging and rawlogging support.
Timo Sirainen <tss@iki.fi>
parents: 15355
diff changeset
81 brain->debug = (flags & DSYNC_BRAIN_FLAG_DEBUG) != 0;
15836
607758d425e7 dsync: Renamed -a parameter to -N. It also now skips invisible namespaces.
Timo Sirainen <tss@iki.fi>
parents: 15825
diff changeset
82 brain->sync_visible_namespaces =
607758d425e7 dsync: Renamed -a parameter to -N. It also now skips invisible namespaces.
Timo Sirainen <tss@iki.fi>
parents: 15825
diff changeset
83 (flags & DSYNC_BRAIN_FLAG_SYNC_VISIBLE_NAMESPACES) != 0;
16084
38774adaddaf dsync: -m '' parameter now syncs mailbox list, but no actual mails.
Timo Sirainen <tss@iki.fi>
parents: 15986
diff changeset
84 brain->no_mail_sync = (flags & DSYNC_BRAIN_FLAG_NO_MAIL_SYNC) != 0;
15231
454d0563927d doveadm: "backup" command is working again.
Timo Sirainen <tss@iki.fi>
parents: 15037
diff changeset
85 }
454d0563927d doveadm: "backup" command is working again.
Timo Sirainen <tss@iki.fi>
parents: 15037
diff changeset
86
9736
d9a96da46d4a dsync: Lots of updates and fixes.
Timo Sirainen <tss@iki.fi>
parents: 9686
diff changeset
87 struct dsync_brain *
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
88 dsync_brain_master_init(struct mail_user *user, struct dsync_ibc *ibc,
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
89 enum dsync_brain_sync_type sync_type,
16396
2d6a3035a6f7 dsync: Small code cleanup.
Timo Sirainen <tss@iki.fi>
parents: 16253
diff changeset
90 enum dsync_brain_flags flags,
2d6a3035a6f7 dsync: Small code cleanup.
Timo Sirainen <tss@iki.fi>
parents: 16253
diff changeset
91 const struct dsync_brain_settings *set)
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
92 {
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
93 struct dsync_ibc_settings ibc_set;
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
94 struct dsync_brain *brain;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
95 const char *error;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
96
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
97 i_assert(sync_type != DSYNC_BRAIN_SYNC_TYPE_UNKNOWN);
16396
2d6a3035a6f7 dsync: Small code cleanup.
Timo Sirainen <tss@iki.fi>
parents: 16253
diff changeset
98 i_assert(sync_type != DSYNC_BRAIN_SYNC_TYPE_STATE ||
2d6a3035a6f7 dsync: Small code cleanup.
Timo Sirainen <tss@iki.fi>
parents: 16253
diff changeset
99 (set->state != NULL && *set->state != '\0'));
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
100
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
101 brain = dsync_brain_common_init(user, ibc);
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
102 brain->sync_type = sync_type;
16396
2d6a3035a6f7 dsync: Small code cleanup.
Timo Sirainen <tss@iki.fi>
parents: 16253
diff changeset
103 if (set->sync_ns != NULL)
2d6a3035a6f7 dsync: Small code cleanup.
Timo Sirainen <tss@iki.fi>
parents: 16253
diff changeset
104 brain->sync_ns = set->sync_ns;
2d6a3035a6f7 dsync: Small code cleanup.
Timo Sirainen <tss@iki.fi>
parents: 16253
diff changeset
105 brain->sync_box = p_strdup(brain->pool, set->sync_box);
2d6a3035a6f7 dsync: Small code cleanup.
Timo Sirainen <tss@iki.fi>
parents: 16253
diff changeset
106 memcpy(brain->sync_box_guid, set->sync_box_guid,
2d6a3035a6f7 dsync: Small code cleanup.
Timo Sirainen <tss@iki.fi>
parents: 16253
diff changeset
107 sizeof(brain->sync_box_guid));
2d6a3035a6f7 dsync: Small code cleanup.
Timo Sirainen <tss@iki.fi>
parents: 16253
diff changeset
108 brain->lock_timeout = set->lock_timeout_secs;
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
109 brain->master_brain = TRUE;
15231
454d0563927d doveadm: "backup" command is working again.
Timo Sirainen <tss@iki.fi>
parents: 15037
diff changeset
110 dsync_brain_set_flags(brain, flags);
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
111
15782
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
112 if (sync_type == DSYNC_BRAIN_SYNC_TYPE_STATE &&
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
113 dsync_mailbox_states_import(brain->mailbox_states,
16396
2d6a3035a6f7 dsync: Small code cleanup.
Timo Sirainen <tss@iki.fi>
parents: 16253
diff changeset
114 brain->pool, set->state, &error) < 0) {
15782
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
115 hash_table_clear(brain->mailbox_states, FALSE);
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
116 i_error("Saved sync state is invalid, "
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
117 "falling back to full sync: %s", error);
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
118 brain->sync_type = sync_type = DSYNC_BRAIN_SYNC_TYPE_FULL;
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
119 }
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
120 dsync_brain_mailbox_trees_init(brain);
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
121
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
122 memset(&ibc_set, 0, sizeof(ibc_set));
15815
e7aabd79c9d5 dsync: Use full hostname+domain when comparing if hosts are different in locking.
Timo Sirainen <tss@iki.fi>
parents: 15782
diff changeset
123 ibc_set.hostname = my_hostdomain();
16396
2d6a3035a6f7 dsync: Small code cleanup.
Timo Sirainen <tss@iki.fi>
parents: 16253
diff changeset
124 ibc_set.sync_ns_prefix = set->sync_ns == NULL ? NULL :
2d6a3035a6f7 dsync: Small code cleanup.
Timo Sirainen <tss@iki.fi>
parents: 16253
diff changeset
125 set->sync_ns->prefix;
2d6a3035a6f7 dsync: Small code cleanup.
Timo Sirainen <tss@iki.fi>
parents: 16253
diff changeset
126 ibc_set.sync_box = set->sync_box;
2d6a3035a6f7 dsync: Small code cleanup.
Timo Sirainen <tss@iki.fi>
parents: 16253
diff changeset
127 memcpy(ibc_set.sync_box_guid, set->sync_box_guid,
16110
c51873a8e0d9 doveadm sync/backup: Added -g <guid> to sync only the specified mailbox (by GUID)
Timo Sirainen <tss@iki.fi>
parents: 16089
diff changeset
128 sizeof(ibc_set.sync_box_guid));
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
129 ibc_set.sync_type = sync_type;
16396
2d6a3035a6f7 dsync: Small code cleanup.
Timo Sirainen <tss@iki.fi>
parents: 16253
diff changeset
130 ibc_set.lock_timeout = set->lock_timeout_secs;
15231
454d0563927d doveadm: "backup" command is working again.
Timo Sirainen <tss@iki.fi>
parents: 15037
diff changeset
131 /* reverse the backup direction for the slave */
454d0563927d doveadm: "backup" command is working again.
Timo Sirainen <tss@iki.fi>
parents: 15037
diff changeset
132 ibc_set.brain_flags = flags & ~(DSYNC_BRAIN_FLAG_BACKUP_SEND |
454d0563927d doveadm: "backup" command is working again.
Timo Sirainen <tss@iki.fi>
parents: 15037
diff changeset
133 DSYNC_BRAIN_FLAG_BACKUP_RECV);
454d0563927d doveadm: "backup" command is working again.
Timo Sirainen <tss@iki.fi>
parents: 15037
diff changeset
134 if ((flags & DSYNC_BRAIN_FLAG_BACKUP_SEND) != 0)
454d0563927d doveadm: "backup" command is working again.
Timo Sirainen <tss@iki.fi>
parents: 15037
diff changeset
135 ibc_set.brain_flags |= DSYNC_BRAIN_FLAG_BACKUP_RECV;
454d0563927d doveadm: "backup" command is working again.
Timo Sirainen <tss@iki.fi>
parents: 15037
diff changeset
136 else if ((flags & DSYNC_BRAIN_FLAG_BACKUP_RECV) != 0)
454d0563927d doveadm: "backup" command is working again.
Timo Sirainen <tss@iki.fi>
parents: 15037
diff changeset
137 ibc_set.brain_flags |= DSYNC_BRAIN_FLAG_BACKUP_SEND;
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
138 dsync_ibc_send_handshake(ibc, &ibc_set);
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
139
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
140 dsync_ibc_set_io_callback(ibc, dsync_brain_run_io, brain);
15782
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
141 brain->state = DSYNC_STATE_MASTER_RECV_HANDSHAKE;
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
142 return brain;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
143 }
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
144
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
145 struct dsync_brain *
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
146 dsync_brain_slave_init(struct mail_user *user, struct dsync_ibc *ibc)
9559
34eef8a2716b dsync: Initial implementation.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
147 {
15782
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
148 struct dsync_ibc_settings ibc_set;
9559
34eef8a2716b dsync: Initial implementation.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
149 struct dsync_brain *brain;
34eef8a2716b dsync: Initial implementation.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
150
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
151 brain = dsync_brain_common_init(user, ibc);
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
152 brain->state = DSYNC_STATE_SLAVE_RECV_HANDSHAKE;
11734
ba9f33f9c6c3 dsync: If verbosity is enabled, log why desyncing was caused.
Timo Sirainen <tss@iki.fi>
parents: 11727
diff changeset
153
15782
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
154 memset(&ibc_set, 0, sizeof(ibc_set));
15815
e7aabd79c9d5 dsync: Use full hostname+domain when comparing if hosts are different in locking.
Timo Sirainen <tss@iki.fi>
parents: 15782
diff changeset
155 ibc_set.hostname = my_hostdomain();
15782
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
156 dsync_ibc_send_handshake(ibc, &ibc_set);
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
157
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
158 dsync_ibc_set_io_callback(ibc, dsync_brain_run_io, brain);
9559
34eef8a2716b dsync: Initial implementation.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
159 return brain;
34eef8a2716b dsync: Initial implementation.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
160 }
34eef8a2716b dsync: Initial implementation.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
161
34eef8a2716b dsync: Initial implementation.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
162 int dsync_brain_deinit(struct dsync_brain **_brain)
34eef8a2716b dsync: Initial implementation.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
163 {
34eef8a2716b dsync: Initial implementation.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
164 struct dsync_brain *brain = *_brain;
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
165 int ret;
11680
49b226835cd2 dsync: Another try at not increasing mailbox uidnext/highestmodseq on failure
Timo Sirainen <tss@iki.fi>
parents: 11677
diff changeset
166
9559
34eef8a2716b dsync: Initial implementation.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
167 *_brain = NULL;
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
168
15986
f6377e089dee dsync: If I/O gets stalled, log the state in which it happened.
Timo Sirainen <tss@iki.fi>
parents: 15836
diff changeset
169 if (dsync_ibc_has_timed_out(brain->ibc)) {
f6377e089dee dsync: If I/O gets stalled, log the state in which it happened.
Timo Sirainen <tss@iki.fi>
parents: 15836
diff changeset
170 i_error("Timeout during state=%s%s",
f6377e089dee dsync: If I/O gets stalled, log the state in which it happened.
Timo Sirainen <tss@iki.fi>
parents: 15836
diff changeset
171 dsync_state_names[brain->state],
f6377e089dee dsync: If I/O gets stalled, log the state in which it happened.
Timo Sirainen <tss@iki.fi>
parents: 15836
diff changeset
172 brain->state != DSYNC_STATE_SYNC_MAILS ? "" :
f6377e089dee dsync: If I/O gets stalled, log the state in which it happened.
Timo Sirainen <tss@iki.fi>
parents: 15836
diff changeset
173 t_strdup_printf(" (send=%s recv=%s)",
f6377e089dee dsync: If I/O gets stalled, log the state in which it happened.
Timo Sirainen <tss@iki.fi>
parents: 15836
diff changeset
174 dsync_box_state_names[brain->box_send_state],
f6377e089dee dsync: If I/O gets stalled, log the state in which it happened.
Timo Sirainen <tss@iki.fi>
parents: 15836
diff changeset
175 dsync_box_state_names[brain->box_recv_state]));
f6377e089dee dsync: If I/O gets stalled, log the state in which it happened.
Timo Sirainen <tss@iki.fi>
parents: 15836
diff changeset
176 }
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
177 if (dsync_ibc_has_failed(brain->ibc) ||
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
178 brain->state != DSYNC_STATE_DONE)
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
179 brain->failed = TRUE;
15632
40dd9a080b6e dsync: Close mail streams earlier on failures to avoid assert-crashing
Timo Sirainen <tss@iki.fi>
parents: 15594
diff changeset
180 dsync_ibc_close_mail_streams(brain->ibc);
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
181
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
182 if (brain->box != NULL)
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
183 dsync_brain_sync_mailbox_deinit(brain);
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
184 if (brain->local_tree_iter != NULL)
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
185 dsync_mailbox_tree_iter_deinit(&brain->local_tree_iter);
15232
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
186 if (brain->mailbox_states_iter != NULL)
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
187 hash_table_iterate_deinit(&brain->mailbox_states_iter);
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
188 hash_table_destroy(&brain->mailbox_states);
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
189
15782
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
190 if (brain->lock_fd != -1) {
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
191 /* unlink the lock file before it gets unlocked */
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
192 if (unlink(brain->lock_path) < 0)
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
193 i_error("unlink(%s) failed: %m", brain->lock_path);
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
194 file_lock_free(&brain->lock);
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
195 i_close_fd(&brain->lock_fd);
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
196 }
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
197
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
198 ret = brain->failed ? -1 : 0;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
199 pool_unref(&brain->pool);
9559
34eef8a2716b dsync: Initial implementation.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
200 return ret;
34eef8a2716b dsync: Initial implementation.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
201 }
34eef8a2716b dsync: Initial implementation.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
202
15782
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
203 static int
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
204 dsync_brain_lock(struct dsync_brain *brain, const char *remote_hostname)
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
205 {
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
206 struct stat st1, st2;
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
207 const char *home;
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
208 int ret;
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
209
15825
df29194c1ab6 dsync: Fix to checking which side should do the locking.
Timo Sirainen <tss@iki.fi>
parents: 15816
diff changeset
210 if ((ret = strcmp(remote_hostname, my_hostdomain())) < 0) {
15782
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
211 /* locking done by remote */
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
212 return 0;
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
213 }
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
214 if (ret == 0 && !brain->master_brain) {
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
215 /* running dsync within the same server.
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
216 locking done by master brain. */
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
217 return 0;
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
218 }
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
219
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
220 if ((ret = mail_user_get_home(brain->user, &home)) < 0) {
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
221 i_error("Couldn't look up user's home dir");
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
222 return -1;
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
223 }
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
224 if (ret == 0) {
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
225 i_error("User has no home directory");
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
226 return -1;
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
227 }
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
228
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
229 brain->lock_path = p_strconcat(brain->pool, home,
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
230 "/"DSYNC_LOCK_FILENAME, NULL);
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
231 for (;;) {
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
232 brain->lock_fd = creat(brain->lock_path, 0600);
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
233 if (brain->lock_fd == -1) {
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
234 i_error("Couldn't create lock %s: %m",
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
235 brain->lock_path);
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
236 return -1;
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
237 }
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
238
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
239 if (file_wait_lock(brain->lock_fd, brain->lock_path, F_WRLCK,
16089
b436c1f6bd06 dsync: Use fcntl() locking instead of flock().
Timo Sirainen <tss@iki.fi>
parents: 16084
diff changeset
240 FILE_LOCK_METHOD_FCNTL, brain->lock_timeout,
15782
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
241 &brain->lock) <= 0) {
16251
e41a13ae504d dsync: If dsync fails due to lock timeout, give a better error message.
Timo Sirainen <tss@iki.fi>
parents: 16222
diff changeset
242 if (errno == EAGAIN) {
e41a13ae504d dsync: If dsync fails due to lock timeout, give a better error message.
Timo Sirainen <tss@iki.fi>
parents: 16222
diff changeset
243 i_error("Couldn't lock %s: Timed out after %u seconds",
e41a13ae504d dsync: If dsync fails due to lock timeout, give a better error message.
Timo Sirainen <tss@iki.fi>
parents: 16222
diff changeset
244 brain->lock_path, brain->lock_timeout);
e41a13ae504d dsync: If dsync fails due to lock timeout, give a better error message.
Timo Sirainen <tss@iki.fi>
parents: 16222
diff changeset
245 } else {
e41a13ae504d dsync: If dsync fails due to lock timeout, give a better error message.
Timo Sirainen <tss@iki.fi>
parents: 16222
diff changeset
246 i_error("Couldn't lock %s: %m", brain->lock_path);
e41a13ae504d dsync: If dsync fails due to lock timeout, give a better error message.
Timo Sirainen <tss@iki.fi>
parents: 16222
diff changeset
247 }
15782
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
248 break;
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
249 }
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
250 if (fstat(brain->lock_fd, &st1) < 0) {
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
251 if (errno != ESTALE) {
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
252 i_error("fstat(%s) failed: %m", brain->lock_path);
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
253 break;
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
254 }
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
255 } else if (stat(brain->lock_path, &st2) < 0) {
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
256 if (errno != ENOENT) {
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
257 i_error("stat(%s) failed: %m", brain->lock_path);
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
258 break;
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
259 }
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
260 } else if (st1.st_ino == st2.st_ino) {
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
261 /* success */
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
262 return 0;
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
263 }
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
264 /* file was recreated, try again */
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
265 i_close_fd(&brain->lock_fd);
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
266 }
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
267 i_close_fd(&brain->lock_fd);
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
268 return -1;
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
269 }
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
270
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
271 static bool dsync_brain_master_recv_handshake(struct dsync_brain *brain)
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
272 {
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
273 const struct dsync_ibc_settings *ibc_set;
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
274
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
275 i_assert(brain->master_brain);
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
276
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
277 if (dsync_ibc_recv_handshake(brain->ibc, &ibc_set) == 0)
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
278 return FALSE;
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
279
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
280 if (brain->lock_timeout > 0) {
15816
adb9857a3879 dsync: If locking fails, fail instead of continuing.
Timo Sirainen <tss@iki.fi>
parents: 15815
diff changeset
281 if (dsync_brain_lock(brain, ibc_set->hostname) < 0) {
adb9857a3879 dsync: If locking fails, fail instead of continuing.
Timo Sirainen <tss@iki.fi>
parents: 15815
diff changeset
282 brain->failed = TRUE;
15782
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
283 return FALSE;
15816
adb9857a3879 dsync: If locking fails, fail instead of continuing.
Timo Sirainen <tss@iki.fi>
parents: 15815
diff changeset
284 }
15782
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
285 }
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
286
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
287 brain->state = brain->sync_type == DSYNC_BRAIN_SYNC_TYPE_STATE ?
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
288 DSYNC_STATE_MASTER_SEND_LAST_COMMON :
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
289 DSYNC_STATE_SEND_MAILBOX_TREE;
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
290 return TRUE;
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
291 }
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
292
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
293 static bool dsync_brain_slave_recv_handshake(struct dsync_brain *brain)
9559
34eef8a2716b dsync: Initial implementation.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
294 {
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
295 const struct dsync_ibc_settings *ibc_set;
9559
34eef8a2716b dsync: Initial implementation.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
296
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
297 i_assert(!brain->master_brain);
9559
34eef8a2716b dsync: Initial implementation.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
298
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
299 if (dsync_ibc_recv_handshake(brain->ibc, &ibc_set) == 0)
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
300 return FALSE;
10331
b5b253d35612 dsync: Fixed subscription syncing to work with namespace prefixes.
Timo Sirainen <tss@iki.fi>
parents: 10328
diff changeset
301
15782
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
302 if (ibc_set->lock_timeout > 0) {
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
303 brain->lock_timeout = ibc_set->lock_timeout;
15816
adb9857a3879 dsync: If locking fails, fail instead of continuing.
Timo Sirainen <tss@iki.fi>
parents: 15815
diff changeset
304 if (dsync_brain_lock(brain, ibc_set->hostname) < 0) {
adb9857a3879 dsync: If locking fails, fail instead of continuing.
Timo Sirainen <tss@iki.fi>
parents: 15815
diff changeset
305 brain->failed = TRUE;
15782
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
306 return FALSE;
15816
adb9857a3879 dsync: If locking fails, fail instead of continuing.
Timo Sirainen <tss@iki.fi>
parents: 15815
diff changeset
307 }
15782
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
308 }
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
309
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
310 if (ibc_set->sync_ns_prefix != NULL) {
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
311 brain->sync_ns = mail_namespace_find(brain->user->namespaces,
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
312 ibc_set->sync_ns_prefix);
11676
cf7f6912af02 dsync: Added backup command, which syncs source to destination, discarding any changes in dest.
Timo Sirainen <tss@iki.fi>
parents: 11524
diff changeset
313 }
15471
88ac919f8afe dsync: Added back support for syncing only one mailbox (-m parameter)
Timo Sirainen <tss@iki.fi>
parents: 15469
diff changeset
314 brain->sync_box = p_strdup(brain->pool, ibc_set->sync_box);
16110
c51873a8e0d9 doveadm sync/backup: Added -g <guid> to sync only the specified mailbox (by GUID)
Timo Sirainen <tss@iki.fi>
parents: 16089
diff changeset
315 memcpy(brain->sync_box_guid, ibc_set->sync_box_guid,
c51873a8e0d9 doveadm sync/backup: Added -g <guid> to sync only the specified mailbox (by GUID)
Timo Sirainen <tss@iki.fi>
parents: 16089
diff changeset
316 sizeof(brain->sync_box_guid));
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
317 i_assert(brain->sync_type == DSYNC_BRAIN_SYNC_TYPE_UNKNOWN);
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
318 brain->sync_type = ibc_set->sync_type;
15231
454d0563927d doveadm: "backup" command is working again.
Timo Sirainen <tss@iki.fi>
parents: 15037
diff changeset
319 dsync_brain_set_flags(brain, ibc_set->brain_flags);
11676
cf7f6912af02 dsync: Added backup command, which syncs source to destination, discarding any changes in dest.
Timo Sirainen <tss@iki.fi>
parents: 11524
diff changeset
320
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
321 dsync_brain_mailbox_trees_init(brain);
11676
cf7f6912af02 dsync: Added backup command, which syncs source to destination, discarding any changes in dest.
Timo Sirainen <tss@iki.fi>
parents: 11524
diff changeset
322
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
323 if (brain->sync_type == DSYNC_BRAIN_SYNC_TYPE_STATE)
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
324 brain->state = DSYNC_STATE_SLAVE_RECV_LAST_COMMON;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
325 else
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
326 brain->state = DSYNC_STATE_SEND_MAILBOX_TREE;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
327 return TRUE;
11727
42dfcf9c896b dsync backup: Fail if it looks like backup is running in wrong direction.
Timo Sirainen <tss@iki.fi>
parents: 11688
diff changeset
328 }
42dfcf9c896b dsync backup: Fail if it looks like backup is running in wrong direction.
Timo Sirainen <tss@iki.fi>
parents: 11688
diff changeset
329
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
330 static void dsync_brain_master_send_last_common(struct dsync_brain *brain)
9559
34eef8a2716b dsync: Initial implementation.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
331 {
15232
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
332 struct dsync_mailbox_state *state;
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
333 uint8_t *guid;
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
334 enum dsync_ibc_send_ret ret = DSYNC_IBC_SEND_RET_OK;
11727
42dfcf9c896b dsync backup: Fail if it looks like backup is running in wrong direction.
Timo Sirainen <tss@iki.fi>
parents: 11688
diff changeset
335
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
336 i_assert(brain->master_brain);
9736
d9a96da46d4a dsync: Lots of updates and fixes.
Timo Sirainen <tss@iki.fi>
parents: 9686
diff changeset
337
15232
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
338 if (brain->mailbox_states_iter == NULL) {
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
339 brain->mailbox_states_iter =
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
340 hash_table_iterate_init(brain->mailbox_states);
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
341 }
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
342
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
343 for (;;) {
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
344 if (ret == DSYNC_IBC_SEND_RET_FULL)
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
345 return;
15232
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
346 if (!hash_table_iterate(brain->mailbox_states_iter,
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
347 brain->mailbox_states, &guid, &state))
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
348 break;
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
349 ret = dsync_ibc_send_mailbox_state(brain->ibc, state);
9638
b11a3eda2477 dsync: Use expunged messages' GUIDs to determine what to do with missing messages at end of mailbox.
Timo Sirainen <tss@iki.fi>
parents: 9592
diff changeset
350 }
15232
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
351 hash_table_iterate_deinit(&brain->mailbox_states_iter);
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
352
16222
d79cf48f1072 dsync: Fixed talking to earlier dsync without mailbox attribute support.
Timo Sirainen <tss@iki.fi>
parents: 16110
diff changeset
353 dsync_ibc_send_end_of_list(brain->ibc, DSYNC_IBC_EOL_MAILBOX_STATE);
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
354 brain->state = DSYNC_STATE_SEND_MAILBOX_TREE;
15232
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
355 }
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
356
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
357 static void dsync_mailbox_state_add(struct dsync_brain *brain,
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
358 const struct dsync_mailbox_state *state)
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
359 {
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
360 struct dsync_mailbox_state *dupstate;
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
361 uint8_t *guid_p;
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
362
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
363 dupstate = p_new(brain->pool, struct dsync_mailbox_state, 1);
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
364 *dupstate = *state;
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
365 guid_p = dupstate->mailbox_guid;
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
366 hash_table_insert(brain->mailbox_states, guid_p, dupstate);
11524
c955d4789553 dsync: Fixed syncing \noselect mailboxes.
Timo Sirainen <tss@iki.fi>
parents: 10976
diff changeset
367 }
c955d4789553 dsync: Fixed syncing \noselect mailboxes.
Timo Sirainen <tss@iki.fi>
parents: 10976
diff changeset
368
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
369 static bool dsync_brain_slave_recv_last_common(struct dsync_brain *brain)
10328
b63fd6156663 dsync: Added support for subscription syncing.
Timo Sirainen <tss@iki.fi>
parents: 10323
diff changeset
370 {
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
371 struct dsync_mailbox_state state;
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
372 enum dsync_ibc_recv_ret ret;
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
373 bool changed = FALSE;
10328
b63fd6156663 dsync: Added support for subscription syncing.
Timo Sirainen <tss@iki.fi>
parents: 10323
diff changeset
374
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
375 i_assert(!brain->master_brain);
10328
b63fd6156663 dsync: Added support for subscription syncing.
Timo Sirainen <tss@iki.fi>
parents: 10323
diff changeset
376
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
377 while ((ret = dsync_ibc_recv_mailbox_state(brain->ibc, &state)) > 0) {
15232
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
378 dsync_mailbox_state_add(brain, &state);
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
379 changed = TRUE;
10328
b63fd6156663 dsync: Added support for subscription syncing.
Timo Sirainen <tss@iki.fi>
parents: 10323
diff changeset
380 }
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
381 if (ret == DSYNC_IBC_RECV_RET_FINISHED) {
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
382 brain->state = DSYNC_STATE_SEND_MAILBOX_TREE;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
383 changed = TRUE;
9736
d9a96da46d4a dsync: Lots of updates and fixes.
Timo Sirainen <tss@iki.fi>
parents: 9686
diff changeset
384 }
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
385 return changed;
10371
b99a19d5a93c dsync: Sync mailbox renames.
Timo Sirainen <tss@iki.fi>
parents: 10368
diff changeset
386 }
b99a19d5a93c dsync: Sync mailbox renames.
Timo Sirainen <tss@iki.fi>
parents: 10368
diff changeset
387
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
388 static bool dsync_brain_run_real(struct dsync_brain *brain, bool *changed_r)
9639
ffda7bd92ebc dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents: 9638
diff changeset
389 {
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
390 bool changed = FALSE, ret = TRUE;
9639
ffda7bd92ebc dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents: 9638
diff changeset
391
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
392 if (brain->failed)
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
393 return FALSE;
9686
4d5cc6ce68aa dsync: Lots of improvements and fixes. Appears to be somewhat working now.
Timo Sirainen <tss@iki.fi>
parents: 9644
diff changeset
394
15464
af3b86a16ac3 dsync: Added debugging and rawlogging support.
Timo Sirainen <tss@iki.fi>
parents: 15355
diff changeset
395 if (brain->debug) {
af3b86a16ac3 dsync: Added debugging and rawlogging support.
Timo Sirainen <tss@iki.fi>
parents: 15355
diff changeset
396 i_debug("brain %c: in state=%s", brain->master_brain ? 'M' : 'S',
af3b86a16ac3 dsync: Added debugging and rawlogging support.
Timo Sirainen <tss@iki.fi>
parents: 15355
diff changeset
397 dsync_state_names[brain->state]);
af3b86a16ac3 dsync: Added debugging and rawlogging support.
Timo Sirainen <tss@iki.fi>
parents: 15355
diff changeset
398 }
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
399 switch (brain->state) {
15782
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
400 case DSYNC_STATE_MASTER_RECV_HANDSHAKE:
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
401 changed = dsync_brain_master_recv_handshake(brain);
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
402 break;
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
403 case DSYNC_STATE_SLAVE_RECV_HANDSHAKE:
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
404 changed = dsync_brain_slave_recv_handshake(brain);
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
405 break;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
406 case DSYNC_STATE_MASTER_SEND_LAST_COMMON:
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
407 dsync_brain_master_send_last_common(brain);
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
408 changed = TRUE;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
409 break;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
410 case DSYNC_STATE_SLAVE_RECV_LAST_COMMON:
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
411 changed = dsync_brain_slave_recv_last_common(brain);
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
412 break;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
413 case DSYNC_STATE_SEND_MAILBOX_TREE:
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
414 dsync_brain_send_mailbox_tree(brain);
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
415 changed = TRUE;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
416 break;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
417 case DSYNC_STATE_RECV_MAILBOX_TREE:
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
418 changed = dsync_brain_recv_mailbox_tree(brain);
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
419 break;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
420 case DSYNC_STATE_SEND_MAILBOX_TREE_DELETES:
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
421 dsync_brain_send_mailbox_tree_deletes(brain);
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
422 changed = TRUE;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
423 break;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
424 case DSYNC_STATE_RECV_MAILBOX_TREE_DELETES:
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
425 changed = dsync_brain_recv_mailbox_tree_deletes(brain);
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
426 break;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
427 case DSYNC_STATE_MASTER_SEND_MAILBOX:
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
428 dsync_brain_master_send_mailbox(brain);
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
429 changed = TRUE;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
430 break;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
431 case DSYNC_STATE_SLAVE_RECV_MAILBOX:
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
432 changed = dsync_brain_slave_recv_mailbox(brain);
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
433 break;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
434 case DSYNC_STATE_SYNC_MAILS:
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
435 changed = dsync_brain_sync_mails(brain);
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
436 break;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
437 case DSYNC_STATE_DONE:
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
438 changed = TRUE;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
439 ret = FALSE;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
440 break;
9639
ffda7bd92ebc dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents: 9638
diff changeset
441 }
15464
af3b86a16ac3 dsync: Added debugging and rawlogging support.
Timo Sirainen <tss@iki.fi>
parents: 15355
diff changeset
442 if (brain->debug) {
af3b86a16ac3 dsync: Added debugging and rawlogging support.
Timo Sirainen <tss@iki.fi>
parents: 15355
diff changeset
443 i_debug("brain %c: out state=%s changed=%d",
af3b86a16ac3 dsync: Added debugging and rawlogging support.
Timo Sirainen <tss@iki.fi>
parents: 15355
diff changeset
444 brain->master_brain ? 'M' : 'S',
af3b86a16ac3 dsync: Added debugging and rawlogging support.
Timo Sirainen <tss@iki.fi>
parents: 15355
diff changeset
445 dsync_state_names[brain->state], changed);
af3b86a16ac3 dsync: Added debugging and rawlogging support.
Timo Sirainen <tss@iki.fi>
parents: 15355
diff changeset
446 }
9736
d9a96da46d4a dsync: Lots of updates and fixes.
Timo Sirainen <tss@iki.fi>
parents: 9686
diff changeset
447
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
448 *changed_r = changed;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
449 return brain->failed ? FALSE : ret;
10371
b99a19d5a93c dsync: Sync mailbox renames.
Timo Sirainen <tss@iki.fi>
parents: 10368
diff changeset
450 }
b99a19d5a93c dsync: Sync mailbox renames.
Timo Sirainen <tss@iki.fi>
parents: 10368
diff changeset
451
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
452 bool dsync_brain_run(struct dsync_brain *brain, bool *changed_r)
9559
34eef8a2716b dsync: Initial implementation.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
453 {
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
454 bool ret;
10373
036db604f86c dsync: Fixed problems with syncing mailbox names that are invalid on other side.
Timo Sirainen <tss@iki.fi>
parents: 10371
diff changeset
455
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
456 *changed_r = FALSE;
9559
34eef8a2716b dsync: Initial implementation.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
457
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
458 if (dsync_ibc_has_failed(brain->ibc)) {
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
459 brain->failed = TRUE;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
460 return FALSE;
12028
6ef21adeb61d dsync: If worker fails, abort earlier.
Timo Sirainen <tss@iki.fi>
parents: 11971
diff changeset
461 }
6ef21adeb61d dsync: If worker fails, abort earlier.
Timo Sirainen <tss@iki.fi>
parents: 11971
diff changeset
462
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
463 T_BEGIN {
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
464 ret = dsync_brain_run_real(brain, changed_r);
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
465 } T_END;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
466 return ret;
10368
9f2e8d230bd5 dsync: If some uid/modseq changes couldn't be done, exit with different value.
Timo Sirainen <tss@iki.fi>
parents: 10356
diff changeset
467 }
12124
eb5adb193064 dsync: Don't crash/hang if syncing fails before ioloop is started.
Timo Sirainen <tss@iki.fi>
parents: 12028
diff changeset
468
15232
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
469 void dsync_brain_get_state(struct dsync_brain *brain, string_t *output)
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
470 {
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
471 struct hash_iterate_context *iter;
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
472 struct dsync_mailbox_node *node;
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
473 const struct dsync_mailbox_state *new_state;
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
474 struct dsync_mailbox_state *state;
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
475 const uint8_t *guid_p;
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
476 uint8_t *guid;
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
477
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
478 /* update mailbox states */
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
479 array_foreach(&brain->remote_mailbox_states, new_state) {
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
480 guid_p = new_state->mailbox_guid;
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
481 state = hash_table_lookup(brain->mailbox_states, guid_p);
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
482 if (state != NULL)
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
483 *state = *new_state;
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
484 else
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
485 dsync_mailbox_state_add(brain, new_state);
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
486 }
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
487
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
488 /* remove nonexistent mailboxes */
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
489 iter = hash_table_iterate_init(brain->mailbox_states);
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
490 while (hash_table_iterate(iter, brain->mailbox_states, &guid, &state)) {
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
491 node = dsync_mailbox_tree_lookup_guid(brain->local_mailbox_tree,
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
492 guid);
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
493 if (node == NULL ||
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
494 node->existence != DSYNC_MAILBOX_NODE_EXISTS)
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
495 hash_table_remove(brain->mailbox_states, guid);
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
496 }
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
497 hash_table_iterate_deinit(&iter);
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
498
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
499 dsync_mailbox_states_export(brain->mailbox_states, output);
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
500 }
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
501
16253
1a58d4bcb49c dsync: -U parameter never updated replicator's full_sync state.
Timo Sirainen <tss@iki.fi>
parents: 16251
diff changeset
502 enum dsync_brain_sync_type dsync_brain_get_sync_type(struct dsync_brain *brain)
1a58d4bcb49c dsync: -U parameter never updated replicator's full_sync state.
Timo Sirainen <tss@iki.fi>
parents: 16251
diff changeset
503 {
1a58d4bcb49c dsync: -U parameter never updated replicator's full_sync state.
Timo Sirainen <tss@iki.fi>
parents: 16251
diff changeset
504 return brain->sync_type;
1a58d4bcb49c dsync: -U parameter never updated replicator's full_sync state.
Timo Sirainen <tss@iki.fi>
parents: 16251
diff changeset
505 }
1a58d4bcb49c dsync: -U parameter never updated replicator's full_sync state.
Timo Sirainen <tss@iki.fi>
parents: 16251
diff changeset
506
12124
eb5adb193064 dsync: Don't crash/hang if syncing fails before ioloop is started.
Timo Sirainen <tss@iki.fi>
parents: 12028
diff changeset
507 bool dsync_brain_has_failed(struct dsync_brain *brain)
eb5adb193064 dsync: Don't crash/hang if syncing fails before ioloop is started.
Timo Sirainen <tss@iki.fi>
parents: 12028
diff changeset
508 {
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
509 return brain->failed;
12124
eb5adb193064 dsync: Don't crash/hang if syncing fails before ioloop is started.
Timo Sirainen <tss@iki.fi>
parents: 12028
diff changeset
510 }