Skip to content

Commit 9da3e80

Browse files
committedNov 21, 2017
[FAB-7035] Configure keepalive params for peer
Currently all of the various client/server gRPC keepalive parameters are hard-coded. This change makes the following settings configurable: - minimum permitted interval for client pings on the peer endpoint - keepalive interval and timeout for connections to other peers - keepalive interval and timeout for connections to orderer nodes - minimum permitted interval for client pings to the events endpoint Change-Id: I17b54a68cc2d974f22efd72e1c7892b284ac6506 Signed-off-by: Gari Singh <gari.r.singh@gmail.com>
1 parent a2ebd1b commit 9da3e80

File tree

7 files changed

+82
-2
lines changed

7 files changed

+82
-2
lines changed
 

‎core/comm/config.go

+6
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ func SetMaxSendMsgSize(size int) {
119119
maxSendMsgSize = size
120120
}
121121

122+
// DefaultKeepaliveOptions returns sane default keepalive settings for gRPC
123+
// servers and clients
124+
func DefaultKeepaliveOptions() *KeepaliveOptions {
125+
return keepaliveOptions
126+
}
127+
122128
// ServerKeepaliveOptions returns gRPC keepalive options for server. If
123129
// opts is nil, the default keepalive options are returned
124130
func ServerKeepaliveOptions(ka *KeepaliveOptions) []grpc.ServerOption {

‎core/comm/config_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ func TestConfig(t *testing.T) {
1717
// check the defaults
1818
assert.EqualValues(t, maxRecvMsgSize, MaxRecvMsgSize())
1919
assert.EqualValues(t, maxSendMsgSize, MaxSendMsgSize())
20+
assert.EqualValues(t, keepaliveOptions, DefaultKeepaliveOptions())
2021
assert.EqualValues(t, false, TLSEnabled())
2122
assert.EqualValues(t, true, configurationCached)
2223

‎core/deliverservice/deliveryclient.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/hyperledger/fabric/gossip/util"
2121
"github.com/hyperledger/fabric/protos/orderer"
2222
"github.com/op/go-logging"
23+
"github.com/spf13/viper"
2324
"golang.org/x/net/context"
2425
"google.golang.org/grpc"
2526
)
@@ -226,7 +227,16 @@ func DefaultConnectionFactory(channelID string) func(endpoint string) (*grpc.Cli
226227
dialOpts = append(dialOpts, grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(comm.MaxRecvMsgSize()),
227228
grpc.MaxCallSendMsgSize(comm.MaxSendMsgSize())))
228229
// set the keepalive options
229-
dialOpts = append(dialOpts, comm.ClientKeepaliveOptions(nil)...)
230+
kaOpts := comm.DefaultKeepaliveOptions()
231+
if viper.IsSet("peer.keepalive.deliveryClient.interval") {
232+
kaOpts.ClientInterval = viper.GetDuration(
233+
"peer.keepalive.deliveryClient.interval")
234+
}
235+
if viper.IsSet("peer.keepalive.deliveryClient.timeout") {
236+
kaOpts.ClientTimeout = viper.GetDuration(
237+
"peer.keepalive.deliveryClient.timeout")
238+
}
239+
dialOpts = append(dialOpts, comm.ClientKeepaliveOptions(kaOpts)...)
230240

231241
if comm.TLSEnabled() {
232242
creds, err := comm.GetCredentialSupport().GetDeliverServiceCredentials(channelID)

‎core/peer/config.go

+6
Original file line numberDiff line numberDiff line change
@@ -153,5 +153,11 @@ func GetServerConfig() (comm.ServerConfig, error) {
153153
secureOptions.ServerRootCAs = [][]byte{rootCert}
154154
}
155155
}
156+
// get the default keepalive options
157+
serverConfig.KaOpts = comm.DefaultKeepaliveOptions()
158+
// check to see if minInterval is set for the env
159+
if viper.IsSet("peer.keepalive.minInterval") {
160+
serverConfig.KaOpts.ServerMinInterval = viper.GetDuration("peer.keepalive.minInterval")
161+
}
156162
return serverConfig, nil
157163
}

‎core/peer/config_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import (
99
"net"
1010
"path/filepath"
1111
"testing"
12+
"time"
1213

14+
"github.com/hyperledger/fabric/core/comm"
1315
"github.com/spf13/viper"
1416
"github.com/stretchr/testify/assert"
1517
)
@@ -123,6 +125,14 @@ func TestGetServerConfig(t *testing.T) {
123125
assert.Equal(t, false, sc.SecOpts.UseTLS,
124126
"ServerConfig.SecOpts.UseTLS should be false")
125127

128+
// keepalive options
129+
assert.Equal(t, comm.DefaultKeepaliveOptions(), sc.KaOpts,
130+
"ServerConfig.KaOpts should be set to default values")
131+
viper.Set("peer.keepalive.minInterval", "2m")
132+
sc, _ = GetServerConfig()
133+
assert.Equal(t, time.Duration(2)*time.Minute, sc.KaOpts.ServerMinInterval,
134+
"ServerConfig.KaOpts.ServerMinInterval should be set to 2 min")
135+
126136
// good config with TLS
127137
viper.Set("peer.tls.enabled", true)
128138
viper.Set("peer.tls.cert.file", filepath.Join("testdata", "Org1-server1-cert.pem"))

‎peer/node/start.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,14 @@ func serve(args []string) error {
207207
dialOpts = append(dialOpts, grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(comm.MaxRecvMsgSize()),
208208
grpc.MaxCallSendMsgSize(comm.MaxSendMsgSize())))
209209
// set the keepalive options
210-
dialOpts = append(dialOpts, comm.ClientKeepaliveOptions(nil)...)
210+
kaOpts := comm.DefaultKeepaliveOptions()
211+
if viper.IsSet("peer.keepalive.client.interval") {
212+
kaOpts.ClientInterval = viper.GetDuration("peer.keepalive.client.interval")
213+
}
214+
if viper.IsSet("peer.keepalive.client.timeout") {
215+
kaOpts.ClientTimeout = viper.GetDuration("peer.keepalive.client.timeout")
216+
}
217+
dialOpts = append(dialOpts, comm.ClientKeepaliveOptions(kaOpts)...)
211218

212219
if comm.TLSEnabled() {
213220
comm.GetCredentialSupport().ClientCert = peerServer.ServerCertificate()
@@ -388,6 +395,12 @@ func createEventHubServer(serverConfig comm.ServerConfig) (comm.GRPCServer, erro
388395
return nil, fmt.Errorf("failed to listen: %v", err)
389396
}
390397

398+
// set the keepalive options
399+
serverConfig.KaOpts = comm.DefaultKeepaliveOptions()
400+
if viper.IsSet("peer.events.keepalive.minInterval") {
401+
serverConfig.KaOpts.ServerMinInterval = viper.GetDuration("peer.events.keepalive.minInterval")
402+
}
403+
391404
grpcServer, err := comm.NewGRPCServerFromListener(lis, serverConfig)
392405
if err != nil {
393406
logger.Errorf("Failed to return new GRPC server: %s", err)

‎sampleconfig/core.yaml

+34
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,33 @@ peer:
9393
# current setting
9494
gomaxprocs: -1
9595

96+
# Keepalive settings for peer server and clients
97+
keepalive:
98+
# MinInterval is the minimum permitted time between client pings.
99+
# If clients send pings more frequently, the peer server will
100+
# disconnect them
101+
minInterval: 60s
102+
# Client keepalive settings for communicating with other peer nodes
103+
client:
104+
# Interval is the time between pings to peer nodes. This must
105+
# greater than or equal to the minInterval specified by peer
106+
# nodes
107+
interval: 60s
108+
# Timeout is the duration the client waits for a response from
109+
# peer nodes before closing the connection
110+
timeout: 20s
111+
# DeliveryClient keepalive settings for communication with ordering
112+
# nodes.
113+
deliveryClient:
114+
# Interval is the time between pings to ordering nodes. This must
115+
# greater than or equal to the minInterval specified by ordering
116+
# nodes.
117+
interval: 60s
118+
# Timeout is the duration the client waits for a response from
119+
# ordering nodes before closing the connection
120+
timeout: 20s
121+
122+
96123
# Gossip related configuration
97124
gossip:
98125
# Bootstrap set to initialize gossip with.
@@ -219,6 +246,13 @@ peer:
219246
# time and the client's time as specified in a registration event
220247
timewindow: 15m
221248

249+
# Keepalive settings for peer server and clients
250+
keepalive:
251+
# MinInterval is the minimum permitted time in seconds which clients
252+
# can send keepalive pings. If clients send pings more frequently,
253+
# the events server will disconnect them
254+
minInterval: 60s
255+
222256
# TLS Settings
223257
# Note that peer-chaincode connections through chaincodeListenAddress is
224258
# not mutual TLS auth. See comments on chaincodeListenAddress for more info

0 commit comments

Comments
 (0)
Please sign in to comment.