/
btr0cur.cc
5617 lines (4627 loc) · 190 KB
/
btr0cur.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
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) 1994, 2022, Oracle and/or its affiliates.
Copyright (c) 2008, Google Inc.
Copyright (c) 2012, Facebook Inc.
Portions of this file contain modifications contributed and copyrighted by
Google, Inc. Those modifications are gratefully acknowledged and are described
briefly in the InnoDB documentation. The contributions by Google are
incorporated with their permission, and subject to the conditions contained in
the file COPYING.Google.
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
*****************************************************************************/
/** @file btr/btr0cur.cc
The index tree cursor
All changes that row operations make to a B-tree or the records
there must go through this module! Undo log records are written here
of every modify or insert of a clustered index record.
NOTE!!!
To make sure we do not run out of disk space during a pessimistic
insert or update, we have to reserve 2 x the height of the index tree
many pages in the tablespace before we start the operation, because
if leaf splitting has been started, it is difficult to undo, except
by crashing the database and doing a roll-forward.
Created 10/16/1994 Heikki Tuuri
*******************************************************/
#include "btr0cur.h"
#include <assert.h>
#include "my_dbug.h"
#ifndef UNIV_HOTBACKUP
#include <zlib.h>
#include "btr0btr.h"
#include "btr0sea.h"
#include "buf0lru.h"
#ifdef UNIV_DEBUG
#include "current_thd.h"
#include "debug_sync.h"
#endif /* UNIV_DEBUG */
#include "ibuf0ibuf.h"
#include "lob0lob.h"
#include "lock0lock.h"
#include "mtr0log.h"
#include "row0upd.h"
#endif /* !UNIV_HOTBACKUP */
#include "page0page.h"
#include "page0zip.h"
#ifndef UNIV_HOTBACKUP
#include "que0que.h"
#endif /* !UNIV_HOTBACKUP */
#include "rem0cmp.h"
#include "rem0rec.h"
#include "row0log.h"
#ifndef UNIV_HOTBACKUP
#include "row0purge.h"
#include "row0row.h"
#endif /* !UNIV_HOTBACKUP */
#include "row0upd.h"
#ifndef UNIV_HOTBACKUP
#include "srv0srv.h"
#endif /* !UNIV_HOTBACKUP */
#include "srv0start.h"
#ifndef UNIV_HOTBACKUP
#include "trx0rec.h"
#include "trx0roll.h"
#endif /* !UNIV_HOTBACKUP */
#include <array>
/** Buffered B-tree operation types, introduced as part of delete buffering. */
enum btr_op_t {
BTR_NO_OP = 0, /*!< Not buffered */
BTR_INSERT_OP, /*!< Insert, do not ignore UNIQUE */
BTR_INSERT_IGNORE_UNIQUE_OP, /*!< Insert, ignoring UNIQUE */
BTR_DELETE_OP, /*!< Purge a delete-marked record */
BTR_DELMARK_OP /*!< Mark a record for deletion */
};
/** Modification types for the B-tree operation. Note that the order of
the enum values is important.*/
enum btr_intention_t {
BTR_INTENTION_DELETE,
BTR_INTENTION_BOTH,
BTR_INTENTION_INSERT
};
/** For the index->lock scalability improvement, only possibility of clear
performance regression observed was caused by grown huge history list length.
That is because the exclusive use of index->lock also worked as reserving
free blocks and read IO bandwidth with priority. To avoid huge glowing history
list as same level with previous implementation, prioritizes pessimistic tree
operations by purge as the previous, when it seems to be growing huge.
Experimentally, the history list length starts to affect to performance
throughput clearly from about 100000. */
constexpr uint32_t BTR_CUR_FINE_HISTORY_LENGTH = 100000;
/** Number of searches down the B-tree in btr_cur_search_to_nth_level(). */
ulint btr_cur_n_non_sea = 0;
/** Number of successful adaptive hash index lookups in
btr_cur_search_to_nth_level(). */
ulint btr_cur_n_sea = 0;
/** Old value of btr_cur_n_non_sea. Copied by
srv_refresh_innodb_monitor_stats(). Referenced by
srv_printf_innodb_monitor(). */
ulint btr_cur_n_non_sea_old = 0;
/** Old value of btr_cur_n_sea. Copied by
srv_refresh_innodb_monitor_stats(). Referenced by
srv_printf_innodb_monitor(). */
ulint btr_cur_n_sea_old = 0;
#ifdef UNIV_DEBUG
/* Flag to limit optimistic insert records */
uint btr_cur_limit_optimistic_insert_debug = 0;
#endif /* UNIV_DEBUG */
/** In the optimistic insert, if the insert does not fit, but this much space
can be released by page reorganize, then it is reorganized */
#define BTR_CUR_PAGE_REORGANIZE_LIMIT (UNIV_PAGE_SIZE / 32)
/** Estimated table level stats from sampled value.
@param value sampled stats
@param index index being sampled
@param sample number of sampled rows
@param ext_size external stored data size
@param not_empty table not empty
@return estimated table wide stats from sampled value */
constexpr uint64_t BTR_TABLE_STATS_FROM_SAMPLE(uint64_t value,
dict_index_t *index,
uint64_t sample, ulint ext_size,
ulint not_empty) {
return (value * index->stat_n_leaf_pages + sample - 1 + ext_size +
not_empty) /
(sample + ext_size);
}
#ifndef UNIV_HOTBACKUP
/** Adds path information to the cursor for the current page, for which
the binary search has been performed.
@param[in, out] cursor Cursor positioned on a page.
@param[in] height Height of the page in the tree; 0 means leaf.
@param[in] root_height Root node height in true. */
static void btr_cur_add_path_info(btr_cur_t *cursor, ulint height,
ulint root_height);
/*==================== B-TREE SEARCH =========================*/
/** Latches the leaf page or pages requested.
@param[in] block Leaf page where the search converged
@param[in] page_id Page id of the leaf
@param[in] page_size Page size
@param[in] latch_mode BTR_SEARCH_LEAF, ...
@param[in] cursor Cursor
@param[in] mtr Mini-transaction
@return blocks and savepoints which actually latched. */
btr_latch_leaves_t btr_cur_latch_leaves(buf_block_t *block,
const page_id_t &page_id,
const page_size_t &page_size,
ulint latch_mode, btr_cur_t *cursor,
mtr_t *mtr) {
ulint mode;
page_no_t left_page_no;
page_no_t right_page_no;
buf_block_t *get_block;
page_t *page = buf_block_get_frame(block);
bool spatial;
btr_latch_leaves_t latch_leaves = {{nullptr, nullptr, nullptr}, {0, 0, 0}};
spatial = dict_index_is_spatial(cursor->index) && cursor->rtr_info;
ut_ad(buf_page_in_file(&block->page));
switch (latch_mode) {
case BTR_SEARCH_LEAF:
case BTR_MODIFY_LEAF:
case BTR_SEARCH_TREE:
if (spatial) {
cursor->rtr_info->tree_savepoints[RTR_MAX_LEVELS] =
mtr_set_savepoint(mtr);
}
mode = latch_mode == BTR_MODIFY_LEAF ? RW_X_LATCH : RW_S_LATCH;
latch_leaves.savepoints[1] = mtr_set_savepoint(mtr);
get_block = btr_block_get(page_id, page_size, mode, UT_LOCATION_HERE,
cursor->index, mtr);
latch_leaves.blocks[1] = get_block;
#ifdef UNIV_BTR_DEBUG
ut_a(page_is_comp(get_block->frame) == page_is_comp(page));
#endif /* UNIV_BTR_DEBUG */
if (spatial) {
cursor->rtr_info->tree_blocks[RTR_MAX_LEVELS] = get_block;
}
return (latch_leaves);
case BTR_MODIFY_TREE:
/* It is exclusive for other operations which calls
btr_page_set_prev() */
ut_ad(mtr_memo_contains_flagged(mtr, dict_index_get_lock(cursor->index),
MTR_MEMO_X_LOCK | MTR_MEMO_SX_LOCK) ||
cursor->index->table->is_intrinsic());
/* x-latch also siblings from left to right */
left_page_no = btr_page_get_prev(page, mtr);
if (left_page_no != FIL_NULL) {
if (spatial) {
cursor->rtr_info->tree_savepoints[RTR_MAX_LEVELS] =
mtr_set_savepoint(mtr);
}
latch_leaves.savepoints[0] = mtr_set_savepoint(mtr);
get_block =
btr_block_get(page_id_t(page_id.space(), left_page_no), page_size,
RW_X_LATCH, UT_LOCATION_HERE, cursor->index, mtr);
latch_leaves.blocks[0] = get_block;
if (spatial) {
cursor->rtr_info->tree_blocks[RTR_MAX_LEVELS] = get_block;
}
}
if (spatial) {
cursor->rtr_info->tree_savepoints[RTR_MAX_LEVELS + 1] =
mtr_set_savepoint(mtr);
}
latch_leaves.savepoints[1] = mtr_set_savepoint(mtr);
get_block = btr_block_get(page_id, page_size, RW_X_LATCH,
UT_LOCATION_HERE, cursor->index, mtr);
latch_leaves.blocks[1] = get_block;
#ifdef UNIV_BTR_DEBUG
/* Sanity check only after both the blocks are latched. */
if (latch_leaves.blocks[0] != nullptr) {
ut_a(page_is_comp(latch_leaves.blocks[0]->frame) == page_is_comp(page));
ut_a(btr_page_get_next(latch_leaves.blocks[0]->frame, mtr) ==
page_get_page_no(page));
}
ut_a(page_is_comp(get_block->frame) == page_is_comp(page));
#endif /* UNIV_BTR_DEBUG */
if (spatial) {
cursor->rtr_info->tree_blocks[RTR_MAX_LEVELS + 1] = get_block;
}
right_page_no = btr_page_get_next(page, mtr);
if (right_page_no != FIL_NULL) {
if (spatial) {
cursor->rtr_info->tree_savepoints[RTR_MAX_LEVELS + 2] =
mtr_set_savepoint(mtr);
}
latch_leaves.savepoints[2] = mtr_set_savepoint(mtr);
get_block =
btr_block_get(page_id_t(page_id.space(), right_page_no), page_size,
RW_X_LATCH, UT_LOCATION_HERE, cursor->index, mtr);
latch_leaves.blocks[2] = get_block;
#ifdef UNIV_BTR_DEBUG
ut_a(page_is_comp(get_block->frame) == page_is_comp(page));
ut_a(btr_page_get_prev(get_block->frame, mtr) ==
page_get_page_no(page));
#endif /* UNIV_BTR_DEBUG */
if (spatial) {
cursor->rtr_info->tree_blocks[RTR_MAX_LEVELS + 2] = get_block;
}
}
return (latch_leaves);
case BTR_SEARCH_PREV:
case BTR_MODIFY_PREV:
mode = latch_mode == BTR_SEARCH_PREV ? RW_S_LATCH : RW_X_LATCH;
/* latch also left sibling */
rw_lock_s_lock(&block->lock, UT_LOCATION_HERE);
left_page_no = btr_page_get_prev(page, mtr);
rw_lock_s_unlock(&block->lock);
if (left_page_no != FIL_NULL) {
latch_leaves.savepoints[0] = mtr_set_savepoint(mtr);
get_block =
btr_block_get(page_id_t(page_id.space(), left_page_no), page_size,
mode, UT_LOCATION_HERE, cursor->index, mtr);
latch_leaves.blocks[0] = get_block;
cursor->left_block = get_block;
#ifdef UNIV_BTR_DEBUG
ut_a(page_is_comp(get_block->frame) == page_is_comp(page));
ut_a(btr_page_get_next(get_block->frame, mtr) ==
page_get_page_no(page));
#endif /* UNIV_BTR_DEBUG */
}
latch_leaves.savepoints[1] = mtr_set_savepoint(mtr);
get_block = btr_block_get(page_id, page_size, mode, UT_LOCATION_HERE,
cursor->index, mtr);
latch_leaves.blocks[1] = get_block;
#ifdef UNIV_BTR_DEBUG
ut_a(page_is_comp(get_block->frame) == page_is_comp(page));
#endif /* UNIV_BTR_DEBUG */
return (latch_leaves);
case BTR_CONT_MODIFY_TREE:
ut_ad(dict_index_is_spatial(cursor->index));
return (latch_leaves);
}
ut_error;
}
/** Optimistically latches the leaf page or pages requested.
@param[in] block Guessed buffer block
@param[in] modify_clock Modify clock value
@param[in,out] latch_mode BTR_SEARCH_LEAF, ...
@param[in,out] cursor Cursor
@param[in] file File name
@param[in] line Line where called
@param[in] mtr Mini-transaction
@return true if success */
bool btr_cur_optimistic_latch_leaves(buf_block_t *block, uint64_t modify_clock,
ulint *latch_mode, btr_cur_t *cursor,
const char *file, ulint line, mtr_t *mtr) {
ulint mode;
page_no_t left_page_no;
ut_ad(block->page.buf_fix_count > 0);
ut_ad(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE);
switch (*latch_mode) {
case BTR_SEARCH_LEAF:
case BTR_MODIFY_LEAF:
return (buf_page_optimistic_get(*latch_mode, block, modify_clock,
cursor->m_fetch_mode, file, line, mtr));
case BTR_SEARCH_PREV:
case BTR_MODIFY_PREV:
mode = *latch_mode == BTR_SEARCH_PREV ? RW_S_LATCH : RW_X_LATCH;
rw_lock_s_lock(&block->lock, UT_LOCATION_HERE);
if (block->modify_clock != modify_clock) {
rw_lock_s_unlock(&block->lock);
return false;
}
left_page_no = btr_page_get_prev(buf_block_get_frame(block), mtr);
rw_lock_s_unlock(&block->lock);
if (left_page_no != FIL_NULL) {
const page_id_t page_id(dict_index_get_space(cursor->index),
left_page_no);
cursor->left_block = buf_page_get_gen(
page_id, dict_table_page_size(cursor->index->table), mode, nullptr,
Page_fetch::POSSIBLY_FREED, UT_LOCATION_HERE, mtr);
} else {
cursor->left_block = nullptr;
}
if (buf_page_optimistic_get(mode, block, modify_clock,
cursor->m_fetch_mode, file, line, mtr)) {
if (btr_page_get_prev(buf_block_get_frame(block), mtr) ==
left_page_no) {
/* We've entered this function with the block already buffer-fixed,
and buf_page_optimistic_get() buffer-fixes it again. The caller should
unfix the block once (to undo their buffer-fixing). */
ut_ad(2 <= block->page.buf_fix_count);
*latch_mode = mode;
return true;
} else {
/* release the block, which will also decrement the buf_fix_count once
undoing the increment in successful buf_page_optimistic_get() */
btr_leaf_page_release(block, mode, mtr);
}
}
/* If we are still here then buf_page_optimistic_get() did not buffer-fix
the page, but it should still be buffer-fixed as it was before the call.*/
ut_ad(0 < block->page.buf_fix_count);
/* release the left block */
if (cursor->left_block != nullptr) {
btr_leaf_page_release(cursor->left_block, mode, mtr);
}
return false;
default:
ut_error;
}
}
/**
Gets intention in btr_intention_t from latch_mode, and cleares the intention
at the latch_mode.
@param latch_mode in/out: pointer to latch_mode
@return intention for latching tree */
static btr_intention_t btr_cur_get_and_clear_intention(ulint *latch_mode) {
btr_intention_t intention;
switch (*latch_mode & (BTR_LATCH_FOR_INSERT | BTR_LATCH_FOR_DELETE)) {
case BTR_LATCH_FOR_INSERT:
intention = BTR_INTENTION_INSERT;
break;
case BTR_LATCH_FOR_DELETE:
intention = BTR_INTENTION_DELETE;
break;
default:
/* both or unknown */
intention = BTR_INTENTION_BOTH;
}
*latch_mode &= ~(BTR_LATCH_FOR_INSERT | BTR_LATCH_FOR_DELETE);
return (intention);
}
/**
Gets the desired latch type for the root leaf (root page is root leaf)
at the latch mode.
@param latch_mode in: BTR_SEARCH_LEAF, ...
@return latch type */
static rw_lock_type_t btr_cur_latch_for_root_leaf(ulint latch_mode) {
switch (latch_mode) {
case BTR_SEARCH_LEAF:
case BTR_SEARCH_TREE:
case BTR_SEARCH_PREV:
return (RW_S_LATCH);
case BTR_MODIFY_LEAF:
case BTR_MODIFY_TREE:
case BTR_MODIFY_PREV:
return (RW_X_LATCH);
case BTR_CONT_MODIFY_TREE:
case BTR_CONT_SEARCH_TREE:
/* A root page should be latched already,
and don't need to be latched here.
fall through (RW_NO_LATCH) */
case BTR_NO_LATCHES:
return (RW_NO_LATCH);
}
ut_error;
}
/** Detects whether the modifying record might need a modifying tree structure.
@param[in] index index
@param[in] page page
@param[in] lock_intention lock intention for the tree operation
@param[in] rec record (current node_ptr)
@param[in] rec_size size of the record or max size of node_ptr
@param[in] page_size page size
@param[in] mtr mtr
@return true if tree modification is needed */
static bool btr_cur_will_modify_tree(dict_index_t *index, const page_t *page,
btr_intention_t lock_intention,
const rec_t *rec, ulint rec_size,
const page_size_t &page_size, mtr_t *mtr) {
ut_ad(!page_is_leaf(page));
ut_ad(mtr_memo_contains_flagged(mtr, dict_index_get_lock(index),
MTR_MEMO_X_LOCK | MTR_MEMO_SX_LOCK) ||
index->table->is_intrinsic());
/* Pessimistic delete of the first record causes delete & insert
of node_ptr at upper level. And a subsequent page shrink is
possible. It causes delete of node_ptr at the upper level.
So we should pay attention also to 2nd record not only
first record and last record. Because if the "delete & insert" are
done for the different page, the 2nd record become
first record and following compress might delete the record and causes
the uppper level node_ptr modification. */
if (lock_intention <= BTR_INTENTION_BOTH) {
ulint margin;
if (lock_intention == BTR_INTENTION_BOTH) {
ulint level = btr_page_get_level(page);
/* This value is the worst expectation for the node_ptr records to be
deleted from this page. It is used to expect whether the cursor position
can be the left_most record in this page or not. */
ulint max_nodes_deleted = 0;
/* By modifying tree operations from the under of this level,
logically (2 ^ (level - 1)) opportunities to deleting records in maximum
even unreally rare case. */
if (level > 7) {
/* TODO: adjust this practical limit, if needed. */
max_nodes_deleted = 64;
} else if (level > 0) {
max_nodes_deleted = (ulint)1 << (level - 1);
}
/* check delete will cause. (BTR_INTENTION_BOTH
or BTR_INTENTION_DELETE) */
if (page_get_n_recs(page) <= max_nodes_deleted * 2 ||
page_rec_is_first(rec, page)) {
/* The cursor record can be the left_most record in this page. */
return (true);
}
if (fil_page_get_prev(page) != FIL_NULL &&
page_rec_distance_is_at_most(page_get_infimum_rec(page), rec,
max_nodes_deleted)) {
return (true);
}
if (fil_page_get_next(page) != FIL_NULL &&
page_rec_distance_is_at_most(rec, page_get_supremum_rec(page),
max_nodes_deleted)) {
return (true);
}
/* Delete at leftmost record in a page causes delete
& insert at its parent page. After that, the delete
might cause btr_compress() and delete record at its
parent page. Thus we should consider max deletes. */
margin = rec_size * max_nodes_deleted;
} else {
ut_ad(lock_intention == BTR_INTENTION_DELETE);
margin = rec_size;
}
/* Safe because we already have SX latch of the index tree */
if (page_get_data_size(page) <
margin + BTR_CUR_PAGE_COMPRESS_LIMIT(index) ||
(fil_page_get_next(page) == FIL_NULL &&
fil_page_get_prev(page) == FIL_NULL)) {
return (true);
}
}
if (lock_intention >= BTR_INTENTION_BOTH) {
/* check insert will cause. BTR_INTENTION_BOTH
or BTR_INTENTION_INSERT*/
/* Once we invoke the btr_cur_limit_optimistic_insert_debug,
we should check it here in advance, since the max allowable
records in a page is limited. */
LIMIT_OPTIMISTIC_INSERT_DEBUG(page_get_n_recs(page), return (true));
/* needs 2 records' space for the case the single split and
insert cannot fit.
page_get_max_insert_size_after_reorganize() includes space
for page directory already */
ulint max_size = page_get_max_insert_size_after_reorganize(page, 2);
if (max_size < BTR_CUR_PAGE_REORGANIZE_LIMIT + rec_size ||
max_size < rec_size * 2) {
return (true);
}
/* TODO: optimize this condition for compressed page.
this is based on the worst compress rate.
currently looking only uncompressed page, but we can look
also compressed page page_zip_available() if already in the
buffer pool */
/* needs 2 records' space also for worst compress rate. */
if (page_size.is_compressed() &&
page_zip_empty_size(index->n_fields, page_size.physical()) <
rec_size * 2 + page_get_data_size(page) +
page_dir_calc_reserved_space(page_get_n_recs(page) + 2) + 1) {
return (true);
}
}
return (false);
}
/** Detects whether the modifying record might need a opposite modification
to the intention.
@param[in] page page
@param[in] lock_intention lock intention for the tree operation
@param[in] rec record (current node_ptr)
@return true if tree modification is needed */
static bool btr_cur_need_opposite_intention(const page_t *page,
btr_intention_t lock_intention,
const rec_t *rec) {
switch (lock_intention) {
case BTR_INTENTION_DELETE:
return ((mach_read_from_4(page + FIL_PAGE_PREV) != FIL_NULL &&
page_rec_is_first(rec, page)) ||
(mach_read_from_4(page + FIL_PAGE_NEXT) != FIL_NULL &&
page_rec_is_last(rec, page)));
case BTR_INTENTION_INSERT:
return (mach_read_from_4(page + FIL_PAGE_NEXT) != FIL_NULL &&
page_rec_is_last(rec, page));
case BTR_INTENTION_BOTH:
return (false);
}
ut_error;
}
/** Searches an index tree and positions a tree cursor on a given level.
NOTE: n_fields_cmp in tuple must be set so that it cannot be compared
to node pointer page number fields on the upper levels of the tree!
Note that if mode is PAGE_CUR_LE, which is used in inserts, then
cursor->up_match and cursor->low_match both will have sensible values.
If mode is PAGE_CUR_GE, then up_match will a have a sensible value.
If mode is PAGE_CUR_LE , cursor is left at the place where an insert of the
search tuple should be performed in the B-tree. InnoDB does an insert
immediately after the cursor. Thus, the cursor may end up on a user record,
or on a page infimum record. */
void btr_cur_search_to_nth_level(
dict_index_t *index, /*!< in: index */
ulint level, /*!< in: the tree level of search */
const dtuple_t *tuple, /*!< in: data tuple; NOTE: n_fields_cmp in
tuple must be set so that it cannot get
compared to the node ptr page number field! */
page_cur_mode_t mode, /*!< in: PAGE_CUR_L, ...;
Inserts should always be made using
PAGE_CUR_LE to search the position! */
ulint latch_mode, /*!< in: BTR_SEARCH_LEAF, ..., ORed with
at most one of BTR_INSERT, BTR_DELETE_MARK,
BTR_DELETE, or BTR_ESTIMATE;
cursor->left_block is used to store a pointer
to the left neighbor page, in the cases
BTR_SEARCH_PREV and BTR_MODIFY_PREV;
NOTE that if has_search_latch
is != 0, we maybe do not have a latch set
on the cursor page, we assume
the caller uses his search latch
to protect the record! */
btr_cur_t *cursor, /*!< in/out: tree cursor; the cursor page is
s- or x-latched, but see also above! */
ulint has_search_latch,
/*!< in: info on the latch mode the
caller currently has on search system:
RW_S_LATCH, or 0 */
const char *file, /*!< in: file name */
ulint line, /*!< in: line where called */
mtr_t *mtr) /*!< in: mtr */
{
page_t *page = nullptr; /* remove warning */
buf_block_t *block;
ulint height;
ulint up_match;
ulint up_bytes;
ulint low_match;
ulint low_bytes;
ulint savepoint;
ulint rw_latch;
page_cur_mode_t page_mode;
page_cur_mode_t search_mode = PAGE_CUR_UNSUPP;
Page_fetch fetch;
ulint node_ptr_max_size = UNIV_PAGE_SIZE / 2;
page_cur_t *page_cursor;
btr_op_t btr_op;
ulint root_height = 0; /* remove warning */
ulint upper_rw_latch, root_leaf_rw_latch;
btr_intention_t lock_intention;
bool modify_external;
buf_block_t *tree_blocks[BTR_MAX_LEVELS];
ulint tree_savepoints[BTR_MAX_LEVELS];
ulint n_blocks = 0;
ulint n_releases = 0;
bool detected_same_key_root = false;
bool retrying_for_search_prev = false;
ulint leftmost_from_level = 0;
buf_block_t **prev_tree_blocks = nullptr;
ulint *prev_tree_savepoints = nullptr;
ulint prev_n_blocks = 0;
ulint prev_n_releases = 0;
bool need_path = true;
bool rtree_parent_modified = false;
bool mbr_adj = false;
bool found = false;
DBUG_TRACE;
btr_search_t *info;
mem_heap_t *heap = nullptr;
ulint offsets_[REC_OFFS_NORMAL_SIZE];
ulint *offsets = offsets_;
ulint offsets2_[REC_OFFS_NORMAL_SIZE];
ulint *offsets2 = offsets2_;
rec_offs_init(offsets_);
rec_offs_init(offsets2_);
/* Currently, PAGE_CUR_LE is the only search mode used for searches
ending to upper levels */
ut_ad(level == 0 || mode == PAGE_CUR_LE || RTREE_SEARCH_MODE(mode));
ut_ad(dict_index_check_search_tuple(index, tuple));
ut_ad(!dict_index_is_ibuf(index) || ibuf_inside(mtr));
UNIV_MEM_INVALID(&cursor->up_match, sizeof cursor->up_match);
UNIV_MEM_INVALID(&cursor->up_bytes, sizeof cursor->up_bytes);
UNIV_MEM_INVALID(&cursor->low_match, sizeof cursor->low_match);
UNIV_MEM_INVALID(&cursor->low_bytes, sizeof cursor->low_bytes);
#ifdef UNIV_DEBUG
cursor->up_match = ULINT_UNDEFINED;
cursor->low_match = ULINT_UNDEFINED;
#endif /* UNIV_DEBUG */
bool s_latch_by_caller = latch_mode & BTR_ALREADY_S_LATCHED;
latch_mode &= ~BTR_ALREADY_S_LATCHED;
ut_ad(!s_latch_by_caller || srv_read_only_mode ||
mtr_memo_contains_flagged(mtr, dict_index_get_lock(index),
MTR_MEMO_S_LOCK | MTR_MEMO_SX_LOCK));
/* These flags are mutually exclusive, they are lumped together
with the latch mode for historical reasons. It's possible for
none of the flags to be set. */
switch (UNIV_EXPECT(latch_mode & (BTR_INSERT | BTR_DELETE | BTR_DELETE_MARK),
0)) {
case 0:
btr_op = BTR_NO_OP;
break;
case BTR_INSERT:
btr_op = (latch_mode & BTR_IGNORE_SEC_UNIQUE)
? BTR_INSERT_IGNORE_UNIQUE_OP
: BTR_INSERT_OP;
break;
case BTR_DELETE:
btr_op = BTR_DELETE_OP;
ut_a(cursor->purge_node);
break;
case BTR_DELETE_MARK:
btr_op = BTR_DELMARK_OP;
break;
default:
/* only one of BTR_INSERT, BTR_DELETE, BTR_DELETE_MARK
should be specified at a time */
ut_error;
}
/* Operations on the insert buffer tree cannot be buffered. */
ut_ad(btr_op == BTR_NO_OP || !dict_index_is_ibuf(index));
/* Operations on the clustered index cannot be buffered. */
ut_ad(btr_op == BTR_NO_OP || !index->is_clustered());
/* Operations on the temporary table(indexes) cannot be buffered. */
ut_ad(btr_op == BTR_NO_OP || !index->table->is_temporary());
/* Operation on the spatial index cannot be buffered. */
ut_ad(btr_op == BTR_NO_OP || !dict_index_is_spatial(index));
auto estimate = latch_mode & BTR_ESTIMATE;
lock_intention = btr_cur_get_and_clear_intention(&latch_mode);
modify_external = latch_mode & BTR_MODIFY_EXTERNAL;
/* Turn the flags unrelated to the latch mode off. */
latch_mode = BTR_LATCH_MODE_WITHOUT_FLAGS(latch_mode);
ut_ad(!modify_external || latch_mode == BTR_MODIFY_LEAF);
ut_ad(!s_latch_by_caller || latch_mode == BTR_SEARCH_LEAF ||
latch_mode == BTR_SEARCH_TREE || latch_mode == BTR_MODIFY_LEAF);
cursor->flag = BTR_CUR_BINARY;
cursor->index = index;
info = btr_search_get_info(index);
#ifdef UNIV_SEARCH_PERF_STAT
info->n_searches++;
#endif
/* Use of AHI is disabled for intrinsic table as these tables re-use
the index-id and AHI validation is based on index-id. */
if (rw_lock_get_writer(btr_get_search_latch(index)) == RW_LOCK_NOT_LOCKED &&
latch_mode <= BTR_MODIFY_LEAF && info->last_hash_succ &&
!index->disable_ahi && !estimate
#ifdef PAGE_CUR_LE_OR_EXTENDS
&& mode != PAGE_CUR_LE_OR_EXTENDS
#endif /* PAGE_CUR_LE_OR_EXTENDS */
&& !dict_index_is_spatial(index)
/* If !has_search_latch, we do a dirty read of
btr_search_enabled below, and btr_search_guess_on_hash()
will have to check it again. */
&& UNIV_LIKELY(btr_search_enabled) && !modify_external &&
btr_search_guess_on_hash(index, info, tuple, mode, latch_mode, cursor,
has_search_latch, mtr)) {
/* Search using the hash index succeeded */
ut_ad(cursor->up_match != ULINT_UNDEFINED || mode != PAGE_CUR_GE);
ut_ad(cursor->up_match != ULINT_UNDEFINED || mode != PAGE_CUR_LE);
ut_ad(cursor->low_match != ULINT_UNDEFINED || mode != PAGE_CUR_LE);
btr_cur_n_sea++;
return;
}
btr_cur_n_non_sea++;
DBUG_EXECUTE_IF("non_ahi_search",
assert(!strcmp(index->table->name.m_name, "test/t1")););
/* If the hash search did not succeed, do binary search down the
tree */
if (has_search_latch) {
/* Release possible search latch to obey latching order */
rw_lock_s_unlock(btr_get_search_latch(index));
}
/* Store the position of the tree latch we push to mtr so that we
know how to release it when we have latched leaf node(s) */
savepoint = mtr_set_savepoint(mtr);
switch (latch_mode) {
case BTR_MODIFY_TREE:
/* Most of delete-intended operations are purging.
Free blocks and read IO bandwidth should be prior
for them, when the history list is glowing huge. */
if (lock_intention == BTR_INTENTION_DELETE &&
trx_sys->rseg_history_len.load() > BTR_CUR_FINE_HISTORY_LENGTH &&
buf_get_n_pending_read_ios()) {
mtr_x_lock(dict_index_get_lock(index), mtr, UT_LOCATION_HERE);
} else if (dict_index_is_spatial(index) &&
lock_intention <= BTR_INTENTION_BOTH) {
/* X lock the if there is possibility of
pessimistic delete on spatial index. As we could
lock upward for the tree */
mtr_x_lock(dict_index_get_lock(index), mtr, UT_LOCATION_HERE);
} else {
mtr_sx_lock(dict_index_get_lock(index), mtr, UT_LOCATION_HERE);
}
upper_rw_latch = RW_X_LATCH;
break;
case BTR_CONT_MODIFY_TREE:
case BTR_CONT_SEARCH_TREE:
/* Do nothing */
ut_ad(srv_read_only_mode ||
mtr_memo_contains_flagged(mtr, dict_index_get_lock(index),
MTR_MEMO_X_LOCK | MTR_MEMO_SX_LOCK));
if (dict_index_is_spatial(index) && latch_mode == BTR_CONT_MODIFY_TREE) {
/* If we are about to locating parent page for split
and/or merge operation for R-Tree index, X latch
the parent */
upper_rw_latch = RW_X_LATCH;
} else {
upper_rw_latch = RW_NO_LATCH;
}
break;
default:
if (!srv_read_only_mode) {
if (s_latch_by_caller) {
/* The BTR_ALREADY_S_LATCHED indicates that the index->lock has been
taken either in RW_S_LATCH or RW_SX_LATCH mode. For parallel reads
another thread can own the dict index lock. */
ut_ad(rw_lock_own_flagged(dict_index_get_lock(index),
RW_LOCK_FLAG_S | RW_LOCK_FLAG_SX));
} else if (!modify_external) {
/* BTR_SEARCH_TREE is intended to be used with
BTR_ALREADY_S_LATCHED */
ut_ad(latch_mode != BTR_SEARCH_TREE);
mtr_s_lock(dict_index_get_lock(index), mtr, UT_LOCATION_HERE);
} else {
/* BTR_MODIFY_EXTERNAL needs to be excluded */
mtr_sx_lock(dict_index_get_lock(index), mtr, UT_LOCATION_HERE);
}
upper_rw_latch = RW_S_LATCH;
} else {
upper_rw_latch = RW_NO_LATCH;
}
}
root_leaf_rw_latch = btr_cur_latch_for_root_leaf(latch_mode);
page_cursor = btr_cur_get_page_cur(cursor);
const space_id_t space = dict_index_get_space(index);
const page_size_t page_size(dict_table_page_size(index->table));
/* Start with the root page. */
page_id_t page_id(space, dict_index_get_page(index));
if (root_leaf_rw_latch == RW_X_LATCH) {
node_ptr_max_size = dict_index_node_ptr_max_size(index);
}
up_match = 0;
up_bytes = 0;
low_match = 0;
low_bytes = 0;
height = ULINT_UNDEFINED;
/* We use these modified search modes on non-leaf levels of the
B-tree. These let us end up in the right B-tree leaf. In that leaf
we use the original search mode. */
switch (mode) {
case PAGE_CUR_GE:
page_mode = PAGE_CUR_L;
break;
case PAGE_CUR_G:
page_mode = PAGE_CUR_LE;
break;
default:
#ifdef PAGE_CUR_LE_OR_EXTENDS
ut_ad(mode == PAGE_CUR_L || mode == PAGE_CUR_LE ||
RTREE_SEARCH_MODE(mode) || mode == PAGE_CUR_LE_OR_EXTENDS);
#else /* PAGE_CUR_LE_OR_EXTENDS */
ut_ad(mode == PAGE_CUR_L || mode == PAGE_CUR_LE ||
RTREE_SEARCH_MODE(mode));
#endif /* PAGE_CUR_LE_OR_EXTENDS */
page_mode = mode;
break;
}
/* Loop and search until we arrive at the desired level */
btr_latch_leaves_t latch_leaves = {{nullptr, nullptr, nullptr}, {0, 0, 0}};
search_loop:
fetch = cursor->m_fetch_mode;
rw_latch = RW_NO_LATCH;
rtree_parent_modified = false;
if (height != 0) {
/* We are about to fetch the root or a non-leaf page. */
if ((latch_mode != BTR_MODIFY_TREE || height == level) &&
!retrying_for_search_prev) {
/* If doesn't have SX or X latch of index,
each pages should be latched before reading. */
if (modify_external && height == ULINT_UNDEFINED &&
upper_rw_latch == RW_S_LATCH) {
/* needs sx-latch of root page
for fseg operation */
rw_latch = RW_SX_LATCH;
} else {
rw_latch = upper_rw_latch;
}
}
} else if (latch_mode <= BTR_MODIFY_LEAF) {
rw_latch = latch_mode;
if (btr_op != BTR_NO_OP &&
ibuf_should_try(index, btr_op != BTR_INSERT_OP)) {
/* Try to buffer the operation if the leaf
page is not in the buffer pool. */
fetch = btr_op == BTR_DELETE_OP ? Page_fetch::IF_IN_POOL_OR_WATCH
: Page_fetch::IF_IN_POOL;
}
}
retry_page_get:
ut_ad(n_blocks < BTR_MAX_LEVELS);
tree_savepoints[n_blocks] = mtr_set_savepoint(mtr);
block =
buf_page_get_gen(page_id, page_size, rw_latch,
(height == ULINT_UNDEFINED ? info->root_guess : nullptr),
fetch, {file, line}, mtr);
tree_blocks[n_blocks] = block;
if (block == nullptr) {
/* This must be a search to perform an insert/delete
mark/ delete; try using the insert/delete buffer */
ut_ad(height == 0);
ut_ad(cursor->thr);
switch (btr_op) {
case BTR_INSERT_OP:
case BTR_INSERT_IGNORE_UNIQUE_OP:
ut_ad(fetch == Page_fetch::IF_IN_POOL);
ut_ad(!dict_index_is_spatial(index));
if (ibuf_insert(IBUF_OP_INSERT, tuple, index, page_id, page_size,
cursor->thr)) {
cursor->flag = BTR_CUR_INSERT_TO_IBUF;
goto func_exit;
}
break;
case BTR_DELMARK_OP:
ut_ad(fetch == Page_fetch::IF_IN_POOL);
ut_ad(!dict_index_is_spatial(index));
if (ibuf_insert(IBUF_OP_DELETE_MARK, tuple, index, page_id, page_size,
cursor->thr)) {
cursor->flag = BTR_CUR_DEL_MARK_IBUF;
goto func_exit;
}
break;