Skip to content

Commit

Permalink
add option to disable lookup read lock (vitessio#11538)
Browse files Browse the repository at this point in the history
DML to lookup VIndexes unconditionally takes a row lock on rows in the
lookup VIndex backing table. Add an option to optionally elide this lock
for cases where we know via business logic that the row will not be
deleted, nor the lookup column changed.

Signed-off-by: Max Englander <max@planetscale.com>

Signed-off-by: Max Englander <max@planetscale.com>
  • Loading branch information
maxenglander authored and arthurschreiber committed Oct 10, 2023
1 parent 66015fd commit 8603fda
Show file tree
Hide file tree
Showing 8 changed files with 676 additions and 88 deletions.
175 changes: 131 additions & 44 deletions go/test/endtoend/vtgate/vindex_bindvars/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ var (
field BIGINT NOT NULL,
field2 BIGINT,
field3 BIGINT,
field4 BIGINT,
field5 BIGINT,
field6 BIGINT,
PRIMARY KEY (id)
) ENGINE=Innodb;
Expand All @@ -63,6 +66,24 @@ CREATE TABLE lookup3 (
UNIQUE KEY (field3)
) ENGINE=Innodb;
CREATE TABLE lookup4 (
field4 BIGINT NOT NULL,
keyspace_id binary(8),
UNIQUE KEY (field4)
) ENGINE=Innodb;
CREATE TABLE lookup5 (
field5 BIGINT NOT NULL,
keyspace_id binary(8),
UNIQUE KEY (field5)
) ENGINE=Innodb;
CREATE TABLE lookup6 (
field6 BIGINT NOT NULL,
keyspace_id binary(8),
UNIQUE KEY (field6)
) ENGINE=Innodb;
CREATE TABLE thex (
id VARBINARY(64) NOT NULL,
field BIGINT NOT NULL,
Expand Down Expand Up @@ -118,6 +139,36 @@ CREATE TABLE thex (
"to": "keyspace_id"
},
"owner": "t1"
},
"lookup4": {
"type": "lookup",
"params": {
"from": "field4",
"read_lock": "exclusive",
"table": "lookup4",
"to": "keyspace_id"
},
"owner": "t1"
},
"lookup5": {
"type": "lookup",
"params": {
"from": "field5",
"read_lock": "shared",
"table": "lookup5",
"to": "keyspace_id"
},
"owner": "t1"
},
"lookup6": {
"type": "lookup",
"params": {
"from": "field6",
"read_lock": "none",
"table": "lookup6",
"to": "keyspace_id"
},
"owner": "t1"
}
},
"tables": {
Expand All @@ -138,6 +189,18 @@ CREATE TABLE thex (
{
"column": "field3",
"name": "lookup3"
},
{
"column": "field4",
"name": "lookup4"
},
{
"column": "field5",
"name": "lookup5"
},
{
"column": "field6",
"name": "lookup6"
}
]
},
Expand Down Expand Up @@ -165,6 +228,30 @@ CREATE TABLE thex (
}
]
},
"lookup4": {
"column_vindexes": [
{
"column": "field4",
"name": "binary_md5_vdx"
}
]
},
"lookup5": {
"column_vindexes": [
{
"column": "field5",
"name": "binary_md5_vdx"
}
]
},
"lookup6": {
"column_vindexes": [
{
"column": "field6",
"name": "binary_md5_vdx"
}
]
},
"thex": {
"column_vindexes": [
{
Expand Down Expand Up @@ -245,51 +332,51 @@ func TestVindexBindVarOverlap(t *testing.T) {
require.Nil(t, err)
defer conn.Close()

utils.Exec(t, conn, "INSERT INTO t1 (id, field, field2, field3) VALUES "+
"(0,1,2,3), "+
"(1,2,3,4), "+
"(2,3,4,5), "+
"(3,4,5,6), "+
"(4,5,6,7), "+
"(5,6,7,8), "+
"(6,7,8,9), "+
"(7,8,9,10), "+
"(8,9,10,11), "+
"(9,10,11,12), "+
"(10,11,12,13), "+
"(11,12,13,14), "+
"(12,13,14,15), "+
"(13,14,15,16), "+
"(14,15,16,17), "+
"(15,16,17,18), "+
"(16,17,18,19), "+
"(17,18,19,20), "+
"(18,19,20,21), "+
"(19,20,21,22), "+
"(20,21,22,23)")
result := utils.Exec(t, conn, "select id, field, field2, field3 from t1 order by id")
utils.Exec(t, conn, "INSERT INTO t1 (id, field, field2, field3, field4, field5, field6) VALUES "+
"(0,1,2,3,4,5,6), "+
"(1,2,3,4,5,6,7), "+
"(2,3,4,5,6,7,8), "+
"(3,4,5,6,7,8,9), "+
"(4,5,6,7,8,9,10), "+
"(5,6,7,8,9,10,11), "+
"(6,7,8,9,10,11,12), "+
"(7,8,9,10,11,12,13), "+
"(8,9,10,11,12,13,14), "+
"(9,10,11,12,13,14,15), "+
"(10,11,12,13,14,15,16), "+
"(11,12,13,14,15,16,17), "+
"(12,13,14,15,16,17,18), "+
"(13,14,15,16,17,18,19), "+
"(14,15,16,17,18,19,20), "+
"(15,16,17,18,19,20,21), "+
"(16,17,18,19,20,21,22), "+
"(17,18,19,20,21,22,23), "+
"(18,19,20,21,22,23,24), "+
"(19,20,21,22,23,24,25), "+
"(20,21,22,23,24,25,26)")
result := utils.Exec(t, conn, "select id, field, field2, field3, field4, field5, field6 from t1 order by id")

expected :=
"[[INT64(0) INT64(1) INT64(2) INT64(3)] " +
"[INT64(1) INT64(2) INT64(3) INT64(4)] " +
"[INT64(2) INT64(3) INT64(4) INT64(5)] " +
"[INT64(3) INT64(4) INT64(5) INT64(6)] " +
"[INT64(4) INT64(5) INT64(6) INT64(7)] " +
"[INT64(5) INT64(6) INT64(7) INT64(8)] " +
"[INT64(6) INT64(7) INT64(8) INT64(9)] " +
"[INT64(7) INT64(8) INT64(9) INT64(10)] " +
"[INT64(8) INT64(9) INT64(10) INT64(11)] " +
"[INT64(9) INT64(10) INT64(11) INT64(12)] " +
"[INT64(10) INT64(11) INT64(12) INT64(13)] " +
"[INT64(11) INT64(12) INT64(13) INT64(14)] " +
"[INT64(12) INT64(13) INT64(14) INT64(15)] " +
"[INT64(13) INT64(14) INT64(15) INT64(16)] " +
"[INT64(14) INT64(15) INT64(16) INT64(17)] " +
"[INT64(15) INT64(16) INT64(17) INT64(18)] " +
"[INT64(16) INT64(17) INT64(18) INT64(19)] " +
"[INT64(17) INT64(18) INT64(19) INT64(20)] " +
"[INT64(18) INT64(19) INT64(20) INT64(21)] " +
"[INT64(19) INT64(20) INT64(21) INT64(22)] " +
"[INT64(20) INT64(21) INT64(22) INT64(23)]]"
"[[INT64(0) INT64(1) INT64(2) INT64(3) INT64(4) INT64(5) INT64(6)] " +
"[INT64(1) INT64(2) INT64(3) INT64(4) INT64(5) INT64(6) INT64(7)] " +
"[INT64(2) INT64(3) INT64(4) INT64(5) INT64(6) INT64(7) INT64(8)] " +
"[INT64(3) INT64(4) INT64(5) INT64(6) INT64(7) INT64(8) INT64(9)] " +
"[INT64(4) INT64(5) INT64(6) INT64(7) INT64(8) INT64(9) INT64(10)] " +
"[INT64(5) INT64(6) INT64(7) INT64(8) INT64(9) INT64(10) INT64(11)] " +
"[INT64(6) INT64(7) INT64(8) INT64(9) INT64(10) INT64(11) INT64(12)] " +
"[INT64(7) INT64(8) INT64(9) INT64(10) INT64(11) INT64(12) INT64(13)] " +
"[INT64(8) INT64(9) INT64(10) INT64(11) INT64(12) INT64(13) INT64(14)] " +
"[INT64(9) INT64(10) INT64(11) INT64(12) INT64(13) INT64(14) INT64(15)] " +
"[INT64(10) INT64(11) INT64(12) INT64(13) INT64(14) INT64(15) INT64(16)] " +
"[INT64(11) INT64(12) INT64(13) INT64(14) INT64(15) INT64(16) INT64(17)] " +
"[INT64(12) INT64(13) INT64(14) INT64(15) INT64(16) INT64(17) INT64(18)] " +
"[INT64(13) INT64(14) INT64(15) INT64(16) INT64(17) INT64(18) INT64(19)] " +
"[INT64(14) INT64(15) INT64(16) INT64(17) INT64(18) INT64(19) INT64(20)] " +
"[INT64(15) INT64(16) INT64(17) INT64(18) INT64(19) INT64(20) INT64(21)] " +
"[INT64(16) INT64(17) INT64(18) INT64(19) INT64(20) INT64(21) INT64(22)] " +
"[INT64(17) INT64(18) INT64(19) INT64(20) INT64(21) INT64(22) INT64(23)] " +
"[INT64(18) INT64(19) INT64(20) INT64(21) INT64(22) INT64(23) INT64(24)] " +
"[INT64(19) INT64(20) INT64(21) INT64(22) INT64(23) INT64(24) INT64(25)] " +
"[INT64(20) INT64(21) INT64(22) INT64(23) INT64(24) INT64(25) INT64(26)]]"
assert.Equal(t, expected, fmt.Sprintf("%v", result.Rows))
}
18 changes: 18 additions & 0 deletions go/vt/vtexplain/testdata/test-schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,24 @@ CREATE TABLE orders_id_lookup (
primary key(id)
);

CREATE TABLE orders_id_lookup_exclusive_read_lock (
id int NOT NULL,
keyspace_id varbinary(128),
primary key(id)
);

CREATE TABLE orders_id_lookup_shared_read_lock (
id int NOT NULL,
keyspace_id varbinary(128),
primary key(id)
);

CREATE TABLE orders_id_lookup_no_read_lock (
id int NOT NULL,
keyspace_id varbinary(128),
primary key(id)
);

CREATE TABLE orders_id_lookup_no_verify (
id int NOT NULL,
keyspace_id varbinary(128),
Expand Down
38 changes: 38 additions & 0 deletions go/vt/vtexplain/testdata/test-vschema.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,36 @@
},
"owner": "orders"
},
"orders_id_vdx_exclusive_read_lock": {
"type": "lookup_unique",
"params": {
"table": "orders_id_lookup_exclusive_read_lock",
"from": "id",
"to": "keyspace_id",
"read_lock": "exclusive"
},
"owner": "orders"
},
"orders_id_vdx_shared_read_lock": {
"type": "lookup_unique",
"params": {
"table": "orders_id_lookup_shared_read_lock",
"from": "id",
"to": "keyspace_id",
"read_lock": "shared"
},
"owner": "orders"
},
"orders_id_vdx_no_read_lock": {
"type": "lookup_unique",
"params": {
"table": "orders_id_lookup_no_read_lock",
"from": "id",
"to": "keyspace_id",
"read_lock": "none"
},
"owner": "orders"
},
"orders_id_vdx_no_verify": {
"type": "lookup_unique",
"params": {
Expand Down Expand Up @@ -175,6 +205,14 @@
}
]
},
"orders_id_lookup_no_read_lock": {
"column_vindexes": [
{
"column": "id",
"name": "hash"
}
]
},
"orders_id_lookup_no_verify": {
"column_vindexes": [
{
Expand Down
Loading

0 comments on commit 8603fda

Please sign in to comment.