Skip to content

Commit 45e4b3e

Browse files
committed
Make LockingIterator work when rocksdb_enable_iterate_bounds=off
This is a followup to: Address review input: lock the "gap" after the last row that was read Made the code work when iterator bounds are not available, in which case it locks till index start/end.
1 parent 5a5c473 commit 45e4b3e

10 files changed

+200
-64
lines changed

mysql-test/suite/rocksdb/r/range_locking_seek_for_update2.result

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,54 @@ ERROR HY000: Lock wait timeout exceeded; try restarting transaction: Timeout on
8989
disconnect con1;
9090
connection default;
9191
drop table t1;
92+
set global rocksdb_enable_iterate_bounds=off;
93+
#
94+
# A testcase for locking at the end of the scan
95+
#
96+
create table t1 (
97+
pk int,
98+
primary key (pk) comment 'rlsfu_test'
99+
) engine=rocksdb;
100+
connect con1,localhost,root,,;
101+
connection con1;
102+
insert into t1 values (1), (10), (100);
103+
begin;
104+
select * from t1 for update;
105+
pk
106+
1
107+
10
108+
100
109+
connection default;
110+
select * from t1;
111+
pk
112+
1
113+
10
114+
100
115+
insert into t1 values (150);
116+
ERROR HY000: Lock wait timeout exceeded; try restarting transaction: Timeout on index: test.t1.PRIMARY
117+
connection con1;
118+
rollback;
119+
begin;
120+
explain
121+
select * from t1 order by pk desc for update;
122+
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
123+
1 SIMPLE t1 NULL index NULL PRIMARY 4 NULL # 100.00 Backward index scan; Using index
124+
Warnings:
125+
Note 1003 /* select#1 */ select `test`.`t1`.`pk` AS `pk` from `test`.`t1` order by `test`.`t1`.`pk` desc
126+
select * from t1 order by pk desc for update;
127+
pk
128+
100
129+
10
130+
1
131+
connection default;
132+
select * from t1;
133+
pk
134+
1
135+
10
136+
100
137+
insert into t1 values (0);
138+
ERROR HY000: Lock wait timeout exceeded; try restarting transaction: Timeout on index: test.t1.PRIMARY
139+
disconnect con1;
140+
connection default;
141+
drop table t1;
142+
set global rocksdb_enable_iterate_bounds=on;

mysql-test/suite/rocksdb/r/range_locking_seek_for_update2_rev_cf.result

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,54 @@ ERROR HY000: Lock wait timeout exceeded; try restarting transaction: Timeout on
8989
disconnect con1;
9090
connection default;
9191
drop table t1;
92+
set global rocksdb_enable_iterate_bounds=off;
93+
#
94+
# A testcase for locking at the end of the scan
95+
#
96+
create table t1 (
97+
pk int,
98+
primary key (pk) comment 'rev:rlsfu_test'
99+
) engine=rocksdb;
100+
connect con1,localhost,root,,;
101+
connection con1;
102+
insert into t1 values (1), (10), (100);
103+
begin;
104+
select * from t1 for update;
105+
pk
106+
1
107+
10
108+
100
109+
connection default;
110+
select * from t1;
111+
pk
112+
1
113+
10
114+
100
115+
insert into t1 values (150);
116+
ERROR HY000: Lock wait timeout exceeded; try restarting transaction: Timeout on index: test.t1.PRIMARY
117+
connection con1;
118+
rollback;
119+
begin;
120+
explain
121+
select * from t1 order by pk desc for update;
122+
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
123+
1 SIMPLE t1 NULL index NULL PRIMARY 4 NULL # 100.00 Backward index scan; Using index
124+
Warnings:
125+
Note 1003 /* select#1 */ select `test`.`t1`.`pk` AS `pk` from `test`.`t1` order by `test`.`t1`.`pk` desc
126+
select * from t1 order by pk desc for update;
127+
pk
128+
100
129+
10
130+
1
131+
connection default;
132+
select * from t1;
133+
pk
134+
1
135+
10
136+
100
137+
insert into t1 values (0);
138+
ERROR HY000: Lock wait timeout exceeded; try restarting transaction: Timeout on index: test.t1.PRIMARY
139+
disconnect con1;
140+
connection default;
141+
drop table t1;
142+
set global rocksdb_enable_iterate_bounds=on;

mysql-test/suite/rocksdb/t/range_locking_seek_for_update2.inc

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -48,43 +48,8 @@ disconnect con1;
4848
connection default;
4949
drop table t0, t1;
5050

51-
--echo #
52-
--echo # A testcase for locking at the end of the scan
53-
--echo #
54-
eval create table t1 (
55-
pk int,
56-
primary key (pk) comment '$cf'
57-
) engine=rocksdb;
58-
59-
connect (con1,localhost,root,,);
60-
connection con1;
61-
62-
insert into t1 values (1), (10), (100);
63-
64-
begin;
65-
select * from t1 for update;
66-
67-
connection default;
68-
select * from t1;
51+
--source range_locking_seek_for_update_iter_end.inc
52+
set global rocksdb_enable_iterate_bounds=off;
53+
--source range_locking_seek_for_update_iter_end.inc
54+
set global rocksdb_enable_iterate_bounds=on;
6955

70-
--error ER_LOCK_WAIT_TIMEOUT
71-
insert into t1 values (150);
72-
73-
connection con1;
74-
rollback;
75-
76-
begin;
77-
--replace_column 10 #
78-
explain
79-
select * from t1 order by pk desc for update;
80-
select * from t1 order by pk desc for update;
81-
82-
connection default;
83-
select * from t1;
84-
85-
--error ER_LOCK_WAIT_TIMEOUT
86-
insert into t1 values (0);
87-
88-
disconnect con1;
89-
connection default;
90-
drop table t1;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
--echo #
2+
--echo # A testcase for locking at the end of the scan
3+
--echo #
4+
eval create table t1 (
5+
pk int,
6+
primary key (pk) comment '$cf'
7+
) engine=rocksdb;
8+
9+
connect (con1,localhost,root,,);
10+
connection con1;
11+
12+
insert into t1 values (1), (10), (100);
13+
14+
begin;
15+
select * from t1 for update;
16+
17+
connection default;
18+
select * from t1;
19+
20+
--error ER_LOCK_WAIT_TIMEOUT
21+
insert into t1 values (150);
22+
23+
connection con1;
24+
rollback;
25+
26+
begin;
27+
--replace_column 10 #
28+
explain
29+
select * from t1 order by pk desc for update;
30+
select * from t1 order by pk desc for update;
31+
32+
connection default;
33+
select * from t1;
34+
35+
--error ER_LOCK_WAIT_TIMEOUT
36+
insert into t1 values (0);
37+
38+
disconnect con1;
39+
connection default;
40+
drop table t1;
41+

storage/rocksdb/ha_rocksdb.cc

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3857,7 +3857,7 @@ class Rdb_transaction {
38573857
virtual rocksdb::Iterator *get_iterator(
38583858
const rocksdb::ReadOptions &options,
38593859
rocksdb::ColumnFamilyHandle *column_family,
3860-
bool is_rev_cf,
3860+
const std::shared_ptr<Rdb_key_def>&,
38613861
bool use_locking_iterator=false) = 0;
38623862

38633863
virtual void multi_get(rocksdb::ColumnFamilyHandle *const column_family,
@@ -3867,7 +3867,8 @@ class Rdb_transaction {
38673867
const bool sorted_input) const = 0;
38683868

38693869
rocksdb::Iterator *get_iterator(
3870-
rocksdb::ColumnFamilyHandle *const column_family, bool is_rev_cf,
3870+
rocksdb::ColumnFamilyHandle *const column_family,
3871+
const std::shared_ptr<Rdb_key_def>& kd,
38713872
bool skip_bloom_filter,
38723873
const rocksdb::Slice &eq_cond_lower_bound,
38733874
const rocksdb::Slice &eq_cond_upper_bound, bool read_current = false,
@@ -3902,8 +3903,7 @@ class Rdb_transaction {
39023903
if (read_current) {
39033904
options.snapshot = nullptr;
39043905
}
3905-
return get_iterator(options, column_family, is_rev_cf,
3906-
use_locking_iterator);
3906+
return get_iterator(options, column_family, kd, use_locking_iterator);
39073907
}
39083908

39093909
virtual bool is_tx_started() const = 0;
@@ -4377,13 +4377,13 @@ class Rdb_transaction_impl : public Rdb_transaction {
43774377
rocksdb::Iterator *get_iterator(
43784378
const rocksdb::ReadOptions &options,
43794379
rocksdb::ColumnFamilyHandle *const column_family,
4380-
bool is_rev_cf,
4380+
const std::shared_ptr<Rdb_key_def>& kd,
43814381
bool use_locking_iterator) override {
43824382
global_stats.queries[QUERIES_RANGE].inc();
43834383
if (use_locking_iterator) {
43844384
locking_iter_created();
43854385
return GetLockingIterator(m_rocksdb_tx, options, column_family,
4386-
is_rev_cf, &m_row_lock_count);
4386+
kd, &m_row_lock_count);
43874387
}
43884388
else
43894389
return m_rocksdb_tx->GetIterator(options, column_family);
@@ -4723,7 +4723,7 @@ class Rdb_writebatch_impl : public Rdb_transaction {
47234723
rocksdb::Iterator *get_iterator(
47244724
const rocksdb::ReadOptions &options,
47254725
rocksdb::ColumnFamilyHandle *const /* column_family */,
4726-
bool /*is_rev_cf*/,
4726+
const std::shared_ptr<Rdb_key_def>&,
47274727
bool /*use_locking_iterator*/) override {
47284728
const auto it = rdb->NewIterator(options);
47294729
return m_batch->NewIteratorWithBase(it);
@@ -16839,17 +16839,19 @@ const rocksdb::ReadOptions &rdb_tx_acquire_snapshot(Rdb_transaction *tx) {
1683916839

1684016840
rocksdb::Iterator *rdb_tx_get_iterator(
1684116841
Rdb_transaction *tx, rocksdb::ColumnFamilyHandle *const column_family,
16842-
bool is_rev_cf,
16842+
const std::shared_ptr<Rdb_key_def> &kd,
1684316843
bool skip_bloom_filter, const rocksdb::Slice &lower_bound_slice,
1684416844
const rocksdb::Slice &upper_bound_slice, bool read_current,
1684516845
bool create_snapshot) {
16846-
return tx->get_iterator(column_family, is_rev_cf, skip_bloom_filter, lower_bound_slice,
16846+
return tx->get_iterator(column_family, kd, skip_bloom_filter, lower_bound_slice,
1684716847
upper_bound_slice, read_current, create_snapshot);
1684816848
}
1684916849

1685016850

1685116851
rocksdb::Iterator *rdb_tx_get_iterator(
16852-
THD *thd, rocksdb::ColumnFamilyHandle *const cf, bool is_rev_cf, bool skip_bloom_filter,
16852+
THD *thd, rocksdb::ColumnFamilyHandle *const cf,
16853+
const std::shared_ptr<Rdb_key_def> &kd,
16854+
bool skip_bloom_filter,
1685316855
const rocksdb::Slice &eq_cond_lower_bound,
1685416856
const rocksdb::Slice &eq_cond_upper_bound,
1685516857
const rocksdb::Snapshot **snapshot, bool read_current,
@@ -16868,7 +16870,7 @@ rocksdb::Iterator *rdb_tx_get_iterator(
1686816870
}
1686916871
} else {
1687016872
Rdb_transaction *tx = get_tx_from_thd(thd);
16871-
return tx->get_iterator(cf, is_rev_cf, skip_bloom_filter, eq_cond_lower_bound,
16873+
return tx->get_iterator(cf, kd, skip_bloom_filter, eq_cond_lower_bound,
1687216874
eq_cond_upper_bound, read_current, create_snapshot, use_locking_iter);
1687316875
}
1687416876
}

storage/rocksdb/ha_rocksdb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,7 @@ const rocksdb::ReadOptions &rdb_tx_acquire_snapshot(Rdb_transaction *tx);
11091109

11101110
rocksdb::Iterator *rdb_tx_get_iterator(
11111111
THD *thd, rocksdb::ColumnFamilyHandle *const cf,
1112-
bool is_rev_cf,
1112+
const std::shared_ptr<Rdb_key_def> &kd,
11131113
bool skip_bloom_filter,
11141114
const rocksdb::Slice &eq_cond_lower_bound,
11151115
const rocksdb::Slice &eq_cond_upper_bound,

storage/rocksdb/nosql_access.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -677,11 +677,11 @@ class select_exec {
677677
}
678678

679679
rocksdb::Iterator *get_iterator(rocksdb::ColumnFamilyHandle *cf,
680-
bool is_rev_cf,
680+
const std::shared_ptr<Rdb_key_def> &kd,
681681
bool use_bloom,
682682
const rocksdb::Slice &lower_bound,
683683
const rocksdb::Slice &upper_bound) {
684-
return rdb_tx_get_iterator(m_thd, cf, is_rev_cf, !use_bloom, lower_bound, //psergey-mergey-todo: or m_tx ?
684+
return rdb_tx_get_iterator(m_thd, cf, kd, !use_bloom, lower_bound,
685685
upper_bound, nullptr);
686686
}
687687

@@ -1519,7 +1519,7 @@ bool INLINE_ATTR select_exec::setup_iterator(THD *thd) {
15191519
} else {
15201520
m_iterator.reset(
15211521
new Rdb_iterator_base(thd, m_key_def, m_pk_def, m_tbl_def));
1522-
m_lower_bound_slice, m_upper_bound_slice);
1522+
// m_lower_bound_slice, m_upper_bound_slice);
15231523
}
15241524

15251525
return m_iterator == nullptr;

storage/rocksdb/rdb_iterator.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ void Rdb_iterator_base::setup_scan_iterator(const rocksdb::Slice *const slice,
170170
*/
171171
if (!m_scan_it) {
172172
m_scan_it = rdb_tx_get_iterator(
173-
m_thd, m_kd->get_cf(), m_kd->m_is_reverse_cf, skip_bloom,
173+
m_thd, m_kd->get_cf(), m_kd, skip_bloom,
174174
m_scan_it_lower_bound_slice,
175175
m_scan_it_upper_bound_slice, &m_scan_it_snapshot, read_current,
176176
!read_current, m_iter_should_use_locking);

storage/rocksdb/rdb_locking_iter.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ rocksdb::Iterator* GetLockingIterator(
1414
rocksdb::Transaction *trx,
1515
const rocksdb::ReadOptions& read_options,
1616
rocksdb::ColumnFamilyHandle* column_family,
17-
bool is_rev_cf,
17+
const std::shared_ptr<Rdb_key_def> &kd,
1818
ulonglong *counter) {
19-
return new LockingIterator(trx, column_family, is_rev_cf, read_options,
19+
return new LockingIterator(trx, column_family, kd, read_options,
2020
counter);
2121
}
2222

0 commit comments

Comments
 (0)