annotate src/doveadm/dsync/dsync-brain.c @ 16253:1a58d4bcb49c

dsync: -U parameter never updated replicator's full_sync state.
author Timo Sirainen <tss@iki.fi>
date Mon, 08 Apr 2013 15:33:23 +0300
parents e41a13ae504d
children 2d6a3035a6f7
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,
15471
88ac919f8afe dsync: Added back support for syncing only one mailbox (-m parameter)
Timo Sirainen <tss@iki.fi>
parents: 15469
diff changeset
89 struct mail_namespace *sync_ns, const char *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
90 const guid_128_t sync_box_guid,
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
91 enum dsync_brain_sync_type sync_type,
15782
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
92 enum dsync_brain_flags flags, unsigned int lock_timeout,
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
93 const char *state)
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
94 {
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
95 struct dsync_ibc_settings ibc_set;
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
96 struct dsync_brain *brain;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
97 const char *error;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
98
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
99 i_assert(sync_type != DSYNC_BRAIN_SYNC_TYPE_UNKNOWN);
14629
c93ca5e46a8a Marked functions parameters that are allowed to be NULL. Some APIs were also changed.
Timo Sirainen <tss@iki.fi>
parents: 14584
diff changeset
100 i_assert(sync_type != DSYNC_BRAIN_SYNC_TYPE_STATE || *state != '\0');
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
101
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
102 brain = dsync_brain_common_init(user, ibc);
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
103 brain->sync_type = sync_type;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
104 if (sync_ns != NULL)
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
105 brain->sync_ns = sync_ns;
15471
88ac919f8afe dsync: Added back support for syncing only one mailbox (-m parameter)
Timo Sirainen <tss@iki.fi>
parents: 15469
diff changeset
106 brain->sync_box = p_strdup(brain->pool, 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
107 memcpy(brain->sync_box_guid, sync_box_guid, sizeof(brain->sync_box_guid));
15782
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
108 brain->lock_timeout = lock_timeout;
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,
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
114 brain->pool, state, &error) < 0) {
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();
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
124 ibc_set.sync_ns_prefix = sync_ns == NULL ? NULL : sync_ns->prefix;
15471
88ac919f8afe dsync: Added back support for syncing only one mailbox (-m parameter)
Timo Sirainen <tss@iki.fi>
parents: 15469
diff changeset
125 ibc_set.sync_box = 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
126 memcpy(ibc_set.sync_box_guid, 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
127 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
128 ibc_set.sync_type = sync_type;
15782
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
129 ibc_set.lock_timeout = lock_timeout;
15231
454d0563927d doveadm: "backup" command is working again.
Timo Sirainen <tss@iki.fi>
parents: 15037
diff changeset
130 /* reverse the backup direction for the slave */
454d0563927d doveadm: "backup" command is working again.
Timo Sirainen <tss@iki.fi>
parents: 15037
diff changeset
131 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
132 DSYNC_BRAIN_FLAG_BACKUP_RECV);
454d0563927d doveadm: "backup" command is working again.
Timo Sirainen <tss@iki.fi>
parents: 15037
diff changeset
133 if ((flags & DSYNC_BRAIN_FLAG_BACKUP_SEND) != 0)
454d0563927d doveadm: "backup" command is working again.
Timo Sirainen <tss@iki.fi>
parents: 15037
diff changeset
134 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
135 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
136 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
137 dsync_ibc_send_handshake(ibc, &ibc_set);
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
138
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
139 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
140 brain->state = DSYNC_STATE_MASTER_RECV_HANDSHAKE;
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
141 return brain;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
142 }
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 struct dsync_brain *
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
145 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
146 {
15782
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
147 struct dsync_ibc_settings ibc_set;
9559
34eef8a2716b dsync: Initial implementation.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
148 struct dsync_brain *brain;
34eef8a2716b dsync: Initial implementation.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
149
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
150 brain = dsync_brain_common_init(user, ibc);
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
151 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
152
15782
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
153 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
154 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
155 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
156
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
157 dsync_ibc_set_io_callback(ibc, dsync_brain_run_io, brain);
9559
34eef8a2716b dsync: Initial implementation.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
158 return brain;
34eef8a2716b dsync: Initial implementation.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
159 }
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 int dsync_brain_deinit(struct dsync_brain **_brain)
34eef8a2716b dsync: Initial implementation.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
162 {
34eef8a2716b dsync: Initial implementation.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
163 struct dsync_brain *brain = *_brain;
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
164 int ret;
11680
49b226835cd2 dsync: Another try at not increasing mailbox uidnext/highestmodseq on failure
Timo Sirainen <tss@iki.fi>
parents: 11677
diff changeset
165
9559
34eef8a2716b dsync: Initial implementation.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
166 *_brain = NULL;
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
167
15986
f6377e089dee dsync: If I/O gets stalled, log the state in which it happened.
Timo Sirainen <tss@iki.fi>
parents: 15836
diff changeset
168 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
169 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
170 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
171 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
172 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
173 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
174 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
175 }
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
176 if (dsync_ibc_has_failed(brain->ibc) ||
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
177 brain->state != DSYNC_STATE_DONE)
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
178 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
179 dsync_ibc_close_mail_streams(brain->ibc);
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
180
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
181 if (brain->box != NULL)
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
182 dsync_brain_sync_mailbox_deinit(brain);
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
183 if (brain->local_tree_iter != NULL)
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
184 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
185 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
186 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
187 hash_table_destroy(&brain->mailbox_states);
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
188
15782
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
189 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
190 /* 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
191 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
192 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
193 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
194 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
195 }
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
196
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
197 ret = brain->failed ? -1 : 0;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
198 pool_unref(&brain->pool);
9559
34eef8a2716b dsync: Initial implementation.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
199 return ret;
34eef8a2716b dsync: Initial implementation.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
200 }
34eef8a2716b dsync: Initial implementation.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
201
15782
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
202 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
203 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
204 {
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
205 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
206 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
207 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
208
15825
df29194c1ab6 dsync: Fix to checking which side should do the locking.
Timo Sirainen <tss@iki.fi>
parents: 15816
diff changeset
209 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
210 /* 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
211 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
212 }
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
213 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
214 /* 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
215 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
216 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
217 }
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 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
220 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
221 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
222 }
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
223 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
224 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
225 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
226 }
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 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
229 "/"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
230 for (;;) {
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
231 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
232 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
233 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
234 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
235 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
236 }
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 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
239 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
240 &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
241 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
242 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
243 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
244 } else {
e41a13ae504d dsync: If dsync fails due to lock timeout, give a better error message.
Timo Sirainen <tss@iki.fi>
parents: 16222
diff changeset
245 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
246 }
15782
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
247 break;
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
248 }
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
249 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
250 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
251 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
252 break;
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
253 }
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
254 } 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
255 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
256 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
257 break;
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
258 }
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
259 } 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
260 /* success */
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
261 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
262 }
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
263 /* 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
264 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
265 }
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
266 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
267 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
268 }
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 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
271 {
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
272 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
273
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
274 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
275
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
276 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
277 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
278
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
279 if (brain->lock_timeout > 0) {
15816
adb9857a3879 dsync: If locking fails, fail instead of continuing.
Timo Sirainen <tss@iki.fi>
parents: 15815
diff changeset
280 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
281 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
282 return FALSE;
15816
adb9857a3879 dsync: If locking fails, fail instead of continuing.
Timo Sirainen <tss@iki.fi>
parents: 15815
diff changeset
283 }
15782
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
284 }
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 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
287 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
288 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
289 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
290 }
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
291
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
292 static bool dsync_brain_slave_recv_handshake(struct dsync_brain *brain)
9559
34eef8a2716b dsync: Initial implementation.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
293 {
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
294 const struct dsync_ibc_settings *ibc_set;
9559
34eef8a2716b dsync: Initial implementation.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
295
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
296 i_assert(!brain->master_brain);
9559
34eef8a2716b dsync: Initial implementation.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
297
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
298 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
299 return FALSE;
10331
b5b253d35612 dsync: Fixed subscription syncing to work with namespace prefixes.
Timo Sirainen <tss@iki.fi>
parents: 10328
diff changeset
300
15782
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
301 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
302 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
303 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
304 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
305 return FALSE;
15816
adb9857a3879 dsync: If locking fails, fail instead of continuing.
Timo Sirainen <tss@iki.fi>
parents: 15815
diff changeset
306 }
15782
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
307 }
6f024e0289da dsync: -l parameter locking is now done on the server with "lower" hostname.
Timo Sirainen <tss@iki.fi>
parents: 15750
diff changeset
308
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
309 if (ibc_set->sync_ns_prefix != NULL) {
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
310 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
311 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
312 }
15471
88ac919f8afe dsync: Added back support for syncing only one mailbox (-m parameter)
Timo Sirainen <tss@iki.fi>
parents: 15469
diff changeset
313 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
314 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
315 sizeof(brain->sync_box_guid));
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
316 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
317 brain->sync_type = ibc_set->sync_type;
15231
454d0563927d doveadm: "backup" command is working again.
Timo Sirainen <tss@iki.fi>
parents: 15037
diff changeset
318 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
319
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
320 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
321
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
322 if (brain->sync_type == DSYNC_BRAIN_SYNC_TYPE_STATE)
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
323 brain->state = DSYNC_STATE_SLAVE_RECV_LAST_COMMON;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
324 else
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
325 brain->state = DSYNC_STATE_SEND_MAILBOX_TREE;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
326 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
327 }
42dfcf9c896b dsync backup: Fail if it looks like backup is running in wrong direction.
Timo Sirainen <tss@iki.fi>
parents: 11688
diff changeset
328
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
329 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
330 {
15232
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
331 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
332 uint8_t *guid;
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
333 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
334
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
335 i_assert(brain->master_brain);
9736
d9a96da46d4a dsync: Lots of updates and fixes.
Timo Sirainen <tss@iki.fi>
parents: 9686
diff changeset
336
15232
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
337 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
338 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
339 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
340 }
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 for (;;) {
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
343 if (ret == DSYNC_IBC_SEND_RET_FULL)
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
344 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
345 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
346 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
347 break;
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
348 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
349 }
15232
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
350 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
351
16222
d79cf48f1072 dsync: Fixed talking to earlier dsync without mailbox attribute support.
Timo Sirainen <tss@iki.fi>
parents: 16110
diff changeset
352 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
353 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
354 }
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 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
357 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
358 {
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
359 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
360 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
361
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
362 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
363 *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
364 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
365 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
366 }
c955d4789553 dsync: Fixed syncing \noselect mailboxes.
Timo Sirainen <tss@iki.fi>
parents: 10976
diff changeset
367
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
368 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
369 {
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
370 struct dsync_mailbox_state state;
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
371 enum dsync_ibc_recv_ret ret;
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
372 bool changed = FALSE;
10328
b63fd6156663 dsync: Added support for subscription syncing.
Timo Sirainen <tss@iki.fi>
parents: 10323
diff changeset
373
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
374 i_assert(!brain->master_brain);
10328
b63fd6156663 dsync: Added support for subscription syncing.
Timo Sirainen <tss@iki.fi>
parents: 10323
diff changeset
375
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
376 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
377 dsync_mailbox_state_add(brain, &state);
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
378 changed = TRUE;
10328
b63fd6156663 dsync: Added support for subscription syncing.
Timo Sirainen <tss@iki.fi>
parents: 10323
diff changeset
379 }
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
380 if (ret == DSYNC_IBC_RECV_RET_FINISHED) {
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
381 brain->state = DSYNC_STATE_SEND_MAILBOX_TREE;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
382 changed = TRUE;
9736
d9a96da46d4a dsync: Lots of updates and fixes.
Timo Sirainen <tss@iki.fi>
parents: 9686
diff changeset
383 }
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
384 return changed;
10371
b99a19d5a93c dsync: Sync mailbox renames.
Timo Sirainen <tss@iki.fi>
parents: 10368
diff changeset
385 }
b99a19d5a93c dsync: Sync mailbox renames.
Timo Sirainen <tss@iki.fi>
parents: 10368
diff changeset
386
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
387 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
388 {
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
389 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
390
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
391 if (brain->failed)
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
392 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
393
15464
af3b86a16ac3 dsync: Added debugging and rawlogging support.
Timo Sirainen <tss@iki.fi>
parents: 15355
diff changeset
394 if (brain->debug) {
af3b86a16ac3 dsync: Added debugging and rawlogging support.
Timo Sirainen <tss@iki.fi>
parents: 15355
diff changeset
395 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
396 dsync_state_names[brain->state]);
af3b86a16ac3 dsync: Added debugging and rawlogging support.
Timo Sirainen <tss@iki.fi>
parents: 15355
diff changeset
397 }
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
398 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
399 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
400 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
401 break;
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
402 case DSYNC_STATE_SLAVE_RECV_HANDSHAKE:
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
403 changed = dsync_brain_slave_recv_handshake(brain);
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
404 break;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
405 case DSYNC_STATE_MASTER_SEND_LAST_COMMON:
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
406 dsync_brain_master_send_last_common(brain);
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
407 changed = TRUE;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
408 break;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
409 case DSYNC_STATE_SLAVE_RECV_LAST_COMMON:
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
410 changed = dsync_brain_slave_recv_last_common(brain);
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
411 break;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
412 case DSYNC_STATE_SEND_MAILBOX_TREE:
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
413 dsync_brain_send_mailbox_tree(brain);
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
414 changed = TRUE;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
415 break;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
416 case DSYNC_STATE_RECV_MAILBOX_TREE:
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
417 changed = dsync_brain_recv_mailbox_tree(brain);
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
418 break;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
419 case DSYNC_STATE_SEND_MAILBOX_TREE_DELETES:
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
420 dsync_brain_send_mailbox_tree_deletes(brain);
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
421 changed = TRUE;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
422 break;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
423 case DSYNC_STATE_RECV_MAILBOX_TREE_DELETES:
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
424 changed = dsync_brain_recv_mailbox_tree_deletes(brain);
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
425 break;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
426 case DSYNC_STATE_MASTER_SEND_MAILBOX:
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
427 dsync_brain_master_send_mailbox(brain);
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
428 changed = TRUE;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
429 break;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
430 case DSYNC_STATE_SLAVE_RECV_MAILBOX:
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
431 changed = dsync_brain_slave_recv_mailbox(brain);
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
432 break;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
433 case DSYNC_STATE_SYNC_MAILS:
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
434 changed = dsync_brain_sync_mails(brain);
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
435 break;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
436 case DSYNC_STATE_DONE:
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
437 changed = TRUE;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
438 ret = FALSE;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
439 break;
9639
ffda7bd92ebc dsync: Skip syncing mailboxes whose uidvalidity/uidnext/highest-modseq hasn't changed.
Timo Sirainen <tss@iki.fi>
parents: 9638
diff changeset
440 }
15464
af3b86a16ac3 dsync: Added debugging and rawlogging support.
Timo Sirainen <tss@iki.fi>
parents: 15355
diff changeset
441 if (brain->debug) {
af3b86a16ac3 dsync: Added debugging and rawlogging support.
Timo Sirainen <tss@iki.fi>
parents: 15355
diff changeset
442 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
443 brain->master_brain ? 'M' : 'S',
af3b86a16ac3 dsync: Added debugging and rawlogging support.
Timo Sirainen <tss@iki.fi>
parents: 15355
diff changeset
444 dsync_state_names[brain->state], changed);
af3b86a16ac3 dsync: Added debugging and rawlogging support.
Timo Sirainen <tss@iki.fi>
parents: 15355
diff changeset
445 }
9736
d9a96da46d4a dsync: Lots of updates and fixes.
Timo Sirainen <tss@iki.fi>
parents: 9686
diff changeset
446
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
447 *changed_r = changed;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
448 return brain->failed ? FALSE : ret;
10371
b99a19d5a93c dsync: Sync mailbox renames.
Timo Sirainen <tss@iki.fi>
parents: 10368
diff changeset
449 }
b99a19d5a93c dsync: Sync mailbox renames.
Timo Sirainen <tss@iki.fi>
parents: 10368
diff changeset
450
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
451 bool dsync_brain_run(struct dsync_brain *brain, bool *changed_r)
9559
34eef8a2716b dsync: Initial implementation.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
452 {
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
453 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
454
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
455 *changed_r = FALSE;
9559
34eef8a2716b dsync: Initial implementation.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
456
15037
920756cd29b8 dsync: Renamed "slave" to "ibc" (= inter-brain communicator)
Timo Sirainen <tss@iki.fi>
parents: 14939
diff changeset
457 if (dsync_ibc_has_failed(brain->ibc)) {
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
458 brain->failed = TRUE;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
459 return FALSE;
12028
6ef21adeb61d dsync: If worker fails, abort earlier.
Timo Sirainen <tss@iki.fi>
parents: 11971
diff changeset
460 }
6ef21adeb61d dsync: If worker fails, abort earlier.
Timo Sirainen <tss@iki.fi>
parents: 11971
diff changeset
461
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
462 T_BEGIN {
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
463 ret = dsync_brain_run_real(brain, changed_r);
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
464 } T_END;
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
465 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
466 }
12124
eb5adb193064 dsync: Don't crash/hang if syncing fails before ioloop is started.
Timo Sirainen <tss@iki.fi>
parents: 12028
diff changeset
467
15232
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
468 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
469 {
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
470 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
471 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
472 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
473 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
474 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
475 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
476
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
477 /* 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
478 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
479 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
480 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
481 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
482 *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
483 else
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
484 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
485 }
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 /* 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
488 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
489 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
490 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
491 guid);
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
492 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
493 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
494 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
495 }
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
496 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
497
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
498 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
499 }
6c850258002f doveadm sync/backup: Added -s <state> parameter to do a fast stateful sync.
Timo Sirainen <tss@iki.fi>
parents: 15231
diff changeset
500
16253
1a58d4bcb49c dsync: -U parameter never updated replicator's full_sync state.
Timo Sirainen <tss@iki.fi>
parents: 16251
diff changeset
501 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
502 {
1a58d4bcb49c dsync: -U parameter never updated replicator's full_sync state.
Timo Sirainen <tss@iki.fi>
parents: 16251
diff changeset
503 return brain->sync_type;
1a58d4bcb49c dsync: -U parameter never updated replicator's full_sync state.
Timo Sirainen <tss@iki.fi>
parents: 16251
diff changeset
504 }
1a58d4bcb49c dsync: -U parameter never updated replicator's full_sync state.
Timo Sirainen <tss@iki.fi>
parents: 16251
diff changeset
505
12124
eb5adb193064 dsync: Don't crash/hang if syncing fails before ioloop is started.
Timo Sirainen <tss@iki.fi>
parents: 12028
diff changeset
506 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
507 {
14584
b2076acc3715 Initial version of dsync rewrite.
Timo Sirainen <tss@iki.fi>
parents: 14253
diff changeset
508 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
509 }