-
Notifications
You must be signed in to change notification settings - Fork 308
/
tikv_cdc.go
320 lines (276 loc) · 9.96 KB
/
tikv_cdc.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// Copyright 2022 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 spec
import (
"context"
"crypto/tls"
"fmt"
"path/filepath"
"strings"
"time"
"github.com/pingcap/errors"
"github.com/pingcap/tiup/pkg/cluster/api"
"github.com/pingcap/tiup/pkg/cluster/ctxt"
"github.com/pingcap/tiup/pkg/cluster/template/scripts"
logprinter "github.com/pingcap/tiup/pkg/logger/printer"
"github.com/pingcap/tiup/pkg/meta"
"github.com/pingcap/tiup/pkg/tidbver"
"github.com/pingcap/tiup/pkg/utils"
)
// TiKVCDCSpec represents the TiKVCDC topology specification in topology.yaml
type TiKVCDCSpec struct {
Host string `yaml:"host"`
ManageHost string `yaml:"manage_host,omitempty" validate:"manage_host:editable"`
SSHPort int `yaml:"ssh_port,omitempty" validate:"ssh_port:editable"`
Imported bool `yaml:"imported,omitempty"`
Patched bool `yaml:"patched,omitempty"`
IgnoreExporter bool `yaml:"ignore_exporter,omitempty"`
Port int `yaml:"port" default:"8600"`
DeployDir string `yaml:"deploy_dir,omitempty"`
DataDir string `yaml:"data_dir,omitempty"`
LogDir string `yaml:"log_dir,omitempty"`
Offline bool `yaml:"offline,omitempty"`
GCTTL int64 `yaml:"gc-ttl,omitempty" validate:"gc-ttl:editable"`
TZ string `yaml:"tz,omitempty" validate:"tz:editable"`
NumaNode string `yaml:"numa_node,omitempty" validate:"numa_node:editable"`
Config map[string]any `yaml:"config,omitempty" validate:"config:ignore"`
ResourceControl meta.ResourceControl `yaml:"resource_control,omitempty" validate:"resource_control:editable"`
Arch string `yaml:"arch,omitempty"`
OS string `yaml:"os,omitempty"`
}
// Role returns the component role of the instance
func (s *TiKVCDCSpec) Role() string {
return ComponentTiKVCDC
}
// SSH returns the host and SSH port of the instance
func (s *TiKVCDCSpec) SSH() (string, int) {
host := s.Host
if s.ManageHost != "" {
host = s.ManageHost
}
return host, s.SSHPort
}
// GetMainPort returns the main port of the instance
func (s *TiKVCDCSpec) GetMainPort() int {
return s.Port
}
// GetManageHost returns the manage host of the instance
func (s *TiKVCDCSpec) GetManageHost() string {
if s.ManageHost != "" {
return s.ManageHost
}
return s.Host
}
// IsImported returns if the node is imported from TiDB-Ansible
func (s *TiKVCDCSpec) IsImported() bool {
// TiDB-Ansible do not support TiKV-CDC
return false
}
// IgnoreMonitorAgent returns if the node does not have monitor agents available
func (s *TiKVCDCSpec) IgnoreMonitorAgent() bool {
return s.IgnoreExporter
}
// TiKVCDCComponent represents TiKV-CDC component.
type TiKVCDCComponent struct{ Topology *Specification }
// Name implements Component interface.
func (c *TiKVCDCComponent) Name() string {
return ComponentTiKVCDC
}
// Role implements Component interface.
func (c *TiKVCDCComponent) Role() string {
return ComponentTiKVCDC
}
// Instances implements Component interface.
func (c *TiKVCDCComponent) Instances() []Instance {
ins := make([]Instance, 0, len(c.Topology.TiKVCDCServers))
for _, s := range c.Topology.TiKVCDCServers {
s := s
instance := &TiKVCDCInstance{BaseInstance{
InstanceSpec: s,
Name: c.Name(),
Host: s.Host,
ManageHost: s.ManageHost,
Port: s.Port,
SSHP: s.SSHPort,
Ports: []int{
s.Port,
},
Dirs: []string{
s.DeployDir,
},
StatusFn: func(_ context.Context, timeout time.Duration, tlsCfg *tls.Config, _ ...string) string {
return statusByHost(s.GetManageHost(), s.Port, "/status", timeout, tlsCfg)
},
UptimeFn: func(_ context.Context, timeout time.Duration, tlsCfg *tls.Config) time.Duration {
return UptimeByHost(s.GetManageHost(), s.Port, timeout, tlsCfg)
},
}, c.Topology}
if s.DataDir != "" {
instance.Dirs = append(instance.Dirs, s.DataDir)
}
ins = append(ins, instance)
}
return ins
}
// TiKVCDCInstance represent the TiKV-CDC instance.
type TiKVCDCInstance struct {
BaseInstance
topo Topology
}
// ScaleConfig deploy temporary config on scaling
func (i *TiKVCDCInstance) ScaleConfig(
ctx context.Context,
e ctxt.Executor,
topo Topology,
clusterName,
clusterVersion,
user string,
paths meta.DirPaths,
) error {
s := i.topo
defer func() {
i.topo = s
}()
i.topo = mustBeClusterTopo(topo)
return i.InitConfig(ctx, e, clusterName, clusterVersion, user, paths)
}
// InitConfig implements Instance interface.
func (i *TiKVCDCInstance) InitConfig(
ctx context.Context,
e ctxt.Executor,
clusterName,
clusterVersion,
deployUser string,
paths meta.DirPaths,
) error {
if !tidbver.TiKVCDCSupportDeploy(clusterVersion) {
return errors.New("tikv-cdc only supports cluster version v6.2.0 or later")
}
topo := i.topo.(*Specification)
if err := i.BaseInstance.InitConfig(ctx, e, topo.GlobalOptions, deployUser, paths); err != nil {
return err
}
enableTLS := topo.GlobalOptions.TLSEnabled
spec := i.InstanceSpec.(*TiKVCDCSpec)
globalConfig := topo.ServerConfigs.TiKVCDC
instanceConfig := spec.Config
pds := []string{}
for _, pdspec := range topo.PDServers {
pds = append(pds, pdspec.GetAdvertiseClientURL(enableTLS))
}
cfg := &scripts.TiKVCDCScript{
Addr: utils.JoinHostPort(i.GetListenHost(), spec.Port),
AdvertiseAddr: utils.JoinHostPort(spec.Host, spec.Port),
PD: strings.Join(pds, ","),
GCTTL: spec.GCTTL,
TZ: spec.TZ,
TLSEnabled: enableTLS,
DeployDir: paths.Deploy,
LogDir: paths.Log,
DataDir: paths.Data[0],
NumaNode: spec.NumaNode,
}
// doesn't work.
if _, err := i.setTLSConfig(ctx, false, nil, paths); err != nil {
return err
}
fp := filepath.Join(paths.Cache, fmt.Sprintf("run_tikv-cdc_%s_%d.sh", i.GetHost(), i.GetPort()))
if err := cfg.ConfigToFile(fp); err != nil {
return err
}
dst := filepath.Join(paths.Deploy, "scripts", "run_tikv-cdc.sh")
if err := e.Transfer(ctx, fp, dst, false, 0, false); err != nil {
return err
}
if _, _, err := e.Execute(ctx, "chmod +x "+dst, false); err != nil {
return err
}
return i.MergeServerConfig(ctx, e, globalConfig, instanceConfig, paths)
}
// setTLSConfig set TLS Config to support enable/disable TLS
func (i *TiKVCDCInstance) setTLSConfig(ctx context.Context, enableTLS bool, configs map[string]any, paths meta.DirPaths) (map[string]any, error) {
return nil, nil
}
var _ RollingUpdateInstance = &TiKVCDCInstance{}
// PreRestart implements RollingUpdateInstance interface.
// All errors are ignored, to trigger hard restart.
func (i *TiKVCDCInstance) PreRestart(ctx context.Context, topo Topology, apiTimeoutSeconds int, tlsCfg *tls.Config) error {
tidbTopo, ok := topo.(*Specification)
if !ok {
panic("should be type of tidb topology")
}
logger, ok := ctx.Value(logprinter.ContextKeyLogger).(*logprinter.Logger)
if !ok {
panic("logger not found")
}
address := utils.JoinHostPort(i.GetHost(), i.GetPort())
// cdc rolling upgrade strategy only works if there are more than 2 captures
if len(tidbTopo.TiKVCDCServers) <= 1 {
logger.Debugf("tikv-cdc pre-restart skipped, only one capture in the topology, addr: %s", address)
return nil
}
start := time.Now()
client := api.NewTiKVCDCOpenAPIClient(ctx, []string{utils.JoinHostPort(i.GetManageHost(), i.GetPort())}, 5*time.Second, tlsCfg)
captures, err := client.GetAllCaptures()
if err != nil {
logger.Debugf("tikv-cdc pre-restart skipped, cannot get all captures, trigger hard restart, addr: %s, elapsed: %+v", address, time.Since(start))
return nil
}
var (
captureID string
found bool
isOwner bool
)
for _, capture := range captures {
if address == capture.AdvertiseAddr {
found = true
captureID = capture.ID
isOwner = capture.IsOwner
break
}
}
// this may happen if the capture crashed right away.
if !found {
logger.Debugf("tikv-cdc pre-restart finished, cannot found the capture, trigger hard restart, captureID: %s, addr: %s, elapsed: %+v", captureID, address, time.Since(start))
return nil
}
if isOwner {
if err := client.ResignOwner(); err != nil {
// if resign the owner failed, no more need to drain the current capture,
// since it's not allowed by the cdc.
// return nil to trigger hard restart.
logger.Debugf("tikv-cdc pre-restart finished, resign owner failed, trigger hard restart, captureID: %s, addr: %s, elapsed: %+v", captureID, address, time.Since(start))
return nil
}
}
// TODO: support drain capture to make restart smooth.
logger.Debugf("tikv-cdc pre-restart success, captureID: %s, addr: %s, elapsed: %+v", captureID, address, time.Since(start))
return nil
}
// PostRestart implements RollingUpdateInstance interface.
func (i *TiKVCDCInstance) PostRestart(ctx context.Context, topo Topology, tlsCfg *tls.Config) error {
logger, ok := ctx.Value(logprinter.ContextKeyLogger).(*logprinter.Logger)
if !ok {
panic("logger not found")
}
start := time.Now()
address := utils.JoinHostPort(i.GetHost(), i.GetPort())
client := api.NewTiKVCDCOpenAPIClient(ctx, []string{utils.JoinHostPort(i.GetManageHost(), i.GetPort())}, 5*time.Second, tlsCfg)
err := client.IsCaptureAlive()
if err != nil {
logger.Debugf("tikv-cdc post-restart finished, get capture status failed, addr: %s, err: %+v, elapsed: %+v", address, err, time.Since(start))
return nil
}
logger.Debugf("tikv-cdc post-restart success, addr: %s, elapsed: %+v", address, time.Since(start))
return nil
}