File: cdcapi.go

package info (click to toggle)
tiup 1.16.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,384 kB
  • sloc: sh: 1,988; makefile: 138; sql: 16
file content (376 lines) | stat: -rw-r--r-- 11,172 bytes parent folder | download
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// 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 api

import (
	"bytes"
	"context"
	"crypto/tls"
	"encoding/json"
	"fmt"
	"net/http"
	"time"

	"github.com/pingcap/errors"
	logprinter "github.com/pingcap/tiup/pkg/logger/printer"
	"github.com/pingcap/tiup/pkg/utils"
)

// CDCOpenAPIClient is client for access TiCDC Open API
type CDCOpenAPIClient struct {
	urls   []string
	client *utils.HTTPClient
	ctx    context.Context
}

// NewCDCOpenAPIClient return a `CDCOpenAPIClient`
func NewCDCOpenAPIClient(ctx context.Context, addresses []string, timeout time.Duration, tlsConfig *tls.Config) *CDCOpenAPIClient {
	httpPrefix := "http"
	if tlsConfig != nil {
		httpPrefix = "https"
	}
	urls := make([]string, 0, len(addresses))
	for _, addr := range addresses {
		urls = append(urls, fmt.Sprintf("%s://%s", httpPrefix, addr))
	}

	return &CDCOpenAPIClient{
		urls:   urls,
		client: utils.NewHTTPClient(timeout, tlsConfig),
		ctx:    ctx,
	}
}

func (c *CDCOpenAPIClient) getEndpoints(api string) (endpoints []string) {
	for _, url := range c.urls {
		endpoints = append(endpoints, fmt.Sprintf("%s/%s", url, api))
	}
	return endpoints
}

func drainCapture(client *CDCOpenAPIClient, target string) (int, error) {
	api := "api/v1/captures/drain"
	endpoints := client.getEndpoints(api)

	request := DrainCaptureRequest{
		CaptureID: target,
	}
	body, err := json.Marshal(request)
	if err != nil {
		return 0, err
	}

	var resp DrainCaptureResp
	_, err = tryURLs(endpoints, func(endpoint string) ([]byte, error) {
		data, statusCode, err := client.client.Put(client.ctx, endpoint, bytes.NewReader(body))
		if err != nil {
			switch statusCode {
			case http.StatusNotFound:
				// old version cdc does not support `DrainCapture`, return nil to trigger hard restart.
				client.l().Debugf("cdc drain capture does not support, ignore it, target: %s, err: %+v", target, err)
				return data, nil
			case http.StatusServiceUnavailable:
				if bytes.Contains(data, []byte("CDC:ErrVersionIncompatible")) {
					client.l().Debugf("cdc drain capture meet version incompatible, ignore it, target: %s, err: %+v", target, err)
					return data, nil
				}
				// cdc is not ready to accept request, return error to trigger retry.
				client.l().Debugf("cdc drain capture meet service unavailable, retry it, target: %s, err: %+v", target, err)
				return data, err
			default:
			}
			// match https://github.com/pingcap/tiflow/blob/e3d0d9d23b77c7884b70016ddbd8030ffeb95dfd/pkg/errors/cdc_errors.go#L55-L57
			if bytes.Contains(data, []byte("CDC:ErrSchedulerRequestFailed")) {
				client.l().Debugf("cdc drain capture failed, data: %s, err: %+v", data, err)
				return data, nil
			}
			// match https://github.com/pingcap/tiflow/blob/e3d0d9d23b77c7884b70016ddbd8030ffeb95dfd/pkg/errors/cdc_errors.go#L51-L54
			if bytes.Contains(data, []byte("CDC:ErrCaptureNotExist")) {
				client.l().Debugf("cdc drain capture failed, data: %s, err: %+v", data, err)
				return data, nil
			}
			client.l().Debugf("cdc drain capture failed, data: %s, statusCode: %d, err: %+v", data, statusCode, err)
			return data, err
		}
		return data, json.Unmarshal(data, &resp)
	})
	return resp.CurrentTableCount, err
}

// DrainCapture request cdc owner move all tables on the target capture to other captures.
func (c *CDCOpenAPIClient) DrainCapture(addr, target string, apiTimeoutSeconds int) error {
	if _, err := c.getCaptureByID(target); err != nil {
		c.l().Debugf("cdc drain capture failed, cannot find the capture, address: %s, target: %s, err: %+v", addr, target, err)
		return err
	}
	c.l().Infof("\t Start drain the capture, address: %s, captureID: %s", addr, target)
	start := time.Now()
	err := utils.Retry(func() error {
		count, err := drainCapture(c, target)
		if err != nil {
			return err
		}
		if count == 0 {
			return nil
		}
		c.l().Infof("\t Still waiting for %d tables to transfer...", count)
		return fmt.Errorf("drain capture not finished yet, target: %s, count: %d", target, count)
	}, utils.RetryOption{
		Delay:   1 * time.Second,
		Timeout: time.Duration(apiTimeoutSeconds) * time.Second,
	})

	c.l().Debugf("cdc drain capture finished, target: %s, elapsed: %+v", target, time.Since(start))
	return err
}

// ResignOwner resign the cdc owner, and wait for a new owner be found
// address is the current owner's address
func (c *CDCOpenAPIClient) ResignOwner(address string) error {
	err := utils.Retry(func() error {
		return resignOwner(c, address)
	}, utils.RetryOption{
		Delay:   2 * time.Second,
		Timeout: 10 * time.Second,
	})
	return err
}

func resignOwner(c *CDCOpenAPIClient, addr string) error {
	api := "api/v1/owner/resign"
	endpoints := c.getEndpoints(api)
	_, err := tryURLs(endpoints, func(endpoint string) ([]byte, error) {
		body, statusCode, err := c.client.PostWithStatusCode(c.ctx, endpoint, nil)
		if err != nil {
			if statusCode == http.StatusNotFound {
				c.l().Debugf("resign owner does not found, ignore it, err: %+v", err)
				return body, nil
			}
			return body, err
		}
		return body, nil
	})

	if err != nil {
		return err
	}

	owner, err := c.GetOwner()
	if err != nil {
		return err
	}

	if owner.AdvertiseAddr == addr {
		return fmt.Errorf("old owner in power again, resign again, owner: %+v", owner)
	}

	c.l().Debugf("cdc resign owner successfully, and new owner found, owner: %+v", owner)
	return nil
}

// GetOwner return the cdc owner capture information
func (c *CDCOpenAPIClient) GetOwner() (result *Capture, err error) {
	err = utils.Retry(func() error {
		captures, err := c.GetAllCaptures()
		if err != nil {
			return err
		}
		for _, capture := range captures {
			if capture.IsOwner {
				result = capture
				return nil
			}
		}
		return fmt.Errorf("no owner found")
	}, utils.RetryOption{
		Delay:   time.Second,
		Timeout: 10 * time.Second,
	})

	return result, err
}

func (c *CDCOpenAPIClient) getCaptureByID(id string) (*Capture, error) {
	var result *Capture
	err := utils.Retry(func() error {
		captures, err := c.GetAllCaptures()
		if err != nil {
			return err
		}
		for _, capture := range captures {
			if capture.ID == id {
				result = capture
				return nil
			}
		}
		return fmt.Errorf("target capture not found")
	}, utils.RetryOption{
		Delay:   time.Second,
		Timeout: 10 * time.Second,
	})
	return result, err
}

// GetCaptureByAddr return the capture information by the address
func (c *CDCOpenAPIClient) GetCaptureByAddr(addr string) (result *Capture, err error) {
	captures, err := c.GetAllCaptures()
	if err != nil {
		return nil, err
	}

	for _, capture := range captures {
		if capture.AdvertiseAddr == addr {
			return capture, nil
		}
	}
	return nil, fmt.Errorf("capture not found, addr: %s", addr)
}

// GetAllCaptures return all captures instantaneously
func (c *CDCOpenAPIClient) GetAllCaptures() ([]*Capture, error) {
	api := "api/v1/captures"
	endpoints := c.getEndpoints(api)

	var result []*Capture
	_, err := tryURLs(endpoints, func(endpoint string) ([]byte, error) {
		body, statusCode, err := c.client.GetWithStatusCode(c.ctx, endpoint)
		if err != nil {
			if statusCode == http.StatusNotFound {
				// old version cdc does not support open api, also the stopped cdc instance
				// return nil to trigger hard restart
				c.l().Debugf("get all captures not support, ignore it, err: %+v", err)
				return body, nil
			}
			return body, err
		}
		return body, json.Unmarshal(body, &result)
	})

	return result, err
}

// IsCaptureAlive return error if the capture is not alive
func (c *CDCOpenAPIClient) IsCaptureAlive() error {
	status, err := c.GetStatus()
	if err != nil {
		return err
	}
	if status.Liveness != LivenessCaptureAlive {
		return fmt.Errorf("capture is not alive, request url: %+v", c.urls[0])
	}
	return nil
}

// GetStatus return the status of the TiCDC server.
func (c *CDCOpenAPIClient) GetStatus() (result ServerStatus, err error) {
	api := "api/v1/status"
	// client should only have address to the target cdc server, not all cdc servers.
	endpoints := c.getEndpoints(api)

	err = utils.Retry(func() error {
		data, statusCode, err := c.client.GetWithStatusCode(c.ctx, endpoints[0])
		if err != nil {
			if statusCode == http.StatusNotFound {
				c.l().Debugf("capture server status api not support, ignore it, err: %+v", err)
				return nil
			}
			err = json.Unmarshal(data, &result)
			if err != nil {
				return err
			}
			if result.Liveness == LivenessCaptureAlive {
				return nil
			}
			return errors.New("capture status is not alive, retry it")
		}
		return nil
	}, utils.RetryOption{
		Timeout: 10 * time.Second,
	})

	return result, err
}

// Healthy return true if the TiCDC cluster is healthy
func (c *CDCOpenAPIClient) Healthy() error {
	err := utils.Retry(func() error {
		return isHealthy(c)
	}, utils.RetryOption{
		Timeout: 10 * time.Second,
	})
	return err
}

func isHealthy(client *CDCOpenAPIClient) error {
	api := "api/v1/health"
	endpoints := client.getEndpoints(api)

	_, err := tryURLs(endpoints, func(endpoint string) ([]byte, error) {
		_, statusCode, err := client.client.GetWithStatusCode(client.ctx, endpoint)
		if err != nil {
			switch statusCode {
			// It's likely the TiCDC does not support the API, return error to trigger hard restart.
			case http.StatusNotFound:
				client.l().Debugf("cdc check healthy does not support, ignore it")
				return nil, nil
			case http.StatusInternalServerError:
				client.l().Debugf("cdc check healthy: internal server error, retry it, err: %+v", err)
			}
			return nil, err
		}
		return nil, nil
	})
	return err
}

func (c *CDCOpenAPIClient) l() *logprinter.Logger {
	return c.ctx.Value(logprinter.ContextKeyLogger).(*logprinter.Logger)
}

// Liveness is the liveness status of a capture.
type Liveness int32

const (
	// LivenessCaptureAlive means the capture is alive, and ready to serve.
	LivenessCaptureAlive Liveness = 0
	// LivenessCaptureStopping means the capture is in the process of graceful shutdown.
	LivenessCaptureStopping Liveness = 1
)

// ServerStatus holds some common information of a TiCDC server
type ServerStatus struct {
	Version  string   `json:"version"`
	GitHash  string   `json:"git_hash"`
	ID       string   `json:"id"`
	Pid      int      `json:"pid"`
	IsOwner  bool     `json:"is_owner"`
	Liveness Liveness `json:"liveness"`
}

// Capture holds common information of a capture in cdc
type Capture struct {
	ID            string `json:"id"`
	IsOwner       bool   `json:"is_owner"`
	AdvertiseAddr string `json:"address"`
}

// DrainCaptureRequest is request for manual `DrainCapture`
type DrainCaptureRequest struct {
	CaptureID string `json:"capture_id"`
}

// DrainCaptureResp is response for manual `DrainCapture`
type DrainCaptureResp struct {
	CurrentTableCount int `json:"current_table_count"`
}