/
director.c
1586 lines (1372 loc) · 45.7 KB
/
director.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2010-2018 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "ioloop.h"
#include "array.h"
#include "str.h"
#include "strescape.h"
#include "log-throttle.h"
#include "ipc-client.h"
#include "program-client.h"
#include "var-expand.h"
#include "istream.h"
#include "ostream.h"
#include "iostream-temp.h"
#include "mail-user-hash.h"
#include "user-directory.h"
#include "mail-host.h"
#include "director-host.h"
#include "director-connection.h"
#include "director.h"
#define DIRECTOR_IPC_PROXY_PATH "ipc"
#define DIRECTOR_DNS_SOCKET_PATH "dns-client"
#define DIRECTOR_RECONNECT_RETRY_SECS 60
#define DIRECTOR_RECONNECT_TIMEOUT_MSECS (30*1000)
#define DIRECTOR_USER_MOVE_TIMEOUT_MSECS (30*1000)
#define DIRECTOR_SYNC_TIMEOUT_MSECS (5*1000)
#define DIRECTOR_RING_MIN_WAIT_SECS 20
#define DIRECTOR_QUICK_RECONNECT_TIMEOUT_MSECS 1000
#define DIRECTOR_DELAYED_DIR_REMOVE_MSECS (1000*30)
const char *user_kill_state_names[USER_KILL_STATE_DELAY+1] = {
"none",
"killing",
"notify-received",
"waiting-for-notify",
"waiting-for-everyone",
"flushing",
"delay",
};
static struct event_category event_category_director = {
.name = "director",
};
static struct log_throttle *user_move_throttle;
static struct log_throttle *user_kill_fail_throttle;
static void director_hosts_purge_removed(struct director *dir);
static const struct log_throttle_settings director_log_throttle_settings = {
.throttle_at_max_per_interval = 100,
.unthrottle_at_max_per_interval = 2,
};
static void
director_user_kill_finish_delayed(struct director *dir, struct user *user,
bool skip_delay);
static bool director_is_self_ip_set(struct director *dir)
{
if (net_ip_compare(&dir->self_ip, &net_ip4_any))
return FALSE;
if (net_ip_compare(&dir->self_ip, &net_ip6_any))
return FALSE;
return TRUE;
}
static void director_find_self_ip(struct director *dir)
{
struct director_host *const *hosts;
unsigned int i, count;
hosts = array_get(&dir->dir_hosts, &count);
for (i = 0; i < count; i++) {
if (net_try_bind(&hosts[i]->ip) == 0) {
dir->self_ip = hosts[i]->ip;
return;
}
}
i_fatal("director_servers doesn't list ourself");
}
void director_find_self(struct director *dir)
{
if (dir->self_host != NULL)
return;
if (!director_is_self_ip_set(dir))
director_find_self_ip(dir);
dir->self_host = director_host_lookup(dir, &dir->self_ip,
dir->self_port);
if (dir->self_host == NULL) {
i_fatal("director_servers doesn't list ourself (%s:%u)",
net_ip2addr(&dir->self_ip), dir->self_port);
}
dir->self_host->self = TRUE;
}
static unsigned int director_find_self_idx(struct director *dir)
{
struct director_host *const *hosts;
unsigned int i, count;
i_assert(dir->self_host != NULL);
hosts = array_get(&dir->dir_hosts, &count);
for (i = 0; i < count; i++) {
if (hosts[i] == dir->self_host)
return i;
}
i_unreached();
}
static bool
director_has_outgoing_connection(struct director *dir,
struct director_host *host)
{
struct director_connection *const *connp;
array_foreach(&dir->connections, connp) {
if (director_connection_get_host(*connp) == host &&
!director_connection_is_incoming(*connp))
return TRUE;
}
return FALSE;
}
static void
director_log_connect(struct director *dir, struct director_host *host,
const char *reason)
{
string_t *str = t_str_new(128);
if (host->last_network_failure > 0) {
str_printfa(str, ", last network failure %ds ago",
(int)(ioloop_time - host->last_network_failure));
}
if (host->last_protocol_failure > 0) {
str_printfa(str, ", last protocol failure %ds ago",
(int)(ioloop_time - host->last_protocol_failure));
}
e_info(dir->event, "Connecting to %s:%u (as %s%s): %s",
host->ip_str, host->port,
net_ip2addr(&dir->self_ip), str_c(str), reason);
}
int director_connect_host(struct director *dir, struct director_host *host,
const char *reason)
{
in_port_t port;
int fd;
if (director_has_outgoing_connection(dir, host))
return 0;
director_log_connect(dir, host, reason);
port = dir->test_port != 0 ? dir->test_port : host->port;
fd = net_connect_ip(&host->ip, port, &dir->self_ip);
if (fd == -1) {
host->last_network_failure = ioloop_time;
e_error(dir->event, "connect(%s) failed: %m", host->name);
return -1;
}
/* Reset timestamp so that director_connect() won't skip this host
while we're still trying to connect to it */
host->last_network_failure = 0;
(void)director_connection_init_out(dir, fd, host);
return 0;
}
static struct director_host *
director_get_preferred_right_host(struct director *dir)
{
struct director_host *const *hosts, *host;
unsigned int i, count, self_idx;
hosts = array_get(&dir->dir_hosts, &count);
if (count == 1) {
/* self */
return NULL;
}
self_idx = director_find_self_idx(dir);
for (i = 0; i < count; i++) {
host = hosts[(self_idx + i + 1) % count];
if (!host->removed)
return host;
}
/* self, with some removed hosts */
return NULL;
}
static void director_quick_reconnect_retry(struct director *dir)
{
director_connect(dir, "Alone in director ring - trying to connect to others");
}
static bool director_wait_for_others(struct director *dir)
{
struct director_host *const *hostp;
/* don't assume we're alone until we've attempted to connect
to others for a while */
if (dir->ring_first_alone != 0 &&
ioloop_time - dir->ring_first_alone > DIRECTOR_RING_MIN_WAIT_SECS)
return FALSE;
if (dir->ring_first_alone == 0)
dir->ring_first_alone = ioloop_time;
/* reset all failures and try again */
array_foreach(&dir->dir_hosts, hostp) {
(*hostp)->last_network_failure = 0;
(*hostp)->last_protocol_failure = 0;
}
timeout_remove(&dir->to_reconnect);
dir->to_reconnect = timeout_add(DIRECTOR_QUICK_RECONNECT_TIMEOUT_MSECS,
director_quick_reconnect_retry, dir);
return TRUE;
}
void director_connect(struct director *dir, const char *reason)
{
struct director_host *const *hosts;
unsigned int i, count, self_idx;
self_idx = director_find_self_idx(dir);
/* try to connect to first working server on our right side.
the left side is supposed to connect to us. */
hosts = array_get(&dir->dir_hosts, &count);
for (i = 1; i < count; i++) {
unsigned int idx = (self_idx + i) % count;
if (hosts[idx]->removed)
continue;
if (hosts[idx]->last_network_failure +
DIRECTOR_RECONNECT_RETRY_SECS > ioloop_time) {
/* connection failed recently, don't try retrying here */
continue;
}
if (hosts[idx]->last_protocol_failure +
DIRECTOR_PROTOCOL_FAILURE_RETRY_SECS > ioloop_time) {
/* the director recently sent invalid protocol data,
don't try retrying yet */
continue;
}
if (director_connect_host(dir, hosts[idx], reason) == 0) {
/* success */
return;
}
}
if (count > 1 && director_wait_for_others(dir))
return;
/* we're the only one */
if (count > 1) {
e_warning(dir->event,
"director: Couldn't connect to right side, "
"we must be the only director left");
}
if (dir->left != NULL) {
/* since we couldn't connect to it,
it must have failed recently */
e_warning(dir->event,
"director: Assuming %s is dead, disconnecting",
director_connection_get_name(dir->left));
director_connection_deinit(&dir->left,
"This connection is dead?");
}
dir->ring_min_version = DIRECTOR_VERSION_MINOR;
if (!dir->ring_handshaked)
director_set_ring_handshaked(dir);
else if (!dir->ring_synced)
director_set_ring_synced(dir);
}
void director_set_ring_handshaked(struct director *dir)
{
i_assert(!dir->ring_handshaked);
timeout_remove(&dir->to_handshake_warning);
if (dir->ring_handshake_warning_sent) {
e_warning(dir->event,
"Directors have been connected, "
"continuing delayed requests");
dir->ring_handshake_warning_sent = FALSE;
}
e_debug(dir->event, "Director ring handshaked");
dir->ring_handshaked = TRUE;
director_set_ring_synced(dir);
}
static void director_reconnect_timeout(struct director *dir)
{
struct director_host *cur_host, *preferred_host =
director_get_preferred_right_host(dir);
cur_host = dir->right == NULL ? NULL :
director_connection_get_host(dir->right);
if (preferred_host == NULL) {
/* all directors have been removed, try again later */
} else if (cur_host != preferred_host) {
(void)director_connect_host(dir, preferred_host,
"Reconnect attempt to preferred director");
} else {
/* the connection hasn't finished sync yet.
keep this timeout for now. */
}
}
void director_set_ring_synced(struct director *dir)
{
struct director_host *host;
i_assert(!dir->ring_synced);
i_assert((dir->left != NULL && dir->right != NULL) ||
(dir->left == NULL && dir->right == NULL));
timeout_remove(&dir->to_handshake_warning);
if (dir->ring_handshake_warning_sent) {
e_warning(dir->event,
"Ring is synced, continuing delayed requests "
"(syncing took %d secs, hosts_hash=%u)",
(int)(ioloop_time - dir->ring_last_sync_time),
mail_hosts_hash(dir->mail_hosts));
dir->ring_handshake_warning_sent = FALSE;
}
host = dir->right == NULL ? NULL :
director_connection_get_host(dir->right);
timeout_remove(&dir->to_reconnect);
if (host != director_get_preferred_right_host(dir)) {
/* try to reconnect to preferred host later */
dir->to_reconnect =
timeout_add(DIRECTOR_RECONNECT_TIMEOUT_MSECS,
director_reconnect_timeout, dir);
}
if (dir->left != NULL)
director_connection_set_synced(dir->left, TRUE);
if (dir->right != NULL)
director_connection_set_synced(dir->right, TRUE);
timeout_remove(&dir->to_sync);
dir->ring_synced = TRUE;
dir->ring_last_sync_time = ioloop_time;
/* If there are any director hosts still marked as "removed", we can
safely remove those now. The entire director cluster knows about the
removal now. */
director_hosts_purge_removed(dir);
mail_hosts_set_synced(dir->mail_hosts);
director_set_state_changed(dir);
}
void director_sync_send(struct director *dir, struct director_host *host,
uint32_t seq, unsigned int minor_version,
unsigned int timestamp, unsigned int hosts_hash)
{
string_t *str;
if (host == dir->self_host) {
dir->last_sync_sent_ring_change_counter = dir->ring_change_counter;
dir->last_sync_start_time = ioloop_timeval;
}
str = t_str_new(128);
str_printfa(str, "SYNC\t%s\t%u\t%u",
host->ip_str, host->port, seq);
if (minor_version > 0 &&
director_connection_get_minor_version(dir->right) > 0) {
/* only minor_version>0 supports extra parameters */
str_printfa(str, "\t%u\t%u\t%u", minor_version,
timestamp, hosts_hash);
}
str_append_c(str, '\n');
director_connection_send(dir->right, str_c(str));
/* ping our connections in case either of them are hanging.
if they are, we want to know it fast. */
if (dir->left != NULL)
director_connection_ping(dir->left);
director_connection_ping(dir->right);
}
static bool
director_has_any_outgoing_connections(struct director *dir)
{
struct director_connection *const *connp;
array_foreach(&dir->connections, connp) {
if (!director_connection_is_incoming(*connp))
return TRUE;
}
return FALSE;
}
bool director_resend_sync(struct director *dir)
{
if (dir->ring_synced) {
/* everything ok, no need to do anything */
return FALSE;
}
if (dir->right == NULL) {
/* right side connection is missing. make sure we're not
hanging due to some bug. */
if (dir->to_reconnect == NULL &&
!director_has_any_outgoing_connections(dir)) {
e_warning(dir->event,
"Right side connection is unexpectedly lost, reconnecting");
director_connect(dir, "Right side connection lost");
}
} else if (dir->left != NULL) {
/* send a new SYNC in case the previous one got dropped */
dir->self_host->last_sync_timestamp = ioloop_time;
director_sync_send(dir, dir->self_host, dir->sync_seq,
DIRECTOR_VERSION_MINOR, ioloop_time,
mail_hosts_hash(dir->mail_hosts));
if (dir->to_sync != NULL)
timeout_reset(dir->to_sync);
return TRUE;
}
return FALSE;
}
static void director_sync_timeout(struct director *dir)
{
i_assert(!dir->ring_synced);
if (director_resend_sync(dir))
e_error(dir->event, "Ring SYNC seq=%u appears to have got lost, resending", dir->sync_seq);
}
void director_set_ring_unsynced(struct director *dir)
{
if (dir->ring_synced) {
dir->ring_synced = FALSE;
dir->ring_last_sync_time = ioloop_time;
}
if (dir->to_sync == NULL) {
dir->to_sync = timeout_add(DIRECTOR_SYNC_TIMEOUT_MSECS,
director_sync_timeout, dir);
} else {
timeout_reset(dir->to_sync);
}
}
static void director_sync(struct director *dir)
{
/* we're synced again when we receive this SYNC back */
dir->sync_seq++;
if (dir->right == NULL && dir->left == NULL) {
/* we're alone. if we're already synced,
don't become unsynced. */
return;
}
director_set_ring_unsynced(dir);
if (dir->sync_frozen) {
dir->sync_pending = TRUE;
return;
}
if (dir->right == NULL) {
i_assert(!dir->ring_synced ||
(dir->left == NULL && dir->right == NULL));
e_debug(dir->event, "Ring is desynced (seq=%u, no right connection)",
dir->sync_seq);
return;
}
e_debug(dir->event, "Ring is desynced (seq=%u, sending SYNC to %s)",
dir->sync_seq, dir->right == NULL ? "(nowhere)" :
director_connection_get_name(dir->right));
/* send PINGs to our connections more rapidly until we've synced again.
if the connection has actually died, we don't need to wait (and
delay requests) for as long to detect it */
if (dir->left != NULL)
director_connection_set_synced(dir->left, FALSE);
director_connection_set_synced(dir->right, FALSE);
director_sync_send(dir, dir->self_host, dir->sync_seq,
DIRECTOR_VERSION_MINOR, ioloop_time,
mail_hosts_hash(dir->mail_hosts));
}
void director_sync_freeze(struct director *dir)
{
struct director_connection *const *connp;
i_assert(!dir->sync_frozen);
i_assert(!dir->sync_pending);
array_foreach(&dir->connections, connp)
director_connection_cork(*connp);
dir->sync_frozen = TRUE;
}
void director_sync_thaw(struct director *dir)
{
struct director_connection *const *connp;
i_assert(dir->sync_frozen);
dir->sync_frozen = FALSE;
if (dir->sync_pending) {
dir->sync_pending = FALSE;
director_sync(dir);
}
array_foreach(&dir->connections, connp)
director_connection_uncork(*connp);
}
void director_notify_ring_added(struct director_host *added_host,
struct director_host *src, bool log)
{
const char *cmd;
if (log) {
e_info(added_host->dir->event,
"Adding director %s to ring (requested by %s)",
added_host->name, src->name);
}
added_host->dir->ring_change_counter++;
cmd = t_strdup_printf("DIRECTOR\t%s\t%u\n",
added_host->ip_str, added_host->port);
director_update_send(added_host->dir, src, cmd);
}
static void director_hosts_purge_removed(struct director *dir)
{
struct director_host *const *hosts, *host;
unsigned int i, count;
timeout_remove(&dir->to_remove_dirs);
hosts = array_get(&dir->dir_hosts, &count);
for (i = 0; i < count; ) {
if (hosts[i]->removed) {
host = hosts[i];
director_host_free(&host);
hosts = array_get(&dir->dir_hosts, &count);
} else {
i++;
}
}
}
void director_ring_remove(struct director_host *removed_host,
struct director_host *src)
{
struct director *dir = removed_host->dir;
struct director_connection *const *conns, *conn;
unsigned int i, count;
const char *cmd;
e_info(dir->event, "Removing director %s from ring (requested by %s)",
removed_host->name, src->name);
if (removed_host->self && !src->self) {
/* others will just disconnect us */
return;
}
if (!removed_host->self) {
/* mark the host as removed and fully remove it later. this
delay is needed, because the removal may trigger director
reconnections, which may send the director back and we don't
want to re-add it */
removed_host->removed = TRUE;
if (dir->to_remove_dirs == NULL) {
dir->to_remove_dirs =
timeout_add(DIRECTOR_DELAYED_DIR_REMOVE_MSECS,
director_hosts_purge_removed, dir);
}
}
/* if our left or ride side gets removed, notify them first
before disconnecting. */
cmd = t_strdup_printf("DIRECTOR-REMOVE\t%s\t%u\n",
removed_host->ip_str, removed_host->port);
director_update_send_version(dir, src,
DIRECTOR_VERSION_RING_REMOVE, cmd);
/* disconnect any connections to the host */
conns = array_get(&dir->connections, &count);
for (i = 0; i < count; ) {
conn = conns[i];
if (director_connection_get_host(conn) != removed_host ||
removed_host->self)
i++;
else {
director_connection_deinit(&conn, "Removing from ring");
conns = array_get(&dir->connections, &count);
}
}
if (dir->right == NULL)
director_connect(dir, "Reconnecting after director was removed");
director_sync(dir);
}
static void
director_send_host(struct director *dir, struct director_host *src,
struct director_host *orig_src,
struct mail_host *host)
{
const char *host_tag = mail_host_get_tag(host);
string_t *str;
if (orig_src == NULL) {
orig_src = dir->self_host;
orig_src->last_seq++;
}
str = t_str_new(128);
str_printfa(str, "HOST\t%s\t%u\t%u\t%s\t%u",
orig_src->ip_str, orig_src->port, orig_src->last_seq,
host->ip_str, host->vhost_count);
if (dir->ring_min_version >= DIRECTOR_VERSION_TAGS_V2) {
str_append_c(str, '\t');
str_append_tabescaped(str, host_tag);
} else if (host_tag[0] != '\0' &&
dir->ring_min_version < DIRECTOR_VERSION_TAGS_V2) {
if (dir->ring_min_version < DIRECTOR_VERSION_TAGS) {
e_error(dir->event, "Ring has directors that don't support tags - removing host %s with tag '%s'",
host->ip_str, host_tag);
} else {
e_error(dir->event, "Ring has directors that support mixed versions of tags - removing host %s with tag '%s'",
host->ip_str, host_tag);
}
director_remove_host(dir, NULL, NULL, host);
return;
}
if (dir->ring_min_version >= DIRECTOR_VERSION_UPDOWN) {
str_printfa(str, "\t%c%ld\t", host->down ? 'D' : 'U',
(long)host->last_updown_change);
/* add any further version checks here - these directors ignore
any extra unknown arguments */
if (host->hostname != NULL)
str_append_tabescaped(str, host->hostname);
}
str_append_c(str, '\n');
director_update_send(dir, src, str_c(str));
}
void director_resend_hosts(struct director *dir)
{
struct mail_host *const *hostp;
array_foreach(mail_hosts_get(dir->mail_hosts), hostp)
director_send_host(dir, dir->self_host, NULL, *hostp);
}
void director_update_host(struct director *dir, struct director_host *src,
struct director_host *orig_src,
struct mail_host *host)
{
/* update state in case this is the first mail host being added */
director_set_state_changed(dir);
e_debug(dir->event, "Updating host %s vhost_count=%u "
"down=%d last_updown_change=%ld (hosts_hash=%u)",
host->ip_str, host->vhost_count, host->down ? 1 : 0,
(long)host->last_updown_change,
mail_hosts_hash(dir->mail_hosts));
director_send_host(dir, src, orig_src, host);
/* mark the host desynced until ring is synced again. except if we're
alone in the ring that never happens. */
if (dir->right != NULL || dir->left != NULL)
host->desynced = TRUE;
director_sync(dir);
}
void director_remove_host(struct director *dir, struct director_host *src,
struct director_host *orig_src,
struct mail_host *host)
{
struct user_directory *users = host->tag->users;
if (src != NULL) {
if (orig_src == NULL) {
orig_src = dir->self_host;
orig_src->last_seq++;
}
director_update_send(dir, src, t_strdup_printf(
"HOST-REMOVE\t%s\t%u\t%u\t%s\n",
orig_src->ip_str, orig_src->port,
orig_src->last_seq, host->ip_str));
}
user_directory_remove_host(users, host);
mail_host_remove(host);
director_sync(dir);
}
void director_flush_host(struct director *dir, struct director_host *src,
struct director_host *orig_src,
struct mail_host *host)
{
struct user_directory *users = host->tag->users;
if (orig_src == NULL) {
orig_src = dir->self_host;
orig_src->last_seq++;
}
director_update_send(dir, src, t_strdup_printf(
"HOST-FLUSH\t%s\t%u\t%u\t%s\n",
orig_src->ip_str, orig_src->port, orig_src->last_seq,
host->ip_str));
user_directory_remove_host(users, host);
director_sync(dir);
}
void director_update_user(struct director *dir, struct director_host *src,
struct user *user)
{
struct director_connection *const *connp;
i_assert(src != NULL);
i_assert(!user->weak);
array_foreach(&dir->connections, connp) {
if (director_connection_get_host(*connp) == src)
continue;
if (director_connection_get_minor_version(*connp) >= DIRECTOR_VERSION_USER_TIMESTAMP) {
director_connection_send(*connp, t_strdup_printf(
"USER\t%u\t%s\t%u\n", user->username_hash, user->host->ip_str,
user->timestamp));
} else {
director_connection_send(*connp, t_strdup_printf(
"USER\t%u\t%s\n", user->username_hash, user->host->ip_str));
}
}
}
void director_update_user_weak(struct director *dir, struct director_host *src,
struct director_connection *src_conn,
struct director_host *orig_src,
struct user *user)
{
const char *cmd;
i_assert(src != NULL);
i_assert(user->weak);
if (orig_src == NULL) {
orig_src = dir->self_host;
orig_src->last_seq++;
}
cmd = t_strdup_printf("USER-WEAK\t%s\t%u\t%u\t%u\t%s\n",
orig_src->ip_str, orig_src->port, orig_src->last_seq,
user->username_hash, user->host->ip_str);
if (src != dir->self_host && dir->left != NULL && dir->right != NULL &&
director_connection_get_host(dir->left) ==
director_connection_get_host(dir->right)) {
/* only two directors in this ring and we're forwarding
USER-WEAK from one director back to itself via another
so it sees we've received it. we can't use
director_update_send() for this, because it doesn't send
data back to the source. */
if (dir->right == src_conn)
director_connection_send(dir->left, cmd);
else if (dir->left == src_conn)
director_connection_send(dir->right, cmd);
else
i_unreached();
} else {
director_update_send(dir, src, cmd);
}
}
static void
director_flush_user_continue(int result, struct director_kill_context *ctx)
{
struct director *dir = ctx->dir;
ctx->callback_pending = FALSE;
struct user *user = user_directory_lookup(ctx->tag->users,
ctx->username_hash);
if (result == 0) {
struct istream *is = iostream_temp_finish(&ctx->reply, SIZE_MAX);
char *data;
i_stream_set_return_partial_line(is, TRUE);
data = i_stream_read_next_line(is);
e_error(dir->event, "%s: Failed to flush user hash %u in host %s: %s",
ctx->socket_path,
ctx->username_hash,
net_ip2addr(&ctx->host_ip),
data == NULL ? "(no output to stdout)" : data);
while((data = i_stream_read_next_line(is)) != NULL) {
e_error(dir->event, "%s: Failed to flush user hash %u in host %s: %s",
ctx->socket_path,
ctx->username_hash,
net_ip2addr(&ctx->host_ip), data);
}
i_stream_unref(&is);
} else {
o_stream_unref(&ctx->reply);
}
program_client_destroy(&ctx->pclient);
if (!DIRECTOR_KILL_CONTEXT_IS_VALID(user, ctx)) {
/* user was already freed - ignore */
e_debug(dir->event, "User %u freed while flushing, result=%d",
ctx->username_hash, result);
i_assert(ctx->to_move == NULL);
i_free(ctx);
} else {
/* ctx is freed later via user->kill_ctx */
e_debug(dir->event, "Flushing user %u finished, result=%d",
ctx->username_hash, result);
director_user_kill_finish_delayed(dir, user, result == 1);
}
}
static void
director_flush_user(struct director *dir, struct user *user)
{
struct director_kill_context *ctx = user->kill_ctx;
struct var_expand_table tab[] = {
{ 'i', user->host->ip_str, "ip" },
{ 'h', user->host->hostname, "host" },
{ '\0', NULL, NULL }
};
const char *error;
/* Execute flush script, if set. Only the director that started the
user moving will call the flush script. Having each director do it
would be redundant since they're all supposed to be performing the
same flush task to the same backend.
Flushing is also not triggered if we're moving a user that we just
created due to the user move. This means that the user doesn't have
an old host, so we couldn't really even perform any flushing on the
backend. */
if (*dir->set->director_flush_socket == '\0' ||
ctx->old_host_ip.family == 0 ||
!ctx->kill_is_self_initiated) {
director_user_kill_finish_delayed(dir, user, FALSE);
return;
}
ctx->host_ip = user->host->ip;
string_t *s_sock = str_new(default_pool, 32);
if (var_expand(s_sock, dir->set->director_flush_socket, tab, &error) <= 0) {
e_error(dir->event, "Failed to expand director_flush_socket=%s: %s",
dir->set->director_flush_socket, error);
director_user_kill_finish_delayed(dir, user, FALSE);
return;
}
ctx->socket_path = str_free_without_data(&s_sock);
struct program_client_settings set = {
.client_connect_timeout_msecs = 10000,
.dns_client_socket_path = DIRECTOR_DNS_SOCKET_PATH,
};
restrict_access_init(&set.restrict_set);
const char *const args[] = {
"FLUSH",
t_strdup_printf("%u", user->username_hash),
net_ip2addr(&ctx->old_host_ip),
user->host->ip_str,
ctx->old_host_down ? "down" : "up",
dec2str(ctx->old_host_vhost_count),
NULL
};
ctx->kill_state = USER_KILL_STATE_FLUSHING;
e_debug(dir->event, "Flushing user %u via %s", user->username_hash,
ctx->socket_path);
if ((program_client_create(ctx->socket_path, args, &set, FALSE,
&ctx->pclient, &error)) != 0) {
e_error(dir->event, "%s: Failed to flush user hash %u in host %s: %s",
ctx->socket_path,
user->username_hash,
user->host->ip_str,
error);
director_flush_user_continue(0, ctx);
return;
}
ctx->reply =
iostream_temp_create_named("/tmp", 0,
t_strdup_printf("flush response from %s",
user->host->ip_str));
o_stream_set_no_error_handling(ctx->reply, TRUE);
program_client_set_output(ctx->pclient, ctx->reply);
ctx->callback_pending = TRUE;
program_client_run_async(ctx->pclient, director_flush_user_continue, ctx);
}
static void director_user_move_finished(struct director *dir)
{
i_assert(dir->users_moving_count > 0);
dir->users_moving_count--;
director_set_state_changed(dir);
}
static void director_user_move_free(struct user *user)
{
struct director *dir = user->kill_ctx->dir;
struct director_kill_context *kill_ctx = user->kill_ctx;
i_assert(kill_ctx != NULL);
e_debug(dir->event, "User %u move finished at state=%s", user->username_hash,
user_kill_state_names[kill_ctx->kill_state]);
if (kill_ctx->ipc_cmd != NULL)
ipc_client_cmd_abort(dir->ipc_proxy, &kill_ctx->ipc_cmd);
timeout_remove(&kill_ctx->to_move);
i_free(kill_ctx->socket_path);
i_free(kill_ctx);
user->kill_ctx = NULL;
director_user_move_finished(dir);
}
static void
director_user_kill_finish_delayed_to(struct user *user)
{
i_assert(user->kill_ctx != NULL);
i_assert(user->kill_ctx->kill_state == USER_KILL_STATE_DELAY);
director_user_move_free(user);
}
static void
director_user_kill_finish_delayed(struct director *dir, struct user *user,
bool skip_delay)
{
if (skip_delay) {
user->kill_ctx->kill_state = USER_KILL_STATE_NONE;
director_user_move_free(user);
return;
}
user->kill_ctx->kill_state = USER_KILL_STATE_DELAY;
/* wait for a while for the kills to finish in the backend server,
so there are no longer any processes running for the user before we
start letting new in connections to the new server. */
timeout_remove(&user->kill_ctx->to_move);
user->kill_ctx->to_move =
timeout_add(dir->set->director_user_kick_delay * 1000,
director_user_kill_finish_delayed_to, user);
}
static void
director_finish_user_kill(struct director *dir, struct user *user, bool self)
{
struct director_kill_context *kill_ctx = user->kill_ctx;
i_assert(kill_ctx != NULL);
i_assert(kill_ctx->kill_state != USER_KILL_STATE_FLUSHING);
i_assert(kill_ctx->kill_state != USER_KILL_STATE_DELAY);
e_debug(dir->event, "User %u kill finished - %sstate=%s", user->username_hash,
self ? "we started it " : "",
user_kill_state_names[kill_ctx->kill_state]);
if (dir->right == NULL) {
/* we're alone */
director_flush_user(dir, user);
} else if (self ||
kill_ctx->kill_state == USER_KILL_STATE_KILLING_NOTIFY_RECEIVED) {
director_connection_send(dir->right, t_strdup_printf(
"USER-KILLED\t%u\n", user->username_hash));
kill_ctx->kill_state = USER_KILL_STATE_KILLED_WAITING_FOR_EVERYONE;
} else {
i_assert(kill_ctx->kill_state == USER_KILL_STATE_KILLING);
kill_ctx->kill_state = USER_KILL_STATE_KILLED_WAITING_FOR_NOTIFY;
}
}
static void director_user_kill_fail_throttled(unsigned int new_events_count,