forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binloginfo.go
110 lines (98 loc) · 2.96 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
// 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 (
"sync"
"time"
log "github.com/Sirupsen/logrus"
"github.com/juju/errors"
"github.com/pingcap/tidb/context"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/terror"
"github.com/pingcap/tipb/go-binlog"
goctx "golang.org/x/net/context"
grpc "google.golang.org/grpc"
)
func init() {
grpc.EnableTracing = false
}
// pumpClient is the gRPC client to write binlog, it is opened on server start and never close,
// shared by all sessions.
var pumpClient binlog.PumpClient
var pumpClientLock sync.RWMutex
// BinlogInfo contains binlog data and binlog client.
type BinlogInfo struct {
Data *binlog.Binlog
Client binlog.PumpClient
}
// GetPumpClient gets the pump client instance.
func GetPumpClient() binlog.PumpClient {
pumpClientLock.RLock()
client := pumpClient
pumpClientLock.RUnlock()
return client
}
// SetPumpClient sets the pump client instance.
func SetPumpClient(client binlog.PumpClient) {
pumpClientLock.Lock()
pumpClient = client
pumpClientLock.Unlock()
}
// GetPrewriteValue gets binlog prewrite value in the context.
func GetPrewriteValue(ctx context.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
}
// WriteBinlog writes a binlog to Pump.
func (info *BinlogInfo) WriteBinlog(clusterID uint64) error {
commitData, err := info.Data.Marshal()
if err != nil {
return errors.Trace(err)
}
req := &binlog.WriteBinlogReq{ClusterID: clusterID, Payload: commitData}
// Retry many times because we may raise CRITICAL error here.
for i := 0; i < 20; i++ {
var resp *binlog.WriteBinlogResp
resp, err = info.Client.WriteBinlog(goctx.Background(), req)
if err == nil && resp.Errmsg != "" {
err = errors.New(resp.Errmsg)
}
if err == nil {
return nil
}
log.Errorf("write binlog error %v", err)
time.Sleep(time.Second)
}
return terror.ErrCritical.GenByArgs(err)
}
// SetDDLBinlog sets DDL binlog in the kv.Transaction.
func SetDDLBinlog(client interface{}, txn kv.Transaction, jobID int64, ddlQuery string) {
if client == nil {
return
}
info := &BinlogInfo{
Data: &binlog.Binlog{
Tp: binlog.BinlogType_Prewrite,
DdlJobId: jobID,
DdlQuery: []byte(ddlQuery),
},
Client: client.(binlog.PumpClient),
}
txn.SetOption(kv.BinlogInfo, info)
}