-
Notifications
You must be signed in to change notification settings - Fork 269
/
openapi.go
339 lines (306 loc) · 8.64 KB
/
openapi.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// 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 dm
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/pingcap/tiflow/dm/pb"
"github.com/pingcap/tiflow/dm/pkg/terror"
"github.com/pingcap/tiflow/engine/jobmaster/dm/config"
"github.com/pingcap/tiflow/engine/jobmaster/dm/openapi"
dmpkg "github.com/pingcap/tiflow/engine/pkg/dm"
engineOpenAPI "github.com/pingcap/tiflow/engine/pkg/openapi"
"github.com/pingcap/tiflow/pkg/errors"
)
// errCodePrefix is the prefix that attach to terror's error code string.
// e.g. codeDBBadConn -> DM:ErrDBBadConn, codeDBInvalidConn -> DM:ErrDBInvalidConn
// This is used to distinguish with other components' error code, such as DFLOW and CDC.
// TODO: replace all terror usage with pkg/errors, need further discussion.
const errCodePrefix = "DM:Err"
func (jm *JobMaster) initOpenAPI(router *gin.RouterGroup) {
router.Use(httpErrorHandler())
router.Use(func(c *gin.Context) {
if !jm.initialized.Load() {
_ = c.Error(errors.ErrJobNotRunning.GenWithStackByArgs(jm.ID()))
c.Abort()
return
}
})
wrapper := openapi.ServerInterfaceWrapper{
Handler: jm,
}
// copy from openapi.RegisterHandlersWithOptions
router.DELETE("/binlog/tasks/:task-name", wrapper.DMAPIDeleteBinlogOperator)
router.GET("/binlog/tasks/:task-name", wrapper.DMAPIGetBinlogOperator)
router.POST("/binlog/tasks/:task-name", wrapper.DMAPISetBinlogOperator)
router.GET("/config", wrapper.DMAPIGetJobConfig)
router.PUT("/config", wrapper.DMAPIUpdateJobConfig)
router.GET("/schema/tasks/:task-name", wrapper.DMAPIGetSchema)
router.PUT("/schema/tasks/:task-name", wrapper.DMAPISetSchema)
router.GET("/status", wrapper.DMAPIGetJobStatus)
router.PUT("/status", wrapper.DMAPIOperateJob)
}
// DMAPIGetJobStatus implements the api of get job status.
func (jm *JobMaster) DMAPIGetJobStatus(c *gin.Context, params openapi.DMAPIGetJobStatusParams) {
var tasks []string
if params.Tasks != nil {
tasks = *params.Tasks
}
resp, err := jm.QueryJobStatus(c.Request.Context(), tasks)
if err != nil {
// nolint:errcheck
_ = c.Error(err)
return
}
c.IndentedJSON(http.StatusOK, resp)
}
// TODO: extract terror from worker response
func httpErrorHandler() gin.HandlerFunc {
return func(c *gin.Context) {
c.Next()
gErr := c.Errors.Last()
if gErr == nil {
return
}
// Adapt to the error format of engine openapi.
var httpErr *engineOpenAPI.HTTPError
var tErr *terror.Error
if errors.As(gErr.Err, &tErr) {
code := errCodePrefix + tErr.Code().String()
message := tErr.Error()
httpErr = &engineOpenAPI.HTTPError{
Code: code,
Message: message,
}
} else {
httpErr = engineOpenAPI.NewHTTPError(gErr.Err)
}
c.JSON(errors.HTTPStatusCode(gErr.Err), httpErr)
}
}
// DMAPIGetJobConfig implements the api of get job config.
func (jm *JobMaster) DMAPIGetJobConfig(c *gin.Context) {
cfg, err := jm.GetJobCfg(c.Request.Context())
if err != nil {
// nolint:errcheck
_ = c.Error(err)
return
}
bs, err := cfg.Yaml()
if err != nil {
// nolint:errcheck
_ = c.Error(err)
return
}
c.IndentedJSON(http.StatusOK, string(bs))
}
// DMAPIUpdateJobConfig implements the api of update job config.
func (jm *JobMaster) DMAPIUpdateJobConfig(c *gin.Context) {
var req openapi.UpdateJobConfigRequest
if err := c.Bind(&req); err != nil {
// nolint:errcheck
_ = c.Error(err)
return
}
var jobCfg config.JobCfg
if err := jobCfg.Decode([]byte(req.Config)); err != nil {
// nolint:errcheck
_ = c.Error(err)
return
}
if err := jm.UpdateJobCfg(c.Request.Context(), &jobCfg); err != nil {
// nolint:errcheck
_ = c.Error(err)
return
}
c.Status(http.StatusOK)
}
// DMAPIOperateJob implements the api of operate job.
func (jm *JobMaster) DMAPIOperateJob(c *gin.Context) {
var req openapi.OperateJobRequest
if err := c.Bind(&req); err != nil {
// nolint:errcheck
_ = c.Error(err)
return
}
var op dmpkg.OperateType
switch req.Op {
case openapi.OperateJobRequestOpPause:
op = dmpkg.Pause
case openapi.OperateJobRequestOpResume:
op = dmpkg.Resume
default:
// nolint:errcheck
_ = c.Error(errors.Errorf("unsupported op type '%s' for operate task", req.Op))
return
}
var tasks []string
if req.Tasks != nil {
tasks = *req.Tasks
}
err := jm.operateTask(c.Request.Context(), op, nil, tasks)
if err != nil {
// nolint:errcheck
_ = c.Error(err)
return
}
c.Status(http.StatusOK)
}
// DMAPIGetBinlogOperator implements the api of get binlog operator.
// TODO: pagination support if needed
func (jm *JobMaster) DMAPIGetBinlogOperator(c *gin.Context, taskName string, params openapi.DMAPIGetBinlogOperatorParams) {
req := &dmpkg.BinlogRequest{
Op: pb.ErrorOp_List,
Sources: []string{taskName},
}
if params.BinlogPos != nil {
req.BinlogPos = *params.BinlogPos
}
resp, err := jm.Binlog(c.Request.Context(), req)
if err != nil {
// nolint:errcheck
_ = c.Error(err)
return
}
c.IndentedJSON(http.StatusOK, resp)
}
// DMAPISetBinlogOperator implements the api of set binlog operator.
func (jm *JobMaster) DMAPISetBinlogOperator(c *gin.Context, taskName string) {
var req openapi.SetBinlogOperatorRequest
if err := c.Bind(&req); err != nil {
// nolint:errcheck
_ = c.Error(err)
return
}
r := &dmpkg.BinlogRequest{
Sources: []string{taskName},
}
if req.BinlogPos != nil {
r.BinlogPos = *req.BinlogPos
}
if req.Sqls != nil {
r.Sqls = *req.Sqls
}
switch req.Op {
case openapi.SetBinlogOperatorRequestOpInject:
r.Op = pb.ErrorOp_Inject
case openapi.SetBinlogOperatorRequestOpSkip:
r.Op = pb.ErrorOp_Skip
case openapi.SetBinlogOperatorRequestOpReplace:
r.Op = pb.ErrorOp_Replace
default:
// nolint:errcheck
_ = c.Error(errors.Errorf("unsupported op type '%s' for set binlog operator", req.Op))
return
}
resp, err := jm.Binlog(c.Request.Context(), r)
if err != nil {
// nolint:errcheck
_ = c.Error(err)
return
}
c.IndentedJSON(http.StatusCreated, resp)
}
// DMAPIDeleteBinlogOperator implements the api of delete binlog operator.
func (jm *JobMaster) DMAPIDeleteBinlogOperator(c *gin.Context, taskName string, params openapi.DMAPIDeleteBinlogOperatorParams) {
req := &dmpkg.BinlogRequest{
Op: pb.ErrorOp_Revert,
Sources: []string{taskName},
}
if params.BinlogPos != nil {
req.BinlogPos = *params.BinlogPos
}
resp, err := jm.Binlog(c.Request.Context(), req)
if err != nil {
// nolint:errcheck
_ = c.Error(err)
return
}
if resp.ErrorMsg != "" {
c.IndentedJSON(http.StatusOK, resp)
return
}
for _, r := range resp.Results {
if r.ErrorMsg != "" {
c.IndentedJSON(http.StatusOK, resp)
return
}
}
c.Status(http.StatusNoContent)
}
// DMAPIGetSchema implements the api of get schema.
func (jm *JobMaster) DMAPIGetSchema(c *gin.Context, taskname string, params openapi.DMAPIGetSchemaParams) {
var (
op pb.SchemaOp
database string
table string
)
if params.Database != nil {
database = *params.Database
}
if params.Table != nil {
table = *params.Table
}
switch {
case params.Target != nil && *params.Target:
op = pb.SchemaOp_ListMigrateTargets
case database != "" && table != "":
op = pb.SchemaOp_GetSchema
case database != "" && table == "":
op = pb.SchemaOp_ListTable
case database == "" && table == "":
op = pb.SchemaOp_ListSchema
default:
// nolint:errcheck
_ = c.Error(errors.New("invalid query params for get schema"))
return
}
r := &dmpkg.BinlogSchemaRequest{
Op: op,
Sources: []string{taskname},
Database: database,
Table: table,
}
resp := jm.BinlogSchema(c.Request.Context(), r)
c.IndentedJSON(http.StatusOK, resp)
}
// DMAPISetSchema implements the api of set schema.
func (jm *JobMaster) DMAPISetSchema(c *gin.Context, taskName string) {
var (
req openapi.SetBinlogSchemaRequest
fromSource bool
fromTarget bool
)
if err := c.Bind(&req); err != nil {
// nolint:errcheck
_ = c.Error(err)
return
}
if req.FromSource != nil {
fromSource = *req.FromSource
}
if req.FromTarget != nil {
fromTarget = *req.FromTarget
}
r := &dmpkg.BinlogSchemaRequest{
Op: pb.SchemaOp_SetSchema,
Sources: []string{taskName},
Database: req.Database,
Table: req.Table,
Schema: req.Sql,
FromSource: fromSource,
FromTarget: fromTarget,
}
resp := jm.BinlogSchema(c.Request.Context(), r)
c.IndentedJSON(http.StatusOK, resp)
}