forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap_test.go
243 lines (218 loc) · 8.01 KB
/
bootstrap_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright 2016 PingCAP, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package tidb
import (
"fmt"
. "github.com/pingcap/check"
"github.com/pingcap/tidb/ast"
"github.com/pingcap/tidb/context"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/meta"
"github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/util/auth"
"github.com/pingcap/tidb/util/testleak"
goctx "golang.org/x/net/context"
)
var _ = Suite(&testBootstrapSuite{})
type testBootstrapSuite struct {
dbName string
dbNameBootstrap string
}
func (s *testBootstrapSuite) SetUpSuite(c *C) {
s.dbName = "test_bootstrap"
s.dbNameBootstrap = "test_main_db_bootstrap"
}
func (s *testBootstrapSuite) TestBootstrap(c *C) {
defer testleak.AfterTest(c)()
store, dom := newStoreWithBootstrap(c, s.dbName)
defer dom.Close()
se := newSession(c, store, s.dbName)
mustExecSQL(c, se, "USE mysql;")
r := mustExecSQL(c, se, `select * from user;`)
c.Assert(r, NotNil)
goCtx := goctx.Background()
row, err := r.Next(goCtx)
c.Assert(err, IsNil)
c.Assert(row, NotNil)
datums := ast.RowToDatums(row, r.Fields())
match(c, datums, []byte("%"), []byte("root"), []byte(""), "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y")
c.Assert(se.Auth(&auth.UserIdentity{Username: "root", Hostname: "anyhost"}, []byte(""), []byte("")), IsTrue)
mustExecSQL(c, se, "USE test;")
// Check privilege tables.
mustExecSQL(c, se, "SELECT * from mysql.db;")
mustExecSQL(c, se, "SELECT * from mysql.tables_priv;")
mustExecSQL(c, se, "SELECT * from mysql.columns_priv;")
// Check privilege tables.
r = mustExecSQL(c, se, "SELECT COUNT(*) from mysql.global_variables;")
c.Assert(r, NotNil)
v, err := r.Next(goCtx)
c.Assert(err, IsNil)
c.Assert(v.GetInt64(0), Equals, globalVarsCount())
// Check a storage operations are default autocommit after the second start.
mustExecSQL(c, se, "USE test;")
mustExecSQL(c, se, "drop table if exists t")
mustExecSQL(c, se, "create table t (id int)")
delete(storeBootstrapped, store.UUID())
se.Close()
se, err = CreateSession4Test(store)
c.Assert(err, IsNil)
mustExecSQL(c, se, "USE test;")
mustExecSQL(c, se, "insert t values (?)", 3)
se, err = CreateSession4Test(store)
c.Assert(err, IsNil)
mustExecSQL(c, se, "USE test;")
r = mustExecSQL(c, se, "select * from t")
c.Assert(r, NotNil)
v, err = r.Next(goCtx)
c.Assert(err, IsNil)
datums = ast.RowToDatums(v, r.Fields())
match(c, datums, 3)
mustExecSQL(c, se, "drop table if exists t")
se.Close()
// Try to do bootstrap dml jobs on an already bootstraped TiDB system will not cause fatal.
// For https://github.com/pingcap/tidb/issues/1096
se, err = CreateSession4Test(store)
c.Assert(err, IsNil)
doDMLWorks(se)
err = store.Close()
c.Assert(err, IsNil)
}
func globalVarsCount() int64 {
var count int64
for _, v := range variable.SysVars {
if v.Scope != variable.ScopeSession {
count++
}
}
return count
}
// bootstrapWithOnlyDDLWork creates a new session on store but only do ddl works.
func (s *testBootstrapSuite) bootstrapWithOnlyDDLWork(store kv.Storage, c *C) {
ss := &session{
store: store,
parser: parser.New(),
sessionVars: variable.NewSessionVars(),
}
ss.mu.values = make(map[fmt.Stringer]interface{})
ss.SetValue(context.Initing, true)
dom, err := domap.Get(store)
c.Assert(err, IsNil)
domain.BindDomain(ss, dom)
b, err := checkBootstrapped(ss)
c.Assert(b, IsFalse)
c.Assert(err, IsNil)
doDDLWorks(ss)
// Leave dml unfinished.
}
// testBootstrapWithError :
// When a session failed in bootstrap process (for example, the session is killed after doDDLWorks()).
// We should make sure that the following session could finish the bootstrap process.
func (s *testBootstrapSuite) testBootstrapWithError(c *C) {
goCtx := goctx.Background()
defer testleak.AfterTest(c)()
store := newStore(c, s.dbNameBootstrap)
s.bootstrapWithOnlyDDLWork(store, c)
BootstrapSession(store)
se := newSession(c, store, s.dbNameBootstrap)
mustExecSQL(c, se, "USE mysql;")
r := mustExecSQL(c, se, `select * from user;`)
row, err := r.Next(goCtx)
c.Assert(err, IsNil)
c.Assert(row, NotNil)
datums := ast.RowToDatums(row, r.Fields())
match(c, datums, []byte("%"), []byte("root"), []byte(""), "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y")
mustExecSQL(c, se, "USE test;")
// Check privilege tables.
mustExecSQL(c, se, "SELECT * from mysql.db;")
mustExecSQL(c, se, "SELECT * from mysql.tables_priv;")
mustExecSQL(c, se, "SELECT * from mysql.columns_priv;")
// Check global variables.
r = mustExecSQL(c, se, "SELECT COUNT(*) from mysql.global_variables;")
v, err := r.Next(goCtx)
c.Assert(err, IsNil)
c.Assert(v.GetInt64(0), Equals, globalVarsCount())
r = mustExecSQL(c, se, `SELECT VARIABLE_VALUE from mysql.TiDB where VARIABLE_NAME="bootstrapped";`)
row, err = r.Next(goCtx)
c.Assert(err, IsNil)
c.Assert(row, NotNil)
c.Assert(row.Len(), Equals, 1)
c.Assert(row.GetBytes(0), BytesEquals, []byte("True"))
err = store.Close()
c.Assert(err, IsNil)
}
// TestUpgrade tests upgrading
func (s *testBootstrapSuite) TestUpgrade(c *C) {
goCtx := goctx.Background()
defer testleak.AfterTest(c)()
store, _ := newStoreWithBootstrap(c, s.dbName)
defer store.Close()
se := newSession(c, store, s.dbName)
mustExecSQL(c, se, "USE mysql;")
// bootstrap with currentBootstrapVersion
r := mustExecSQL(c, se, `SELECT VARIABLE_VALUE from mysql.TiDB where VARIABLE_NAME="tidb_server_version";`)
row, err := r.Next(goCtx)
c.Assert(err, IsNil)
c.Assert(row, NotNil)
c.Assert(row.Len(), Equals, 1)
c.Assert(row.GetBytes(0), BytesEquals, []byte(fmt.Sprintf("%d", currentBootstrapVersion)))
se1 := newSession(c, store, s.dbName)
ver, err := getBootstrapVersion(se1)
c.Assert(err, IsNil)
c.Assert(ver, Equals, int64(currentBootstrapVersion))
// Do something to downgrade the store.
// downgrade meta bootstrap version
txn, err := store.Begin()
c.Assert(err, IsNil)
m := meta.NewMeta(txn)
err = m.FinishBootstrap(int64(1))
c.Assert(err, IsNil)
err = txn.Commit(goctx.Background())
c.Assert(err, IsNil)
mustExecSQL(c, se1, `delete from mysql.TiDB where VARIABLE_NAME="tidb_server_version";`)
mustExecSQL(c, se1, fmt.Sprintf(`delete from mysql.global_variables where VARIABLE_NAME="%s";`,
variable.TiDBDistSQLScanConcurrency))
mustExecSQL(c, se1, `commit;`)
delete(storeBootstrapped, store.UUID())
// Make sure the version is downgraded.
r = mustExecSQL(c, se1, `SELECT VARIABLE_VALUE from mysql.TiDB where VARIABLE_NAME="tidb_server_version";`)
row, err = r.Next(goCtx)
c.Assert(err, IsNil)
c.Assert(row, IsNil)
ver, err = getBootstrapVersion(se1)
c.Assert(err, IsNil)
c.Assert(ver, Equals, int64(0))
// Create a new session then upgrade() will run automatically.
dom1, err := BootstrapSession(store)
c.Assert(err, IsNil)
defer dom1.Close()
se2 := newSession(c, store, s.dbName)
r = mustExecSQL(c, se2, `SELECT VARIABLE_VALUE from mysql.TiDB where VARIABLE_NAME="tidb_server_version";`)
row, err = r.Next(goCtx)
c.Assert(err, IsNil)
c.Assert(row, NotNil)
c.Assert(row.Len(), Equals, 1)
c.Assert(row.GetBytes(0), BytesEquals, []byte(fmt.Sprintf("%d", currentBootstrapVersion)))
ver, err = getBootstrapVersion(se2)
c.Assert(err, IsNil)
c.Assert(ver, Equals, int64(currentBootstrapVersion))
}
func (s *testBootstrapSuite) TestOldPasswordUpgrade(c *C) {
defer testleak.AfterTest(c)()
pwd := "abc"
oldpwd := fmt.Sprintf("%X", auth.Sha1Hash([]byte(pwd)))
newpwd, err := oldPasswordUpgrade(oldpwd)
c.Assert(err, IsNil)
c.Assert(newpwd, Equals, "*0D3CED9BEC10A777AEC23CCC353A8C08A633045E")
}