/
server.go
441 lines (372 loc) · 11 KB
/
server.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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// Copyright 2019 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 drainer
import (
"fmt"
"net"
"net/http"
"net/url"
"os"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/gorilla/mux"
"github.com/pingcap/errors"
"github.com/pingcap/log"
"github.com/pingcap/tidb-binlog/drainer/checkpoint"
"github.com/pingcap/tidb-binlog/pkg/flags"
"github.com/pingcap/tidb-binlog/pkg/node"
"github.com/pingcap/tidb-binlog/pkg/util"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/store/tikv"
"github.com/pingcap/tidb/store/tikv/oracle"
"github.com/pingcap/tipb/go-binlog"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/soheilhy/cmux"
"github.com/unrolled/render"
"go.uber.org/zap"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
var (
nodePrefix = "drainers"
heartbeatInterval = 1 * time.Second
getPdClient = util.GetPdClient
)
type drainerKeyType string
// Server implements the gRPC interface,
// and maintains the runtime status
type Server struct {
ID string
host string
cfg *Config
collector *Collector
tcpAddr string
gs *grpc.Server
metrics *util.MetricClient
ctx context.Context
cancel context.CancelFunc
tg taskGroup
syncer *Syncer
cp checkpoint.CheckPoint
isClosed int32
statusMu sync.RWMutex
status *node.Status
latestTS int64
latestTime time.Time
}
func init() {
// tracing has suspicious leak problem, so disable it here.
// it must be set before any real grpc operation.
grpc.EnableTracing = false
}
// NewServer return a instance of binlog-server
func NewServer(cfg *Config) (*Server, error) {
ID, err := genDrainerID(cfg.ListenAddr)
if err != nil {
return nil, errors.Trace(err)
}
if err := os.MkdirAll(cfg.DataDir, 0700); err != nil {
return nil, err
}
// get pd client and cluster ID
pdCli, err := getPdClient(cfg.EtcdURLs, cfg.Security)
if err != nil {
return nil, errors.Trace(err)
}
ctx, cancel := context.WithCancel(context.Background())
ctx = context.WithValue(ctx, drainerKeyType("compressor"), cfg.Compressor)
clusterID := pdCli.GetClusterID(ctx)
log.Info("get cluster id from pd", zap.Uint64("id", clusterID))
// update latestTS and latestTime
latestTS, err := util.GetTSO(pdCli)
if err != nil {
return nil, errors.Trace(err)
}
latestTime := time.Now()
cfg.SyncerCfg.To.ClusterID = clusterID
pdCli.Close()
cpCfg := GenCheckPointCfg(cfg, clusterID)
cp, err := checkpoint.NewCheckPoint(cfg.SyncerCfg.DestDBType, cpCfg)
if err != nil {
return nil, errors.Trace(err)
}
checkpointTSOGauge.Set(float64(oracle.ExtractPhysical(uint64(cp.TS()))))
syncer, err := createSyncer(cfg.EtcdURLs, cp, cfg.SyncerCfg)
if err != nil {
return nil, errors.Trace(err)
}
c, err := NewCollector(cfg, clusterID, syncer, cp)
if err != nil {
return nil, errors.Trace(err)
}
var metrics *util.MetricClient
if cfg.MetricsAddr != "" && cfg.MetricsInterval != 0 {
metrics = util.NewMetricClient(
cfg.MetricsAddr,
time.Duration(cfg.MetricsInterval)*time.Second,
registry,
)
}
advURL, err := url.Parse(cfg.AdvertiseAddr)
if err != nil {
return nil, errors.Annotatef(err, "invalid configuration of advertise addr(%s)", cfg.AdvertiseAddr)
}
status := node.NewStatus(ID, advURL.Host, node.Online, 0, syncer.GetLatestCommitTS(), util.GetApproachTS(latestTS, latestTime))
return &Server{
ID: ID,
host: advURL.Host,
cfg: cfg,
collector: c,
metrics: metrics,
tcpAddr: cfg.ListenAddr,
gs: grpc.NewServer(),
ctx: ctx,
cancel: cancel,
syncer: syncer,
cp: cp,
status: status,
latestTS: latestTS,
latestTime: latestTime,
}, nil
}
func createSyncer(etcdURLs string, cp checkpoint.CheckPoint, cfg *SyncerConfig) (syncer *Syncer, err error) {
tiStore, err := createTiStore(etcdURLs)
if err != nil {
return nil, errors.Trace(err)
}
defer tiStore.Close()
jobs, err := loadHistoryDDLJobs(tiStore)
if err != nil {
return nil, errors.Trace(err)
}
syncer, err = NewSyncer(cp, cfg, jobs)
if err != nil {
return nil, errors.Trace(err)
}
return
}
// DumpBinlog implements the gRPC interface of drainer server
func (s *Server) DumpBinlog(req *binlog.DumpBinlogReq, stream binlog.Cistern_DumpBinlogServer) (err error) {
return nil
}
// Notify implements the gRPC interface of drainer server
func (s *Server) Notify(ctx context.Context, in *binlog.NotifyReq) (*binlog.NotifyResp, error) {
log.Debug("recv Notify")
err := s.collector.Notify()
if err != nil {
log.Error("grpc call notify failed", zap.Error(err))
}
return nil, errors.Trace(err)
}
// DumpDDLJobs implements the gRPC interface of drainer server
func (s *Server) DumpDDLJobs(ctx context.Context, req *binlog.DumpDDLJobsReq) (resp *binlog.DumpDDLJobsResp, err error) {
return
}
func (s *Server) heartbeat(ctx context.Context) <-chan error {
errc := make(chan error, 1)
s.tg.Go("heartbeat", func() {
defer func() {
close(errc)
s.Close()
}()
for {
err := s.updateStatus()
if err != nil {
errc <- errors.Trace(err)
}
select {
case <-time.After(heartbeatInterval):
case <-ctx.Done():
return
}
}
})
return errc
}
// Start runs CisternServer to serve the listening addr, and starts to collect binlog
func (s *Server) Start() error {
// register drainer
if err := s.updateStatus(); err != nil {
return errors.Trace(err)
}
log.Info("register success", zap.String("drainer node id", s.ID))
// start heartbeat
errc := s.heartbeat(s.ctx)
go func() {
for err := range errc {
log.Error("send heart failed", zap.Error(err))
}
}()
s.tg.GoNoPanic("collect", func() {
defer s.Close()
s.collector.Start(s.ctx)
})
if s.metrics != nil {
s.tg.GoNoPanic("metrics", func() {
s.metrics.Start(s.ctx, map[string]string{"instance": s.ID})
})
}
s.tg.GoNoPanic("syncer", func() {
defer s.Close()
if err := s.syncer.Start(); err != nil {
log.Error("syncer exited abnormal", zap.Error(err))
}
})
// start a TCP listener
tcpURL, err := url.Parse(s.tcpAddr)
if err != nil {
return errors.Annotatef(err, "invalid listening tcp addr (%s)", s.tcpAddr)
}
tcpLis, err := net.Listen("tcp", tcpURL.Host)
if err != nil {
return errors.Annotatef(err, "fail to start TCP listener on %s", tcpURL.Host)
}
m := cmux.New(tcpLis)
grpcL := m.Match(cmux.HTTP2HeaderField("content-type", "application/grpc"))
httpL := m.Match(cmux.HTTP1Fast())
// register drainer server with gRPC server and start to serve listener
binlog.RegisterCisternServer(s.gs, s)
go s.gs.Serve(grpcL)
router := s.initAPIRouter()
http.Handle("/", router)
go http.Serve(httpL, nil)
log.Info("start to server request", zap.String("addr", s.tcpAddr))
if err := m.Serve(); !strings.Contains(err.Error(), "use of closed network connection") {
return errors.Trace(err)
}
return nil
}
// ApplyAction change the pump's state, now can be pause or close.
func (s *Server) ApplyAction(w http.ResponseWriter, r *http.Request) {
rd := render.New(render.Options{
IndentJSON: true,
})
vars := mux.Vars(r)
nodeID, action := vars["nodeID"], vars["action"]
log.Info("receive apply action request", zap.String("nodeID", nodeID), zap.String("action", action))
if nodeID != s.ID {
rd.JSON(w, http.StatusOK, util.ErrResponsef("invalid nodeID %s, this pump's nodeID is %s", nodeID, s.ID))
return
}
s.statusMu.RLock()
if s.status.State != node.Online {
rd.JSON(w, http.StatusOK, util.ErrResponsef("this pump's state is %s, apply %s failed!", s.status.State, action))
s.statusMu.RUnlock()
return
}
s.statusMu.RUnlock()
s.statusMu.Lock()
switch action {
case "pause":
s.status.State = node.Pausing
case "close":
s.status.State = node.Closing
default:
s.statusMu.Unlock()
rd.JSON(w, http.StatusOK, util.ErrResponsef("invalid action %s", action))
return
}
s.statusMu.Unlock()
go s.Close()
rd.JSON(w, http.StatusOK, util.SuccessResponse(fmt.Sprintf("apply action %s success!", action), nil))
}
// GetLatestTS returns the last binlog's commit ts which synced to downstream.
func (s *Server) GetLatestTS(w http.ResponseWriter, r *http.Request) {
rd := render.New(render.Options{
IndentJSON: true,
})
ts := s.syncer.GetLatestCommitTS()
rd.JSON(w, http.StatusOK, util.SuccessResponse("get drainer's latest ts success!", map[string]int64{"ts": ts}))
}
// commitStatus commit the node's last status to pd when close the server.
func (s *Server) commitStatus() {
// update this node
s.statusMu.Lock()
switch s.status.State {
case node.Pausing, node.Online:
s.status.State = node.Paused
case node.Closing:
s.status.State = node.Offline
}
s.statusMu.Unlock()
err := s.updateStatus()
if err != nil {
log.Error("update status failed", zap.String("id", s.ID))
return
}
log.Info("has already update status", zap.String("id", s.ID))
}
func (s *Server) updateStatus() error {
s.statusMu.Lock()
s.status.UpdateTS = util.GetApproachTS(s.latestTS, s.latestTime)
s.status.MaxCommitTS = s.syncer.GetLatestCommitTS()
status := node.CloneStatus(s.status)
s.statusMu.Unlock()
err := s.collector.reg.UpdateNode(context.Background(), nodePrefix, status)
if err != nil {
return errors.Trace(err)
}
return nil
}
func (s *Server) initAPIRouter() *mux.Router {
router := mux.NewRouter()
router.HandleFunc("/status", s.collector.Status).Methods("GET")
router.HandleFunc("/commit_ts", s.GetLatestTS).Methods("GET")
router.HandleFunc("/state/{nodeID}/{action}", s.ApplyAction).Methods("PUT")
prometheus.DefaultGatherer = registry
router.Handle("/metrics", promhttp.Handler())
return router
}
// Close stops all goroutines started by drainer server gracefully
func (s *Server) Close() {
if !atomic.CompareAndSwapInt32(&s.isClosed, 0, 1) {
log.Debug("server had closed")
return
}
log.Info("begin to close drainer server")
// update drainer's status
s.commitStatus()
log.Info("commit status done")
// notify all goroutines to exit
s.cancel()
s.syncer.Close()
// waiting for goroutines exit
s.tg.Wait()
// close the CheckPoint
err := s.cp.Close()
if err != nil {
log.Error("close checkpoint failed", zap.Error(err))
}
// stop gRPC server
s.gs.Stop()
log.Info("drainer exit")
}
func createTiStore(urls string) (kv.Storage, error) {
urlv, err := flags.NewURLsValue(urls)
if err != nil {
return nil, errors.Trace(err)
}
if err := session.RegisterStore("tikv", tikv.Driver{}); err != nil {
return nil, errors.Trace(err)
}
tiPath := fmt.Sprintf("tikv://%s?disableGC=true", urlv.HostString())
tiStore, err := session.NewStore(tiPath)
if err != nil {
return nil, errors.Trace(err)
}
return tiStore, nil
}