replication

package
v2.0.0-beta.2+incompat... Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2016 License: BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Overview

Package replication contains data structures to deal with MySQL replication.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EncodeGTID

func EncodeGTID(gtid GTID) string

EncodeGTID returns a string that contains both the flavor and value of the GTID, so that the correct parser can be selected when that string is passed to DecodeGTID.

func EncodePosition

func EncodePosition(rp Position) string

EncodePosition returns a string that contains both the flavor and value of the Position, so that the correct parser can be selected when that string is passed to DecodePosition.

func StatusToProto

func StatusToProto(r Status) *replicationdatapb.Status

StatusToProto translates a Status to proto3

Types

type BinlogEvent

type BinlogEvent interface {
	// IsValid returns true if the underlying data buffer contains a valid event.
	// This should be called first on any BinlogEvent, and other methods should
	// only be called if this one returns true. This ensures you won't get panics
	// due to bounds checking on the byte array.
	IsValid() bool

	// IsFormatDescription returns true if this is a FORMAT_DESCRIPTION_EVENT.
	IsFormatDescription() bool
	// IsQuery returns true if this is a QUERY_EVENT, which encompasses all SQL
	// statements.
	IsQuery() bool
	// IsXID returns true if this is an XID_EVENT, which is an alternate form of
	// COMMIT.
	IsXID() bool
	// IsGTID returns true if this is a GTID_EVENT.
	IsGTID() bool
	// IsRotate returns true if this is a ROTATE_EVENT.
	IsRotate() bool
	// IsIntVar returns true if this is an INTVAR_EVENT.
	IsIntVar() bool
	// IsRand returns true if this is a RAND_EVENT.
	IsRand() bool
	// HasGTID returns true if this event contains a GTID. That could either be
	// because it's a GTID_EVENT (MariaDB, MySQL 5.6), or because it is some
	// arbitrary event type that has a GTID in the header (Google MySQL).
	HasGTID(BinlogFormat) bool

	// Timestamp returns the timestamp from the event header.
	Timestamp() uint32

	// Format returns a BinlogFormat struct based on the event data.
	// This is only valid if IsFormatDescription() returns true.
	Format() (BinlogFormat, error)
	// GTID returns the GTID from the event.
	// This is only valid if HasGTID() returns true.
	GTID(BinlogFormat) (GTID, error)
	// IsBeginGTID returns true if this is a GTID_EVENT that also serves as a
	// BEGIN statement. Otherwise, the GTID_EVENT is just providing the GTID for
	// the following QUERY_EVENT.
	// This is only valid if IsGTID() returns true.
	IsBeginGTID(BinlogFormat) bool
	// Query returns a Query struct representing data from a QUERY_EVENT.
	// This is only valid if IsQuery() returns true.
	Query(BinlogFormat) (Query, error)
	// IntVar returns the name and value of the variable for an INTVAR_EVENT.
	// This is only valid if IsIntVar() returns true.
	IntVar(BinlogFormat) (string, uint64, error)
	// Rand returns the two seed values for a RAND_EVENT.
	// This is only valid if IsRand() returns true.
	Rand(BinlogFormat) (uint64, uint64, error)

	// StripChecksum returns the checksum and a modified event with the checksum
	// stripped off, if any. If there is no checksum, it returns the same event
	// and a nil checksum.
	StripChecksum(BinlogFormat) (ev BinlogEvent, checksum []byte, err error)
}

BinlogEvent represents a single event from a raw MySQL binlog dump stream. The implementation is provided by each supported flavor in go/vt/mysqlctl.

binlog.Streamer receives these events through a mysqlctl.SlaveConnection and processes them, grouping statements into BinlogTransactions as appropriate.

Methods that only access header fields can't fail as long as IsValid() returns true, so they have a single return value. Methods that might fail even when IsValid() is true return an error value.

Methods that require information from the initial FORMAT_DESCRIPTION_EVENT will have a BinlogFormat parameter.

A BinlogEvent should never be sent over the wire. UpdateStream service will send BinlogTransactions from these events.

type BinlogFormat

type BinlogFormat struct {
	// FormatVersion is the version number of the binlog file format.
	FormatVersion uint16
	// ServerVersion is the name of the MySQL server version.
	ServerVersion string
	// HeaderLength is the size in bytes of event headers other than FORMAT_DESCRIPTION_EVENT.
	HeaderLength byte
	// ChecksumAlgorithm is the ID number of the binlog checksum algorithm.
	ChecksumAlgorithm byte
}

BinlogFormat contains relevant data from the FORMAT_DESCRIPTION_EVENT. This structure is passed to subsequent event types to let them know how to parse themselves.

func (BinlogFormat) IsZero

func (f BinlogFormat) IsZero() bool

IsZero returns true if the BinlogFormat has not been initialized.

type GTID

type GTID interface {
	// String returns the canonical printed form of the GTID as expected by a
	// particular flavor of MySQL.
	String() string

	// Flavor returns the key under which the corresponding GTID parser function
	// is registered in the gtidParsers map.
	Flavor() string

	// SourceServer returns the ID of the server that generated the transaction.
	SourceServer() interface{}

	// SequenceNumber returns the ID number that increases with each transaction.
	// It is only valid to compare the sequence numbers of two GTIDs if they have
	// the same domain value.
	SequenceNumber() interface{}

	// SequenceDomain returns the ID of the domain within which two sequence
	// numbers can be meaningfully compared.
	SequenceDomain() interface{}

	// GTIDSet returns a GTIDSet of the same flavor as this GTID, containing only
	// this GTID.
	GTIDSet() GTIDSet
}

GTID represents a Global Transaction ID, also known as Transaction Group ID. Each flavor of MySQL has its own format for the GTID. This interface is used along with various MysqlFlavor implementations to abstract the differences.

Types that implement GTID should use a non-pointer receiver. This ensures that comparing GTID interface values with == has the expected semantics.

func DecodeGTID

func DecodeGTID(s string) (GTID, error)

DecodeGTID converts a string in the format returned by EncodeGTID back into a GTID interface value with the correct underlying flavor.

func MustDecodeGTID

func MustDecodeGTID(s string) GTID

MustDecodeGTID calls DecodeGTID and panics on error.

func MustParseGTID

func MustParseGTID(flavor, value string) GTID

MustParseGTID calls ParseGTID and panics on error.

func ParseGTID

func ParseGTID(flavor, value string) (GTID, error)

ParseGTID calls the GTID parser for the specified flavor.

type GTIDSet

type GTIDSet interface {
	// String returns the canonical printed form of the set as expected by a
	// particular flavor of MySQL.
	String() string

	// Flavor returns the key under which the corresponding parser function is
	// registered in the transactionSetParsers map.
	Flavor() string

	// ContainsGTID returns true if the set contains the specified transaction.
	ContainsGTID(GTID) bool

	// Contains returns true if the set is a superset of another set.
	Contains(GTIDSet) bool

	// Equal returns true if the set is equal to another set.
	Equal(GTIDSet) bool

	// AddGTID returns a new GTIDSet that is expanded to contain the given GTID.
	AddGTID(GTID) GTIDSet
}

GTIDSet represents the set of transactions received or applied by a server. In some flavors, a single GTID is enough to specify the set of all transactions that came before it, but in others a more complex structure is required.

GTIDSet is wrapped by replication.Position, which is a concrete struct. When sending a GTIDSet over RPCs, encode/decode it as a string. Most code outside of this package should use replication.Position rather than GTIDSet.

type MariadbGTID

type MariadbGTID struct {
	// Domain is the ID number of the domain within which sequence numbers apply.
	Domain uint32
	// Server is the ID of the server that generated the transaction.
	Server uint32
	// Sequence is the sequence number of the transaction within the domain.
	Sequence uint64
}

MariadbGTID implements GTID.

func (MariadbGTID) AddGTID

func (gtid MariadbGTID) AddGTID(other GTID) GTIDSet

AddGTID implements GTIDSet.AddGTID().

func (MariadbGTID) Contains

func (gtid MariadbGTID) Contains(other GTIDSet) bool

Contains implements GTIDSet.Contains().

func (MariadbGTID) ContainsGTID

func (gtid MariadbGTID) ContainsGTID(other GTID) bool

ContainsGTID implements GTIDSet.ContainsGTID().

func (MariadbGTID) Equal

func (gtid MariadbGTID) Equal(other GTIDSet) bool

Equal implements GTIDSet.Equal().

func (MariadbGTID) Flavor

func (gtid MariadbGTID) Flavor() string

Flavor implements GTID.Flavor().

func (MariadbGTID) GTIDSet

func (gtid MariadbGTID) GTIDSet() GTIDSet

GTIDSet implements GTID.GTIDSet().

func (MariadbGTID) SequenceDomain

func (gtid MariadbGTID) SequenceDomain() interface{}

SequenceDomain implements GTID.SequenceDomain().

func (MariadbGTID) SequenceNumber

func (gtid MariadbGTID) SequenceNumber() interface{}

SequenceNumber implements GTID.SequenceNumber().

func (MariadbGTID) SourceServer

func (gtid MariadbGTID) SourceServer() interface{}

SourceServer implements GTID.SourceServer().

func (MariadbGTID) String

func (gtid MariadbGTID) String() string

String implements GTID.String().

type Mysql56GTID

type Mysql56GTID struct {
	// Server is the SID of the server that originally committed the transaction.
	Server SID
	// Sequence is the sequence number of the transaction within a given Server's
	// scope.
	Sequence int64
}

Mysql56GTID implements GTID

func (Mysql56GTID) Flavor

func (gtid Mysql56GTID) Flavor() string

Flavor implements GTID.Flavor().

func (Mysql56GTID) GTIDSet

func (gtid Mysql56GTID) GTIDSet() GTIDSet

GTIDSet implements GTID.GTIDSet().

func (Mysql56GTID) SequenceDomain

func (gtid Mysql56GTID) SequenceDomain() interface{}

SequenceDomain implements GTID.SequenceDomain().

func (Mysql56GTID) SequenceNumber

func (gtid Mysql56GTID) SequenceNumber() interface{}

SequenceNumber implements GTID.SequenceNumber().

func (Mysql56GTID) SourceServer

func (gtid Mysql56GTID) SourceServer() interface{}

SourceServer implements GTID.SourceServer().

func (Mysql56GTID) String

func (gtid Mysql56GTID) String() string

String implements GTID.String().

type Mysql56GTIDSet

type Mysql56GTIDSet map[SID][]interval

Mysql56GTIDSet implements GTIDSet for MySQL 5.6.

func (Mysql56GTIDSet) AddGTID

func (set Mysql56GTIDSet) AddGTID(gtid GTID) GTIDSet

AddGTID implements GTIDSet.

func (Mysql56GTIDSet) Contains

func (set Mysql56GTIDSet) Contains(other GTIDSet) bool

Contains implements GTIDSet.

func (Mysql56GTIDSet) ContainsGTID

func (set Mysql56GTIDSet) ContainsGTID(gtid GTID) bool

ContainsGTID implements GTIDSet.

func (Mysql56GTIDSet) Equal

func (set Mysql56GTIDSet) Equal(other GTIDSet) bool

Equal implements GTIDSet.

func (Mysql56GTIDSet) Flavor

func (Mysql56GTIDSet) Flavor() string

Flavor implements GTIDSet.

func (Mysql56GTIDSet) SIDBlock

func (set Mysql56GTIDSet) SIDBlock() []byte

SIDBlock returns the binary encoding of a MySQL 5.6 GTID set as expected by internal commands that refer to an "SID block".

e.g. https://dev.mysql.com/doc/internals/en/com-binlog-dump-gtid.html

func (Mysql56GTIDSet) SIDs

func (set Mysql56GTIDSet) SIDs() []SID

SIDs returns a sorted list of SIDs in the set.

func (Mysql56GTIDSet) String

func (set Mysql56GTIDSet) String() string

String implements GTIDSet.

type Position

type Position struct {
	GTIDSet GTIDSet
	// contains filtered or unexported fields
}

Position represents the information necessary to describe which transactions a server has seen, so that it can request a replication stream from a new master that picks up where it left off.

This must be a concrete struct because custom Unmarshalers can't be registered on an interface.

The == operator should not be used with Position, because the underlying GTIDSet might use slices, which are not comparable. Using == in those cases will result in a run-time panic.

func AppendGTID

func AppendGTID(rp Position, gtid GTID) Position

AppendGTID returns a new Position that represents the position after the given GTID is replicated.

func DecodePosition

func DecodePosition(s string) (rp Position, err error)

DecodePosition converts a string in the format returned by EncodePosition back into a Position value with the correct underlying flavor.

func MustParsePosition

func MustParsePosition(flavor, value string) Position

MustParsePosition calls ParsePosition and panics on error.

func ParsePosition

func ParsePosition(flavor, value string) (rp Position, err error)

ParsePosition calls the parser for the specified flavor.

func (Position) AtLeast

func (rp Position) AtLeast(other Position) bool

AtLeast returns true if this position is equal to or after another.

func (Position) Equal

func (rp Position) Equal(other Position) bool

Equal returns true if this position is equal to another.

func (Position) IsZero

func (rp Position) IsZero() bool

IsZero returns true if this is the zero value, Position{}.

func (Position) MarshalJSON

func (rp Position) MarshalJSON() ([]byte, error)

MarshalJSON implements encoding/json.Marshaler.

func (Position) String

func (rp Position) String() string

String returns a string representation of the underlying GTIDSet. If the set is nil, it returns "<nil>" in the style of Sprintf("%v", nil).

func (*Position) UnmarshalJSON

func (rp *Position) UnmarshalJSON(buf []byte) error

UnmarshalJSON implements encoding/json.Unmarshaler.

type Query

type Query struct {
	Database string
	Charset  *binlogdatapb.Charset
	SQL      string
}

Query contains data from a QUERY_EVENT.

func (Query) String

func (q Query) String() string

String pretty-prints a Query.

type SID

type SID [16]byte

SID is the 16-byte unique ID of a MySQL 5.6 server.

func ParseSID

func ParseSID(s string) (sid SID, err error)

ParseSID parses an SID in the form used by MySQL 5.6.

func (SID) String

func (sid SID) String() string

String prints an SID in the form used by MySQL 5.6.

type Status

type Status struct {
	Position            Position
	SlaveIORunning      bool
	SlaveSQLRunning     bool
	SecondsBehindMaster uint
	MasterHost          string
	MasterPort          int
	MasterConnectRetry  int
}

Status holds replication information from SHOW SLAVE STATUS.

func NewStatus

func NewStatus(masterAddr string) (*Status, error)

NewStatus creates a Status pointing to masterAddr.

func ProtoToStatus

func ProtoToStatus(r *replicationdatapb.Status) Status

ProtoToStatus translates a proto Status, or panics

func (*Status) MasterAddr

func (rs *Status) MasterAddr() string

MasterAddr returns the host:port address of the master.

func (*Status) SlaveRunning

func (rs *Status) SlaveRunning() bool

SlaveRunning returns true iff both the Slave IO and Slave SQL threads are running.

Jump to

Keyboard shortcuts

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