server

package
v0.18.1 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2024 License: Apache-2.0 Imports: 33 Imported by: 27

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// QueryCounter describes a metric that accumulates number of queries monotonically.
	QueryCounter = discard.NewCounter()

	// QueryErrorCounter describes a metric that accumulates number of failed queries monotonically.
	QueryErrorCounter = discard.NewCounter()

	// QueryHistogram describes a queries latency.
	QueryHistogram = discard.NewHistogram()
)
View Source
var ErrConnectionWasClosed = errors.NewKind("connection was closed")

ErrConnectionWasClosed will be returned if we try to use a previously closed connection

View Source
var ErrRowTimeout = errors.NewKind("row read wait bigger than connection timeout")

ErrRowTimeout will be returned if the wait for the row is longer than the connection timeout

View Source
var UnixSocketInUseError = errors.New("bind address at given unix socket path is already in use")

Functions

func Intercept added in v0.18.0

func Intercept(h Interceptor)

Types

type Chain added in v0.18.0

type Chain interface {

	// ComQuery is called when a connection receives a query.
	// Note the contents of the query slice may change after
	// the first call to callback. So the Handler should not
	// hang on to the byte slice.
	ComQuery(c *mysql.Conn, query string, callback mysql.ResultSpoolFn) error

	// ComMultiQuery is called when a connection receives a query and the
	// client supports MULTI_STATEMENT. It should process the first
	// statement in |query| and return the remainder. It will be called
	// multiple times until the remainder is |""|.
	ComMultiQuery(c *mysql.Conn, query string, callback mysql.ResultSpoolFn) (string, error)

	// ComPrepare is called when a connection receives a prepared
	// statement query.
	ComPrepare(c *mysql.Conn, query string, prepare *mysql.PrepareData) ([]*querypb.Field, error)

	// ComStmtExecute is called when a connection receives a statement
	// execute query.
	ComStmtExecute(c *mysql.Conn, prepare *mysql.PrepareData, callback func(*sqltypes.Result) error) error
}

type Config

type Config struct {
	// Protocol for the connection.
	Protocol string
	// Address of the server.
	Address string
	// Custom listener for the mysql server. Use this if you don't want ports or unix sockets to be opened automatically.
	// This can be useful in testing by using a pure go net.Conn implementation.
	Listener net.Listener
	// Tracer to use in the server. By default, a noop tracer will be used if
	// no tracer is provided.
	Tracer trace.Tracer
	// Version string to advertise in running server
	Version string
	// ConnReadTimeout is the server's read timeout
	ConnReadTimeout time.Duration
	// ConnWriteTimeout is the server's write timeout
	ConnWriteTimeout time.Duration
	// MaxConnections is the maximum number of simultaneous connections that the server will allow.
	MaxConnections uint64
	// TLSConfig is the configuration for TLS on this server. If |nil|, TLS is not supported.
	TLSConfig *tls.Config
	// RequestSecureTransport will require incoming connections to be TLS. Requires non-|nil| TLSConfig.
	RequireSecureTransport bool
	// DisableClientMultiStatements will prevent processing of incoming
	// queries as if they contain more than one query. This processing
	// currently works in some simple cases, but breaks in the presence of
	// statements (such as in CREATE TRIGGER queries). Configuring the
	// server to disable processing these is one option for users to get
	// support back for single queries that contain statements, at the cost
	// of not supporting the CLIENT_MULTI_STATEMENTS option on the server.
	DisableClientMultiStatements bool
	// NoDefaults prevents using persisted configuration for new server sessions
	NoDefaults bool
	// Socket is a path to unix socket file
	Socket                   string
	AllowClearTextWithoutTLS bool
	// MaxLoggedQueryLen sets the length at which queries written to the logs are truncated.  A value of 0 will
	// result in no truncation. A value less than 0 will result in the queries being omitted from the logs completely
	MaxLoggedQueryLen int
	// EncodeLoggedQuery determines if logged queries are base64 encoded.
	// If true, queries will be logged as base64 encoded strings.
	// If false (default behavior), queries will be logged as strings, but newlines and tabs will be replaced with spaces.
	EncodeLoggedQuery bool
	// Options add additional options to customize the server.
	Options []Option
}

Config for the mysql server.

func (Config) NewConfig added in v0.12.0

func (c Config) NewConfig() (Config, error)

type DoneFunc

type DoneFunc func()

DoneFunc is a function that must be executed when the session is used and it can be disposed.

type Handler

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

Handler is a connection handler for a SQLe engine, implementing the Vitess mysql.Handler interface.

func (*Handler) ComBind added in v0.18.0

func (h *Handler) ComBind(c *mysql.Conn, query string, parsedQuery mysql.ParsedQuery, prepare *mysql.PrepareData) (mysql.BoundQuery, []*querypb.Field, error)

func (*Handler) ComBinlogDumpGTID added in v0.18.1

func (h *Handler) ComBinlogDumpGTID(c *mysql.Conn, logFile string, logPos uint64, gtidSet mysql.GTIDSet) error

ComBinlogDumpGTID implements the mysql.BinlogReplicaHandler interface.

func (*Handler) ComExecuteBound added in v0.18.0

func (h *Handler) ComExecuteBound(c *mysql.Conn, query string, boundQuery mysql.BoundQuery, callback mysql.ResultSpoolFn) error

func (*Handler) ComInitDB

func (h *Handler) ComInitDB(c *mysql.Conn, schemaName string) error

func (*Handler) ComMultiQuery added in v0.12.0

func (h *Handler) ComMultiQuery(
	c *mysql.Conn,
	query string,
	callback mysql.ResultSpoolFn,
) (string, error)

func (*Handler) ComParsedQuery added in v0.18.0

func (h *Handler) ComParsedQuery(
	c *mysql.Conn,
	query string,
	parsed sqlparser.Statement,
	callback mysql.ResultSpoolFn,
) error

ComParsedQuery executes a pre-parsed SQL query on the SQLe engine.

func (*Handler) ComPrepare

func (h *Handler) ComPrepare(c *mysql.Conn, query string, prepare *mysql.PrepareData) ([]*querypb.Field, error)

ComPrepare parses, partially analyzes, and caches a prepared statement's plan with the given [c.ConnectionID].

func (*Handler) ComPrepareParsed added in v0.18.0

func (h *Handler) ComPrepareParsed(c *mysql.Conn, query string, parsed sqlparser.Statement, prepare *mysql.PrepareData) (mysql.ParsedQuery, []*querypb.Field, error)

func (*Handler) ComQuery

func (h *Handler) ComQuery(
	c *mysql.Conn,
	query string,
	callback mysql.ResultSpoolFn,
) error

ComQuery executes a SQL query on the SQLe engine.

func (*Handler) ComRegisterReplica added in v0.18.1

func (h *Handler) ComRegisterReplica(c *mysql.Conn, replicaHost string, replicaPort uint16, replicaUser string, replicaPassword string) error

ComRegisterReplica implements the mysql.BinlogReplicaHandler interface.

func (*Handler) ComResetConnection

func (h *Handler) ComResetConnection(c *mysql.Conn) error

ComResetConnection implements the mysql.Handler interface.

This command resets the connection's session, clearing out any cached prepared statements, locks, user and session variables. The currently selected database is preserved.

The COM_RESET command can be sent manually through the mysql client by issuing the "resetconnection" (or "\x") client command.

func (*Handler) ComStmtExecute

func (h *Handler) ComStmtExecute(c *mysql.Conn, prepare *mysql.PrepareData, callback func(*sqltypes.Result) error) error

func (*Handler) ConnectionClosed

func (h *Handler) ConnectionClosed(c *mysql.Conn)

ConnectionClosed reports that a connection has been closed.

func (*Handler) NewConnection

func (h *Handler) NewConnection(c *mysql.Conn)

NewConnection reports that a new connection has been established.

func (*Handler) ParserOptionsForConnection added in v0.17.0

func (h *Handler) ParserOptionsForConnection(c *mysql.Conn) (sqlparser.ParserOptions, error)

func (*Handler) WarningCount

func (h *Handler) WarningCount(c *mysql.Conn) uint16

WarningCount is called at the end of each query to obtain the value to be returned to the client in the EOF packet. Note that this will be called either in the context of the ComQuery callback if the result does not contain any fields, or after the last ComQuery call completes.

type HandlerWrapper added in v0.18.1

type HandlerWrapper func(h mysql.Handler) (mysql.Handler, error)

HandlerWrapper provides a way for clients to wrap the mysql.Handler used by the server with a custom implementation that wraps it.

type Interceptor added in v0.18.0

type Interceptor interface {

	// Priority returns the priority of the interceptor.
	Priority() int

	// Query is called when a connection receives a query.
	// Note the contents of the query slice may change after
	// the first call to callback. So the Handler should not
	// hang on to the byte slice.
	Query(chain Chain, c *mysql.Conn, query string, callback func(res *sqltypes.Result, more bool) error) error

	// ParsedQuery is called when a connection receives a
	// query that has already been parsed. Note the contents
	// of the query slice may change after the first call to
	// callback. So the Handler should not hang on to the byte
	// slice.
	ParsedQuery(chain Chain, c *mysql.Conn, query string, parsed sqlparser.Statement, callback func(res *sqltypes.Result, more bool) error) error

	// MultiQuery is called when a connection receives a query and the
	// client supports MULTI_STATEMENT. It should process the first
	// statement in |query| and return the remainder. It will be called
	// multiple times until the remainder is |""|.
	MultiQuery(chain Chain, c *mysql.Conn, query string, callback func(res *sqltypes.Result, more bool) error) (string, error)

	// Prepare is called when a connection receives a prepared
	// statement query.
	Prepare(chain Chain, c *mysql.Conn, query string, prepare *mysql.PrepareData) ([]*querypb.Field, error)

	// StmtExecute is called when a connection receives a statement
	// execute query.
	StmtExecute(chain Chain, c *mysql.Conn, prepare *mysql.PrepareData, callback func(*sqltypes.Result) error) error
}

type Listener

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

Listener implements a single listener with two net.Listener, one for TCP socket and another for unix socket connections.

func NewListener

func NewListener(protocol, address string, unixSocketPath string) (*Listener, error)

NewListener creates a new Listener. 'protocol' takes "tcp" and 'address' takes "host:port" information for TCP socket connection. For unix socket connection, 'unixSocketPath' takes a path for the unix socket file. If 'unixSocketPath' is empty, no need to create the second listener.

func (*Listener) Accept

func (l *Listener) Accept() (net.Conn, error)

func (*Listener) Addr added in v0.14.0

func (l *Listener) Addr() net.Addr

func (*Listener) Close added in v0.14.0

func (l *Listener) Close() error

type MultiStmtMode added in v0.12.0

type MultiStmtMode int
const (
	MultiStmtModeOff MultiStmtMode = 0
	MultiStmtModeOn  MultiStmtMode = 1
)

type Option added in v0.18.0

type Option func(e *sqle.Engine, sm *SessionManager, handler mysql.Handler)

Option is an option to customize server.

func WithChain added in v0.18.0

func WithChain() Option

type ProtocolListener added in v0.18.0

type ProtocolListener interface {
	Addr() net.Addr
	Accept()
	Close()
}

ProtocolListener handles connections based on the configuration it was given. These listeners also implement their own protocol, which by default will be the MySQL wire protocol, but another protocol may be provided.

type ProtocolListenerFunc added in v0.18.0

type ProtocolListenerFunc func(cfg mysql.ListenerConfig) (ProtocolListener, error)

ProtocolListenerFunc returns a ProtocolListener based on the configuration it was given.

var DefaultProtocolListenerFunc ProtocolListenerFunc = func(cfg mysql.ListenerConfig) (ProtocolListener, error) {
	return mysql.NewListenerWithConfig(cfg)
}

DefaultProtocolListenerFunc is the protocol listener, which defaults to Vitess' protocol listener. Changing this function will change the protocol listener used when creating all servers. If multiple servers are needed with different protocols, then create each server after changing this function. Servers retain the protocol that they were created with.

type QueryExecutor added in v0.18.0

type QueryExecutor func(
	ctx *sql.Context,
	query string,
	parsed sqlparser.Statement,
	analyzed sql.Node,
	bindings map[string]*querypb.BindVariable,
) (sql.Schema, sql.RowIter, error)

QueryExecutor is a function that executes a query and returns the result as a schema and iterator. Either of |parsed| or |analyzed| can be nil depending on the use case

type Server

type Server struct {
	Listener ProtocolListener

	Engine *gms.Engine
	// contains filtered or unexported fields
}

Server is a MySQL server for SQLe engines.

func NewServer

func NewServer(cfg Config, e *sqle.Engine, sb SessionBuilder, listener ServerEventListener) (*Server, error)

NewServer creates a server with the given protocol, address, authentication details given a SQLe engine and a session builder.

func NewServerWithHandler added in v0.18.1

func NewServerWithHandler(
	cfg Config,
	e *sqle.Engine,
	sb SessionBuilder,
	listener ServerEventListener,
	wrapper HandlerWrapper,
) (*Server, error)

NewServerWithHandler creates a Server with a handler wrapped by the provided wrapper function.

func (*Server) Close

func (s *Server) Close() error

Close closes the server connection.

func (*Server) SessionManager added in v0.12.0

func (s *Server) SessionManager() *SessionManager

SessionManager returns the session manager for this server.

func (*Server) Start

func (s *Server) Start() error

Start starts accepting connections on the server.

func (*Server) WarnIfLoadFileInsecure added in v0.18.0

func (s *Server) WarnIfLoadFileInsecure()

type ServerEventListener added in v0.12.0

type ServerEventListener interface {
	ClientConnected()
	ClientDisconnected()
	QueryStarted()
	QueryCompleted(success bool, duration time.Duration)
}

type SessionBuilder

type SessionBuilder func(ctx context.Context, conn *mysql.Conn, addr string) (sql.Session, error)

SessionBuilder creates sessions given a MySQL connection and a server address.

type SessionManager

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

SessionManager is in charge of creating new sessions for the given connections and keep track of which sessions are in each connection, so they can be cancelled if the connection is closed.

func NewSessionManager

func NewSessionManager(
	builder SessionBuilder,
	tracer trace.Tracer,
	getDbFunc func(ctx *sql.Context, db string) (sql.Database, error),
	memory *sql.MemoryManager,
	processlist sql.ProcessList,
	addr string,
) *SessionManager

NewSessionManager creates a SessionManager with the given SessionBuilder.

func (*SessionManager) AddConn added in v0.15.0

func (s *SessionManager) AddConn(conn *mysql.Conn)

Add a connection to be tracked by the SessionManager. Should be called as soon as possible after the server has accepted the connection. Results in the connection being tracked by ProcessList and being available through KillConnection. The connection will be tracked until RemoveConn is called, so clients should ensure a call to AddConn is always paired up with a call to RemoveConn.

func (*SessionManager) Iter added in v0.12.0

func (s *SessionManager) Iter(f func(session sql.Session) (stop bool, err error)) error

Iter iterates over the active sessions and executes the specified callback function on each one.

func (*SessionManager) KillConnection added in v0.14.0

func (s *SessionManager) KillConnection(connID uint32) error

Exposed through (*sql.Context).Services.KillConnection. Calls Close on the tracked connection with |connID|. The full teardown of the connection is asychronous, similar to how |Process.Kill| for tearing down an inflight query is asynchronous. The connection and any running query will remain in the ProcessList and in the SessionManager until it has been torn down by the server handler.

func (*SessionManager) NewContext

func (s *SessionManager) NewContext(conn *mysql.Conn) (*sql.Context, error)

NewContext creates a new context for the session at the given conn.

func (*SessionManager) NewContextWithQuery

func (s *SessionManager) NewContextWithQuery(conn *mysql.Conn, query string) (*sql.Context, error)

NewContextWithQuery creates a new context for the session at the given conn.

func (*SessionManager) NewSession

func (s *SessionManager) NewSession(ctx context.Context, conn *mysql.Conn) error

NewSession creates a Session for the given connection and saves it to the session pool.

func (*SessionManager) RemoveConn added in v0.15.0

func (s *SessionManager) RemoveConn(conn *mysql.Conn)

Remove the session assosiated with |conn| from the session manager.

func (*SessionManager) SetDB

func (s *SessionManager) SetDB(conn *mysql.Conn, dbName string) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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