forked from bloomberg/comdb2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rep.c
6078 lines (5066 loc) · 203 KB
/
rep.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 2015, 2022, Bloomberg Finance L.P.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/* berkeley db specific rep stuff in this file, keep net.c free
of berkeley db dependencies */
#include <sys/types.h>
#include <sys/stat.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <poll.h>
#include "debug_switches.h"
#include "bdb_int.h"
#include "locks.h"
#include "locks_wrap.h"
#include <endian_core.h>
#include "memory_sync.h"
#include "ctrace.h"
#include "nodemap.h"
#include "util.h"
#include "crc32c.h"
#include "gettimeofday_ms.h"
#include <build/db_int.h>
#include "dbinc/log.h"
#include "dbinc/mp.h"
#include <trigger.h>
#include "printformats.h"
#include "phys_rep.h"
#include "phys_rep_lsn.h"
#include <compat.h>
#include "str0.h"
#include <thrman.h>
#ifdef _LINUX_SOURCE
#include <sys/syscall.h>
#endif
#define REP_PRI 100 /* we are all equal in the eyes of god */
#define REPTIME 3000000 /* default 3 second timeout on election */
#define MILLISEC 1000
/* XXX this is dangerous i think. */
/*#define DECOM_LOGIC*/
int gbl_watcher_thread_ran = 0;
int gbl_lost_master_time = 0;
int gbl_ignore_lost_master_time = 0;
int gbl_prefault_latency = 0;
int gbl_long_log_truncation_warn_thresh_sec = INT_MAX;
int gbl_long_log_truncation_abort_thresh_sec = INT_MAX;
int gbl_dump_sql_on_repwait_sec = 10;
int gbl_debug_drop_nth_rep_message = 0;
extern struct thdpool *gbl_udppfault_thdpool;
extern int gbl_commit_delay_trace;
/* osqlcomm.c code, hurray! */
extern void osql_decom_node(char *decom_host);
extern void thd_dump(void);
void *mymalloc(size_t size);
void *myrealloc(void *ptr, size_t size);
void reset_aa_counter(char *tblname);
void create_coherency_lease_thread(bdb_state_type *bdb_state);
void create_master_lease_thread(bdb_state_type *bdb_state);
int gbl_net_lmt_upd_incoherent_nodes = 70;
char *lsn_to_str(char lsn_str[], DB_LSN *lsn);
void comdb2_dump_blockers(DB_ENV *);
static int bdb_wait_for_seqnum_from_node_nowait_int(bdb_state_type *bdb_state,
seqnum_type *seqnum,
char *host);
static void bdb_zap_lsn_waitlist(bdb_state_type *bdb_state, const char *host);
static int last_slow_node_check_time = 0;
static pthread_mutex_t slow_node_check_lk = PTHREAD_MUTEX_INITIALIZER;
struct rep_type_berkdb_rep_buf_hdr {
int recbufsz;
int recbufcrc;
};
enum { REP_TYPE_BERKDB_REP_BUF_HDR_LEN = 4 + 4 };
BB_COMPILE_TIME_ASSERT(rep_type_berkdb_rep_buf_hdr,
sizeof(struct rep_type_berkdb_rep_buf_hdr) ==
REP_TYPE_BERKDB_REP_BUF_HDR_LEN);
static uint8_t *rep_type_berkdb_rep_buf_hdr_put(
const struct rep_type_berkdb_rep_buf_hdr *p_rep_type_berkdb_rep_buf_hdr,
uint8_t *p_buf, const uint8_t *p_buf_end)
{
if (p_buf_end < p_buf ||
REP_TYPE_BERKDB_REP_BUF_HDR_LEN > (p_buf_end - p_buf))
return NULL;
p_buf = buf_put(&(p_rep_type_berkdb_rep_buf_hdr->recbufsz),
sizeof(p_rep_type_berkdb_rep_buf_hdr->recbufsz), p_buf,
p_buf_end);
p_buf = buf_put(&(p_rep_type_berkdb_rep_buf_hdr->recbufcrc),
sizeof(p_rep_type_berkdb_rep_buf_hdr->recbufcrc), p_buf,
p_buf_end);
return p_buf;
}
static const uint8_t *rep_type_berkdb_rep_buf_hdr_get(
struct rep_type_berkdb_rep_buf_hdr *p_rep_type_berkdb_rep_buf_hdr,
const uint8_t *p_buf, const uint8_t *p_buf_end)
{
if (p_buf_end < p_buf ||
REP_TYPE_BERKDB_REP_BUF_HDR_LEN > (p_buf_end - p_buf))
return NULL;
p_buf = buf_get(&(p_rep_type_berkdb_rep_buf_hdr->recbufsz),
sizeof(p_rep_type_berkdb_rep_buf_hdr->recbufsz), p_buf,
p_buf_end);
p_buf = buf_get(&(p_rep_type_berkdb_rep_buf_hdr->recbufcrc),
sizeof(p_rep_type_berkdb_rep_buf_hdr->recbufcrc), p_buf,
p_buf_end);
return p_buf;
}
struct rep_type_berkdb_rep_ctrlbuf_hdr {
int controlbufsz;
int controlbufcrc;
};
enum { REP_TYPE_BERKDB_REP_CTRLBUF_HDR_LEN = 4 + 4 };
BB_COMPILE_TIME_ASSERT(rep_type_berkdb_rep_ctrlbuf_hdr,
sizeof(struct rep_type_berkdb_rep_ctrlbuf_hdr) ==
REP_TYPE_BERKDB_REP_CTRLBUF_HDR_LEN);
static uint8_t *rep_type_berkdb_rep_ctrlbuf_hdr_put(
const struct rep_type_berkdb_rep_ctrlbuf_hdr *
p_rep_type_berkdb_rep_ctrlbuf_hdr,
uint8_t *p_buf, const uint8_t *p_buf_end)
{
if (p_buf_end < p_buf ||
REP_TYPE_BERKDB_REP_CTRLBUF_HDR_LEN > (p_buf_end - p_buf))
return NULL;
p_buf = buf_put(&(p_rep_type_berkdb_rep_ctrlbuf_hdr->controlbufsz),
sizeof(p_rep_type_berkdb_rep_ctrlbuf_hdr->controlbufsz),
p_buf, p_buf_end);
p_buf = buf_put(&(p_rep_type_berkdb_rep_ctrlbuf_hdr->controlbufcrc),
sizeof(p_rep_type_berkdb_rep_ctrlbuf_hdr->controlbufcrc),
p_buf, p_buf_end);
return p_buf;
}
static const uint8_t *rep_type_berkdb_rep_ctrlbuf_hdr_get(
struct rep_type_berkdb_rep_ctrlbuf_hdr *p_rep_type_berkdb_rep_ctrlbuf_hdr,
const uint8_t *p_buf, const uint8_t *p_buf_end)
{
if (p_buf_end < p_buf ||
REP_TYPE_BERKDB_REP_CTRLBUF_HDR_LEN > (p_buf_end - p_buf))
return NULL;
p_buf = buf_get(&(p_rep_type_berkdb_rep_ctrlbuf_hdr->controlbufsz),
sizeof(p_rep_type_berkdb_rep_ctrlbuf_hdr->controlbufsz),
p_buf, p_buf_end);
p_buf = buf_get(&(p_rep_type_berkdb_rep_ctrlbuf_hdr->controlbufcrc),
sizeof(p_rep_type_berkdb_rep_ctrlbuf_hdr->controlbufcrc),
p_buf, p_buf_end);
return p_buf;
}
struct rep_type_berkdb_rep_seqnum {
int seqnum;
};
enum { REP_TYPE_BERKDB_REP_SEQNUM = 4 };
BB_COMPILE_TIME_ASSERT(rep_type_berkdb_rep_seqnum,
sizeof(struct rep_type_berkdb_rep_seqnum) ==
REP_TYPE_BERKDB_REP_SEQNUM);
static uint8_t *rep_type_berkdb_rep_seqnum_put(
const struct rep_type_berkdb_rep_seqnum *p_rep_type_berkdb_rep_seqnum,
uint8_t *p_buf, const uint8_t *p_buf_end)
{
if (p_buf_end < p_buf || REP_TYPE_BERKDB_REP_SEQNUM > (p_buf_end - p_buf))
return NULL;
p_buf = buf_put(&(p_rep_type_berkdb_rep_seqnum->seqnum), sizeof(int), p_buf,
p_buf_end);
return p_buf;
}
static const uint8_t *rep_type_berkdb_rep_seqnum_get(
struct rep_type_berkdb_rep_seqnum *p_rep_type_berkdb_rep_seqnum,
const uint8_t *p_buf, const uint8_t *p_buf_end)
{
if (p_buf_end < p_buf || REP_TYPE_BERKDB_REP_SEQNUM > (p_buf_end - p_buf))
return NULL;
p_buf = buf_get(&(p_rep_type_berkdb_rep_seqnum->seqnum), sizeof(int), p_buf,
p_buf_end);
return p_buf;
}
uint8_t *rep_berkdb_seqnum_type_put(const seqnum_type *p_seqnum_type,
uint8_t *p_buf, const uint8_t *p_buf_end)
{
if (p_buf_end < p_buf || BDB_SEQNUM_TYPE_LEN > (p_buf_end - p_buf))
return NULL;
p_buf = buf_put(&(p_seqnum_type->lsn.file), sizeof(p_seqnum_type->lsn.file),
p_buf, p_buf_end);
p_buf = buf_put(&(p_seqnum_type->lsn.offset),
sizeof(p_seqnum_type->lsn.offset), p_buf, p_buf_end);
p_buf = buf_put(&(p_seqnum_type->issue_time),
sizeof(p_seqnum_type->issue_time), p_buf, p_buf_end);
p_buf = buf_put(&(p_seqnum_type->lease_ms), sizeof(p_seqnum_type->lease_ms),
p_buf, p_buf_end);
p_buf = buf_put(&(p_seqnum_type->commit_generation),
sizeof(p_seqnum_type->commit_generation), p_buf, p_buf_end);
p_buf = buf_put(&(p_seqnum_type->generation),
sizeof(p_seqnum_type->generation), p_buf, p_buf_end);
return p_buf;
}
static const uint8_t *rep_berkdb_seqnum_type_get(seqnum_type *p_seqnum_type,
const uint8_t *p_buf,
const uint8_t *p_buf_end)
{
if (p_buf_end < p_buf || BDB_SEQNUM_TYPE_LEN > (p_buf_end - p_buf))
return NULL;
p_buf = buf_get(&(p_seqnum_type->lsn.file), sizeof(p_seqnum_type->lsn.file),
p_buf, p_buf_end);
p_buf = buf_get(&(p_seqnum_type->lsn.offset),
sizeof(p_seqnum_type->lsn.offset), p_buf, p_buf_end);
p_buf = buf_get(&(p_seqnum_type->issue_time),
sizeof(p_seqnum_type->issue_time), p_buf, p_buf_end);
p_buf = buf_get(&(p_seqnum_type->lease_ms), sizeof(p_seqnum_type->lease_ms),
p_buf, p_buf_end);
p_buf = buf_get(&(p_seqnum_type->commit_generation),
sizeof(p_seqnum_type->commit_generation), p_buf, p_buf_end);
p_buf = buf_get(&(p_seqnum_type->generation),
sizeof(p_seqnum_type->generation), p_buf, p_buf_end);
return p_buf;
}
uint8_t *rep_udp_filepage_type_put(const filepage_type *p_filepage_type,
uint8_t *p_buf, const uint8_t *p_buf_end)
{
if (p_buf_end < p_buf || BDB_FILEPAGE_TYPE_LEN > (p_buf_end - p_buf))
return NULL;
p_buf = buf_put(&(p_filepage_type->fileid), sizeof(p_filepage_type->fileid),
p_buf, p_buf_end);
p_buf = buf_put(&(p_filepage_type->pgno), sizeof(p_filepage_type->pgno),
p_buf, p_buf_end);
return p_buf;
}
const uint8_t *rep_udp_filepage_type_get(filepage_type *p_filepage_type,
const uint8_t *p_buf,
const uint8_t *p_buf_end)
{
if (p_buf_end < p_buf || BDB_FILEPAGE_TYPE_LEN > (p_buf_end - p_buf))
return NULL;
p_buf = buf_get(&(p_filepage_type->fileid), sizeof(p_filepage_type->fileid),
p_buf, p_buf_end);
p_buf = buf_get(&(p_filepage_type->pgno), sizeof(p_filepage_type->pgno),
p_buf, p_buf_end);
return p_buf;
}
enum { DB_LSN_TYPE_SIZE = 4 + 4 };
BB_COMPILE_TIME_ASSERT(db_lsn_type, sizeof(DB_LSN) == DB_LSN_TYPE_SIZE);
const uint8_t *db_lsn_type_get(DB_LSN *p_db_lsn, const uint8_t *p_buf,
const uint8_t *p_buf_end)
{
if (p_buf_end < p_buf || DB_LSN_TYPE_SIZE > (p_buf_end - p_buf))
return NULL;
p_buf =
buf_get(&(p_db_lsn->file), sizeof(p_db_lsn->file), p_buf, p_buf_end);
p_buf = buf_get(&(p_db_lsn->offset), sizeof(p_db_lsn->offset), p_buf,
p_buf_end);
return p_buf;
}
const uint8_t *db_lsn_type_put(const DB_LSN *p_db_lsn, uint8_t *p_buf,
const uint8_t *p_buf_end)
{
if (p_buf_end < p_buf || DB_LSN_TYPE_SIZE > (p_buf_end - p_buf))
return NULL;
p_buf =
buf_put(&(p_db_lsn->file), sizeof(p_db_lsn->file), p_buf, p_buf_end);
p_buf = buf_put(&(p_db_lsn->offset), sizeof(p_db_lsn->offset), p_buf,
p_buf_end);
return p_buf;
}
uint8_t *pgcomp_snd_type_put(const pgcomp_snd_t *p_snd, uint8_t *p_buf,
const uint8_t *p_buf_end, const void *data)
{
if (p_buf_end < p_buf || BDB_PGCOMP_SND_TYPE_LEN > (p_buf_end - p_buf))
return NULL;
p_buf = buf_put(&(p_snd->id), sizeof(p_snd->id), p_buf, p_buf_end);
p_buf = buf_put(&(p_snd->size), sizeof(p_snd->size), p_buf, p_buf_end);
p_buf = buf_no_net_put(data, p_snd->size, p_buf, p_buf_end);
return p_buf;
}
const uint8_t *pgcomp_snd_type_get(pgcomp_snd_t *p_snd, const uint8_t *p_buf,
const uint8_t *p_buf_end)
{
if (p_buf_end < p_buf || BDB_PGCOMP_SND_TYPE_LEN > (p_buf_end - p_buf))
return NULL;
p_buf = buf_get(&(p_snd->id), sizeof(p_snd->id), p_buf, p_buf_end);
p_buf = buf_get(&(p_snd->size), sizeof(p_snd->size), p_buf, p_buf_end);
return p_buf;
}
/* XXX ripped from berkdb innards, only used for informational puroposes */
typedef struct {
u_int32_t rep_version; /* Replication version number. */
u_int32_t log_version; /* Log version number. */
DB_LSN lsn; /* Log sequence number. */
u_int32_t rectype; /* Message type. */
u_int32_t gen; /* Generation number. */
u_int32_t flags; /* log_put flag value. */
} rep_control_type;
#include <dbinc/rep_types.h>
int is_electable(bdb_state_type *bdb_state, int *out_num_up,
int *out_num_connected);
extern int gbl_rep_node_pri;
extern int gbl_handoff_node;
extern int gbl_use_node_pri;
void bdb_losemaster(bdb_state_type *bdb_state)
{
if (bdb_state->parent)
bdb_state = bdb_state->parent;
if (bdb_state->repinfo->master_host == bdb_state->repinfo->myhost) {
logmsg(LOGMSG_INFO, "trapped to lost mastership\n");
bdb_state->need_to_downgrade_and_lose = 1;
}
}
int bdb_is_an_unconnected_master(bdb_state_type *bdb_state)
{
if (bdb_state->repinfo->master_host != bdb_state->repinfo->myhost) {
/*not a master, so not standalone */
return 0;
}
const char *hostlist[REPMAX];
return (net_get_all_nodes_connected(bdb_state->repinfo->netinfo, hostlist) == 0);
}
void bdb_transfermaster(bdb_state_type *bdb_state)
{
int rc = 0;
const char *hostlist[REPMAX];
if (bdb_state->parent)
bdb_state = bdb_state->parent;
if (bdb_state->repinfo->master_host != bdb_state->repinfo->myhost) {
/*not a master */
return;
}
if(net_get_all_nodes_connected(bdb_state->repinfo->netinfo, hostlist) == 0) {
logmsg(LOGMSG_INFO, "This was standalone\n");
return;
}
rc = bdb_downgrade(bdb_state, 0, NULL);
if (rc) {
logmsg(LOGMSG_ERROR, "%s:%d bdb_downgrade failed rc=%d ?\n", __FILE__,
__LINE__, rc);
}
}
int gbl_set_coherent_state_trace = 1;
char *coherent_state_to_str(int state)
{
switch (state) {
case STATE_INCOHERENT:
return "INCOHERENT";
case STATE_INCOHERENT_WAIT:
return "INCOHERENT_WAIT";
case STATE_INCOHERENT_SLOW:
return "INCOHERENT_SLOW";
case STATE_COHERENT:
return "COHERENT";
default:
return "?UNKNOWN_STATE?";
}
}
/* You should have the lock */
static inline void set_coherent_state(bdb_state_type *bdb_state,
const char *hostname, int state,
const char *func, int line)
{
if (bdb_state->coherent_state[nodeix(hostname)] != state) {
bdb_state->coherent_state[nodeix(hostname)] = state;
if (gbl_set_coherent_state_trace) {
logmsg(LOGMSG_USER, "%s line %d setting %s coherent state to %s\n",
func, line, hostname, coherent_state_to_str(state));
}
}
}
void bdb_transfermaster_tonode(bdb_state_type *bdb_state, char *tohost)
{
const char *hostlist[REPMAX];
int count;
int i;
int rc;
int numsleeps;
int electable;
int num_up;
int num_connected;
char *myhost = bdb_state->repinfo->myhost;
/* if we were passed a child, find his parent */
if (bdb_state->parent)
bdb_state = bdb_state->parent;
/* get out of here if we arent the master */
if (bdb_state->repinfo->master_host != myhost) {
return;
}
/* get out of here if the cluster isnt electable */
electable = is_electable(bdb_state, &num_up, &num_connected);
if (!electable) {
logmsg(LOGMSG_ERROR, "%s: cluster unelectable\n", __func__);
return;
}
count = net_get_all_nodes_connected(bdb_state->repinfo->netinfo, hostlist);
/* the goal is to give it up quickly. stop looking for candidates as
soon as you find anyone */
for (i = 0; i < count; i++) {
if (hostlist[i] == tohost)
break;
}
if (hostlist[i] != tohost) {
logmsg(LOGMSG_ERROR, "%s: node %s not in the connection list\n", __func__,
tohost);
return;
}
/* give me a break, mr watcher thread */
bdb_state->repinfo->disable_watcher = comdb2_time_epoch() + 10;
if (bdb_state->callback->scabort_rtn)
/* if there is a schema change going on, kill it */
bdb_state->callback->scabort_rtn();
/* we are about to lose mastership, but there is no big lock to protect
writes anymore;
downgrade w/out election so we stop temporarely writes from coming in
while I try to pass mastership to the provided node */
bdb_state->repinfo->dont_elect_untill_time = comdb2_time_epoch() + 5;
bdb_downgrade_noelect(bdb_state);
numsleeps = 0;
DB_LSN tohost_lsn = bdb_state->seqnum_info->seqnums[nodeix(tohost)].lsn;
DB_LSN myhost_lsn = bdb_state->seqnum_info->seqnums[nodeix(myhost)].lsn;
again:
if (((tohost_lsn.file > myhost_lsn.file) ||
(tohost_lsn.file == myhost_lsn.file &&
tohost_lsn.offset >= myhost_lsn.offset)) &&
(bdb_state->callback->nodeup_rtn(bdb_state, tohost))) {
logmsg(LOGMSG_INFO,
"%s: to-be-upgraded node %s is up-to-date %d:%d, us %d:%d\n",
__func__, tohost, tohost_lsn.file, tohost_lsn.offset,
myhost_lsn.file, myhost_lsn.offset);
} else {
numsleeps++;
if (numsleeps > 2) {
logmsg(LOGMSG_ERROR, "transfer master falling back to election\n");
bdb_downgrade(bdb_state, 0, NULL);
return;
}
logmsg(LOGMSG_WARN, "node %s is still behind, waiting 1 second\n", tohost);
sleep(1);
goto again;
}
logmsg(LOGMSG_INFO, "%s: transferring master to %s\n", __func__, tohost);
int hostlen;
hostlen = strlen(tohost) + 1;
/* wait 1 second for ack */
rc = net_send_message(bdb_state->repinfo->netinfo, tohost,
USER_TYPE_TRANSFERMASTER_NAME, tohost, hostlen, 1,
1 * 1000);
/* didnt ack the message? kick off an election */
if (rc != 0) {
bdb_state->repinfo->dont_elect_untill_time = comdb2_time_epoch();
call_for_election(bdb_state, __func__, __LINE__);
}
}
int bdb_sync_cluster(bdb_state_type *bdb_state, int sync_all)
{
int rc;
seqnum_type seqnum;
/* The DB is in async mode, force a checkpoint at this point. */
bdb_flush(bdb_state, &rc);
if (rc) {
logmsg(LOGMSG_ERROR, "failed to Flush DB %d\n", rc);
return rc;
}
/* fill in the lsn part in seqnum */
get_master_lsn(bdb_state, &(seqnum.lsn));
if (sync_all) {
rc = bdb_wait_for_seqnum_from_all(bdb_state, (seqnum_type *)&seqnum);
} else {
rc = bdb_wait_for_seqnum_from_room(bdb_state, (seqnum_type *)&seqnum);
}
return rc;
}
static void send_context_to_all(bdb_state_type *bdb_state)
{
if (!bdb_state->attr->net_send_gblcontext)
return;
/* only the master can send these */
if (bdb_state->repinfo->master_host != bdb_state->repinfo->myhost)
return;
unsigned long long gblcontext = get_gblcontext(bdb_state);
void *data[] = {&gblcontext};
int sz[] = {sizeof(gblcontext)};
int type[] = {USER_TYPE_GBLCONTEXT};
int flag[] = {0};
net_send_all(bdb_state->repinfo->netinfo, 1, data, sz, type, flag);
}
static inline int is_incoherent_complete(bdb_state_type *bdb_state,
const char *host, int *incohwait)
{
int is_incoherent, state;
is_incoherent = 0;
if (incohwait)
*incohwait = 0;
Pthread_mutex_lock(&(bdb_state->coherent_state_lock));
state = bdb_state->coherent_state[nodeix(host)];
Pthread_mutex_unlock(&(bdb_state->coherent_state_lock));
/* STATE_COHERENT and STATE_INCOHERENT_LOCAL return COHERENT. */
if (state == STATE_INCOHERENT || state == STATE_INCOHERENT_SLOW)
is_incoherent = 1;
if (incohwait && state == STATE_INCOHERENT_WAIT)
*incohwait = 1;
return is_incoherent;
}
int is_incoherent(bdb_state_type *bdb_state, const char *host)
{
return is_incoherent_complete(bdb_state, host, NULL);
}
int gbl_throttle_logput_trace = 0;
int gbl_incoherent_logput_window = 0;
static int throttle_updates_incoherent_nodes(netinfo_type *netinfo_ptr, const char *host)
{
bdb_state_type *bdb_state = net_get_usrptr(netinfo_ptr);
int ret = 0, now, pr = 0;
static int lastpr = 0;
static unsigned long long throttles = 0;
unsigned long long cntbytes;
uint32_t window = gbl_incoherent_logput_window;
/* Disabled */
if (window <= 0) {
return 0;
}
if (gbl_throttle_logput_trace && ((now = time(NULL)) - lastpr)) {
pr = 1;
lastpr = now;
}
/* INCOHERENT & INCOHERENT_SLOW */
if (is_incoherent(bdb_state, host)) {
DB_LSN *lsnp, *masterlsn;
lsnp = &bdb_state->seqnum_info->seqnums[nodeix(host)].lsn;
masterlsn = &bdb_state->seqnum_info->seqnums[nodeix(bdb_state->repinfo->master_host)].lsn;
cntbytes = subtract_lsn(bdb_state, masterlsn, lsnp);
if (cntbytes > window) {
ret = 1;
throttles++;
if (pr) {
logmsg(LOGMSG_USER,
"%s throttling logput to %s, incoherent"
" %llu bytes behind, total throttles=%llu\n",
__func__, host, cntbytes, throttles);
}
} else {
if (pr) {
logmsg(LOGMSG_USER,
"%s NOT throttling logput to %s, "
"incoherent and %llu bytes behind, total, "
"throttles=%llu\n",
__func__, host, cntbytes, throttles);
}
}
} else if (pr) {
logmsg(LOGMSG_USER, "%s allowing logput to %s\n", __func__, host);
}
return ret;
}
void net_rep_throttle_init(netinfo_type *netinfo_ptr)
{
net_register_throttle(netinfo_ptr, throttle_updates_incoherent_nodes);
}
extern int gbl_rowlocks;
static unsigned long long callcount = 0;
static unsigned long long bytecount = 0;
unsigned long long rep_get_send_callcount(void) { return callcount; }
unsigned long long rep_get_send_bytecount(void) { return bytecount; }
void rep_reset_send_callcount(void) { callcount = 0; }
void rep_reset_send_bytecount(void) { bytecount = 0; }
extern int gbl_decoupled_logputs;
int berkdb_send_rtn(DB_ENV *dbenv, const DBT *control, const DBT *rec,
const DB_LSN *lsnp, char *host, uint32_t flags,
void *usr_ptr)
{
bdb_state_type *bdb_state;
int bufsz;
int rc;
int outrc;
struct rep_type_berkdb_rep_ctrlbuf_hdr p_rep_type_berkdb_rep_ctrlbuf_hdr = {0};
struct rep_type_berkdb_rep_buf_hdr p_rep_type_berkdb_rep_buf_hdr = {0};
struct rep_type_berkdb_rep_seqnum p_rep_type_berkdb_rep_seqnum = {0};
uint8_t *p_buf, *p_buf_end;
int rectype = 0;
int *seqnum;
int nodelay;
int useheap = 0;
int is_logput = 0;
tran_type *tran = NULL;
/* get a pointer back to our bdb_state */
bdb_state = (bdb_state_type *)dbenv->app_private;
if (bdb_state->parent)
bdb_state = bdb_state->parent;
callcount++;
/*
int now;
static int lastpr = 0;
if((now=comdb2_time_epoch()) - lastpr)
{
fprintf(stderr, "Called %s %llu times
flags=0x%x\n",__func__,callcount,flags);
lastpr=now;
}
*/
rep_control_type *rep_control;
rep_control = control->data;
/* We want to be able to throttle log propagation to avoid filling
the net queue; this will allow signal messages and catching up
log replies to be transferred even though the database is under heavy
load.
Problem is, in berkdb_send_rtn both regular log messages and catching
up log replies are coming as REP_LOG
In __log_push we replace REP_LOG with REP_LOG_LOGPUT so we know
that this must be throttled; we revert to REP_LOG in the same routine
before sending the message
*/
p_buf = (uint8_t *)&rep_control->rectype;
p_buf_end =
((uint8_t *)&rep_control->rectype + sizeof(rep_control->rectype));
buf_get(&rectype, sizeof(rectype), p_buf, p_buf_end);
if (rectype == REP_LOG_LOGPUT) {
is_logput = 1;
rectype = REP_LOG;
p_buf = (uint8_t *)&rep_control->rectype;
p_buf_end =
((uint8_t *)&rep_control->rectype + sizeof(rep_control->rectype));
buf_put(&rectype, sizeof(rectype), p_buf, p_buf_end);
}
/* good return code until proven otherwise */
outrc = 0;
/*
if (__bdb_no_send)
return 0;
*/
bufsz = sizeof(int) + /* seqnum */
sizeof(int) + /* recsz */
sizeof(int) + /* reccrc */
rec->size + /* recbuf */
sizeof(int) + /* controlsz */
sizeof(int) + /* controlcrc */
control->size; /* controlbuf */
if (bufsz > 1024 * 65)
useheap = 1;
char *buf = useheap ? malloc(bufsz) : alloca(bufsz);
bytecount += bufsz;
p_buf = (uint8_t *)buf;
p_buf_end = ((uint8_t *)buf + bufsz);
/* not included in the buf headers as it's set multiple times */
seqnum = (int *)p_buf;
*seqnum = 0;
p_buf += sizeof(int);
if (bdb_state->rep_trace) {
char str[80];
DB_LSN tmp;
tmp.file = ntohl(rep_control->lsn.file);
tmp.offset = ntohl(rep_control->lsn.offset);
if (host == db_eid_broadcast)
logmsg(LOGMSG_USER, "--- berkdb told us to send to all "
"(rectype %d lsn %s)\n",
rectype, lsn_to_str(str, &tmp));
else
logmsg(LOGMSG_USER, "--- berkdb told us to send to %s (%s) "
"(rectype %d lsn %s)\n",
host, host, rectype, lsn_to_str(str, &tmp));
}
if (bdb_state->attr->repchecksum) {
/*fprintf(stderr, "1) repchecksum\n");*/
p_rep_type_berkdb_rep_buf_hdr.recbufcrc = crc32c(rec->data, rec->size);
p_rep_type_berkdb_rep_ctrlbuf_hdr.controlbufcrc =
crc32c(control->data, control->size);
}
p_rep_type_berkdb_rep_buf_hdr.recbufsz = rec->size;
p_rep_type_berkdb_rep_ctrlbuf_hdr.controlbufsz = control->size;
/* pack buffer header */
p_buf = rep_type_berkdb_rep_buf_hdr_put(&(p_rep_type_berkdb_rep_buf_hdr),
p_buf, p_buf_end);
/* pack buffer */
p_buf = buf_no_net_put(rec->data, rec->size, p_buf, p_buf_end);
/* pack control buffer header */
p_buf = rep_type_berkdb_rep_ctrlbuf_hdr_put(
&(p_rep_type_berkdb_rep_ctrlbuf_hdr), p_buf, p_buf_end);
/* pack control buffer payload */
p_buf = buf_no_net_put(control->data, control->size, p_buf, p_buf_end);
nodelay = 0;
tran = pthread_getspecific(bdb_state->seqnum_info->key);
if (tran && tran->is_rowlocks_trans) {
/* if about to commit, remember the lsn to wait for as the first
lsn of the logical transaction. if not about to commit the
logical transaction, don't wait for replication. */
if (tran->is_about_to_commit) {
nodelay = 1;
}
} else {
if ((flags & DB_REP_PERMANENT) || (flags & DB_REP_NOBUFFER))
nodelay = 1;
}
if (flags & DB_REP_FLUSH)
nodelay = 1;
if ((flags & DB_REP_PERMANENT) || (flags & DB_REP_LOGPROGRESS)) {
if (lsnp) {
if (tran != NULL) {
if (!tran->is_rowlocks_trans ||
(tran->is_rowlocks_trans && tran->is_about_to_commit)) {
/* squirel away the lsn into the memory we allocated in
tran_begin. we wait on it and free the memory in
tran_commit */
p_buf = (uint8_t *)lsnp;
p_buf_end = (uint8_t *)lsnp + sizeof(DB_LSN);
db_lsn_type_get(&(tran->savelsn), p_buf, p_buf_end);
/*
char str[80];
fprintf(stderr, "setting savesln to
%s\n",lsn_to_str(str,savelsn));
*/
/* just sanity check that we arent screwing up */
if (tran->savelsn.file == 0) {
logmsg(LOGMSG_FATAL, "set lsn to 0\n");
exit(1);
}
}
}
}
}
if (host == db_eid_broadcast) {
void *data[2];
int sz[2];
int type[2];
int flag[2];
int num = 0;
uint64_t gblcontext;
if (bdb_state->repinfo->master_host == bdb_state->repinfo->myhost &&
is_logput && tran && bdb_state->attr->net_send_gblcontext &&
nodelay && !gbl_rowlocks &&
(gblcontext = get_gblcontext(bdb_state)) != 0
) {
if (gblcontext == -1ULL) {
logmsg(LOGMSG_ERROR, "SENDING context -1 to all nodes\n");
cheap_stack_trace();
}
data[num] = &gblcontext;
sz[num] = sizeof(gblcontext);
type[num] = USER_TYPE_GBLCONTEXT;
flag[num] = 0;
++num;
}
data[num] = buf;
sz[num] = bufsz;
type[num] = USER_TYPE_BERKDB_REP;
flag[num] = (is_logput ? NET_SEND_LOGPUT : (NET_SEND_NODROP | NET_SEND_NODELAY)) |
((flags & DB_REP_NODROP) ? NET_SEND_NODROP : 0) |
(bdb_state->attr->net_inorder_logputs ? NET_SEND_INORDER : 0) |
(nodelay ? NET_SEND_NODELAY : 0) |
(flags & DB_REP_TRACE ? NET_SEND_TRACE : 0);
++num;
rc = net_send_all(bdb_state->repinfo->netinfo, num, data, sz, type, flag);
} else {
int tmpseq;
uint8_t *p_seq_num = (uint8_t *)seqnum;
uint8_t *p_seq_num_end = ((uint8_t *)seqnum + sizeof(int));
p_rep_type_berkdb_rep_seqnum.seqnum = tmpseq =
get_seqnum(bdb_state, host);
rep_type_berkdb_rep_seqnum_put(&p_rep_type_berkdb_rep_seqnum, p_seq_num,
p_seq_num_end);
p_seq_num = (uint8_t *)seqnum;
p_seq_num_end = ((uint8_t *)seqnum + sizeof(int));
rep_type_berkdb_rep_seqnum_put(&p_rep_type_berkdb_rep_seqnum, p_seq_num,
p_seq_num_end);
if (bdb_state->rep_trace)
logmsg(LOGMSG_USER, "--- sending seq %d to %s, nodelay is %d\n", tmpseq,
host, nodelay);
uint32_t sendflags = 0;
if (!is_logput)
sendflags |= (NET_SEND_NODROP | NET_SEND_NODELAY);
if (flags & DB_REP_NODROP)
sendflags |= NET_SEND_NODROP;
if (bdb_state->attr->net_inorder_logputs)
sendflags |= NET_SEND_INORDER;
if (nodelay)
sendflags |= NET_SEND_NODELAY;
if (flags & DB_REP_TRACE) {
logmsg(LOGMSG_USER, "%s line %d calling net_send_flags\n", __func__,
__LINE__);
sendflags |= NET_SEND_TRACE;
}
rc = net_send_flags(bdb_state->repinfo->netinfo, host,
USER_TYPE_BERKDB_REP, buf, bufsz, sendflags);
}
if (rc != 0) {
outrc = 1;
}
if (useheap)
free(buf);
return outrc;
}
enum { DONT_LOSE = 1, LOSE = 2, REOPEN_AND_LOSE = 3 };
typedef struct {
bdb_state_type *bdb_state;
int op;
} elect_thread_args_type;
static int get_num_up(bdb_state_type *bdb_state)
{
const char *nodelist[REPMAX];
int num_up;
int tot;
int i;
if (!bdb_state->callback->nodeup_rtn)
return net_count_nodes(bdb_state->repinfo->netinfo);
num_up = 0;
tot = net_get_all_nodes(bdb_state->repinfo->netinfo, nodelist);
for (i = 0; i < tot; i++)
if ((bdb_state->callback->nodeup_rtn)(bdb_state, nodelist[i]))
num_up++;