servenv

package
v0.11.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 14, 2021 License: Apache-2.0 Imports: 54 Imported by: 32

Documentation

Overview

Package servenv contains functionality that is common for all Vitess server programs. It defines and initializes command line flags that control the runtime environment.

After a server program has called flag.Parse, it needs to call env.Init to make env use the command line variables to initialize the environment. It also needs to call env.Close before exiting.

Note: If you need to plug in any custom initialization/cleanup for a vitess distribution, register them using OnInit and onClose. A clean way of achieving that is adding to this package a file with an init() function that registers the hooks.

Index

Constants

View Source
const JQueryIncludes = `` /* 305-byte string literal not displayed */

JQueryIncludes is the include to use to be able to use jquery and jquery-ui

View Source
const Name = "proto"

Name is the name registered for the proto compressor.

Variables

View Source
var (
	// MySQLServerVersion is what Vitess will present as it's version during the connection handshake,
	// and as the value to the @@version system variable. If nothing is provided, Vitess will report itself as
	// a specific MySQL version with the vitess version appended to it
	MySQLServerVersion = flag.String("mysql_server_version", "", "MySQL server version to advertise.")

	// Version registers the command line flag to expose build info.
	Version = flag.Bool("version", false, "print binary version")
)
View Source
var (
	// GRPCPort is the port to listen on for gRPC. If not set or zero, don't listen.
	GRPCPort = flag.Int("grpc_port", 0, "Port to listen on for gRPC calls")

	// GRPCCert is the cert to use if TLS is enabled
	GRPCCert = flag.String("grpc_cert", "", "server certificate to use for gRPC connections, requires grpc_key, enables TLS")

	// GRPCKey is the key to use if TLS is enabled
	GRPCKey = flag.String("grpc_key", "", "server private key to use for gRPC connections, requires grpc_cert, enables TLS")

	// GRPCCA is the CA to use if TLS is enabled
	GRPCCA = flag.String("grpc_ca", "", "server CA to use for gRPC connections, requires TLS, and enforces client certificate check")

	GRPCEnableOptionalTLS = flag.Bool("grpc_enable_optional_tls", false, "enable optional TLS mode when a server accepts both TLS and plain-text connections on the same port")

	// GRPCServerCA if specified will combine server cert and server CA
	GRPCServerCA = flag.String("grpc_server_ca", "", "path to server CA in PEM format, which will be combine with server cert, return full certificate chain to clients")

	// GRPCAuth which auth plugin to use (at the moment now only static is supported)
	GRPCAuth = flag.String("grpc_auth_mode", "", "Which auth plugin implementation to use (eg: static)")

	// GRPCServer is the global server to serve gRPC.
	GRPCServer *grpc.Server

	// GRPCMaxConnectionAge is the maximum age of a client connection, before GoAway is sent.
	// This is useful for L4 loadbalancing to ensure rebalancing after scaling.
	GRPCMaxConnectionAge = flag.Duration("grpc_max_connection_age", time.Duration(math.MaxInt64), "Maximum age of a client connection before GoAway is sent.")

	// GRPCMaxConnectionAgeGrace is an additional grace period after GRPCMaxConnectionAge, after which
	// connections are forcibly closed.
	GRPCMaxConnectionAgeGrace = flag.Duration("grpc_max_connection_age_grace", time.Duration(math.MaxInt64), "Additional grace period after grpc_max_connection_age, after which connections are forcibly closed.")

	// GRPCInitialConnWindowSize ServerOption that sets window size for a connection.
	// The lower bound for window size is 64K and any value smaller than that will be ignored.
	GRPCInitialConnWindowSize = flag.Int("grpc_server_initial_conn_window_size", 0, "gRPC server initial connection window size")

	// GRPCInitialWindowSize ServerOption that sets window size for stream.
	// The lower bound for window size is 64K and any value smaller than that will be ignored.
	GRPCInitialWindowSize = flag.Int("grpc_server_initial_window_size", 0, "gRPC server initial window size")

	// EnforcementPolicy MinTime that sets the keepalive enforcement policy on the server.
	// This is the minimum amount of time a client should wait before sending a keepalive ping.
	GRPCKeepAliveEnforcementPolicyMinTime = flag.Duration("grpc_server_keepalive_enforcement_policy_min_time", 10*time.Second, "gRPC server minimum keepalive time")

	// EnforcementPolicy PermitWithoutStream - If true, server allows keepalive pings
	// even when there are no active streams (RPCs). If false, and client sends ping when
	// there are no active streams, server will send GOAWAY and close the connection.
	GRPCKeepAliveEnforcementPolicyPermitWithoutStream = flag.Bool("grpc_server_keepalive_enforcement_policy_permit_without_stream", false, "gRPC server permit client keepalive pings even when there are no active streams (RPCs)")
)

This file handles gRPC server, on its own port. Clients register servers, based on service map:

servenv.RegisterGRPCFlags()

servenv.OnRun(func() {
  if servenv.GRPCCheckServiceMap("XXX") {
    pb.RegisterXXX(servenv.GRPCServer, XXX)
  }
}

Note servenv.GRPCServer can only be used in servenv.OnRun, and not before, as it is initialized right before calling OnRun.

View Source
var (
	// Port is part of the flags used when calling RegisterDefaultFlags.
	Port *int

	// ListeningURL is filled in when calling Run, contains the server URL.
	ListeningURL url.URL
)
View Source
var AppVersion versionInfo

AppVersion is the struct to store build info.

View Source
var (
	// ClientCertSubstrings list of substrings of at least one of the client certificate names to use during authorization
	ClientCertSubstrings = flag.String("grpc_auth_mtls_allowed_substrings", "", "List of substrings of at least one of the client certificate names (separated by colon).")
)
View Source
var (

	// ExitChan waits for a signal that tells the process to terminate
	ExitChan chan os.Signal
)
View Source
var (
	// SocketFile has the flag used when calling
	// RegisterDefaultSocketFileFlags.
	SocketFile *string
)

Functions

func AddStatusFuncs

func AddStatusFuncs(fmap template.FuncMap)

AddStatusFuncs merges the provided functions into the set of functions used to render /debug/status. Call this before AddStatusPart if your template requires custom functions.

func AddStatusPart

func AddStatusPart(banner, fragment string, f func() interface{})

AddStatusPart adds a new section to status. fragment is used as a subtemplate of the template used to render /debug/status, and will be executed using the value of invoking f at the time of the /debug/status request. fragment is parsed and executed with the html/template package. Functions registered with AddStatusFuncs may be used in the template.

func AddStatusSection

func AddStatusSection(banner string, f func() string)

AddStatusSection registers a function that generates extra information for the global status page, it will be used as a header before the information. If more complex output than a simple string is required use AddStatusPart instead.

func CheckServiceMap

func CheckServiceMap(protocol, name string) bool

CheckServiceMap returns if we should register a RPC service (and also logs how to enable / disable it)

func Close

func Close()

Close runs any registered exit hooks in parallel.

func FakeAuthStreamInterceptor

func FakeAuthStreamInterceptor(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error

FakeAuthStreamInterceptor fake interceptor to test plugin

func FakeAuthUnaryInterceptor

func FakeAuthUnaryInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error)

FakeAuthUnaryInterceptor fake interceptor to test plugin

func FireRunHooks

func FireRunHooks()

FireRunHooks fires the hooks registered by OnHook. Use this in a non-server to run the hooks registered by servenv.OnRun().

func GRPCCheckServiceMap

func GRPCCheckServiceMap(name string) bool

GRPCCheckServiceMap returns if we should register a gRPC service (and also logs how to enable / disable it)

func GetAuthenticator

func GetAuthenticator(name string) func() (Authenticator, error)

GetAuthenticator returns an AuthPlugin by name, or log.Fatalf.

func HandlePanic

func HandlePanic(component string, err *error)

HandlePanic should be called using 'defer' in the RPC code that executes the command.

func Init

func Init()

Init is the first phase of the server startup.

func InitServiceMap

func InitServiceMap(protocol, name string)

InitServiceMap will set the default value for a protocol/name to be served.

func OnClose

func OnClose(f func())

OnClose registers f to be run at the end of the app lifecycle. This happens after the lameduck period just before the program exits. All hooks are run in parallel.

func OnInit

func OnInit(f func())

OnInit registers f to be run at the beginning of the app lifecycle. It should be called in an init() function.

func OnRun

func OnRun(f func())

OnRun registers f to be run right at the beginning of Run. All hooks are run in parallel.

func OnTerm

func OnTerm(f func())

OnTerm registers a function to be run when the process receives a SIGTERM. This allows the program to change its behavior during the lameduck period.

All hooks are run in parallel, and there is no guarantee that the process will wait for them to finish before dying when the lameduck period expires.

See also: OnTermSync

func OnTermSync

func OnTermSync(f func())

OnTermSync registers a function to be run when the process receives SIGTERM. This allows the program to change its behavior during the lameduck period.

All hooks are run in parallel, and the process will do its best to wait (up to -onterm_timeout) for all of them to finish before dying.

See also: OnTerm

func ParseFlags

func ParseFlags(cmd string)

ParseFlags initializes flags and handles the common case when no positional arguments are expected.

func ParseFlagsWithArgs

func ParseFlagsWithArgs(cmd string) []string

ParseFlagsWithArgs initializes flags and returns the positional arguments

func RegisterAuthPlugin

func RegisterAuthPlugin(name string, authPlugin func() (Authenticator, error))

RegisterAuthPlugin registers an implementation of AuthServer.

func RegisterDefaultFlags

func RegisterDefaultFlags()

RegisterDefaultFlags registers the default flags for listening to a given port for standard connections. If calling this, then call RunDefault()

func RegisterDefaultSocketFileFlags

func RegisterDefaultSocketFileFlags()

RegisterDefaultSocketFileFlags registers the default flags for listening to a socket. This needs to be called before flags are parsed.

func Run

func Run(port int)

Run starts listening for RPC and HTTP requests, and blocks until it the process gets a signal.

func RunDefault

func RunDefault()

RunDefault calls Run() with the parameters from the flags.

func StatusURLPath

func StatusURLPath() string

StatusURLPath returns the path to the status page.

Types

type Authenticator

type Authenticator interface {
	Authenticate(ctx context.Context, fullMethod string) (context.Context, error)
}

Authenticator provides an interface to implement auth in Vitess in grpc server

type Exporter

type Exporter struct {
	// contains filtered or unexported fields
}

Exporter remaps http and stats end-points to distinct namespaces.

Unnamed exporters are treated as unscoped, and requests are passed through to the underlying functions.

For named exporters, http handle requests of the form /path will be remapped to /name/path. In the case of stats variables, a new dimension will be added. For example, a Counter of value 1 will be changed to a map {"name": 1}. A multi-counter like { "a.b": 1, "c.d": 2} will be mapped to {"name.a.b": 1, "name.c.d": 2}. Stats vars of the same name are merged onto a single map. For example, if exporters name1 and name2 independently create a stats Counter named foo and export values 1 and 2, the result is a merged stats var named foo with the following content: {"name1": 1, "name2": 2}.

The functions that create the stat vars don't always return the actual exported variable. Instead they return a variable that only affects the dimension that was assigned to the exporter. The exported variables are named evar, and the variables returned to the caller are named lvar (local var).

If there are duplicates, "Func" vars will be changed to invoke the latest callback function. Non-Func vars will be reused. For counters, the adds will continue to add on top of existing values. For gauges, this is less material because a new "Set" will overwrite the previous value. This behavior of reusing counters is necessary because we build derived variables like Rates, which need to continue referencing the original variable that was created.

func NewExporter

func NewExporter(name, label string) *Exporter

NewExporter creates a new Exporter with name as namespace. label is the name of the additional dimension for the stats vars.

func (*Exporter) AddStatusPart

func (e *Exporter) AddStatusPart(banner, frag string, f func() interface{})

AddStatusPart adds a status part to the status page. If Exporter has a name, the part is added to a url named /name/debug/status. Otherwise, it's /debug/status.

func (*Exporter) HandleFunc

func (e *Exporter) HandleFunc(url string, f func(w http.ResponseWriter, r *http.Request))

HandleFunc sets or overwrites the handler for url. If Exporter has a name, url remapped from /path to /name/path. If name is empty, the request is passed through to http.HandleFunc.

func (*Exporter) Name

func (e *Exporter) Name() string

Name returns the name of the exporter.

func (*Exporter) NewCounter

func (e *Exporter) NewCounter(name string, help string) *stats.Counter

NewCounter creates a name-spaced equivalent for stats.NewCounter.

func (*Exporter) NewCounterDurationFunc

func (e *Exporter) NewCounterDurationFunc(name string, help string, f func() time.Duration) *stats.CounterDurationFunc

NewCounterDurationFunc creates a name-spaced equivalent for stats.NewCounterDurationFunc.

func (*Exporter) NewCounterFunc

func (e *Exporter) NewCounterFunc(name string, help string, f func() int64) *stats.CounterFunc

NewCounterFunc creates a name-spaced equivalent for stats.NewCounterFunc.

func (*Exporter) NewCountersFuncWithMultiLabels

func (e *Exporter) NewCountersFuncWithMultiLabels(name, help string, labels []string, f func() map[string]int64) *stats.CountersFuncWithMultiLabels

NewCountersFuncWithMultiLabels creates a name-spaced equivalent for stats.NewCountersFuncWithMultiLabels.

func (*Exporter) NewCountersWithMultiLabels

func (e *Exporter) NewCountersWithMultiLabels(name, help string, labels []string) *stats.CountersWithMultiLabels

NewCountersWithMultiLabels creates a name-spaced equivalent for stats.NewCountersWithMultiLabels.

func (*Exporter) NewCountersWithSingleLabel

func (e *Exporter) NewCountersWithSingleLabel(name, help string, label string, tags ...string) *stats.CountersWithSingleLabel

NewCountersWithSingleLabel creates a name-spaced equivalent for stats.NewCountersWithSingleLabel. Tags are ignored if the exporter is named.

func (*Exporter) NewGauge

func (e *Exporter) NewGauge(name string, help string) *stats.Gauge

NewGauge creates a name-spaced equivalent for stats.NewGauge.

func (*Exporter) NewGaugeDurationFunc

func (e *Exporter) NewGaugeDurationFunc(name string, help string, f func() time.Duration) *stats.GaugeDurationFunc

NewGaugeDurationFunc creates a name-spaced equivalent for stats.NewGaugeDurationFunc.

func (*Exporter) NewGaugeFunc

func (e *Exporter) NewGaugeFunc(name string, help string, f func() int64) *stats.GaugeFunc

NewGaugeFunc creates a name-spaced equivalent for stats.NewGaugeFunc.

func (*Exporter) NewGaugesFuncWithMultiLabels

func (e *Exporter) NewGaugesFuncWithMultiLabels(name, help string, labels []string, f func() map[string]int64) *stats.GaugesFuncWithMultiLabels

NewGaugesFuncWithMultiLabels creates a name-spaced equivalent for stats.NewGaugesFuncWithMultiLabels.

func (*Exporter) NewGaugesWithMultiLabels

func (e *Exporter) NewGaugesWithMultiLabels(name, help string, labels []string) *stats.GaugesWithMultiLabels

NewGaugesWithMultiLabels creates a name-spaced equivalent for stats.NewGaugesWithMultiLabels.

func (*Exporter) NewGaugesWithSingleLabel

func (e *Exporter) NewGaugesWithSingleLabel(name, help string, label string, tags ...string) *stats.GaugesWithSingleLabel

NewGaugesWithSingleLabel creates a name-spaced equivalent for stats.NewGaugesWithSingleLabel. Tags are ignored if the exporter is named.

func (*Exporter) NewHistogram

func (e *Exporter) NewHistogram(name, help string, cutoffs []int64) *stats.Histogram

NewHistogram creates a name-spaced equivalent for stats.NewHistogram. The function currently just returns an unexported variable.

func (*Exporter) NewMultiTimings

func (e *Exporter) NewMultiTimings(name string, help string, labels []string) *MultiTimingsWrapper

NewMultiTimings creates a name-spaced equivalent for stats.NewMultiTimings. The function currently just returns an unexported variable.

func (*Exporter) NewRates

func (e *Exporter) NewRates(name string, singleCountVar multiCountVar, samples int, interval time.Duration) *stats.Rates

NewRates creates a name-spaced equivalent for stats.NewRates. The function currently just returns an unexported variable.

func (*Exporter) NewTimings

func (e *Exporter) NewTimings(name string, help string, label string) *TimingsWrapper

NewTimings creates a name-spaced equivalent for stats.NewTimings. The function currently just returns an unexported variable.

func (*Exporter) Publish

func (e *Exporter) Publish(name string, v expvar.Var)

Publish creates a name-spaced equivalent for stats.Publish. The function just passes through if the Exporter name is empty.

func (*Exporter) URLPrefix

func (e *Exporter) URLPrefix() string

URLPrefix returns the URL prefix for the exporter.

type MtlsAuthPlugin

type MtlsAuthPlugin struct {
	// contains filtered or unexported fields
}

MtlsAuthPlugin implements static username/password authentication for grpc. It contains an array of username/passwords that will be authorized to connect to the grpc server.

func (*MtlsAuthPlugin) Authenticate

func (ma *MtlsAuthPlugin) Authenticate(ctx context.Context, fullMethod string) (context.Context, error)

Authenticate implements Authenticator interface. This method will be used inside a middleware in grpc_server to authenticate incoming requests.

type MultiTimingsWrapper

type MultiTimingsWrapper struct {
	// contains filtered or unexported fields
}

MultiTimingsWrapper provides a namespaced version of stats.MultiTimings.

func (*MultiTimingsWrapper) Add

func (tw *MultiTimingsWrapper) Add(names []string, elapsed time.Duration)

Add behaves like MultiTimings.Add.

func (*MultiTimingsWrapper) Counts

func (tw *MultiTimingsWrapper) Counts() map[string]int64

Counts behaves lie MultiTimings.Counts.

func (*MultiTimingsWrapper) Record

func (tw *MultiTimingsWrapper) Record(names []string, startTime time.Time)

Record behaves like MultiTimings.Record.

func (*MultiTimingsWrapper) Reset added in v0.8.0

func (tw *MultiTimingsWrapper) Reset()

Reset will clear histograms: used during testing

type StaticAuthConfigEntry

type StaticAuthConfigEntry struct {
	Username string
	Password string
}

StaticAuthConfigEntry is the container for server side credentials. Current implementation matches the the one from the client but this will change in the future as we hooked this pluging into ACL features.

type StaticAuthPlugin

type StaticAuthPlugin struct {
	// contains filtered or unexported fields
}

StaticAuthPlugin implements static username/password authentication for grpc. It contains an array of username/passwords that will be authorized to connect to the grpc server.

func (*StaticAuthPlugin) Authenticate

func (sa *StaticAuthPlugin) Authenticate(ctx context.Context, fullMethod string) (context.Context, error)

Authenticate implements AuthPlugin interface. This method will be used inside a middleware in grpc_server to authenticate incoming requests.

type TimingsWrapper

type TimingsWrapper struct {
	// contains filtered or unexported fields
}

TimingsWrapper provides a namespaced version of stats.Timings.

func (*TimingsWrapper) Add

func (tw *TimingsWrapper) Add(name string, elapsed time.Duration)

Add behaves like Timings.Add.

func (*TimingsWrapper) Counts

func (tw *TimingsWrapper) Counts() map[string]int64

Counts behaves like Timings.Counts.

func (*TimingsWrapper) Record

func (tw *TimingsWrapper) Record(name string, startTime time.Time)

Record behaves like Timings.Record.

func (*TimingsWrapper) Reset added in v0.8.0

func (tw *TimingsWrapper) Reset()

Reset will clear histograms: used during testing

type WrappedServerStream

type WrappedServerStream struct {
	grpc.ServerStream
	WrappedContext context.Context
}

WrappedServerStream is based on the service stream wrapper from: https://github.com/grpc-ecosystem/go-grpc-middleware

func WrapServerStream

func WrapServerStream(stream grpc.ServerStream) *WrappedServerStream

WrapServerStream returns a ServerStream that has the ability to overwrite context.

func (*WrappedServerStream) Context

func (w *WrappedServerStream) Context() context.Context

Context returns the wrapper's WrappedContext, overwriting the nested grpc.ServerStream.Context()

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL