vitess.io/vitess@v0.16.2/go/vt/vttablet/endtoend/sequence_test.go (about) 1 /* 2 Copyright 2019 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 endtoend 18 19 import ( 20 "testing" 21 22 "vitess.io/vitess/go/test/utils" 23 24 "github.com/stretchr/testify/assert" 25 "github.com/stretchr/testify/require" 26 27 "vitess.io/vitess/go/sqltypes" 28 "vitess.io/vitess/go/vt/vttablet/endtoend/framework" 29 30 querypb "vitess.io/vitess/go/vt/proto/query" 31 topodatapb "vitess.io/vitess/go/vt/proto/topodata" 32 ) 33 34 func TestSequence(t *testing.T) { 35 want := &sqltypes.Result{ 36 Fields: []*querypb.Field{{ 37 Name: "nextval", 38 Type: sqltypes.Int64, 39 }}, 40 Rows: [][]sqltypes.Value{{ 41 sqltypes.NewInt64(0), 42 }}, 43 } 44 for wantval := int64(1); wantval < 10; wantval += 2 { 45 want.Rows[0][0] = sqltypes.NewInt64(wantval) 46 qr, err := framework.NewClient().Execute("select next 2 values from vitess_seq", nil) 47 require.NoError(t, err) 48 utils.MustMatch(t, want, qr) 49 assert.Equal(t, want, qr) 50 } 51 52 // Verify that the table got updated according to chunk size. 53 qr, err := framework.NewClient().Execute("select next_id, cache from vitess_seq", nil) 54 require.NoError(t, err) 55 qr.Fields = nil 56 57 want = &sqltypes.Result{ 58 Rows: [][]sqltypes.Value{{ 59 sqltypes.NewInt64(13), 60 sqltypes.NewInt64(3), 61 }}, 62 StatusFlags: sqltypes.ServerStatusNoIndexUsed | sqltypes.ServerStatusAutocommit, 63 } 64 utils.MustMatch(t, want, qr) 65 66 // Mess up the sequence by reducing next_id 67 _, err = framework.NewClient().Execute("update vitess_seq set next_id=1", nil) 68 require.NoError(t, err) 69 qr, err = framework.NewClient().Execute("select next 3 values from vitess_seq", nil) 70 require.NoError(t, err) 71 qr.Fields = nil 72 73 // Next value generated should be based on the LastVal 74 want = &sqltypes.Result{ 75 Rows: [][]sqltypes.Value{{ 76 sqltypes.NewInt64(13), 77 }}, 78 } 79 utils.MustMatch(t, want, qr) 80 81 // next_id should be reset to LastVal+cache 82 qr, err = framework.NewClient().Execute("select next_id, cache from vitess_seq", nil) 83 require.NoError(t, err) 84 qr.Fields = nil 85 86 want = &sqltypes.Result{ 87 Rows: [][]sqltypes.Value{{ 88 sqltypes.NewInt64(16), 89 sqltypes.NewInt64(3), 90 }}, 91 StatusFlags: sqltypes.ServerStatusNoIndexUsed | sqltypes.ServerStatusAutocommit, 92 } 93 utils.MustMatch(t, want, qr) 94 95 // Change next_id to a very high value 96 _, err = framework.NewClient().Execute("update vitess_seq set next_id=100", nil) 97 require.NoError(t, err) 98 qr, err = framework.NewClient().Execute("select next 3 values from vitess_seq", nil) 99 require.NoError(t, err) 100 qr.Fields = nil 101 102 // Next value should jump to the high value 103 want = &sqltypes.Result{ 104 Rows: [][]sqltypes.Value{{ 105 sqltypes.NewInt64(100), 106 }}, 107 } 108 utils.MustMatch(t, want, qr) 109 } 110 111 func TestResetSequence(t *testing.T) { 112 client := framework.NewClient() 113 want := sqltypes.Result{ 114 Fields: []*querypb.Field{{ 115 Name: "nextval", 116 Type: sqltypes.Int64, 117 }}, 118 Rows: [][]sqltypes.Value{{ 119 sqltypes.NewInt64(1), 120 }}, 121 } 122 qr, err := client.Execute("select next value from vitess_reset_seq", nil) 123 if err != nil { 124 t.Fatal(err) 125 } 126 utils.MustMatch(t, &want, qr) 127 128 // Reset primaryship 129 err = client.SetServingType(topodatapb.TabletType_REPLICA) 130 if err != nil { 131 t.Fatal(err) 132 } 133 err = client.SetServingType(topodatapb.TabletType_PRIMARY) 134 if err != nil { 135 t.Fatal(err) 136 } 137 138 // Ensure the next value skips previously cached values. 139 want.Rows[0][0] = sqltypes.NewInt64(4) 140 qr, err = client.Execute("select next value from vitess_reset_seq", nil) 141 if err != nil { 142 t.Fatal(err) 143 } 144 utils.MustMatch(t, &want, qr) 145 }