/
debug.go
235 lines (201 loc) · 6.42 KB
/
debug.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
// 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 collector
import (
"context"
"crypto/tls"
"fmt"
"path/filepath"
"time"
"github.com/joomcode/errorx"
"github.com/pingcap/diag/pkg/models"
perrs "github.com/pingcap/errors"
"github.com/pingcap/tiup/pkg/cluster/ctxt"
operator "github.com/pingcap/tiup/pkg/cluster/operation"
"github.com/pingcap/tiup/pkg/cluster/task"
logprinter "github.com/pingcap/tiup/pkg/logger/printer"
"github.com/pingcap/tiup/pkg/set"
"github.com/pingcap/tiup/pkg/utils"
)
// DebugCollectOptions are options used collecting debug info
type DebugCollectOptions struct {
*BaseOptions
opt *operator.Options // global operations from cli
resultDir string
fileStats map[string][]CollectStat
tlsCfg *tls.Config
}
// Desc implements the Collector interface
func (c *DebugCollectOptions) Desc() string {
return "Debug info of components"
}
// GetBaseOptions implements the Collector interface
func (c *DebugCollectOptions) GetBaseOptions() *BaseOptions {
return c.BaseOptions
}
// SetBaseOptions implements the Collector interface
func (c *DebugCollectOptions) SetBaseOptions(opt *BaseOptions) {
c.BaseOptions = opt
}
// SetGlobalOperations sets the global operation fileds
func (c *DebugCollectOptions) SetGlobalOperations(opt *operator.Options) {
c.opt = opt
}
// SetDir sets the result directory path
func (c *DebugCollectOptions) SetDir(dir string) {
c.resultDir = dir
}
// Prepare implements the Collector interface
func (c *DebugCollectOptions) Prepare(m *Manager, topo *models.TiDBCluster) (map[string][]CollectStat, error) {
switch m.mode {
case CollectModeTiUP:
return c.prepareForTiUP(m, topo)
}
return nil, nil
}
// // prepareForTiUP implements preparation for tiup-cluster deployed clusters
func (c *DebugCollectOptions) prepareForTiUP(_ *Manager, topo *models.TiDBCluster) (map[string][]CollectStat, error) {
// filter nodes or roles
roleFilter := set.NewStringSet(c.opt.Roles...)
nodeFilter := set.NewStringSet(c.opt.Nodes...)
comps := topo.Components()
comps = models.FilterComponent(comps, roleFilter)
instances := models.FilterInstance(comps, nodeFilter)
for _, inst := range instances {
switch inst.Type() {
case models.ComponentTypeTiCDC:
stat := CollectStat{
Target: fmt.Sprintf("%s:%d %s debug", inst.Host(), inst.MainPort(), inst.Type()),
Size: 1024 * 5 * 5,
}
c.fileStats[inst.Host()] = append(c.fileStats[inst.Host()], stat)
}
}
return c.fileStats, nil
}
// Collect implements the Collector interface
func (c *DebugCollectOptions) Collect(m *Manager, topo *models.TiDBCluster) error {
ctx := ctxt.New(
context.Background(),
c.opt.Concurrency,
m.logger,
)
collecteDebugTasks := []*task.StepDisplay{}
// filter nodes or roles
roleFilter := set.NewStringSet(c.opt.Roles...)
nodeFilter := set.NewStringSet(c.opt.Nodes...)
comps := topo.Components()
comps = models.FilterComponent(comps, roleFilter)
instances := models.FilterInstance(comps, nodeFilter)
// build tsaks
for _, inst := range instances {
if t := buildDebugCollectingTasks(ctx, inst, c); len(t) != 0 {
collecteDebugTasks = append(collecteDebugTasks, t...)
}
}
t := task.NewBuilder(m.logger).
ParallelStep("+ Query Debug info", false, collecteDebugTasks...).Build()
if err := t.Execute(ctx); err != nil {
if errorx.Cast(err) != nil {
// FIXME: Map possible task errors and give suggestions.
return err
}
return perrs.Trace(err)
}
return nil
}
type debugConfig struct {
filepath string
url string
}
// buildDebugCollectingTasks build collect debug information tasks
func buildDebugCollectingTasks(ctx context.Context, inst models.Component, c *DebugCollectOptions) []*task.StepDisplay {
var (
debugTasks []*task.StepDisplay
debugConfigs []debugConfig
)
host := inst.Host()
instDir, ok := inst.Attributes()["deploy_dir"].(string)
if !ok {
// for tidb-operator deployed cluster
instDir = ""
}
if pod, ok := inst.Attributes()["pod"].(string); ok {
host = pod
}
switch inst.Type() {
case models.ComponentTypeTiCDC:
// /debug/info
debugConfigs = append(debugConfigs,
debugConfig{
filepath: filepath.Join(c.resultDir, host, instDir, CollectTypeDebug, "info.txt"),
url: fmt.Sprintf("%s/debug/info", inst.StatusURL()),
})
// /status
debugConfigs = append(debugConfigs,
debugConfig{
filepath: filepath.Join(c.resultDir, host, instDir, CollectTypeDebug, "status.txt"),
url: fmt.Sprintf("%s/status", inst.StatusURL()),
})
// changefeeds
debugConfigs = append(debugConfigs,
debugConfig{
filepath: filepath.Join(c.resultDir, host, instDir, CollectTypeDebug, "changefeeds.txt"),
url: fmt.Sprintf("%s/api/v1/changefeeds", inst.StatusURL()),
})
// captures
debugConfigs = append(debugConfigs,
debugConfig{
filepath: filepath.Join(c.resultDir, host, instDir, CollectTypeDebug, "captures.txt"),
url: fmt.Sprintf("%s/api/v1/captures", inst.StatusURL()),
})
// processors
debugConfigs = append(debugConfigs,
debugConfig{
filepath: filepath.Join(c.resultDir, host, instDir, CollectTypeDebug, "processors.txt"),
url: fmt.Sprintf("%s/api/v1/processors", inst.StatusURL()),
})
default:
// not supported yet, just ignore
return nil
}
scheme := "http"
if c.tlsCfg != nil {
scheme = "https"
}
logger := ctx.Value(logprinter.ContextKeyLogger).(*logprinter.Logger)
t := task.NewBuilder(logger).
Func(
fmt.Sprintf("querying %s:%d", host, inst.MainPort()),
func(ctx context.Context) error {
c := utils.NewHTTPClient(time.Second*15, c.tlsCfg)
for _, config := range debugConfigs {
url := fmt.Sprintf("%s://%s", scheme, config.url)
err := c.Download(ctx, url, config.filepath)
if err != nil {
logger.Warnf("fail querying debug info %s: %s, continue", url, err)
return err
}
}
return nil
},
).
BuildAsStep(fmt.Sprintf(
" - Querying debug info for %s %s:%d",
inst.Type(),
inst.Host(),
inst.MainPort(),
))
debugTasks = append(debugTasks, t)
return debugTasks
}