-
Notifications
You must be signed in to change notification settings - Fork 312
/
binlog.go
235 lines (192 loc) · 5.59 KB
/
binlog.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 2020 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 (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/pingcap/errors"
"go.etcd.io/etcd/clientv3"
)
// BinlogClient is the client of binlog.
type BinlogClient struct {
tls *tls.Config
httpClient *http.Client
etcdClient *clientv3.Client
}
// NewBinlogClient create a BinlogClient.
func NewBinlogClient(pdEndpoint []string, tlsConfig *tls.Config) (*BinlogClient, error) {
etcdClient, err := clientv3.New(clientv3.Config{
Endpoints: pdEndpoint,
DialTimeout: time.Second * 5,
TLS: tlsConfig,
})
if err != nil {
return nil, errors.AddStack(err)
}
return &BinlogClient{
tls: tlsConfig,
httpClient: &http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsConfig,
},
},
etcdClient: etcdClient,
}, nil
}
func (c *BinlogClient) getURL(addr string) string {
schema := "http"
if c.tls != nil {
schema = "https"
}
return fmt.Sprintf("%s://%s", schema, addr)
}
func (c *BinlogClient) getOfflineURL(addr string, nodeID string) string {
return fmt.Sprintf("%s/state/%s/close", c.getURL(addr), nodeID)
}
// StatusResp represents the response of status api.
type StatusResp struct {
Code int `json:"code"`
Message string `json:"message"`
}
// NodeStatus represents the status saved in etcd.
type NodeStatus struct {
NodeID string `json:"nodeId"`
Addr string `json:"host"`
State string `json:"state"`
MaxCommitTS int64 `json:"maxCommitTS"`
UpdateTS int64 `json:"updateTS"`
}
// IsPumpTombstone check if drainer is tombstone.
func (c *BinlogClient) IsPumpTombstone(nodeID string) (bool, error) {
return c.isTombstone("pumps", nodeID)
}
// IsDrainerTombstone check if drainer is tombstone.
func (c *BinlogClient) IsDrainerTombstone(nodeID string) (bool, error) {
return c.isTombstone("drainer", nodeID)
}
func (c *BinlogClient) isTombstone(ty string, nodeID string) (bool, error) {
status, err := c.nodeStatus(ty)
if err != nil {
return false, err
}
for _, s := range status {
if s.NodeID == nodeID {
if s.State == "offline" {
return true, nil
}
return false, nil
}
}
return false, errors.Errorf("node not exist: %s", nodeID)
}
// nolint (unused)
func (c *BinlogClient) pumpNodeStatus() (status []*NodeStatus, err error) {
return c.nodeStatus("pumps")
}
// nolint (unused)
func (c *BinlogClient) drainerNodeStatus() (status []*NodeStatus, err error) {
return c.nodeStatus("drainers")
}
// UpdateDrainerState update the specify state as the specified state.
func (c *BinlogClient) UpdateDrainerState(nodeID string, state string) error {
return c.updateStatus("drainers", nodeID, state)
}
// UpdatePumpState update the specify state as the specified state.
func (c *BinlogClient) UpdatePumpState(nodeID string, state string) error {
return c.updateStatus("pumps", nodeID, state)
}
// updateStatus update the specify state as the specified state.
func (c *BinlogClient) updateStatus(ty string, nodeID string, state string) error {
key := fmt.Sprintf("/tidb-binlog/v1/%s/%s", ty, nodeID)
ctx := context.Background()
resp, err := c.etcdClient.KV.Get(ctx, key)
if err != nil {
return errors.AddStack(err)
}
var nodeStatus NodeStatus
err = json.Unmarshal(resp.Kvs[0].Value, &nodeStatus)
if err != nil {
return errors.AddStack(err)
}
if nodeStatus.State == state {
return nil
}
nodeStatus.State = state
data, err := json.Marshal(&nodeStatus)
if err != nil {
return errors.AddStack(err)
}
_, err = c.etcdClient.Put(ctx, key, string(data))
if err != nil {
return errors.AddStack(err)
}
return nil
}
func (c *BinlogClient) nodeStatus(ty string) (status []*NodeStatus, err error) {
key := fmt.Sprintf("/tidb-binlog/v1/%s", ty)
resp, err := c.etcdClient.KV.Get(context.Background(), key, clientv3.WithPrefix())
if err != nil {
return nil, errors.AddStack(err)
}
for _, kv := range resp.Kvs {
var s NodeStatus
err = json.Unmarshal(kv.Value, &s)
if err != nil {
return nil, errors.Annotatef(err, "key: %s,data: %s", string(kv.Key), string(kv.Value))
}
status = append(status, &s)
}
return
}
func (c *BinlogClient) offline(addr string, nodeID string) error {
url := c.getOfflineURL(addr, nodeID)
req, err := http.NewRequest("PUT", url, nil)
if err != nil {
return errors.AddStack(err)
}
resp, err := c.httpClient.Do(req)
if err != nil {
return errors.AddStack(err)
}
if resp.StatusCode < 200 || resp.StatusCode >= 400 {
return errors.Errorf("error requesting %s, code: %d",
resp.Request.URL, resp.StatusCode)
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return errors.AddStack(err)
}
var status StatusResp
err = json.Unmarshal(data, &status)
if err != nil {
return errors.Annotatef(err, "data: %s", string(data))
}
if status.Code != 200 {
return errors.Errorf("server error: %s", status.Message)
}
return nil
}
// OfflinePump offline a pump.
func (c *BinlogClient) OfflinePump(addr string, nodeID string) error {
return c.offline(addr, nodeID)
}
// OfflineDrainer offline a drainer.
func (c *BinlogClient) OfflineDrainer(addr string, nodeID string) error {
return c.offline(addr, nodeID)
}