[cli] [grpccommon] Migrate flags to `pflag` (#11122)

Closes #10807.

Signed-off-by: Andrew Mason <andrew@planetscale.com>
This commit is contained in:
Andrew Mason 2022-08-31 06:29:02 -04:00 коммит произвёл GitHub
Родитель a6dd52a74a
Коммит 39ee44f132
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
13 изменённых файлов: 80 добавлений и 37 удалений

Просмотреть файл

@ -25,6 +25,7 @@ import (
"github.com/spf13/pflag"
"vitess.io/vitess/go/exit"
"vitess.io/vitess/go/vt/grpccommon"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/logutil"
"vitess.io/vitess/go/vt/topo"
@ -56,6 +57,7 @@ func main() {
defer logutil.Flush()
fs := pflag.NewFlagSet("topo2topo", pflag.ExitOnError)
grpccommon.RegisterFlags(fs)
log.RegisterFlags(fs)
logutil.RegisterFlags(fs)
_flag.Parse(fs)

Просмотреть файл

@ -27,6 +27,7 @@ import (
"vitess.io/vitess/go/exit"
"vitess.io/vitess/go/vt/dbconfigs"
"vitess.io/vitess/go/vt/grpccommon"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/logutil"
"vitess.io/vitess/go/vtbench"
@ -107,6 +108,7 @@ func main() {
flag.Lookup("logtostderr").Value.Set("true")
fs := pflag.NewFlagSet("vtbench", pflag.ExitOnError)
grpccommon.RegisterFlags(fs)
log.RegisterFlags(fs)
logutil.RegisterFlags(fs)
_flag.Parse(fs)

Просмотреть файл

@ -34,6 +34,7 @@ import (
"github.com/spf13/pflag"
"vitess.io/vitess/go/vt/concurrency"
"vitess.io/vitess/go/vt/grpccommon"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/logutil"
"vitess.io/vitess/go/vt/sqlparser"
@ -149,6 +150,7 @@ func main() {
func run() (*results, error) {
fs := pflag.NewFlagSet("vtclient", pflag.ExitOnError)
grpccommon.RegisterFlags(fs)
log.RegisterFlags(fs)
logutil.RegisterFlags(fs)
_flag.Parse(fs)

Просмотреть файл

@ -26,6 +26,7 @@ import (
"github.com/spf13/pflag"
_flag "vitess.io/vitess/go/internal/flag"
"vitess.io/vitess/go/vt/grpccommon"
"vitess.io/vitess/go/vt/log"
vtlog "vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/logutil"
@ -102,6 +103,7 @@ func main() {
// TODO(ajm188): after v15, remove this pflag hack and use servenv.ParseFlags
// directly.
fs := pflag.NewFlagSet("vtorc", pflag.ExitOnError)
grpccommon.RegisterFlags(fs)
vtlog.RegisterFlags(fs)
logutil.RegisterFlags(fs)

Просмотреть файл

@ -79,7 +79,7 @@ Usage of vtctld:
--grpc_compression string Which protocol to use for compressing gRPC. Default: nothing. Supported: snappy
--grpc_crl string path to a certificate revocation list in PEM format, client certificates will be further verified against this file during TLS handshake
--grpc_enable_optional_tls enable optional TLS mode when a server accepts both TLS and plain-text connections on the same port
--grpc_enable_tracing Enable GRPC tracing
--grpc_enable_tracing Enable gRPC tracing.
--grpc_initial_conn_window_size int gRPC initial connection window size
--grpc_initial_window_size int gRPC initial window size
--grpc_keepalive_time duration After a duration of this time, if the client doesn't see any activity, it pings the server to see if the transport is still alive. (default 10s)
@ -89,7 +89,7 @@ Usage of vtctld:
--grpc_max_connection_age_grace duration Additional grace period after grpc_max_connection_age, after which connections are forcibly closed. (default 2562047h47m16.854775807s)
--grpc_max_message_size int Maximum allowed RPC message size. Larger messages will be rejected by gRPC with the error 'exceeding the max size'. (default 16777216)
--grpc_port int Port to listen on for gRPC calls
--grpc_prometheus Enable gRPC monitoring with Prometheus
--grpc_prometheus Enable gRPC monitoring with Prometheus.
--grpc_server_ca string path to server CA in PEM format, which will be combine with server cert, return full certificate chain to clients
--grpc_server_initial_conn_window_size int gRPC server initial connection window size
--grpc_server_initial_window_size int gRPC server initial window size

Просмотреть файл

@ -74,13 +74,10 @@ Usage of vtexplain:
--grpc_cert string server certificate to use for gRPC connections, requires grpc_key, enables TLS
--grpc_crl string path to a certificate revocation list in PEM format, client certificates will be further verified against this file during TLS handshake
--grpc_enable_optional_tls enable optional TLS mode when a server accepts both TLS and plain-text connections on the same port
--grpc_enable_tracing Enable GRPC tracing
--grpc_key string server private key to use for gRPC connections, requires grpc_cert, enables TLS
--grpc_max_connection_age duration Maximum age of a client connection before GoAway is sent. (default 2562047h47m16.854775807s)
--grpc_max_connection_age_grace duration Additional grace period after grpc_max_connection_age, after which connections are forcibly closed. (default 2562047h47m16.854775807s)
--grpc_max_message_size int Maximum allowed RPC message size. Larger messages will be rejected by gRPC with the error 'exceeding the max size'. (default 16777216)
--grpc_port int Port to listen on for gRPC calls
--grpc_prometheus Enable gRPC monitoring with Prometheus
--grpc_server_ca string path to server CA in PEM format, which will be combine with server cert, return full certificate chain to clients
--grpc_server_initial_conn_window_size int gRPC server initial connection window size
--grpc_server_initial_window_size int gRPC server initial window size

Просмотреть файл

@ -42,7 +42,7 @@ Usage of vtgate:
--grpc_compression string Which protocol to use for compressing gRPC. Default: nothing. Supported: snappy
--grpc_crl string path to a certificate revocation list in PEM format, client certificates will be further verified against this file during TLS handshake
--grpc_enable_optional_tls enable optional TLS mode when a server accepts both TLS and plain-text connections on the same port
--grpc_enable_tracing Enable GRPC tracing
--grpc_enable_tracing Enable gRPC tracing.
--grpc_initial_conn_window_size int gRPC initial connection window size
--grpc_initial_window_size int gRPC initial window size
--grpc_keepalive_time duration After a duration of this time, if the client doesn't see any activity, it pings the server to see if the transport is still alive. (default 10s)
@ -52,7 +52,7 @@ Usage of vtgate:
--grpc_max_connection_age_grace duration Additional grace period after grpc_max_connection_age, after which connections are forcibly closed. (default 2562047h47m16.854775807s)
--grpc_max_message_size int Maximum allowed RPC message size. Larger messages will be rejected by gRPC with the error 'exceeding the max size'. (default 16777216)
--grpc_port int Port to listen on for gRPC calls
--grpc_prometheus Enable gRPC monitoring with Prometheus
--grpc_prometheus Enable gRPC monitoring with Prometheus.
--grpc_server_ca string path to server CA in PEM format, which will be combine with server cert, return full certificate chain to clients
--grpc_server_initial_conn_window_size int gRPC server initial connection window size
--grpc_server_initial_window_size int gRPC server initial window size

Просмотреть файл

@ -21,7 +21,7 @@ Usage of vtgr:
--grpc_compression string Which protocol to use for compressing gRPC. Default: nothing. Supported: snappy
--grpc_crl string path to a certificate revocation list in PEM format, client certificates will be further verified against this file during TLS handshake
--grpc_enable_optional_tls enable optional TLS mode when a server accepts both TLS and plain-text connections on the same port
--grpc_enable_tracing Enable GRPC tracing
--grpc_enable_tracing Enable gRPC tracing.
--grpc_initial_conn_window_size int gRPC initial connection window size
--grpc_initial_window_size int gRPC initial window size
--grpc_keepalive_time duration After a duration of this time, if the client doesn't see any activity, it pings the server to see if the transport is still alive. (default 10s)
@ -31,7 +31,7 @@ Usage of vtgr:
--grpc_max_connection_age_grace duration Additional grace period after grpc_max_connection_age, after which connections are forcibly closed. (default 2562047h47m16.854775807s)
--grpc_max_message_size int Maximum allowed RPC message size. Larger messages will be rejected by gRPC with the error 'exceeding the max size'. (default 16777216)
--grpc_port int Port to listen on for gRPC calls
--grpc_prometheus Enable gRPC monitoring with Prometheus
--grpc_prometheus Enable gRPC monitoring with Prometheus.
--grpc_server_ca string path to server CA in PEM format, which will be combine with server cert, return full certificate chain to clients
--grpc_server_initial_conn_window_size int gRPC server initial connection window size
--grpc_server_initial_window_size int gRPC server initial window size

Просмотреть файл

@ -220,7 +220,7 @@ Usage of vttablet:
--grpc_compression string Which protocol to use for compressing gRPC. Default: nothing. Supported: snappy
--grpc_crl string path to a certificate revocation list in PEM format, client certificates will be further verified against this file during TLS handshake
--grpc_enable_optional_tls enable optional TLS mode when a server accepts both TLS and plain-text connections on the same port
--grpc_enable_tracing Enable GRPC tracing
--grpc_enable_tracing Enable gRPC tracing.
--grpc_initial_conn_window_size int gRPC initial connection window size
--grpc_initial_window_size int gRPC initial window size
--grpc_keepalive_time duration After a duration of this time, if the client doesn't see any activity, it pings the server to see if the transport is still alive. (default 10s)
@ -230,7 +230,7 @@ Usage of vttablet:
--grpc_max_connection_age_grace duration Additional grace period after grpc_max_connection_age, after which connections are forcibly closed. (default 2562047h47m16.854775807s)
--grpc_max_message_size int Maximum allowed RPC message size. Larger messages will be rejected by gRPC with the error 'exceeding the max size'. (default 16777216)
--grpc_port int Port to listen on for gRPC calls
--grpc_prometheus Enable gRPC monitoring with Prometheus
--grpc_prometheus Enable gRPC monitoring with Prometheus.
--grpc_server_ca string path to server CA in PEM format, which will be combine with server cert, return full certificate chain to clients
--grpc_server_initial_conn_window_size int gRPC server initial connection window size
--grpc_server_initial_window_size int gRPC server initial window size

Просмотреть файл

@ -102,10 +102,11 @@ func Dial(target string, failFast FailFast, opts ...grpc.DialOption) (*grpc.Clie
// what that should be.
func DialContext(ctx context.Context, target string, failFast FailFast, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
grpccommon.EnableTracingOpt()
msgSize := grpccommon.MaxMessageSize()
newopts := []grpc.DialOption{
grpc.WithDefaultCallOptions(
grpc.MaxCallRecvMsgSize(*grpccommon.MaxMessageSize),
grpc.MaxCallSendMsgSize(*grpccommon.MaxMessageSize),
grpc.MaxCallRecvMsgSize(msgSize),
grpc.MaxCallSendMsgSize(msgSize),
grpc.WaitForReady(bool(!failFast)),
),
}
@ -146,7 +147,7 @@ func DialContext(ctx context.Context, target string, failFast FailFast, opts ...
func interceptors() []grpc.DialOption {
builder := &clientInterceptorBuilder{}
if *grpccommon.EnableGRPCPrometheus {
if grpccommon.EnableGRPCPrometheus() {
builder.Add(grpc_prometheus.StreamClientInterceptor, grpc_prometheus.UnaryClientInterceptor)
}
trace.AddGrpcClientOptions(builder.Add)

Просмотреть файл

@ -17,38 +17,59 @@ limitations under the License.
package grpccommon
import (
"flag"
"sync"
"github.com/spf13/pflag"
"google.golang.org/grpc"
"vitess.io/vitess/go/stats"
)
var (
defaultMaxMessageSize = 16 * 1024 * 1024
// MaxMessageSize is the maximum message size which the gRPC server will
// maxMessageSize is the maximum message size which the gRPC server will
// accept. Larger messages will be rejected.
// Note: We're using 16 MiB as default value because that's the default in MySQL
MaxMessageSize = flag.Int("grpc_max_message_size", defaultMaxMessageSize, "Maximum allowed RPC message size. Larger messages will be rejected by gRPC with the error 'exceeding the max size'.")
// EnableTracing sets a flag to enable grpc client/server tracing.
EnableTracing = flag.Bool("grpc_enable_tracing", false, "Enable GRPC tracing")
// EnableGRPCPrometheus sets a flag to enable grpc client/server grpc monitoring.
EnableGRPCPrometheus = flag.Bool("grpc_prometheus", false, "Enable gRPC monitoring with Prometheus")
maxMessageSize = 16 * 1024 * 1024
// enableTracing sets a flag to enable grpc client/server tracing.
enableTracing bool
// enablePrometheus sets a flag to enable grpc client/server grpc monitoring.
enablePrometheus bool
)
var enableTracing sync.Once
// RegisterFlags installs grpccommon flags on the given FlagSet.
//
// `go/cmd/*` entrypoints should either use servenv.ParseFlags(WithArgs)? which
// calls this function, or call this function directly before parsing
// command-line arguments.
func RegisterFlags(fs *pflag.FlagSet) {
fs.IntVar(&maxMessageSize, "grpc_max_message_size", maxMessageSize, "Maximum allowed RPC message size. Larger messages will be rejected by gRPC with the error 'exceeding the max size'.")
fs.BoolVar(&enableTracing, "grpc_enable_tracing", enableTracing, "Enable gRPC tracing.")
fs.BoolVar(&enablePrometheus, "grpc_prometheus", enablePrometheus, "Enable gRPC monitoring with Prometheus.")
}
var (
enableTracingOnce sync.Once
)
// EnableTracingOpt enables grpc tracing if requested.
// It must be called before any grpc server or client is created but is safe
// to be called multiple times.
func EnableTracingOpt() {
enableTracing.Do(func() {
grpc.EnableTracing = *EnableTracing
enableTracingOnce.Do(func() {
grpc.EnableTracing = enableTracing
})
}
// EnableGRPCPrometheus returns the value of the --grpc_prometheus flag.
func EnableGRPCPrometheus() bool {
return enablePrometheus
}
// MaxMessageSize returns the value of the --grpc_max_message_size flag.
func MaxMessageSize() int {
return maxMessageSize
}
func init() {
stats.NewString("GrpcVersion").Set(grpc.Version)
}

Просмотреть файл

@ -17,6 +17,7 @@ limitations under the License.
package servenv
import (
"context"
"crypto/tls"
"flag"
"fmt"
@ -26,18 +27,14 @@ import (
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
"vitess.io/vitess/go/trace"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/health"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/keepalive"
"google.golang.org/grpc/reflection"
"context"
"vitess.io/vitess/go/trace"
"vitess.io/vitess/go/vt/grpccommon"
"vitess.io/vitess/go/vt/grpcoptionaltls"
"vitess.io/vitess/go/vt/log"
@ -159,9 +156,10 @@ func createGRPCServer() {
// grpc: received message length XXXXXXX exceeding the max size 4194304
// Note: For gRPC 1.0.0 it's sufficient to set the limit on the server only
// because it's not enforced on the client side.
log.Infof("Setting grpc max message size to %d", *grpccommon.MaxMessageSize)
opts = append(opts, grpc.MaxRecvMsgSize(*grpccommon.MaxMessageSize))
opts = append(opts, grpc.MaxSendMsgSize(*grpccommon.MaxMessageSize))
msgSize := grpccommon.MaxMessageSize()
log.Infof("Setting grpc max message size to %d", msgSize)
opts = append(opts, grpc.MaxRecvMsgSize(msgSize))
opts = append(opts, grpc.MaxSendMsgSize(msgSize))
if *GRPCInitialConnWindowSize != 0 {
log.Infof("Setting grpc server initial conn window size to %d", int32(*GRPCInitialConnWindowSize))
@ -209,7 +207,7 @@ func interceptors() []grpc.ServerOption {
interceptors.Add(authenticatingStreamInterceptor, authenticatingUnaryInterceptor)
}
if *grpccommon.EnableGRPCPrometheus {
if grpccommon.EnableGRPCPrometheus() {
interceptors.Add(grpc_prometheus.StreamServerInterceptor, grpc_prometheus.UnaryServerInterceptor)
}
@ -219,7 +217,7 @@ func interceptors() []grpc.ServerOption {
}
func serveGRPC() {
if *grpccommon.EnableGRPCPrometheus {
if grpccommon.EnableGRPCPrometheus() {
grpc_prometheus.Register(GRPCServer)
grpc_prometheus.EnableHandlingTimeHistogram()
}

Просмотреть файл

@ -47,6 +47,7 @@ import (
"vitess.io/vitess/go/netutil"
"vitess.io/vitess/go/stats"
"vitess.io/vitess/go/trace"
"vitess.io/vitess/go/vt/grpccommon"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/logutil"
"vitess.io/vitess/go/vt/vterrors"
@ -343,6 +344,23 @@ func init() {
OnParseFor(cmd, trace.RegisterFlags)
}
// These are the binaries that make gRPC calls.
for _, cmd := range []string{
"vtbackup",
"vtcombo",
"vtctl",
"vtctlclient",
"vtctld",
"vtctldclient",
"vtgate",
"vtgateclienttest",
"vtgr",
"vttablet",
"vttestserver",
} {
OnParseFor(cmd, grpccommon.RegisterFlags)
}
// Flags in package log are installed for all binaries.
OnParse(log.RegisterFlags)
// Flags in package logutil are installed for all binaries.