Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manage read your own writes system settings #6871

Merged
merged 6 commits into from
Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions go/sqltypes/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,15 @@ func (v Value) ToInt64() (int64, error) {
return strconv.ParseInt(v.ToString(), 10, 64)
}

// ToFloat64 returns the value as MySQL would return it as a float64.
func (v Value) ToFloat64() (float64, error) {
if !IsNumber(v.typ) {
return 0, ErrIncompatibleTypeCast
}

return strconv.ParseFloat(v.ToString(), 64)
}

// ToUint64 returns the value as MySQL would return it as a uint64.
func (v Value) ToUint64() (uint64, error) {
if !v.IsIntegral() {
Expand Down
175 changes: 175 additions & 0 deletions go/test/endtoend/vtgate/readafterwrite/raw_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/*
Copyright 2020 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 readafterwrite

import (
"context"
"flag"
"fmt"
"os"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"

"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/test/endtoend/cluster"
)

var (
clusterInstance *cluster.LocalProcessCluster
vtParams mysql.ConnParams
keyspaceName = "ks"
cell = "zone1"
hostname = "localhost"
sqlSchema = `
create table test(
id bigint,
val1 varchar(16),
val2 int,
val3 float,
primary key(id)
)Engine=InnoDB;

CREATE TABLE test_vdx (
val1 varchar(16) NOT NULL,
keyspace_id binary(8),
UNIQUE KEY (val1)
) ENGINE=Innodb;
`

vSchema = `
{
"sharded":true,
"vindexes": {
"hash_index": {
"type": "hash"
},
"lookup1": {
"type": "consistent_lookup",
"params": {
"table": "test_vdx",
"from": "val1",
"to": "keyspace_id",
"ignore_nulls": "true"
},
"owner": "test"
},
"unicode_vdx":{
"type": "unicode_loose_md5"
}
},
"tables": {
"test":{
"column_vindexes": [
{
"column": "id",
"name": "hash_index"
},
{
"column": "val1",
"name": "lookup1"
}
]
},
"test_vdx":{
"column_vindexes": [
{
"column": "val1",
"name": "unicode_vdx"
}
]
}
}
}
`
)

func TestMain(m *testing.M) {
defer cluster.PanicHandler(nil)
flag.Parse()

exitCode := func() int {
clusterInstance = cluster.NewCluster(cell, hostname)
defer clusterInstance.Teardown()

// Start topo server
if err := clusterInstance.StartTopo(); err != nil {
return 1
}

// Start keyspace
keyspace := &cluster.Keyspace{
Name: keyspaceName,
SchemaSQL: sqlSchema,
VSchema: vSchema,
}
clusterInstance.VtTabletExtraArgs = []string{"-queryserver-config-transaction-timeout", "5"}
if err := clusterInstance.StartKeyspace(*keyspace, []string{"-80", "80-"}, 1, false); err != nil {
return 1
}

// Start vtgate
clusterInstance.VtGateExtraArgs = []string{"-lock_heartbeat_time", "2s"}
vtgateProcess := clusterInstance.NewVtgateInstance()
vtgateProcess.SysVarSetEnabled = true
if err := vtgateProcess.Setup(); err != nil {
return 1
}

vtParams = mysql.ConnParams{
Host: clusterInstance.Hostname,
Port: clusterInstance.VtgateMySQLPort,
}
return m.Run()
}()
os.Exit(exitCode)
}

func TestRAWSettings(t *testing.T) {
defer cluster.PanicHandler(t)
conn, err := mysql.Connect(context.Background(), &vtParams)
require.NoError(t, err)
defer conn.Close()

assertMatches(t, conn, `select @@read_after_write_gtid, @@read_after_write_timeout, @@session_track_gtids`, `[[VARBINARY("") FLOAT64(0) VARBINARY("off")]]`)
exec(t, conn, `set read_after_write_gtid = 'some-gtid:1', read_after_write_timeout = 0.2, session_track_gtids = own_gtid`)
assertMatches(t, conn, `select @@read_after_write_gtid, @@read_after_write_timeout, @@session_track_gtids`, `[[VARBINARY("some-gtid:1") FLOAT64(0.2) VARBINARY("own_gtid")]]`)
}

func exec(t *testing.T, conn *mysql.Conn, query string) (*sqltypes.Result, error) {
t.Helper()
return conn.ExecuteFetch(query, 1000, true)
}

func checkedExec(t *testing.T, conn *mysql.Conn, query string) *sqltypes.Result {
t.Helper()
qr, err := conn.ExecuteFetch(query, 1000, true)
require.NoError(t, err)
return qr
}

func assertMatches(t *testing.T, conn *mysql.Conn, query, expected string) {
t.Helper()
qr := checkedExec(t, conn, query)
got := fmt.Sprintf("%v", qr.Rows)
diff := cmp.Diff(expected, got)
if diff != "" {
t.Errorf("Query: %s (-want +got):\n%s", query, diff)
}
}
Loading