-
Notifications
You must be signed in to change notification settings - Fork 0
/
binloginfo.go
192 lines (166 loc) · 5.25 KB
/
binloginfo.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
// Copyright 2016 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 binloginfo
import (
"regexp"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/daiguadaidai/parser/terror"
"github.com/pingcap/tidb-tools/tidb-binlog/node"
pumpcli "github.com/pingcap/tidb-tools/tidb-binlog/pump_client"
"github.com/daiguadaidai/tidb/kv"
"github.com/daiguadaidai/tidb/metrics"
"github.com/daiguadaidai/tidb/sessionctx"
"github.com/daiguadaidai/tidb/util/logutil"
"github.com/pingcap/errors"
binlog "github.com/pingcap/tipb/go-binlog"
"go.uber.org/zap"
"google.golang.org/grpc"
)
func init() {
grpc.EnableTracing = false
}
// pumpsClient is the client to write binlog, it is opened on server start and never close,
// shared by all sessions.
var pumpsClient *pumpcli.PumpsClient
var pumpsClientLock sync.RWMutex
var shardPat = regexp.MustCompile(`SHARD_ROW_ID_BITS\s*=\s*\d+`)
// BinlogInfo contains binlog data and binlog client.
type BinlogInfo struct {
Data *binlog.Binlog
Client *pumpcli.PumpsClient
}
// GetPumpsClient gets the pumps client instance.
func GetPumpsClient() *pumpcli.PumpsClient {
pumpsClientLock.RLock()
client := pumpsClient
pumpsClientLock.RUnlock()
return client
}
// SetPumpsClient sets the pumps client instance.
func SetPumpsClient(client *pumpcli.PumpsClient) {
pumpsClientLock.Lock()
pumpsClient = client
pumpsClientLock.Unlock()
}
// GetPrewriteValue gets binlog prewrite value in the context.
func GetPrewriteValue(ctx sessionctx.Context, createIfNotExists bool) *binlog.PrewriteValue {
vars := ctx.GetSessionVars()
v, ok := vars.TxnCtx.Binlog.(*binlog.PrewriteValue)
if !ok && createIfNotExists {
schemaVer := ctx.GetSessionVars().TxnCtx.SchemaVersion
v = &binlog.PrewriteValue{SchemaVersion: schemaVer}
vars.TxnCtx.Binlog = v
}
return v
}
var skipBinlog uint32
var ignoreError uint32
// DisableSkipBinlogFlag disable the skipBinlog flag.
func DisableSkipBinlogFlag() {
atomic.StoreUint32(&skipBinlog, 0)
logutil.BgLogger().Warn("[binloginfo] disable the skipBinlog flag")
}
// SetIgnoreError sets the ignoreError flag, this function called when TiDB start
// up and find config.Binlog.IgnoreError is true.
func SetIgnoreError(on bool) {
if on {
atomic.StoreUint32(&ignoreError, 1)
} else {
atomic.StoreUint32(&ignoreError, 0)
}
}
// WriteBinlog writes a binlog to Pump.
func (info *BinlogInfo) WriteBinlog(clusterID uint64) error {
skip := atomic.LoadUint32(&skipBinlog)
if skip > 0 {
metrics.CriticalErrorCounter.Add(1)
return nil
}
if info.Client == nil {
return errors.New("pumps client is nil")
}
// it will retry in PumpsClient if write binlog fail.
err := info.Client.WriteBinlog(info.Data)
if err != nil {
logutil.BgLogger().Error("write binlog failed", zap.Error(err))
if atomic.LoadUint32(&ignoreError) == 1 {
logutil.BgLogger().Error("write binlog fail but error ignored")
metrics.CriticalErrorCounter.Add(1)
// If error happens once, we'll stop writing binlog.
atomic.CompareAndSwapUint32(&skipBinlog, skip, skip+1)
return nil
}
if strings.Contains(err.Error(), "received message larger than max") {
// This kind of error is not critical, return directly.
return errors.Errorf("binlog data is too large (%s)", err.Error())
}
return terror.ErrCritical.GenWithStackByArgs(err)
}
return nil
}
// SetDDLBinlog sets DDL binlog in the kv.Transaction.
func SetDDLBinlog(client *pumpcli.PumpsClient, txn kv.Transaction, jobID int64, ddlQuery string) {
if client == nil {
return
}
ddlQuery = addSpecialComment(ddlQuery)
info := &BinlogInfo{
Data: &binlog.Binlog{
Tp: binlog.BinlogType_Prewrite,
DdlJobId: jobID,
DdlQuery: []byte(ddlQuery),
},
Client: client,
}
txn.SetOption(kv.BinlogInfo, info)
}
const specialPrefix = `/*!90000 `
func addSpecialComment(ddlQuery string) string {
if strings.Contains(ddlQuery, specialPrefix) {
return ddlQuery
}
loc := shardPat.FindStringIndex(strings.ToUpper(ddlQuery))
if loc == nil {
return ddlQuery
}
return ddlQuery[:loc[0]] + specialPrefix + ddlQuery[loc[0]:loc[1]] + ` */` + ddlQuery[loc[1]:]
}
// MockPumpsClient creates a PumpsClient, used for test.
func MockPumpsClient(client binlog.PumpClient) *pumpcli.PumpsClient {
nodeID := "pump-1"
pump := &pumpcli.PumpStatus{
Status: node.Status{
NodeID: nodeID,
State: node.Online,
},
Client: client,
}
pumpInfos := &pumpcli.PumpInfos{
Pumps: make(map[string]*pumpcli.PumpStatus),
AvaliablePumps: make(map[string]*pumpcli.PumpStatus),
UnAvaliablePumps: make(map[string]*pumpcli.PumpStatus),
}
pumpInfos.Pumps[nodeID] = pump
pumpInfos.AvaliablePumps[nodeID] = pump
pCli := &pumpcli.PumpsClient{
ClusterID: 1,
Pumps: pumpInfos,
Selector: pumpcli.NewSelector(pumpcli.Range),
BinlogWriteTimeout: time.Second,
}
pCli.Selector.SetPumps([]*pumpcli.PumpStatus{pump})
return pCli
}