-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LookupVindex: fix CLI to allow creating non-unique lookups with singl…
…e column (#17301) Signed-off-by: Rohit Nayak <[email protected]>
- Loading branch information
1 parent
3cdda35
commit 9a03317
Showing
7 changed files
with
331 additions
and
12 deletions.
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
go/test/endtoend/vreplication/lookupindex_helper_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
Copyright 2024 The Vitess Authors. | ||
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. | ||
*/ | ||
|
||
package vreplication | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/tidwall/gjson" | ||
|
||
"vitess.io/vitess/go/vt/sqlparser" | ||
|
||
binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata" | ||
) | ||
|
||
type lookupIndex struct { | ||
typ string | ||
name string | ||
tableKeyspace string | ||
table string | ||
columns []string | ||
ownerTable string | ||
ownerTableKeyspace string | ||
ignoreNulls bool | ||
|
||
t *testing.T | ||
} | ||
|
||
func (li *lookupIndex) String() string { | ||
return li.typ + " " + li.name + " on " + li.tableKeyspace + "." + li.table + " (" + li.columns[0] + ")" | ||
} | ||
|
||
func (li *lookupIndex) create() { | ||
cols := strings.Join(li.columns, ",") | ||
args := []string{ | ||
"LookupVindex", | ||
"--name", li.name, | ||
"--table-keyspace=" + li.ownerTableKeyspace, | ||
"create", | ||
"--keyspace=" + li.tableKeyspace, | ||
"--type=" + li.typ, | ||
"--table-owner=" + li.ownerTable, | ||
"--table-owner-columns=" + cols, | ||
"--tablet-types=PRIMARY", | ||
} | ||
if li.ignoreNulls { | ||
args = append(args, "--ignore-nulls") | ||
} | ||
|
||
err := vc.VtctldClient.ExecuteCommand(args...) | ||
require.NoError(li.t, err, "error executing LookupVindex create: %v", err) | ||
waitForWorkflowState(li.t, vc, fmt.Sprintf("%s.%s", li.ownerTableKeyspace, li.name), binlogdatapb.VReplicationWorkflowState_Running.String()) | ||
li.expectWriteOnly(true) | ||
} | ||
|
||
func (li *lookupIndex) cancel() { | ||
panic("not implemented") | ||
} | ||
|
||
func (li *lookupIndex) externalize() { | ||
args := []string{ | ||
"LookupVindex", | ||
"--name", li.name, | ||
"--table-keyspace=" + li.ownerTableKeyspace, | ||
"externalize", | ||
"--keyspace=" + li.tableKeyspace, | ||
} | ||
err := vc.VtctldClient.ExecuteCommand(args...) | ||
require.NoError(li.t, err, "error executing LookupVindex externalize: %v", err) | ||
li.expectWriteOnly(false) | ||
} | ||
|
||
func (li *lookupIndex) show() error { | ||
return nil | ||
} | ||
|
||
func (li *lookupIndex) expectWriteOnly(expected bool) { | ||
vschema, err := vc.VtctldClient.ExecuteCommandWithOutput("GetVSchema", li.ownerTableKeyspace) | ||
require.NoError(li.t, err, "error executing GetVSchema: %v", err) | ||
vdx := gjson.Get(vschema, fmt.Sprintf("vindexes.%s", li.name)) | ||
require.NotNil(li.t, vdx, "lookup vindex %s not found", li.name) | ||
want := "" | ||
if expected { | ||
want = "true" | ||
} | ||
require.Equal(li.t, want, vdx.Get("params.write_only").String(), "expected write_only parameter to be %s", want) | ||
} | ||
|
||
func getNumRowsInQuery(t *testing.T, query string) int { | ||
stmt, err := sqlparser.NewTestParser().Parse(query) | ||
require.NoError(t, err) | ||
insertStmt, ok := stmt.(*sqlparser.Insert) | ||
require.True(t, ok) | ||
rows, ok := insertStmt.Rows.(sqlparser.Values) | ||
require.True(t, ok) | ||
return len(rows) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
/* | ||
Copyright 2024 The Vitess Authors. | ||
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. | ||
*/ | ||
|
||
package vreplication | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"vitess.io/vitess/go/test/endtoend/cluster" | ||
vttablet "vitess.io/vitess/go/vt/vttablet/common" | ||
) | ||
|
||
type TestClusterSpec struct { | ||
keyspaceName string | ||
vschema string | ||
schema string | ||
} | ||
|
||
var lookupClusterSpec = TestClusterSpec{ | ||
keyspaceName: "lookup", | ||
vschema: ` | ||
{ | ||
"sharded": true, | ||
"vindexes": { | ||
"reverse_bits": { | ||
"type": "reverse_bits" | ||
} | ||
}, | ||
"tables": { | ||
"t1": { | ||
"column_vindexes": [ | ||
{ | ||
"column": "c1", | ||
"name": "reverse_bits" | ||
} | ||
] | ||
} | ||
} | ||
} | ||
`, | ||
schema: ` | ||
create table t1( | ||
c1 int, | ||
c2 int, | ||
val varchar(128), | ||
primary key(c1) | ||
); | ||
`, | ||
} | ||
|
||
func setupLookupIndexKeyspace(t *testing.T) map[string]*cluster.VttabletProcess { | ||
tablets := make(map[string]*cluster.VttabletProcess) | ||
if _, err := vc.AddKeyspace(t, []*Cell{vc.Cells["zone1"]}, lookupClusterSpec.keyspaceName, "-80,80-", | ||
lookupClusterSpec.vschema, lookupClusterSpec.schema, defaultReplicas, defaultRdonly, 200, nil); err != nil { | ||
require.NoError(t, err) | ||
} | ||
defaultCell := vc.Cells[vc.CellNames[0]] | ||
ks := vc.Cells[defaultCell.Name].Keyspaces[lookupClusterSpec.keyspaceName] | ||
targetTab1 = ks.Shards["-80"].Tablets["zone1-200"].Vttablet | ||
targetTab2 = ks.Shards["80-"].Tablets["zone1-300"].Vttablet | ||
tablets["-80"] = targetTab1 | ||
tablets["80-"] = targetTab2 | ||
return tablets | ||
} | ||
|
||
type lookupTestCase struct { | ||
name string | ||
li *lookupIndex | ||
initQuery string | ||
runningQuery string | ||
postExternalizeQuery string | ||
cleanupQuery string | ||
} | ||
|
||
func TestLookupIndex(t *testing.T) { | ||
setSidecarDBName("_vt") | ||
origDefaultReplicas := defaultReplicas | ||
origDefaultRdonly := defaultRdonly | ||
defer func() { | ||
defaultReplicas = origDefaultReplicas | ||
defaultRdonly = origDefaultRdonly | ||
}() | ||
defaultReplicas = 1 | ||
defaultRdonly = 0 | ||
vc = setupMinimalCluster(t) | ||
defer vc.TearDown() | ||
vttablet.InitVReplicationConfigDefaults() | ||
|
||
_ = setupLookupIndexKeyspace(t) | ||
|
||
initQuery := "insert into t1 (c1, c2, val) values (1, 1, 'val1'), (2, 2, 'val2'), (3, 3, 'val3')" | ||
runningQuery := "insert into t1 (c1, c2, val) values (4, 4, 'val4'), (5, 5, 'val5'), (6, 6, 'val6')" | ||
postExternalizeQuery := "insert into t1 (c1, c2, val) values (7, 7, 'val7'), (8, 8, 'val8'), (9, 9, 'val9')" | ||
cleanupQuery := "delete from t1" | ||
|
||
testCases := []lookupTestCase{ | ||
{ | ||
name: "non-unique lookup index, one column", | ||
li: &lookupIndex{ | ||
typ: "consistent_lookup", | ||
name: "t1_c2_lookup", | ||
tableKeyspace: lookupClusterSpec.keyspaceName, | ||
table: "t1", | ||
columns: []string{"c2"}, | ||
ownerTable: "t1", | ||
ownerTableKeyspace: lookupClusterSpec.keyspaceName, | ||
ignoreNulls: true, | ||
t: t, | ||
}, | ||
}, | ||
{ | ||
name: "lookup index, two columns", | ||
li: &lookupIndex{ | ||
typ: "lookup", | ||
name: "t1_c2_val_lookup", | ||
tableKeyspace: lookupClusterSpec.keyspaceName, | ||
table: "t1", | ||
columns: []string{"c2", "val"}, | ||
ownerTable: "t1", | ||
ownerTableKeyspace: lookupClusterSpec.keyspaceName, | ||
ignoreNulls: true, | ||
t: t, | ||
}, | ||
}, | ||
{ | ||
name: "unique lookup index, one column", | ||
li: &lookupIndex{ | ||
typ: "lookup_unique", | ||
name: "t1_c2_unique_lookup", | ||
tableKeyspace: lookupClusterSpec.keyspaceName, | ||
table: "t1", | ||
columns: []string{"c2"}, | ||
ownerTable: "t1", | ||
ownerTableKeyspace: lookupClusterSpec.keyspaceName, | ||
ignoreNulls: true, | ||
t: t, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
tc.initQuery = initQuery | ||
tc.runningQuery = runningQuery | ||
tc.postExternalizeQuery = postExternalizeQuery | ||
tc.cleanupQuery = cleanupQuery | ||
testLookupVindex(t, &tc) | ||
}) | ||
} | ||
} | ||
|
||
func testLookupVindex(t *testing.T, tc *lookupTestCase) { | ||
vtgateConn, cancel := getVTGateConn() | ||
defer cancel() | ||
var totalRows int | ||
li := tc.li | ||
|
||
t.Run("init data", func(t *testing.T) { | ||
totalRows += getNumRowsInQuery(t, tc.initQuery) | ||
_, err := vtgateConn.ExecuteFetch(tc.initQuery, 1000, false) | ||
require.NoError(t, err) | ||
}) | ||
|
||
t.Run("create", func(t *testing.T) { | ||
tc.li.create() | ||
|
||
lks := li.tableKeyspace | ||
vindexName := li.name | ||
waitForRowCount(t, vtgateConn, lks, vindexName, totalRows) | ||
totalRows += getNumRowsInQuery(t, tc.runningQuery) | ||
_, err := vtgateConn.ExecuteFetch(tc.runningQuery, 1000, false) | ||
require.NoError(t, err) | ||
waitForRowCount(t, vtgateConn, tc.li.ownerTableKeyspace, li.name, totalRows) | ||
}) | ||
|
||
t.Run("externalize", func(t *testing.T) { | ||
tc.li.externalize() | ||
totalRows += getNumRowsInQuery(t, tc.postExternalizeQuery) | ||
_, err := vtgateConn.ExecuteFetch(tc.postExternalizeQuery, 1000, false) | ||
require.NoError(t, err) | ||
waitForRowCount(t, vtgateConn, tc.li.ownerTableKeyspace, li.name, totalRows) | ||
}) | ||
|
||
t.Run("cleanup", func(t *testing.T) { | ||
_, err := vtgateConn.ExecuteFetch(tc.cleanupQuery, 1000, false) | ||
require.NoError(t, err) | ||
waitForRowCount(t, vtgateConn, tc.li.ownerTableKeyspace, li.name, 0) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters