forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex_test.go
148 lines (134 loc) · 3.97 KB
/
index_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
// Copyright 2015 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,
// 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 ddltest
import (
goctx "context"
"fmt"
"math"
"os"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/pingcap/log"
"github.com/pingcap/tidb/store/gcworker"
"github.com/pingcap/tidb/table"
"github.com/stretchr/testify/require"
)
func getIndex(t table.Table, name string) table.Index {
for _, idx := range t.Indices() {
if idx.Meta().Name.O == name {
return idx
}
}
return nil
}
func (s *ddlSuite) checkDropIndex(t *testing.T, tableName string) {
gcWorker, err := gcworker.NewMockGCWorker(s.store)
require.NoError(t, err)
err = gcWorker.DeleteRanges(goctx.Background(), uint64(math.MaxInt32))
require.NoError(t, err)
s.mustExec(fmt.Sprintf("admin check table %s", tableName))
}
// TestIndex operations on table test_index (c int, c1 bigint, c2 double, c3 varchar(256), primary key(c)).
func TestIndex(t *testing.T) {
err := os.Setenv("tidb_manager_ttl", fmt.Sprintf("%d", *lease+5))
if err != nil {
log.Fatal("set tidb_manager_ttl failed")
}
s := createDDLSuite(t)
defer s.teardown(t)
// first add many data
workerNum := 10
base := *dataNum / workerNum
var wg sync.WaitGroup
wg.Add(workerNum)
for i := 0; i < workerNum; i++ {
go func(i int) {
defer wg.Done()
for j := 0; j < base; j++ {
k := base*i + j
s.execInsert(
fmt.Sprintf("insert into test_index values (%d, %d, %f, '%s')",
k, randomInt(), randomFloat(), randomString(10)))
}
}(i)
}
wg.Wait()
tbl := []struct {
Query string
IndexName string
Add bool
}{
{"create index c1_index on test_index (c1)", "c1_index", true},
{"drop index c1_index on test_index", "c1_index", false},
{"create index c2_index on test_index (c2)", "c2_index", true},
{"drop index c2_index on test_index", "c2_index", false},
{"create index c3_index on test_index (c3)", "c3_index", true},
{"drop index c3_index on test_index", "c3_index", false},
}
insertID := int64(*dataNum)
for _, col := range tbl {
done := s.runDDL(col.Query)
ticker := time.NewTicker(time.Duration(*lease) * time.Second / 2)
//nolint:all_revive,revive
defer ticker.Stop()
LOOP:
for {
select {
case err := <-done:
require.NoError(t, err)
break LOOP
case <-ticker.C:
// add count new data
// delete count old data randomly
// update count old data randomly
count := 10
s.execIndexOperations(t, workerNum, count, &insertID)
}
}
tbl := s.getTable(t, "test_index")
index := getIndex(tbl, col.IndexName)
if col.Add {
require.NotNil(t, index)
s.mustExec("admin check table test_index")
} else {
require.Nil(t, index)
s.checkDropIndex(t, "test_index")
}
}
}
func (s *ddlSuite) execIndexOperations(t *testing.T, workerNum, count int, insertID *int64) {
var wg sync.WaitGroup
// workerNum = 10
wg.Add(workerNum)
for i := 0; i < workerNum; i++ {
go func() {
defer wg.Done()
for j := 0; j < count; j++ {
id := atomic.AddInt64(insertID, 1)
sql := fmt.Sprintf("insert into test_index values (%d, %d, %f, '%s')", id, randomInt(), randomFloat(), randomString(10))
s.execInsert(sql)
t.Logf("sql %s", sql)
sql = fmt.Sprintf("delete from test_index where c = %d", randomIntn(int(id)))
s.mustExec(sql)
t.Logf("sql %s", sql)
sql = fmt.Sprintf("update test_index set c1 = %d, c2 = %f, c3 = '%s' where c = %d", randomInt(), randomFloat(), randomString(10), randomIntn(int(id)))
s.mustExec(sql)
t.Logf("sql %s", sql)
}
}()
}
wg.Wait()
}