/
replication.cc
932 lines (836 loc) · 25.3 KB
/
replication.cc
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
/*
* Copyright 2010-2016, Tarantool AUTHORS, please see AUTHORS file.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* 1. Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "replication.h"
#include <fiber.h> /* &cord->slabc */
#include <fiber_channel.h>
#include <scoped_guard.h>
#include <small/mempool.h>
#include "box.h"
#include "gc.h"
#include "error.h"
#include "relay.h"
#include "vclock.h" /* VCLOCK_MAX */
#include "sio.h"
uint32_t instance_id = REPLICA_ID_NIL;
struct tt_uuid INSTANCE_UUID;
struct tt_uuid REPLICASET_UUID;
double replication_timeout = 1.0; /* seconds */
double replication_connect_timeout = 30.0; /* seconds */
int replication_connect_quorum = REPLICATION_CONNECT_QUORUM_ALL;
double replication_sync_lag = 10.0; /* seconds */
double replication_sync_timeout = 300.0; /* seconds */
bool replication_skip_conflict = false;
struct replicaset replicaset;
static int
replica_compare_by_uuid(const struct replica *a, const struct replica *b)
{
return tt_uuid_compare(&a->uuid, &b->uuid);
}
rb_gen(MAYBE_UNUSED static, replica_hash_, replica_hash_t,
struct replica, in_hash, replica_compare_by_uuid);
#define replica_hash_foreach_safe(hash, item, next) \
for (item = replica_hash_first(hash); \
item != NULL && ((next = replica_hash_next(hash, item)) || 1); \
item = next)
/**
* Return the number of replicas that have to be synchronized
* in order to form a quorum in the replica set.
*/
static inline int
replicaset_quorum(void)
{
return MIN(replication_connect_quorum, replicaset.applier.total);
}
void
replication_init(void)
{
memset(&replicaset, 0, sizeof(replicaset));
replica_hash_new(&replicaset.hash);
rlist_create(&replicaset.anon);
vclock_create(&replicaset.vclock);
fiber_cond_create(&replicaset.applier.cond);
replicaset.replica_by_id = (struct replica **)calloc(VCLOCK_MAX, sizeof(struct replica *));
latch_create(&replicaset.applier.order_latch);
vclock_create(&replicaset.applier.vclock);
vclock_copy(&replicaset.applier.vclock, &replicaset.vclock);
rlist_create(&replicaset.applier.on_rollback);
rlist_create(&replicaset.applier.on_commit);
diag_create(&replicaset.applier.diag);
}
void
replication_free(void)
{
/*
* Relay threads keep sending messages to tx via
* cbus upon shutdown, which could lead to segfaults.
* So cancel them.
*/
replicaset_foreach(replica)
relay_cancel(replica->relay);
diag_destroy(&replicaset.applier.diag);
free(replicaset.replica_by_id);
}
void
replica_check_id(uint32_t replica_id)
{
if (replica_id == REPLICA_ID_NIL)
tnt_raise(ClientError, ER_REPLICA_ID_IS_RESERVED,
(unsigned) replica_id);
if (replica_id >= VCLOCK_MAX)
tnt_raise(LoggedError, ER_REPLICA_MAX,
(unsigned) replica_id);
/*
* It's okay to update the instance id while it is joining to
* a cluster as long as the id is set by the time bootstrap is
* complete, which is checked in box_cfg() anyway.
*
* For example, the replica could be deleted from the _cluster
* space on the master manually before rebootstrap, in which
* case it will replay this operation during the final join
* stage.
*/
if (!replicaset.is_joining && replica_id == instance_id)
tnt_raise(ClientError, ER_LOCAL_INSTANCE_ID_IS_READ_ONLY,
(unsigned) replica_id);
}
/* Return true if replica doesn't have id, relay and applier */
static bool
replica_is_orphan(struct replica *replica)
{
assert(replica->relay != NULL);
return replica->id == REPLICA_ID_NIL && replica->applier == NULL &&
relay_get_state(replica->relay) != RELAY_FOLLOW;
}
static void
replica_on_applier_state_f(struct trigger *trigger, void *event);
static struct replica *
replica_new(void)
{
struct replica *replica = (struct replica *)
malloc(sizeof(struct replica));
if (replica == NULL) {
tnt_raise(OutOfMemory, sizeof(*replica), "malloc",
"struct replica");
}
replica->relay = relay_new(replica);
if (replica->relay == NULL) {
free(replica);
diag_raise();
}
replica->id = 0;
replica->uuid = uuid_nil;
replica->applier = NULL;
replica->gc = NULL;
rlist_create(&replica->in_anon);
trigger_create(&replica->on_applier_state,
replica_on_applier_state_f, NULL, NULL);
replica->applier_sync_state = APPLIER_DISCONNECTED;
latch_create(&replica->order_latch);
return replica;
}
static void
replica_delete(struct replica *replica)
{
assert(replica_is_orphan(replica));
if (replica->relay != NULL)
relay_delete(replica->relay);
if (replica->gc != NULL)
gc_consumer_unregister(replica->gc);
TRASH(replica);
free(replica);
}
struct replica *
replicaset_add(uint32_t replica_id, const struct tt_uuid *replica_uuid)
{
assert(!tt_uuid_is_nil(replica_uuid));
assert(replica_id != REPLICA_ID_NIL && replica_id < VCLOCK_MAX);
assert(replica_by_uuid(replica_uuid) == NULL);
struct replica *replica = replica_new();
replica->uuid = *replica_uuid;
replica_hash_insert(&replicaset.hash, replica);
replica_set_id(replica, replica_id);
return replica;
}
void
replica_set_id(struct replica *replica, uint32_t replica_id)
{
assert(replica_id < VCLOCK_MAX);
assert(replica->id == REPLICA_ID_NIL); /* replica id is read-only */
replica->id = replica_id;
if (tt_uuid_is_equal(&INSTANCE_UUID, &replica->uuid)) {
/* Assign local replica id */
assert(instance_id == REPLICA_ID_NIL);
instance_id = replica_id;
}
replicaset.replica_by_id[replica_id] = replica;
say_info("assigned id %d to replica %s",
replica->id, tt_uuid_str(&replica->uuid));
}
void
replica_clear_id(struct replica *replica)
{
assert(replica->id != REPLICA_ID_NIL);
/*
* Don't remove replicas from vclock here.
* The vclock_sum() must always grow, it is a core invariant of
* the recovery subsystem. Further attempts to register a replica
* with the removed replica_id will re-use LSN from the last value.
* Replicas with LSN == 0 also can't not be safely removed.
* Some records may arrive later on due to asynchronous nature of
* replication.
*/
replicaset.replica_by_id[replica->id] = NULL;
if (replica->id == instance_id) {
/* See replica_check_id(). */
assert(replicaset.is_joining);
instance_id = REPLICA_ID_NIL;
}
replica->id = REPLICA_ID_NIL;
say_info("removed replica %s", tt_uuid_str(&replica->uuid));
/*
* The replica will never resubscribe so we don't need to keep
* WALs for it anymore. Unregister it with the garbage collector
* if the relay thread is stopped. In case the relay thread is
* still running, it may need to access replica->gc so leave the
* job to replica_on_relay_stop, which will be called as soon as
* the relay thread exits.
*/
if (replica->gc != NULL &&
relay_get_state(replica->relay) != RELAY_FOLLOW) {
gc_consumer_unregister(replica->gc);
replica->gc = NULL;
}
if (replica_is_orphan(replica)) {
replica_hash_remove(&replicaset.hash, replica);
replica_delete(replica);
}
}
static void
replica_set_applier(struct replica *replica, struct applier *applier)
{
assert(replica->applier == NULL);
replica->applier = applier;
trigger_add(&replica->applier->on_state,
&replica->on_applier_state);
}
static void
replica_clear_applier(struct replica *replica)
{
assert(replica->applier != NULL);
replica->applier = NULL;
trigger_clear(&replica->on_applier_state);
}
static void
replica_on_applier_sync(struct replica *replica)
{
assert(replica->applier_sync_state == APPLIER_CONNECTED);
replica->applier_sync_state = APPLIER_SYNC;
replicaset.applier.synced++;
replicaset_check_quorum();
}
static void
replica_on_applier_connect(struct replica *replica)
{
struct applier *applier = replica->applier;
assert(tt_uuid_is_nil(&replica->uuid));
assert(!tt_uuid_is_nil(&applier->uuid));
assert(replica->applier_sync_state == APPLIER_DISCONNECTED);
replica->uuid = applier->uuid;
replica->applier_sync_state = APPLIER_CONNECTED;
replicaset.applier.connected++;
struct replica *orig = replica_hash_search(&replicaset.hash, replica);
if (orig != NULL && orig->applier != NULL) {
say_error("duplicate connection to the same replica: "
"instance uuid %s, addr1 %s, addr2 %s",
tt_uuid_str(&orig->uuid), applier->source,
orig->applier->source);
fiber_cancel(fiber());
/*
* Raise an exception to force the applier
* to disconnect.
*/
fiber_testcancel();
}
rlist_del_entry(replica, in_anon);
if (orig != NULL) {
/* Use existing struct replica */
assert(orig->applier_sync_state == APPLIER_DISCONNECTED);
orig->applier_sync_state = replica->applier_sync_state;
replica_set_applier(orig, applier);
replica_clear_applier(replica);
replica_delete(replica);
replica = orig;
} else {
/* Add a new struct replica */
replica_hash_insert(&replicaset.hash, replica);
}
}
static void
replica_on_applier_reconnect(struct replica *replica)
{
struct applier *applier = replica->applier;
assert(!tt_uuid_is_nil(&replica->uuid));
assert(!tt_uuid_is_nil(&applier->uuid));
assert(replica->applier_sync_state == APPLIER_LOADING ||
replica->applier_sync_state == APPLIER_DISCONNECTED);
if (replica->applier_sync_state == APPLIER_LOADING) {
assert(replicaset.applier.loading > 0);
replicaset.applier.loading--;
}
if (!tt_uuid_is_equal(&replica->uuid, &applier->uuid)) {
/*
* Master's UUID changed, most likely because it was
* rebootstrapped. Try to look up a replica matching
* the new UUID and reassign the applier to it.
*/
struct replica *orig = replica_by_uuid(&applier->uuid);
if (orig == NULL) {
orig = replica_new();
orig->uuid = applier->uuid;
replica_hash_insert(&replicaset.hash, orig);
}
if (orig->applier != NULL) {
tnt_raise(ClientError, ER_CFG, "replication",
"duplicate connection to the same replica");
}
replica_set_applier(orig, applier);
replica_clear_applier(replica);
replica->applier_sync_state = APPLIER_DISCONNECTED;
replica = orig;
}
replica->applier_sync_state = APPLIER_CONNECTED;
replicaset.applier.connected++;
}
static void
replica_on_applier_disconnect(struct replica *replica)
{
switch (replica->applier_sync_state) {
case APPLIER_SYNC:
assert(replicaset.applier.synced > 0);
replicaset.applier.synced--;
FALLTHROUGH;
case APPLIER_CONNECTED:
assert(replicaset.applier.connected > 0);
replicaset.applier.connected--;
break;
case APPLIER_LOADING:
assert(replicaset.applier.loading > 0);
replicaset.applier.loading--;
break;
case APPLIER_DISCONNECTED:
break;
default:
unreachable();
}
replica->applier_sync_state = replica->applier->state;
if (replica->applier_sync_state == APPLIER_LOADING)
replicaset.applier.loading++;
}
static void
replica_on_applier_state_f(struct trigger *trigger, void *event)
{
(void)event;
struct replica *replica = container_of(trigger,
struct replica, on_applier_state);
switch (replica->applier->state) {
case APPLIER_INITIAL_JOIN:
replicaset.is_joining = true;
break;
case APPLIER_JOINED:
replicaset.is_joining = false;
break;
case APPLIER_CONNECTED:
if (tt_uuid_is_nil(&replica->uuid))
replica_on_applier_connect(replica);
else
replica_on_applier_reconnect(replica);
break;
case APPLIER_LOADING:
case APPLIER_DISCONNECTED:
replica_on_applier_disconnect(replica);
break;
case APPLIER_FOLLOW:
replica_on_applier_sync(replica);
break;
case APPLIER_OFF:
/*
* Connection to self, duplicate connection
* to the same master, or the applier fiber
* has been cancelled. Assume synced.
*/
replica_on_applier_sync(replica);
break;
case APPLIER_STOPPED:
/* Unrecoverable error. */
replica_on_applier_disconnect(replica);
break;
default:
break;
}
fiber_cond_signal(&replicaset.applier.cond);
}
/**
* Update the replica set with new "applier" objects
* upon reconfiguration of box.cfg.replication.
*/
static void
replicaset_update(struct applier **appliers, int count)
{
replica_hash_t uniq;
memset(&uniq, 0, sizeof(uniq));
replica_hash_new(&uniq);
RLIST_HEAD(anon_replicas);
struct replica *replica, *next;
struct applier *applier;
auto uniq_guard = make_scoped_guard([&]{
replica_hash_foreach_safe(&uniq, replica, next) {
replica_hash_remove(&uniq, replica);
replica_clear_applier(replica);
replica_delete(replica);
}
});
/* Check for duplicate UUID */
for (int i = 0; i < count; i++) {
applier = appliers[i];
replica = replica_new();
replica_set_applier(replica, applier);
if (applier->state != APPLIER_CONNECTED) {
/*
* The replica has not received its UUID from
* the master yet and thus cannot be added to
* the replica set. Instead, add it to the list
* of anonymous replicas and setup a trigger
* that will insert it into the replica set
* when it is finally connected.
*/
rlist_add_entry(&anon_replicas, replica, in_anon);
continue;
}
assert(!tt_uuid_is_nil(&applier->uuid));
replica->uuid = applier->uuid;
if (replica_hash_search(&uniq, replica) != NULL) {
replica_clear_applier(replica);
replica_delete(replica);
tnt_raise(ClientError, ER_CFG, "replication",
"duplicate connection to the same replica");
}
replica_hash_insert(&uniq, replica);
}
/*
* All invariants and conditions are checked, now it is safe to
* apply the new configuration. Nothing can fail after this point.
*/
/* Prune old appliers */
replicaset_foreach(replica) {
if (replica->applier == NULL)
continue;
applier = replica->applier;
replica_clear_applier(replica);
replica->applier_sync_state = APPLIER_DISCONNECTED;
applier_stop(applier);
applier_delete(applier);
}
rlist_foreach_entry_safe(replica, &replicaset.anon, in_anon, next) {
applier = replica->applier;
replica_clear_applier(replica);
replica_delete(replica);
applier_stop(applier);
applier_delete(applier);
}
rlist_create(&replicaset.anon);
/* Save new appliers */
replicaset.applier.total = count;
replicaset.applier.connected = 0;
replicaset.applier.loading = 0;
replicaset.applier.synced = 0;
replica_hash_foreach_safe(&uniq, replica, next) {
replica_hash_remove(&uniq, replica);
struct replica *orig = replica_hash_search(&replicaset.hash,
replica);
if (orig != NULL) {
/* Use existing struct replica */
replica_set_applier(orig, replica->applier);
replica_clear_applier(replica);
replica_delete(replica);
replica = orig;
} else {
/* Add a new struct replica */
replica_hash_insert(&replicaset.hash, replica);
}
replica->applier_sync_state = APPLIER_CONNECTED;
replicaset.applier.connected++;
}
rlist_swap(&replicaset.anon, &anon_replicas);
assert(replica_hash_first(&uniq) == NULL);
replica_hash_foreach_safe(&replicaset.hash, replica, next) {
if (replica_is_orphan(replica)) {
replica_hash_remove(&replicaset.hash, replica);
replica_delete(replica);
}
}
}
/**
* Replica set configuration state, shared among appliers.
*/
struct replicaset_connect_state {
/** Number of successfully connected appliers. */
int connected;
/** Number of appliers that failed to connect. */
int failed;
/** Signaled when an applier connects or stops. */
struct fiber_cond wakeup;
};
struct applier_on_connect {
struct trigger base;
struct replicaset_connect_state *state;
};
static void
applier_on_connect_f(struct trigger *trigger, void *event)
{
struct applier_on_connect *on_connect = container_of(trigger,
struct applier_on_connect, base);
struct replicaset_connect_state *state = on_connect->state;
struct applier *applier = (struct applier *)event;
switch (applier->state) {
case APPLIER_OFF:
case APPLIER_STOPPED:
state->failed++;
break;
case APPLIER_CONNECTED:
state->connected++;
break;
default:
return;
}
fiber_cond_signal(&state->wakeup);
applier_pause(applier);
}
void
replicaset_connect(struct applier **appliers, int count,
bool connect_quorum)
{
if (count == 0) {
/* Cleanup the replica set. */
replicaset_update(appliers, count);
return;
}
say_info("connecting to %d replicas", count);
/*
* Simultaneously connect to remote peers to receive their UUIDs
* and fill the resulting set:
*
* - create a single control channel;
* - register a trigger in each applier to wake up our
* fiber via this channel when the remote peer becomes
* connected and a UUID is received;
* - wait up to CONNECT_TIMEOUT seconds for `count` messages;
* - on timeout, raise a CFG error, cancel and destroy
* the freshly created appliers (done in a guard);
* - an success, unregister the trigger, check the UUID set
* for duplicates, fill the result set, return.
*/
/* Memory for on_state triggers registered in appliers */
struct applier_on_connect triggers[VCLOCK_MAX];
struct replicaset_connect_state state;
state.connected = state.failed = 0;
fiber_cond_create(&state.wakeup);
double timeout = replication_connect_timeout;
int quorum = MIN(count, replication_connect_quorum);
/* Add triggers and start simulations connection to remote peers */
for (int i = 0; i < count; i++) {
struct applier *applier = appliers[i];
struct applier_on_connect *trigger = &triggers[i];
/* Register a trigger to wake us up when peer is connected */
trigger_create(&trigger->base, applier_on_connect_f, NULL, NULL);
trigger->state = &state;
trigger_add(&applier->on_state, &trigger->base);
/* Start background connection */
applier_start(applier);
}
while (state.connected < count) {
double wait_start = ev_monotonic_now(loop());
if (fiber_cond_wait_timeout(&state.wakeup, timeout) != 0)
break;
if (count - state.failed < quorum)
break;
timeout -= ev_monotonic_now(loop()) - wait_start;
}
if (state.connected < count) {
say_crit("failed to connect to %d out of %d replicas",
count - state.connected, count);
/* Timeout or connection failure. */
if (connect_quorum && state.connected < quorum) {
diag_set(ClientError, ER_CFG, "replication",
"failed to connect to one or more replicas");
goto error;
}
} else {
say_info("connected to %d replicas", state.connected);
}
for (int i = 0; i < count; i++) {
/* Unregister the temporary trigger used to wake us up */
trigger_clear(&triggers[i].base);
/*
* Stop appliers that failed to connect.
* They will be restarted once we proceed
* to 'subscribe', see replicaset_follow().
*/
struct applier *applier = appliers[i];
if (applier->state != APPLIER_CONNECTED)
applier_stop(applier);
}
/* Now all the appliers are connected, update the replica set. */
try {
replicaset_update(appliers, count);
} catch (Exception *e) {
goto error;
}
return;
error:
/* Destroy appliers */
for (int i = 0; i < count; i++) {
trigger_clear(&triggers[i].base);
applier_stop(appliers[i]);
}
diag_raise();
}
bool
replicaset_needs_rejoin(struct replica **master)
{
struct replica *leader = NULL;
replicaset_foreach(replica) {
struct applier *applier = replica->applier;
if (applier == NULL)
continue;
const struct ballot *ballot = &applier->ballot;
if (vclock_compare(&ballot->gc_vclock,
&replicaset.vclock) <= 0) {
/*
* There's at least one master that still stores
* WALs needed by this instance. Proceed to local
* recovery.
*/
return false;
}
const char *uuid_str = tt_uuid_str(&replica->uuid);
const char *addr_str = sio_strfaddr(&applier->addr,
applier->addr_len);
const char *local_vclock_str = vclock_to_string(&replicaset.vclock);
const char *remote_vclock_str = vclock_to_string(&ballot->vclock);
const char *gc_vclock_str = vclock_to_string(&ballot->gc_vclock);
say_info("can't follow %s at %s: required %s available %s",
uuid_str, addr_str, local_vclock_str, gc_vclock_str);
if (vclock_compare(&replicaset.vclock, &ballot->vclock) > 0) {
/*
* Replica has some rows that are not present on
* the master. Don't rebootstrap as we don't want
* to lose any data.
*/
say_info("can't rebootstrap from %s at %s: "
"replica has local rows: local %s remote %s",
uuid_str, addr_str, local_vclock_str,
remote_vclock_str);
continue;
}
/* Prefer a master with the max vclock. */
if (leader == NULL ||
vclock_sum(&ballot->vclock) >
vclock_sum(&leader->applier->ballot.vclock))
leader = replica;
}
if (leader == NULL)
return false;
*master = leader;
return true;
}
void
replicaset_follow(void)
{
struct replica *replica;
replicaset_foreach(replica) {
/* Resume connected appliers. */
if (replica->applier != NULL)
applier_resume(replica->applier);
}
rlist_foreach_entry(replica, &replicaset.anon, in_anon) {
/* Restart appliers that failed to connect. */
applier_start(replica->applier);
}
}
void
replicaset_sync(void)
{
int quorum = replicaset_quorum();
if (quorum == 0) {
/*
* Quorum is 0 or replication is not configured.
* Leaving 'orphan' state immediately.
*/
box_set_orphan(false);
return;
}
say_info("synchronizing with %d replicas", quorum);
/*
* Wait until all connected replicas synchronize up to
* replication_sync_lag or return on replication_sync_timeout
*/
double deadline = ev_monotonic_now(loop()) + replication_sync_timeout;
while (replicaset.applier.synced < quorum &&
replicaset.applier.connected +
replicaset.applier.loading >= quorum) {
if (fiber_cond_wait_deadline(&replicaset.applier.cond,
deadline) != 0)
break;
}
if (replicaset.applier.synced < quorum) {
/*
* Not enough replicas connected to form a quorum.
* Do not stall configuration, leave the instance
* in 'orphan' state.
*/
say_crit("failed to synchronize with %d out of %d replicas",
replicaset.applier.total - replicaset.applier.synced,
replicaset.applier.total);
box_set_orphan(true);
} else {
say_info("replica set sync complete");
box_set_orphan(false);
}
}
void
replicaset_check_quorum(void)
{
if (replicaset.applier.synced >= replicaset_quorum())
box_set_orphan(false);
}
void
replica_on_relay_stop(struct replica *replica)
{
/*
* If the replica was evicted from the cluster, we don't
* need to keep WALs for it anymore. Unregister it with
* the garbage collector then. See also replica_clear_id.
*/
assert(replica->gc != NULL);
if (replica->id == REPLICA_ID_NIL) {
gc_consumer_unregister(replica->gc);
replica->gc = NULL;
}
if (replica_is_orphan(replica)) {
replica_hash_remove(&replicaset.hash, replica);
replica_delete(replica);
}
}
struct replica *
replicaset_first(void)
{
return replica_hash_first(&replicaset.hash);
}
struct replica *
replicaset_next(struct replica *replica)
{
return replica_hash_next(&replicaset.hash, replica);
}
/**
* Compare vclock and read only mode of all connected
* replicas and elect a leader.
* Initiallly, skip read-only replicas, since they
* can not properly act as bootstrap masters (register
* new nodes in _cluster table). If there are no read-write
* replicas, choose a read-only replica with biggest vclock
* as a leader, in hope it will become read-write soon.
*/
static struct replica *
replicaset_round(bool skip_ro)
{
struct replica *leader = NULL;
replicaset_foreach(replica) {
struct applier *applier = replica->applier;
if (applier == NULL)
continue;
/**
* While bootstrapping a new cluster, read-only
* replicas shouldn't be considered as a leader.
* The only exception if there is no read-write
* replicas since there is still a possibility
* that all replicas exist in cluster table.
*/
if (skip_ro && applier->ballot.is_ro)
continue;
if (leader == NULL) {
leader = replica;
continue;
}
/*
* Choose the replica with the most advanced
* vclock. If there are two or more replicas
* with the same vclock, prefer the one with
* the lowest uuid.
*/
int cmp = vclock_compare(&applier->ballot.vclock,
&leader->applier->ballot.vclock);
if (cmp < 0)
continue;
if (cmp == 0 && tt_uuid_compare(&replica->uuid,
&leader->uuid) > 0)
continue;
leader = replica;
}
return leader;
}
struct replica *
replicaset_leader(void)
{
bool skip_ro = true;
/**
* Two loops, first prefers read-write replicas among others.
* Second for backward compatibility, if there is no such
* replicas at all.
*/
struct replica *leader = replicaset_round(skip_ro);
if (leader == NULL) {
skip_ro = false;
leader = replicaset_round(skip_ro);
}
return leader;
}
struct replica *
replica_by_uuid(const struct tt_uuid *uuid)
{
struct replica key;
key.uuid = *uuid;
return replica_hash_search(&replicaset.hash, &key);
}
struct replica *
replica_by_id(uint32_t replica_id)
{
return replicaset.replica_by_id[replica_id];
}