forked from pingcap/tidb-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pump.go
182 lines (151 loc) · 4.4 KB
/
pump.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
// Copyright 2018 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 client
import (
"context"
"crypto/tls"
"net"
"sync"
"sync/atomic"
"time"
"github.com/pingcap/errors"
"github.com/pingcap/log"
"github.com/pingcap/tidb-tools/tidb-binlog/node"
pb "github.com/pingcap/tipb/go-binlog"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
var (
// localPump is used to write local pump through unix socket connection.
localPump = "localPump"
// if pump failed more than defaultMaxErrNums times, this pump can treated as unavaliable.
defaultMaxErrNums int64 = 10
)
// PumpStatus saves pump's status.
type PumpStatus struct {
/*
Pump has these state:
Online:
only when pump's state is online that pumps client can write binlog to.
Pausing:
this pump is pausing, and can't provide write binlog service. And this state will turn into Paused when pump is quit.
Paused:
this pump is paused, and can't provide write binlog service.
Closing:
this pump is closing, and can't provide write binlog service. And this state will turn into Offline when pump is quit.
Offline:
this pump is offline, and can't provide write binlog service forever.
*/
sync.RWMutex
node.Status
// the pump is avaliable or not, obsolete now
IsAvaliable bool
security *tls.Config
grpcConn *grpc.ClientConn
// the client of this pump
Client pb.PumpClient
ErrNum int64
}
// NewPumpStatus returns a new PumpStatus according to node's status.
func NewPumpStatus(status *node.Status, security *tls.Config) *PumpStatus {
pumpStatus := &PumpStatus{}
pumpStatus.Status = *status
pumpStatus.security = security
return pumpStatus
}
// createGrpcClient create grpc client for online pump.
func (p *PumpStatus) createGrpcClient() error {
// release the old connection, and create a new one
if p.grpcConn != nil {
p.grpcConn.Close()
}
var dialerOpt grpc.DialOption
if p.NodeID == localPump {
dialerOpt = grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
return net.DialTimeout("unix", addr, timeout)
})
} else {
dialerOpt = grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
return net.DialTimeout("tcp", addr, timeout)
})
}
log.Debug("[pumps client] create grpc client", zap.String("address", p.Addr))
var clientConn *grpc.ClientConn
var err error
if p.security != nil {
clientConn, err = grpc.Dial(p.Addr, dialerOpt, grpc.WithTransportCredentials(credentials.NewTLS(p.security)))
} else {
clientConn, err = grpc.Dial(p.Addr, dialerOpt, grpc.WithInsecure())
}
if err != nil {
atomic.AddInt64(&p.ErrNum, 1)
return errors.Trace(err)
}
p.grpcConn = clientConn
p.Client = pb.NewPumpClient(clientConn)
return nil
}
// ResetGrpcClient closes the pump's grpc connection.
func (p *PumpStatus) ResetGrpcClient() {
p.Lock()
defer p.Unlock()
if p.grpcConn != nil {
p.grpcConn.Close()
p.Client = nil
}
}
// Reset resets the pump's grpc conn and err num.
func (p *PumpStatus) Reset() {
p.ResetGrpcClient()
atomic.StoreInt64(&p.ErrNum, 0)
}
// WriteBinlog write binlog by grpc client.
func (p *PumpStatus) WriteBinlog(req *pb.WriteBinlogReq, timeout time.Duration) (*pb.WriteBinlogResp, error) {
p.RLock()
client := p.Client
p.RUnlock()
if client == nil {
p.Lock()
if p.Client != nil {
client = p.Client
} else {
err := p.createGrpcClient()
if err != nil {
p.Unlock()
return nil, errors.Errorf("create grpc connection for pump %s failed, error %v", p.NodeID, err)
}
client = p.Client
}
p.Unlock()
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
resp, err := client.WriteBinlog(ctx, req)
cancel()
if err != nil {
atomic.AddInt64(&p.ErrNum, 1)
} else {
atomic.StoreInt64(&p.ErrNum, 0)
}
return resp, err
}
// IsUsable returns true if pump is usable.
func (p *PumpStatus) IsUsable() bool {
if p.Status.State != node.Online {
return false
}
if atomic.LoadInt64(&p.ErrNum) > defaultMaxErrNums {
return false
}
return true
}