forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager_test.go
195 lines (180 loc) · 4.99 KB
/
manager_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
// Copyright 2019 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 owner_test
import (
"context"
"fmt"
"testing"
"time"
"github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/clientv3/concurrency"
"github.com/coreos/etcd/integration"
"github.com/pingcap/errors"
"github.com/pingcap/parser/terror"
. "github.com/pingcap/tidb/ddl"
"github.com/pingcap/tidb/owner"
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/util/logutil"
goctx "golang.org/x/net/context"
)
const testLease = 5 * time.Millisecond
func checkOwner(d DDL, fbVal bool) (isOwner bool) {
manager := d.OwnerManager()
// The longest to wait for 3 seconds to
// make sure that campaigning owners is completed.
for i := 0; i < 600; i++ {
time.Sleep(5 * time.Millisecond)
isOwner = manager.IsOwner()
if isOwner == fbVal {
break
}
}
return
}
func TestSingle(t *testing.T) {
store, err := mockstore.NewMockTikvStore()
if err != nil {
t.Fatal(err)
}
defer store.Close()
clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
defer clus.Terminate(t)
cli := clus.RandClient()
ctx := goctx.Background()
d := NewDDL(
ctx,
WithEtcdClient(cli),
WithStore(store),
WithLease(testLease),
)
defer d.Stop()
isOwner := checkOwner(d, true)
if !isOwner {
t.Fatalf("expect true, got isOwner:%v", isOwner)
}
// test for newSession failed
ctx, cancel := goctx.WithCancel(ctx)
cancel()
manager := owner.NewOwnerManager(cli, "ddl", "ddl_id", DDLOwnerKey, nil)
err = manager.CampaignOwner(ctx)
if !terror.ErrorEqual(err, goctx.Canceled) &&
!terror.ErrorEqual(err, goctx.DeadlineExceeded) {
t.Fatalf("campaigned result don't match, err %v", err)
}
isOwner = checkOwner(d, true)
if !isOwner {
t.Fatalf("expect true, got isOwner:%v", isOwner)
}
// The test is used to exit campaign loop.
d.OwnerManager().Cancel()
isOwner = checkOwner(d, false)
if isOwner {
t.Fatalf("expect false, got isOwner:%v", isOwner)
}
time.Sleep(10 * time.Millisecond)
ownerID, _ := manager.GetOwnerID(goctx.Background())
// The error is ok to be not nil since we canceled the manager.
if ownerID != "" {
t.Fatalf("owner %s is not empty", ownerID)
}
}
func TestCluster(t *testing.T) {
tmpTTL := 3
orignalTTL := owner.ManagerSessionTTL
owner.ManagerSessionTTL = tmpTTL
defer func() {
owner.ManagerSessionTTL = orignalTTL
}()
store, err := mockstore.NewMockTikvStore()
if err != nil {
t.Fatal(err)
}
defer store.Close()
clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 4})
defer clus.Terminate(t)
cli := clus.Client(0)
d := NewDDL(
goctx.Background(),
WithEtcdClient(cli),
WithStore(store),
WithLease(testLease),
)
isOwner := checkOwner(d, true)
if !isOwner {
t.Fatalf("expect true, got isOwner:%v", isOwner)
}
cli1 := clus.Client(1)
d1 := NewDDL(
goctx.Background(),
WithEtcdClient(cli1),
WithStore(store),
WithLease(testLease),
)
isOwner = checkOwner(d1, false)
if isOwner {
t.Fatalf("expect false, got isOwner:%v", isOwner)
}
// Delete the leader key, the d1 become the owner.
cliRW := clus.Client(2)
err = deleteLeader(cliRW, DDLOwnerKey)
if err != nil {
t.Fatal(err)
}
isOwner = checkOwner(d, false)
if isOwner {
t.Fatalf("expect false, got isOwner:%v", isOwner)
}
d.Stop()
// d3 (not owner) stop
cli3 := clus.Client(3)
d3 := NewDDL(
goctx.Background(),
WithEtcdClient(cli3),
WithStore(store),
WithLease(testLease),
)
defer d3.Stop()
isOwner = checkOwner(d3, false)
if isOwner {
t.Fatalf("expect false, got isOwner:%v", isOwner)
}
d3.Stop()
// Cancel the owner context, there is no owner.
d1.Stop()
time.Sleep(time.Duration(tmpTTL+1) * time.Second)
session, err := concurrency.NewSession(cliRW)
if err != nil {
t.Fatalf("new session failed %v", err)
}
elec := concurrency.NewElection(session, DDLOwnerKey)
logPrefix := fmt.Sprintf("[ddl] %s ownerManager %s", DDLOwnerKey, "useless id")
logCtx := logutil.WithKeyValue(context.Background(), "owner info", logPrefix)
_, err = owner.GetOwnerInfo(goctx.Background(), logCtx, elec, "useless id")
if !terror.ErrorEqual(err, concurrency.ErrElectionNoLeader) {
t.Fatalf("get owner info result don't match, err %v", err)
}
}
func deleteLeader(cli *clientv3.Client, prefixKey string) error {
session, err := concurrency.NewSession(cli)
if err != nil {
return errors.Trace(err)
}
defer session.Close()
elec := concurrency.NewElection(session, prefixKey)
resp, err := elec.Leader(goctx.Background())
if err != nil {
return errors.Trace(err)
}
_, err = cli.Delete(goctx.Background(), string(resp.Kvs[0].Key))
return errors.Trace(err)
}