Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server: add grpc server config for a suitable behavior #30774

Merged
merged 4 commits into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,18 @@ type Status struct {
MetricsInterval uint `toml:"metrics-interval" json:"metrics-interval"`
ReportStatus bool `toml:"report-status" json:"report-status"`
RecordQPSbyDB bool `toml:"record-db-qps" json:"record-db-qps"`
// After a duration of this time in seconds if the server doesn't see any activity it pings
// the client to see if the transport is still alive.
GRPCKeepAliveTime uint `toml:"grpc-keepalive-time" json:"grpc-keepalive-time"`
// After having pinged for keepalive check, the server waits for a duration of timeout in seconds
// and if no activity is seen even after that the connection is closed.
GRPCKeepAliveTimeout uint `toml:"grpc-keepalive-timeout" json:"grpc-keepalive-timeout"`
// The number of max concurrent streams/requests on a client connection.
GRPCConcurrentStreams uint `toml:"grpc-concurrent-streams" json:"grpc-concurrent-streams"`
// Sets window size for stream. The default value is 2MB.
GRPCInitialWindowSize int `toml:"grpc-initial-window-size" json:"grpc-initial-window-size"`
// Set maximum message length in bytes that gRPC can send. `-1` means unlimited. The default value is 10MB.
GRPCMaxSendMsgSize int `toml:"grpc-max-send-msg-size" json:"grpc-max-send-msg-size"`
}

// Performance is the performance section of the config.
Expand Down Expand Up @@ -658,11 +670,16 @@ var defaultConf = Config{
EnableSlowLog: *NewAtomicBool(logutil.DefaultTiDBEnableSlowLog),
},
Status: Status{
ReportStatus: true,
StatusHost: DefStatusHost,
StatusPort: DefStatusPort,
MetricsInterval: 15,
RecordQPSbyDB: false,
ReportStatus: true,
StatusHost: DefStatusHost,
StatusPort: DefStatusPort,
MetricsInterval: 15,
RecordQPSbyDB: false,
GRPCKeepAliveTime: 10,
GRPCKeepAliveTimeout: 3,
GRPCConcurrentStreams: 1024,
GRPCInitialWindowSize: 2 * 1024 * 1024,
GRPCMaxSendMsgSize: 10 * 1024 * 1024,
},
Performance: Performance{
MaxMemory: 0,
Expand Down
11 changes: 11 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,12 @@ deadlock-history-capacity = 123
deadlock-history-collect-retryable = true
[top-sql]
receiver-address = "127.0.0.1:10100"
[status]
grpc-keepalive-time = 20
grpc-keepalive-timeout = 10
grpc-concurrent-streams = 2048
grpc-initial-window-size = 10240
grpc-max-send-msg-size = 40960
`)

require.NoError(t, err)
Expand Down Expand Up @@ -318,6 +324,11 @@ receiver-address = "127.0.0.1:10100"
require.False(t, conf.Experimental.EnableNewCharset)
require.Equal(t, "127.0.0.1:10100", conf.TopSQL.ReceiverAddress)
require.True(t, conf.Experimental.AllowsExpressionIndex)
require.Equal(t, uint(20), conf.Status.GRPCKeepAliveTime)
require.Equal(t, uint(10), conf.Status.GRPCKeepAliveTimeout)
require.Equal(t, uint(2048), conf.Status.GRPCConcurrentStreams)
require.Equal(t, 10240, conf.Status.GRPCInitialWindowSize)
require.Equal(t, 40960, conf.Status.GRPCMaxSendMsgSize)

err = f.Truncate(0)
require.NoError(t, err)
Expand Down
12 changes: 11 additions & 1 deletion server/rpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"fmt"
"net"
"time"

"github.com/pingcap/kvproto/pkg/coprocessor"
"github.com/pingcap/kvproto/pkg/diagnosticspb"
Expand All @@ -34,6 +35,7 @@ import (
"github.com/pingcap/tidb/util/memory"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/keepalive"
"google.golang.org/grpc/peer"
)

Expand All @@ -46,7 +48,15 @@ func NewRPCServer(config *config.Config, dom *domain.Domain, sm util.SessionMana
}
}()

s := grpc.NewServer()
s := grpc.NewServer(
grpc.KeepaliveParams(keepalive.ServerParameters{
Time: time.Duration(config.Status.GRPCKeepAliveTime) * time.Second,
Timeout: time.Duration(config.Status.GRPCKeepAliveTimeout) * time.Second,
}),
grpc.MaxConcurrentStreams(uint32(config.Status.GRPCConcurrentStreams)),
grpc.InitialWindowSize(int32(config.Status.GRPCInitialWindowSize)),
grpc.MaxSendMsgSize(config.Status.GRPCMaxSendMsgSize),
)
rpcSrv := &rpcServer{
DiagnosticsServer: sysutil.NewDiagnosticsServer(config.Log.File.Filename),
dom: dom,
Expand Down