/
Backup.cpp
17777 lines (16429 loc) · 585 KB
/
Backup.cpp
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) 2003, 2020, Oracle and/or its affiliates.
Copyright (c) 2022, 2023, Hopsworks and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is also distributed with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have included with MySQL.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "Backup.hpp"
#include <ndb_version.h>
#include <NdbTCP.h>
#include "util/ndb_math.h"
#include <Bitmask.hpp>
#include <signaldata/NodeFailRep.hpp>
#include <signaldata/ReadNodesConf.hpp>
#include <signaldata/DihScanTab.hpp>
#include <signaldata/DiGetNodes.hpp>
#include <signaldata/ScanFrag.hpp>
#include <signaldata/GetTabInfo.hpp>
#include <signaldata/DictTabInfo.hpp>
#include <signaldata/ListTables.hpp>
#include <signaldata/FsOpenReq.hpp>
#include <signaldata/FsAppendReq.hpp>
#include <signaldata/FsCloseReq.hpp>
#include <signaldata/FsConf.hpp>
#include <signaldata/FsRef.hpp>
#include <signaldata/FsRemoveReq.hpp>
#include <signaldata/FsReadWriteReq.hpp>
#include <signaldata/GetCpuUsage.hpp>
#include <signaldata/BackupImpl.hpp>
#include <signaldata/BackupSignalData.hpp>
#include <signaldata/BackupContinueB.hpp>
#include <signaldata/EventReport.hpp>
#include <signaldata/UtilSequence.hpp>
#include <signaldata/CreateTrigImpl.hpp>
#include <signaldata/DropTrigImpl.hpp>
#include <signaldata/FireTrigOrd.hpp>
#include <signaldata/TrigAttrInfo.hpp>
#include <AttributeHeader.hpp>
#include <signaldata/WaitGCP.hpp>
#include <signaldata/LCP.hpp>
#include <signaldata/BackupLockTab.hpp>
#include <signaldata/DumpStateOrd.hpp>
#include <signaldata/DumpStateOrd.hpp>
#include <signaldata/DbinfoScan.hpp>
#include <signaldata/TransIdAI.hpp>
#include <NdbTick.h>
#include <dbtup/Dbtup.hpp>
#include <EventLogger.hpp>
extern EventLogger * g_eventLogger;
#include <math.h>
#define JAM_FILE_ID 475
static const Uint32 WaitDiskBufferCapacityMillis = 1;
static const Uint32 WaitScanTempErrorRetryMillis = 10;
static NDB_TICKS startTime;
#if (defined(VM_TRACE) || defined(ERROR_INSERT))
//#define DEBUG_EMPTY_LCP 1
//#define DEBUG_END_LCP 1
//#define DEBUG_LCP_DEL_FILES 1
//#define DEBUG_LCP 1
//#define DEBUG_EMPTY_LCP 1
//#define DEBUG_UNDO_LCP 1
//#define DEBUG_LCP_ROW 1
//#define DEBUG_LCP_DEL 1
//#define DEBUG_EXTRA_LCP 1
//#define DEBUG_REDO_CONTROL 1
//#define DEBUG_REDO_CONTROL_DETAIL 1
//#define DEBUG_LCP_DD 1
//#define DEBUG_LCP_STAT 1
//#define DEBUG_LCP_LAG 1
#endif
#ifdef DEBUG_END_LCP
#define DEB_END_LCP(arglist) do { g_eventLogger->info arglist ; } while (0)
#else
#define DEB_END_LCP(arglist) do { } while (0)
#endif
#ifdef DEBUG_EMPTY_LCP
#define DEB_EMPTY_LCP(arglist) do { g_eventLogger->info arglist ; } while (0)
#else
#define DEB_EMPTY_LCP(arglist) do { } while (0)
#endif
#ifdef DEBUG_REDO_CONTROL
#define DEB_REDO_CONTROL(arglist) do { g_eventLogger->info arglist ; } while (0)
#else
#define DEB_REDO_CONTROL(arglist) do { } while (0)
#endif
#ifdef DEBUG_REDO_CONTROL_DETAIL
#define DEB_REDO_CONTROL_DETAIL(arglist) do { g_eventLogger->info arglist ; } while (0)
#else
#define DEB_REDO_CONTROL_DETAIL(arglist) do { } while (0)
#endif
#ifdef DEBUG_UNDO_LCP
#define DEB_UNDO_LCP(arglist) do { g_eventLogger->info arglist ; } while (0)
#else
#define DEB_UNDO_LCP(arglist) do { } while (0)
#endif
#ifdef DEBUG_LCP
#define DEB_LCP(arglist) do { g_eventLogger->info arglist ; } while (0)
#else
#define DEB_LCP(arglist) do { } while (0)
#endif
#ifdef DEBUG_LCP_DD
#define DEB_LCP_DD(arglist) do { g_eventLogger->info arglist ; } while (0)
#else
#define DEB_LCP_DD(arglist) do { } while (0)
#endif
#ifdef DEBUG_LCP_DEL_FILES
#define DEB_LCP_DEL_FILES(arglist) do { g_eventLogger->info arglist ; } while (0)
#else
#define DEB_LCP_DEL_FILES(arglist) do { } while (0)
#endif
#ifdef DEBUG_LCP_DEL
#define DEB_LCP_DEL(arglist) do { g_eventLogger->info arglist ; } while (0)
#else
#define DEB_LCP_DEL(arglist) do { } while (0)
#endif
#ifdef DEBUG_LCP_STAT
#define DEB_LCP_STAT(arglist) do { g_eventLogger->info arglist ; } while (0)
#else
#define DEB_LCP_STAT(arglist) do { } while (0)
#endif
#ifdef DEBUG_LCP_LAG
#define DEB_LCP_LAG(arglist) do { g_eventLogger->info arglist ; } while (0)
#else
#define DEB_LCP_LAG(arglist) do { } while (0)
#endif
#ifdef DEBUG_EXTRA_LCP
#define DEB_EXTRA_LCP(arglist) do { g_eventLogger->info arglist ; } while (0)
#else
#define DEB_EXTRA_LCP(arglist) do { } while (0)
#endif
#ifdef VM_TRACE
#define DEBUG_OUT(x) ndbout << x << endl
#else
#define DEBUG_OUT(x)
#endif
//#define DEBUG_ABORT
//#define dbg globalSignalLoggers.log
static Uint32 g_TypeOfStart = NodeState::ST_ILLEGAL_TYPE;
#define SEND_BACKUP_STARTED_FLAG(A) (((A) & BackupReq::WAITCOMPLETED) > 0)
#define SEND_BACKUP_COMPLETED_FLAG(A) (((A) & BackupReq::WAITCOMPLETED) > 1)
#define MT_BACKUP_FLAG(A) (((A) & BackupReq::MT_BACKUP) > 0)
/**
* "Magic" constants used for adaptive LCP speed algorithm. These magic
* constants tries to ensure a smooth LCP load which is high enough to
* avoid slowing down LCPs such that we run out of REDO logs. Also low
* enough to avoid that we use so much CPU on LCPs that we block out
* most user transactions. We also want to avoid destroying real-time
* characteristics due to LCPs.
*
* See much longer explanation of these values below.
*/
#define HIGH_LOAD_LEVEL 32
#define VERY_HIGH_LOAD_LEVEL 48
#define MAX_RAISE_PRIO_MEMORY 16
void
Backup::execSTTOR(Signal* signal)
{
jamEntry();
const Uint32 startphase = signal->theData[1];
const Uint32 typeOfStart = signal->theData[7];
if (startphase == 1)
{
ndbrequire((c_lqh = (Dblqh*)globalData.getBlock(DBLQH, instance())) != 0);
ndbrequire((c_tup = (Dbtup*)globalData.getBlock(DBTUP, instance())) != 0);
ndbrequire((c_lgman =
(Lgman*)globalData.getBlock(LGMAN, instance())) != 0);
ndbrequire((c_pgman =
(Pgman*)globalData.getBlock(PGMAN, instance())) != 0);
m_words_written_this_period = 0;
m_backup_words_written_this_period = 0;
last_disk_write_speed_report = 0;
next_disk_write_speed_report = 0;
m_monitor_words_written = 0;
m_backup_monitor_words_written = 0;
m_periods_passed_in_monitor_period = 0;
m_monitor_snapshot_start = NdbTick_getCurrentTicks();
m_curr_lcp_id = 0;
m_curr_disk_write_speed = c_defaults.m_disk_write_speed_max_own_restart;
m_curr_backup_disk_write_speed =
c_defaults.m_disk_write_speed_max_own_restart;
m_overflow_disk_write = 0;
m_backup_overflow_disk_write = 0;
slowdowns_due_to_io_lag = 0;
slowdowns_due_to_high_cpu = 0;
disk_write_speed_set_to_min = 0;
m_is_lcp_running = false;
m_is_backup_running = false;
m_is_any_node_restarting = false;
m_node_restart_check_sent = false;
m_our_node_started = false;
m_lcp_ptr.i = RNIL;
m_lcp_ptr.p = 0;
m_first_lcp_started = false;
m_newestRestorableGci = 0;
m_delete_lcp_files_ongoing = false;
m_reset_disk_speed_time = NdbTick_getCurrentTicks();
m_reset_delay_used = Backup::DISK_SPEED_CHECK_DELAY;
c_initial_start_lcp_not_done_yet = false;
m_redo_alert_factor = 1;
m_redo_alert_state = RedoStateRep::NO_REDO_ALERT;
}
if (startphase == 2)
{
if (!m_is_query_block)
{
jam();
signal->theData[0] = BackupContinueB::RESET_DISK_SPEED_COUNTER;
sendSignalWithDelay(reference(), GSN_CONTINUEB, signal,
Backup::DISK_SPEED_CHECK_DELAY, 1);
}
}
if (startphase == 3)
{
jam();
g_TypeOfStart = typeOfStart;
if (g_TypeOfStart == NodeState::ST_INITIAL_START ||
g_TypeOfStart == NodeState::ST_INITIAL_NODE_RESTART)
{
jam();
c_initial_start_lcp_not_done_yet = true;
}
signal->theData[0] = reference();
sendSignal(NDBCNTR_REF, GSN_READ_NODESREQ, signal, 1, JBB);
return;
}
if (startphase == 7)
{
m_monitor_words_written = 0;
m_backup_monitor_words_written = 0;
m_periods_passed_in_monitor_period = 0;
m_monitor_snapshot_start = NdbTick_getCurrentTicks();
m_curr_disk_write_speed = c_defaults.m_disk_write_speed_min;
m_curr_backup_disk_write_speed = c_defaults.m_disk_write_speed_min;
m_our_node_started = true;
c_initial_start_lcp_not_done_yet = false;
}
if(startphase == 7 && g_TypeOfStart == NodeState::ST_INITIAL_START &&
c_masterNodeId == getOwnNodeId() && !isNdbMtLqh()){
jam();
createSequence(signal);
return;
}//if
sendSTTORRY(signal);
return;
}//Dbdict::execSTTOR()
void
Backup::execREAD_NODESCONF(Signal* signal)
{
jamEntry();
ReadNodesConf * conf = (ReadNodesConf *)signal->getDataPtr();
{
ndbrequire(signal->getNoOfSections() == 1);
SegmentedSectionPtr ptr;
SectionHandle handle(this, signal);
handle.getSection(ptr, 0);
ndbrequire(ptr.sz == 5 * NdbNodeBitmask::Size);
copy((Uint32*)&conf->definedNodes.rep.data, ptr);
releaseSections(handle);
}
c_aliveNodes.clear();
Uint32 count = 0;
for (Uint32 i = 0; i<MAX_NDB_NODES; i++) {
jam();
if (conf->definedNodes.get(i))
{
jam();
count++;
NodePtr node;
ndbrequire(c_nodes.seizeFirst(node));
node.p->nodeId = i;
if (conf->inactiveNodes.get(i))
{
jam();
node.p->alive = 0;
} else {
jam();
node.p->alive = 1;
c_aliveNodes.set(i);
}//if
}//if
}//for
c_masterNodeId = conf->masterNodeId;
ndbrequire(count == conf->noOfNodes);
sendSTTORRY(signal);
}
void
Backup::sendSTTORRY(Signal* signal)
{
Uint32 sig_len;
if (m_is_query_block)
{
jam();
signal->theData[0] = 0;
signal->theData[3] = 1;
signal->theData[4] = 255;
sig_len = 5;
}
else
{
jam();
signal->theData[0] = 0;
signal->theData[3] = 1;
signal->theData[4] = 2;
signal->theData[5] = 3;
signal->theData[6] = 7;
signal->theData[7] = 255; // No more start phases from missra
sig_len = 8;
}
BlockReference cntrRef = !isNdbMtLqh() ? NDBCNTR_REF :
m_is_query_block ? QBACKUP_REF : BACKUP_REF;
sendSignal(cntrRef, GSN_STTORRY, signal, sig_len, JBB);
}
void
Backup::createSequence(Signal* signal)
{
UtilSequenceReq * req = (UtilSequenceReq*)signal->getDataPtrSend();
req->senderData = RNIL;
req->sequenceId = NDB_BACKUP_SEQUENCE;
req->requestType = UtilSequenceReq::Create;
sendSignal(DBUTIL_REF, GSN_UTIL_SEQUENCE_REQ,
signal, UtilSequenceReq::SignalLength, JBB);
}
void
Backup::handle_overflow(Uint64& overflow_disk_write,
Uint64& words_written_this_period,
Uint64& curr_disk_write_speed)
{
jam();
/**
* If we overflowed in the last period, count it in
* this new period, potentially overflowing again into
* future periods...
*
* The overflow can only come from the last write we did in this
* period, but potentially this write is bigger than what we are
* allowed to write during one period.
*
* Calculate the overflow to pass into the new period
* (overflowThisPeriod). It can never be more than what is
* allowed to be written during a period.
*
* We could rarely end up in the case that the overflow of the
* last write in the period even overflows the entire next period.
* If so we put this into the remainingOverFlow and put this into
* overflow_disk_write (in this case nothing will be written in
* this period so ready_to_write need not worry about this case
* when setting overflow_disk_write since it isn't written any time
* in this case and in all other cases only written by the last write
* in a period.
*
* This routine is called both for collective LCP and Backup overflow
* and for only Backup overflow.
*/
Uint64 overflowThisPeriod = MIN(overflow_disk_write,
curr_disk_write_speed + 1);
/* How much overflow remains after this period? */
Uint64 remainingOverFlow = overflow_disk_write - overflowThisPeriod;
if (overflowThisPeriod)
{
jam();
#ifdef DEBUG_CHECKPOINTSPEED
ndbout_c("Overflow of %u bytes (max/period is %u bytes)",
overflowThisPeriod * 4, curr_disk_write_speed * 4);
#endif
if (remainingOverFlow)
{
jam();
#ifdef DEBUG_CHECKPOINTSPEED
ndbout_c(" Extra overflow : %u bytes, will take %u further periods"
" to clear", remainingOverFlow * 4,
remainingOverFlow / curr_disk_write_speed);
#endif
}
}
if (true || curr_disk_write_speed == m_curr_disk_write_speed)
{
DEB_REDO_CONTROL_DETAIL(("(%u)bytes_written_this_period: %llu kB, "
" overflowThisPeriod: %llu kB, "
" remainingOverFlow: %llu kB, "
" curr_disk_write_speed %llu kB",
instance(),
words_written_this_period / 256,
overflowThisPeriod / 256,
remainingOverFlow / 256,
curr_disk_write_speed / 256));
}
words_written_this_period = overflowThisPeriod;
overflow_disk_write = remainingOverFlow;
}
void
Backup::calculate_next_delay(const NDB_TICKS curr_time)
{
/**
* Adjust for upto 10 millisecond delay of this signal. Longer
* delays will not be handled, in this case the system is most
* likely under too high load and it won't matter very much that
* we decrease the speed of checkpoints.
*
* We use a technique where we allow an overflow write in one
* period. This overflow will be removed from the next period
* such that the load will at average be as specified.
* Calculate new delay time based on if we overslept or underslept
* this time. We will never regulate more than 10ms, if the
* oversleep is bigger than we will simply ignore it. We will
* decrease the delay by as much as we overslept or increase it by
* as much as we underslept.
*/
int delay_time = m_reset_delay_used;
int sig_delay = int(NdbTick_Elapsed(m_reset_disk_speed_time,
curr_time).milliSec());
if (sig_delay > delay_time + 10)
{
delay_time = Backup::DISK_SPEED_CHECK_DELAY - 10;
}
else if (sig_delay < delay_time - 10)
{
delay_time = Backup::DISK_SPEED_CHECK_DELAY + 10;
}
else
{
delay_time = Backup::DISK_SPEED_CHECK_DELAY -
(sig_delay - delay_time);
}
m_periods_passed_in_monitor_period++;
m_reset_delay_used= delay_time;
m_reset_disk_speed_time = curr_time;
#if 0
ndbout << "Signal delay was = " << sig_delay;
ndbout << " Current time = " << curr_time << endl;
ndbout << " Delay time will be = " << delay_time << endl << endl;
#endif
}
void
Backup::report_disk_write_speed_report(Uint64 bytes_written_this_period,
Uint64 backup_bytes_written_this_period,
Uint64 millis_passed)
{
Uint32 report = next_disk_write_speed_report;
disk_write_speed_rep[report].backup_bytes_written =
backup_bytes_written_this_period;
disk_write_speed_rep[report].backup_lcp_bytes_written =
bytes_written_this_period;
disk_write_speed_rep[report].millis_passed =
millis_passed;
disk_write_speed_rep[report].redo_bytes_written =
c_lqh->report_redo_written_bytes();
disk_write_speed_rep[report].target_disk_write_speed =
m_curr_disk_write_speed * CURR_DISK_SPEED_CONVERSION_FACTOR_TO_SECONDS;
disk_write_speed_rep[report].target_backup_disk_write_speed =
m_curr_backup_disk_write_speed * CURR_DISK_SPEED_CONVERSION_FACTOR_TO_SECONDS;
next_disk_write_speed_report++;
if (next_disk_write_speed_report == DISK_WRITE_SPEED_REPORT_SIZE)
{
next_disk_write_speed_report = 0;
}
if (next_disk_write_speed_report == last_disk_write_speed_report)
{
last_disk_write_speed_report++;
if (last_disk_write_speed_report == DISK_WRITE_SPEED_REPORT_SIZE)
{
last_disk_write_speed_report = 0;
}
}
}
#define DELETE_RECOVERY_WORK 120
/**
* This method is a check that we haven't been writing faster than we're
* supposed to during the last interval.
*/
void
Backup::monitor_disk_write_speed(const NDB_TICKS curr_time,
const Uint64 millisPassed)
{
/**
* Independent check of DiskCheckpointSpeed.
* We check every second or so that we are roughly sticking
* to our diet.
*/
jam();
const Uint64 periodsPassed =
(millisPassed / DISK_SPEED_CHECK_DELAY) + 1;
const Uint64 quotaWordsPerPeriod = m_curr_disk_write_speed;
const Uint64 quotaWordsPerPeriodBackup = m_curr_backup_disk_write_speed;
const Uint64 maxOverFlowWords = c_defaults.m_maxWriteSize / 4;
const Uint64 maxExpectedWords = (periodsPassed * quotaWordsPerPeriod) +
maxOverFlowWords;
const Uint64 maxExpectedWordsBackup = (periodsPassed *
quotaWordsPerPeriodBackup) +
maxOverFlowWords;
if (unlikely((m_monitor_words_written > maxExpectedWords) ||
(m_backup_monitor_words_written > maxExpectedWordsBackup)))
{
jam();
/**
* In the last monitoring interval, we have written more words
* than allowed by the quota (DiskCheckpointSpeed), including
* transient spikes due to a single MaxBackupWriteSize write
*/
ndbout << "Backup : Excessive Backup/LCP write rate in last"
<< " monitoring period - recorded = "
<< (m_monitor_words_written * 4 * 1000) / millisPassed
<< " bytes/s, "
<< endl
<< "Recorded writes to backup: "
<< (m_backup_monitor_words_written * 4 * 1000) / millisPassed
<< " bytes/s, "
<< endl;
ndbout << "Current speed is = "
<< m_curr_disk_write_speed *
CURR_DISK_SPEED_CONVERSION_FACTOR_TO_SECONDS
<< " bytes/s"
<< endl;
ndbout << "Current backup speed is = "
<< m_curr_backup_disk_write_speed *
CURR_DISK_SPEED_CONVERSION_FACTOR_TO_SECONDS
<< " bytes/s"
<< endl;
ndbout << "Backup : Monitoring period : " << millisPassed
<< " millis. Bytes written : " << (m_monitor_words_written * 4)
<< ". Max allowed : " << (maxExpectedWords * 4) << endl;
ndbout << "Backup : Monitoring period : " << millisPassed
<< " millis. Bytes written : "
<< (m_backup_monitor_words_written * 4)
<< ". Max allowed : " << (maxExpectedWordsBackup * 4) << endl;
ndbout << "Actual number of periods in this monitoring interval: ";
ndbout << m_periods_passed_in_monitor_period;
ndbout << " calculated number was: " << periodsPassed << endl;
}
report_disk_write_speed_report(4 * m_monitor_words_written,
4 * m_backup_monitor_words_written,
millisPassed);
/**
* The LCP write rate is removed from the calculated LCP change rate to
* derive the lag (a lag is a positive number, if we are ahead of the
* calculated rate we report it as a negative number).
* We keep track of the lag since the start of the LCP and since the
* start of the previous LCP.
*/
Int64 lag = m_lcp_change_rate -
((4 * m_monitor_words_written) -
(4 * m_backup_monitor_words_written));
m_lcp_lag[1] += lag;
DEB_REDO_CONTROL(("(%u)change_rate: %llu kB, LCP+Backup: %llu kB,"
" Backup: %llu kB, lag: %lld kB",
instance(),
m_lcp_change_rate / 1024,
m_monitor_words_written / 256,
m_backup_monitor_words_written / 256,
lag / 1024));
m_monitor_words_written = 0;
m_backup_monitor_words_written = 0;
m_periods_passed_in_monitor_period = 0;
m_monitor_snapshot_start = curr_time;
}
void
Backup::debug_report_redo_control(Uint32 cpu_usage)
{
#ifdef DEBUG_REDO_CONTROL
{
Uint64 millis_passed;
Uint64 backup_lcp_bytes_written;
Uint64 backup_bytes_written;
Uint64 redo_bytes_written;
calculate_disk_write_speed_seconds_back(1,
millis_passed,
backup_lcp_bytes_written,
backup_bytes_written,
redo_bytes_written,
true);
backup_bytes_written *= Uint64(1000);
backup_bytes_written /= (millis_passed * Uint64(1024));
backup_lcp_bytes_written *= Uint64(1000);
backup_lcp_bytes_written /= (millis_passed * Uint64(1024));
redo_bytes_written *= Uint64(1000);
redo_bytes_written /= (millis_passed * Uint64(1024));
/* Report new disk write speed and last seconds achievement on disk */
DEB_REDO_CONTROL(("(%u)Current disk write speed is %llu kB/sec"
" and current backup disk write speed is %llu kB/sec"
", last sec REDO write speed %llu kB/sec, "
"LCP+Backup write speed %llu kB/sec"
", Backup write speed %llu kB/sec"
", cpu_usage: %u",
instance(),
((m_curr_disk_write_speed *
CURR_DISK_SPEED_CONVERSION_FACTOR_TO_SECONDS) /
Uint64(1024)),
((m_curr_backup_disk_write_speed *
CURR_DISK_SPEED_CONVERSION_FACTOR_TO_SECONDS) /
Uint64(1024)),
redo_bytes_written,
backup_lcp_bytes_written,
backup_bytes_written,
cpu_usage));
}
#else
(void)cpu_usage;
#endif
}
void
Backup::execREDO_STATE_REP(Signal* signal)
{
RedoStateRep *rep = (RedoStateRep*)signal->getDataPtr();
ndbrequire(rep->receiverInfo == RedoStateRep::ToBackup);
m_global_redo_alert_state = (RedoStateRep::RedoAlertState)rep->redoState;
DEB_REDO_CONTROL(("(%u) New global redo alert state: %u",
instance(),
m_global_redo_alert_state));
}
/**
* Initialise LCP timers at the time we hear of the first writes to the
* REDO log. Could also be initialised by the start of the first LCP.
*/
void
Backup::init_lcp_timers(Uint64 redo_written_since_last_call)
{
if (redo_written_since_last_call > 0)
{
if (!NdbTick_IsValid(m_lcp_start_time))
{
m_lcp_start_time = getHighResTimer();
m_prev_lcp_start_time = m_lcp_start_time;
}
}
}
void
Backup::lcp_start_point(Signal *signal)
{
/**
* A new LCP is starting up, we need to keep track of this to handle
* REDO control.
* The start and end points of LCPs currently only come with an
* accuracy of about 1 second, so if the LCP time is shorter than
* this we can definitely ignore any REDO alerts.
*/
if (!NdbTick_IsValid(m_prev_lcp_start_time))
{
jam();
m_prev_lcp_start_time = getHighResTimer();
}
else
{
m_prev_lcp_start_time = m_lcp_start_time;
}
c_pgman->lcp_start_point(signal,
m_max_undo_log_level_percentage + 1,
m_max_redo_percentage);
m_max_undo_log_level_percentage = m_undo_log_level_percentage;
m_max_redo_percentage = m_redo_percentage;
m_first_lcp_started = true;
m_lcp_start_time = getHighResTimer();
ndbrequire(NdbTick_IsValid(m_lcp_start_time));
m_lcp_current_cut_point = m_prev_lcp_start_time;
m_update_size_lcp[0] = m_update_size_lcp[1];
m_update_size_lcp[1] = m_update_size_lcp_last;
m_insert_size_lcp[0] = m_insert_size_lcp[1];
m_insert_size_lcp[1] = m_insert_size_lcp_last;
m_delete_size_lcp[0] = m_delete_size_lcp[1];
m_delete_size_lcp[1] = m_delete_size_lcp_last;
DEB_REDO_CONTROL(("(%u)m_insert_size_lcp[0]: %llu MByte, "
"m_insert_size_lcp[1]: %llu MByte, "
"m_insert_size_lcp_last: %llu MByte",
instance(),
(m_insert_size_lcp[0] / (1024 * 1024)),
(m_insert_size_lcp[1] / (1024 * 1024)),
(m_insert_size_lcp_last / (1024 * 1024))));
}
bool
Backup::lcp_end_point(BackupRecordPtr ptr)
{
Uint32 num_fragments = ptr.p->m_num_fragments;
ptr.p->backupId = 0; /* Ensure next LCP_PREPARE_REQ sees a new LCP id */
ptr.p->m_num_fragments = 0;
ptr.p->m_first_fragment = false;
NDB_TICKS current_time = getHighResTimer();
if (num_fragments == 0)
{
jam();
/**
* We haven't started LCP in this LDM since there are no fragments, so
* won't update any LCP sizes here.
*/
m_last_lcp_exec_time_in_ms = 0;
return true;
}
jam();
ndbrequire(NdbTick_IsValid(m_lcp_start_time));
m_last_lcp_exec_time_in_ms =
NdbTick_Elapsed(m_lcp_start_time, current_time).milliSec();
m_lcp_current_cut_point = m_lcp_start_time;
bool ready = true;
if (isNdbMt())
{
/**
* Only call for ndbmtd since ndbd has no extra PGMAN worker.
*/
ready = c_pgman->lcp_end_point(m_last_lcp_exec_time_in_ms, true, false);
}
reset_lcp_timing_factors();
#ifdef DEBUG_REDO_CONTROL
Uint64 checkpoint_size = m_insert_size_lcp[1] - m_insert_size_lcp[0];
Uint64 checkpoint_rate = 0;
if (m_last_lcp_exec_time_in_ms > 0)
{
checkpoint_rate = checkpoint_size / m_last_lcp_exec_time_in_ms;
}
DEB_REDO_CONTROL(("(%u)LCP END: m_insert_size_lcp[0]: %llu MByte, "
"Remaining lag: %lld MB, "
"Removed lag: %lld MB, "
"Checkpoint rate in this LCP: %llu kB/sec",
instance(),
(checkpoint_size / (1024 * 1024)),
(m_lcp_lag[1] / (1024 * 1024)),
(m_lcp_lag[0] / (1024 * 1024)),
checkpoint_rate));
#endif
m_update_size_lcp[0] = m_update_size_lcp[1];
m_insert_size_lcp[0] = m_insert_size_lcp[1];
m_delete_size_lcp[0] = m_delete_size_lcp[1];
m_lcp_lag[0] = m_lcp_lag[1];
m_lcp_lag[1] = Int64(0);
return ready;
}
Uint64
Backup::init_change_size(Uint64 update_size,
Uint64 insert_size,
Uint64 delete_size,
Uint64 total_memory)
{
/**
* The initial value for change_size is based on that the new
* rows or deleted rows are always changes, but updates can
* at times be updates of the same row. We use an exponential
* probability distribution that a row has been updated or not.
*/
Uint64 change_size = insert_size + delete_size;
long double f_total_memory = (long double)total_memory;
long double f_change_size = update_size;
long double f_change_percentage = f_change_size / f_total_memory;
long double f_real_change_percentage = ((long double)1) -
exp(-f_change_percentage);
long double f_real_change_size = f_real_change_percentage *
f_total_memory;
change_size += (Uint64)f_real_change_size;
return change_size;
}
Uint64
Backup::modify_change_size(Uint64 update_size,
Uint64 insert_size,
Uint64 delete_size,
Uint64 total_size,
Uint64 change_size)
{
/**
* Now we have calculated an estimate that is comparable
* to the row_change_count that we get per fragment before
* calculating the number of parts to checkpoint.
*
* The next step is now to modify this estimate based on
* the amount of inserts and deletes compared to the updates.
*/
Uint64 updates_percent = (update_size * Uint64(1005)) /
(Uint64(10) * total_size);
Uint64 inserts_percent = (insert_size * Uint64(1005)) /
(Uint64(10) * total_size);
Uint64 insert_recovery_work = (Uint64)get_insert_recovery_work();
inserts_percent *= insert_recovery_work;
inserts_percent /= Uint64(100);
Uint64 deletes_percent = (delete_size * Uint64(1005)) /
(Uint64(10) * total_size);
deletes_percent *= Uint64(DELETE_RECOVERY_WORK);
deletes_percent /= Uint64(100);
Uint64 change_factor = updates_percent +
inserts_percent +
deletes_percent;
change_size *= change_factor;
change_size /= Uint64(100);
return change_size;
}
Uint32
Backup::calculate_parts(Uint64 change_size,
Uint64 total_memory)
{
Uint64 part_total_memory = total_memory / Uint64(10);
Uint32 min_parts = calculate_min_parts(total_memory,
change_size,
part_total_memory,
total_memory);
return min_parts;
}
void
Backup::calculate_seconds_since_lcp_cut(Uint64& seconds_since_lcp_cut)
{
NDB_TICKS now = getHighResTimer();
if (!NdbTick_IsValid(m_lcp_current_cut_point))
{
jam();
seconds_since_lcp_cut = 0;
return;
}
seconds_since_lcp_cut =
NdbTick_Elapsed(m_lcp_current_cut_point, now).seconds();
}
Uint64
Backup::calculate_change_rate(Uint64 change_size,
Uint64& seconds_since_lcp_cut)
{
if (seconds_since_lcp_cut < 2)
{
jam();
/**
* We ignore very short LCPs, in this case it is hard to see
* how we could run out of REDO log and need more disk write
* speed.
*/
return 0;
}
Uint64 change_size_per_sec = change_size / seconds_since_lcp_cut;
return change_size_per_sec;
}
Uint64
Backup::calculate_checkpoint_rate(Uint64 update_size,
Uint64 insert_size,
Uint64 delete_size,
Uint64 total_memory,
Uint64& seconds_since_lcp_cut)
{
Uint64 checkpoint_size = 0;
Uint32 all_parts = 0;
Uint64 all_size = 0;
Uint64 change_size = 0;
Uint64 mod_change_size = 0;
Uint64 total_size = update_size + insert_size + delete_size;
if (total_size != 0)
{
if (delete_size > insert_size)
{
update_size += insert_size;
delete_size -= insert_size;
insert_size = 0;
}
else
{
update_size += delete_size;
insert_size -= delete_size;
delete_size = 0;
}
calculate_seconds_since_lcp_cut(seconds_since_lcp_cut);
change_size = init_change_size(update_size,
insert_size,
delete_size,
total_memory);
mod_change_size = modify_change_size(update_size,
insert_size,
delete_size,
total_size,
change_size);
all_parts = calculate_parts(mod_change_size, total_memory);
all_size = total_memory * Uint64(all_parts);
all_size /= Uint64(BackupFormat::NDB_MAX_LCP_PARTS);
change_size = (BackupFormat::NDB_MAX_LCP_PARTS - all_parts) *
change_size;
change_size /= BackupFormat::NDB_MAX_LCP_PARTS;
checkpoint_size = all_size + change_size;
}
Uint64 change_rate = calculate_change_rate(checkpoint_size,
seconds_since_lcp_cut);
DEB_REDO_CONTROL(("(%u)update_size: %llu MB, insert_size: %llu MB,"
" delete_size: %llu MB, checkpoint_size: %llu MB"
", all_parts: %u, total_memory: %llu MB, "
"all_size: %llu MB, change_size: %llu MB, "
"mod_change_size: %llu MB, "
"seconds_since_lcp_cut: %llu",
instance(),
update_size / (Uint64(1024) * Uint64(1024)),
insert_size / (Uint64(1024) * Uint64(1024)),
delete_size / (Uint64(1024) * Uint64(1024)),
checkpoint_size / (Uint64(1024) * Uint64(1024)),
all_parts,
total_memory / (Uint64(1024 * Uint64(1024))),
all_size / (Uint64(1024) * Uint64(1024)),
change_size / (Uint64(1024) * Uint64(1024)),
mod_change_size / (Uint64(1024) * Uint64(1024)),
seconds_since_lcp_cut));
return change_rate;
}
void
Backup::calculate_redo_parameters(Uint64 redo_usage,
Uint64 redo_size,
Uint64 redo_written_since_last_call,
Uint64 millis_since_last_call,
Uint64& max_redo_used_before_cut,
Uint64& mean_redo_used_before_cut,
Uint64& mean_redo_speed_per_sec,
Uint64& current_redo_speed_per_sec,
Uint64& redo_available)
{
/* redo_size and redo_usage is in MBytes, convert to bytes */
redo_size *= (Uint64(1024) * Uint64(1024));
redo_usage *= (Uint64(1024) * Uint64(1024));
redo_available = redo_size - redo_usage;
current_redo_speed_per_sec = redo_written_since_last_call * Uint64(1000);