Skip to content

Commit 78b49e8

Browse files
authored
sessionctx/binloginfo: add a timeout for writing binlog (#6588)
1 parent 0c78aac commit 78b49e8

4 files changed

Lines changed: 22 additions & 1 deletion

File tree

config/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ type TiKVClient struct {
223223
// Binlog is the config for binlog.
224224
type Binlog struct {
225225
BinlogSocket string `toml:"binlog-socket" json:"binlog-socket"`
226+
WriteTimeout string `toml:"write-timeout" json:"write-timeout"`
226227
// If IgnoreError is true, when writting binlog meets error, TiDB would
227228
// ignore the error.
228229
IgnoreError bool `toml:"ignore-error" json:"ignore-error"`
@@ -296,6 +297,9 @@ var defaultConf = Config{
296297
GrpcConnectionCount: 16,
297298
CommitTimeout: "41s",
298299
},
300+
Binlog: Binlog{
301+
WriteTimeout: "15s",
302+
},
299303
}
300304

301305
var globalConf = defaultConf

config/config.toml.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,9 @@ commit-timeout = "41s"
220220
# Socket file to write binlog.
221221
binlog-socket = ""
222222

223+
# WriteTimeout specifies how long it will wait for writing binlog to pump.
224+
write-timeout = "15s"
225+
223226
# If IgnoreError is true, when writting binlog meets error, TiDB would stop writting binlog,
224227
# but still provide service.
225228
ignore-error = false

sessionctx/binloginfo/binloginfo.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ func init() {
3535
grpc.EnableTracing = false
3636
}
3737

38+
var binlogWriteTimeout = 15 * time.Second
39+
3840
// pumpClient is the gRPC client to write binlog, it is opened on server start and never close,
3941
// shared by all sessions.
4042
var pumpClient binlog.PumpClient
@@ -54,6 +56,15 @@ func GetPumpClient() binlog.PumpClient {
5456
return client
5557
}
5658

59+
// SetGRPCTimeout sets grpc timeout for writing binlog.
60+
func SetGRPCTimeout(timeout time.Duration) {
61+
if timeout < 300*time.Millisecond {
62+
log.Warnf("set binlog grpc timeout %s ignored, use default value %s", timeout, binlogWriteTimeout)
63+
return // Avoid invalid value
64+
}
65+
binlogWriteTimeout = timeout
66+
}
67+
5768
// SetPumpClient sets the pump client instance.
5869
func SetPumpClient(client binlog.PumpClient) {
5970
pumpClientLock.Lock()
@@ -109,7 +120,9 @@ func (info *BinlogInfo) WriteBinlog(clusterID uint64) error {
109120
// Retry many times because we may raise CRITICAL error here.
110121
for i := 0; i < 20; i++ {
111122
var resp *binlog.WriteBinlogResp
112-
resp, err = info.Client.WriteBinlog(context.Background(), req)
123+
ctx, cancel := context.WithTimeout(context.Background(), binlogWriteTimeout)
124+
resp, err = info.Client.WriteBinlog(ctx, req)
125+
cancel()
113126
if err == nil && resp.Errmsg != "" {
114127
err = errors.New(resp.Errmsg)
115128
}

tidb-server/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ func setupBinlogClient() {
176176
if cfg.Binlog.IgnoreError {
177177
binloginfo.SetIgnoreError(true)
178178
}
179+
binloginfo.SetGRPCTimeout(parseDuration(cfg.Binlog.WriteTimeout))
179180
binloginfo.SetPumpClient(binlog.NewPumpClient(clientConn))
180181
log.Infof("created binlog client at %s, ignore error %v", cfg.Binlog.BinlogSocket, cfg.Binlog.IgnoreError)
181182
}

0 commit comments

Comments
 (0)