|
| 1 | +/* |
| 2 | +Copyright 2020 The Vitess Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package unsharded |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + _ "embed" |
| 22 | + "flag" |
| 23 | + "os" |
| 24 | + "testing" |
| 25 | + "time" |
| 26 | + |
| 27 | + "github.com/stretchr/testify/require" |
| 28 | + |
| 29 | + "vitess.io/vitess/go/vt/log" |
| 30 | + |
| 31 | + "vitess.io/vitess/go/mysql" |
| 32 | + "vitess.io/vitess/go/test/endtoend/cluster" |
| 33 | +) |
| 34 | + |
| 35 | +var ( |
| 36 | + clusterInstance *cluster.LocalProcessCluster |
| 37 | + vtParams mysql.ConnParams |
| 38 | + cell = "zone1" |
| 39 | + hostname = "localhost" |
| 40 | + sks = "source" |
| 41 | + tks = "target" |
| 42 | + |
| 43 | + //go:embed schema.sql |
| 44 | + SchemaSQL string |
| 45 | +) |
| 46 | + |
| 47 | +func TestMain(m *testing.M) { |
| 48 | + flag.Parse() |
| 49 | + |
| 50 | + exitCode := func() int { |
| 51 | + clusterInstance = cluster.NewCluster(cell, hostname) |
| 52 | + defer clusterInstance.Teardown() |
| 53 | + |
| 54 | + // Start topo server |
| 55 | + if err := clusterInstance.StartTopo(); err != nil { |
| 56 | + return 1 |
| 57 | + } |
| 58 | + |
| 59 | + // Start keyspace |
| 60 | + sourceKs := cluster.Keyspace{Name: sks, SchemaSQL: SchemaSQL} |
| 61 | + if err := clusterInstance.StartUnshardedKeyspace(sourceKs, 1, false); err != nil { |
| 62 | + log.Fatal(err.Error()) |
| 63 | + return 1 |
| 64 | + } |
| 65 | + |
| 66 | + targetKs := cluster.Keyspace{Name: tks} |
| 67 | + if err := clusterInstance.StartUnshardedKeyspace(targetKs, 1, false); err != nil { |
| 68 | + log.Fatal(err.Error()) |
| 69 | + return 1 |
| 70 | + } |
| 71 | + |
| 72 | + // Start vtgate |
| 73 | + if err := clusterInstance.StartVtgate(); err != nil { |
| 74 | + log.Fatal(err.Error()) |
| 75 | + return 1 |
| 76 | + } |
| 77 | + |
| 78 | + // Also check we can create procedures through the vtgate. |
| 79 | + vtParams = mysql.ConnParams{ |
| 80 | + Host: "localhost", |
| 81 | + Port: clusterInstance.VtgateMySQLPort, |
| 82 | + } |
| 83 | + conn, err := mysql.Connect(context.Background(), &vtParams) |
| 84 | + if err != nil { |
| 85 | + log.Fatal(err.Error()) |
| 86 | + return 1 |
| 87 | + } |
| 88 | + defer conn.Close() |
| 89 | + |
| 90 | + return m.Run() |
| 91 | + }() |
| 92 | + os.Exit(exitCode) |
| 93 | +} |
| 94 | + |
| 95 | +func TestQueriesWithRoutingRules(t *testing.T) { |
| 96 | + ctx, cancel := context.WithCancel(context.Background()) |
| 97 | + defer func() { |
| 98 | + cancel() |
| 99 | + time.Sleep(2 * time.Second) |
| 100 | + }() |
| 101 | + startQueries(t, ctx) |
| 102 | + |
| 103 | + workflow := "TestQueriesWithRoutingRules" |
| 104 | + mtw := cluster.NewMoveTables(t, clusterInstance, workflow, tks, sks, "t1", nil) |
| 105 | + out, err := mtw.Create() |
| 106 | + t.Logf("movetables created: %s", out) |
| 107 | + require.NoError(t, err) |
| 108 | + |
| 109 | + mtw.WaitForVreplCatchup(5 * time.Second) |
| 110 | + t.Logf("movetables catchup phase completed") |
| 111 | + |
| 112 | + time.Sleep(2 * time.Second) |
| 113 | + out, err = mtw.SwitchReads() |
| 114 | + t.Logf("movetables switch reads: %v", out) |
| 115 | + require.NoError(t, err) |
| 116 | +} |
| 117 | + |
| 118 | +func startQueries(t *testing.T, ctx context.Context) { |
| 119 | + users := 1000 |
| 120 | + conns := make([]*mysql.Conn, 0, users) |
| 121 | + for range users { |
| 122 | + conn, err := mysql.Connect(ctx, &vtParams) |
| 123 | + require.NoError(t, err) |
| 124 | + conns = append(conns, conn) |
| 125 | + } |
| 126 | + |
| 127 | + query := "select * from t1" |
| 128 | + for _, conn := range conns { |
| 129 | + go func(conn *mysql.Conn) { |
| 130 | + defer conn.Close() |
| 131 | + if _, err := conn.ExecuteFetch("use @replica", 1000, true); err != nil { |
| 132 | + t.Logf("error in use @primary: (%d, %v)", conn.ID(), err) |
| 133 | + } |
| 134 | + for { |
| 135 | + if ctx.Err() != nil { |
| 136 | + return |
| 137 | + } |
| 138 | + // if _, err := conn.ExecuteFetch("use @primary", 1000, true); err != nil { |
| 139 | + // t.Logf("error in use @primary: (%d, %v)", conn.ID(), err) |
| 140 | + // } |
| 141 | + // if _, err := conn.ExecuteFetch(query, 1000, true); err != nil { |
| 142 | + // t.Logf("error in primary query: (%d, %v)", conn.ID(), err) |
| 143 | + // } |
| 144 | + if _, err := conn.ExecuteFetch(query, 1000, true); err != nil { |
| 145 | + t.Logf("error in replica query: (%d, %v)", conn.ID(), err) |
| 146 | + } |
| 147 | + // time.Sleep(10 * time.Millisecond) |
| 148 | + } |
| 149 | + }(conn) |
| 150 | + } |
| 151 | +} |
0 commit comments