binlog

package
v0.0.0-...-9fe22c2 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2023 License: Apache-2.0, Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MySQLTypeDecimal   = 0x00
	MySQLTypeTiny      = 0x01
	MySQLTypeShort     = 0x02
	MySQLTypeLong      = 0x03
	MySQLTypeFloat     = 0x04
	MySQLTypeDouble    = 0x05
	MySQLTypeNull      = 0x06
	MySQLTypeTimestamp = 0x07
	MySQLTypeLonglong  = 0x08
	MySQLTypeInt24     = 0x09
	MySQLTypeDate      = 0x0a
	MySQLTypeTime      = 0x0b
	MySQLTypeDatetime  = 0x0c
	MySQLTypeYear      = 0x0d
	MySQLTypeNewDate   = 0x0e
	MySQLTypeVarchar   = 0x0f
	MySQLTypeBit       = 0x10

	// mysql 5.6
	MySQLTypeTimestamp2 = 0x11
	MySQLTypeDatetime2  = 0x12
	MySQLTypeTime2      = 0x13

	// start with 0xf5
	MySQLTypeJSON       = 0xf5
	MySQLTypeNewDecimal = 0xf6
	MySQLTypeEnum       = 0xf7
	MySQLTypeSet        = 0xf8
	MySQLTypeTinyBlob   = 0xf9
	MySQLTypeMediumBlob = 0xfa
	MySQLTypeLongBlob   = 0xfb
	MySQLTypeBlob       = 0xfc
	MySQLTypeVarString  = 0xfd
	MySQLTypeString     = 0xfe
	MySQLTypeGeometry   = 0xff
)

https://dev.mysql.com/doc/internals/en/com-query-response.html#packet-Protocol::MYSQL_TYPE_STRING

View Source
const (
	UnknownEvent              = 0x00
	StartEventV3              = 0x01
	QueryEvent                = 0x02
	StopEvent                 = 0x03
	RotateEvent               = 0x04
	IntvarEvent               = 0x05
	LoadEvent                 = 0x06
	SlaveEvent                = 0x07
	CreateFileEvent           = 0x08
	AppendBlockEvent          = 0x09
	ExecLoadEvent             = 0x0a
	DeleteFileEvent           = 0x0b
	NewLoadEvent              = 0x0c
	RandEvent                 = 0x0d
	UserVarEvent              = 0x0e
	FormatDescriptionEvent    = 0x0f
	XIDEvent                  = 0x10
	BeginLoadQueryEvent       = 0x11
	ExecuteLoadQueryEvent     = 0x12
	TableMapEvent             = 0x13
	WriteRowsEventV0          = 0x14
	UpdateRowsEventV0         = 0x15
	DeleteRowsEventV0         = 0x16
	WriteRowsEventV1          = 0x17
	UpdateRowsEventV1         = 0x18
	DeleteRowsEventV1         = 0x19
	IncidentEvent             = 0x1a
	HeartbeatEvent            = 0x1b
	IgnorableEvent            = 0x1c
	RowsQueryEvent            = 0x1d
	WriteRowsEventV2          = 0x1e
	UpdateRowsEventV2         = 0x1f
	DeleteRowsEventV2         = 0x20
	GTIDEvent                 = 0x21
	AnonymousGTIDEvent        = 0x22
	PreviousGTIDEvent         = 0x23
	TransactionContextEvent   = 0x27
	ViewChangeEvent           = 0x28
	XAEvent                   = 0x29
	BinlogCheckpointEvent     = 0x2a
	BinlogDumpGTIDEvent       = 0x2b
	BinlogDumpMGTIDEvent      = 0x2c
	BinlogDumpBGTIDEvent      = 0x2d
	BinlogTransaction         = 0x2e
	RotateEventV2             = 0x2f
	IntvarEventV2             = 0x30
	RandEventV2               = 0x31
	UserVarEventV2            = 0x32
	FormatDescriptionEventV2  = 0x33
	XIDEventV2                = 0x34
	BinlogCheckpointEventV2   = 0x35
	IntvarEventV3             = 0x36
	HeartbeatEventV2          = 0x37
	IgnorableEventV2          = 0x38
	RowsQueryEventV2          = 0x39
	TableMapEventV3           = 0x3a
	WriteRowsEventV3          = 0x3b
	UpdateRowsEventV3         = 0x3c
	DeleteRowsEventV3         = 0x3d
	GtidEventV1               = 0x3e
	AnonymousGtidEventV1      = 0x3f
	PreviousGtidEventV1       = 0x40
	BeginLoadQueryEventV3     = 0x41
	ExecuteLoadQueryEventV3   = 0x42
	TableMapEventV4           = 0x43
	WriteRowsEventV4          = 0x44
	UpdateRowsEventV4         = 0x45
	DeleteRowsEventV4         = 0x46
	WriteRowsEventV5          = 0x47
	UpdateRowsEventV5         = 0x48
	DeleteRowsEventV5         = 0x49
	PartitionedRowsQueryEvent = 0x4a
	HeartbeatEventV3          = 0x4b
	HeartbeatLogEvent         = 0x4c
)

https://dev.mysql.com/doc/internals/en/binlog-event-type.html

View Source
const (
	BinlogChecksumAlgOff   byte = 0
	BinlogChecksumAlgCRC32 byte = 1
	BinlogChecksumAlgUndef byte = 255
)

BINGLOG_CHECKSUM_ALG

View Source
const (
	QFlags2Code            = 0x00
	QSQLModeCode           = 0x01
	QCatalog               = 0x02
	QAutoIncrement         = 0x03
	QCharsetCode           = 0x04
	QTimeZoneCode          = 0x05
	QCatalogNZCode         = 0x06
	QLCTimeNamesCode       = 0x07
	QCharsetDatabaseCode   = 0x08
	QTableMapForUpdateCode = 0x09
	QMasterDataWrittenCode = 0x0a
	QInvokers              = 0x0b
	QUpdatedDBNames        = 0x0c
	QMicroseconds          = 0x0d
)

QUERY_EVENT status_vars

Variables

View Source
var EventType2Str = map[uint8]string{
	UnknownEvent:             "UNKNOWN_EVENT",
	StartEventV3:             "START_EVENT_V3",
	QueryEvent:               "QUERY_EVENT",
	StopEvent:                "STOP_EVENT",
	RotateEvent:              "ROTATE_EVENT",
	IntvarEvent:              "INTVAR_EVENT",
	LoadEvent:                "LOAD_EVENT",
	SlaveEvent:               "SLAVE_EVENT",
	CreateFileEvent:          "CREATE_FILE_EVENT",
	AppendBlockEvent:         "APPEND_BLOCK_EVENT",
	ExecLoadEvent:            "EXEC_LOAD_EVENT",
	DeleteFileEvent:          "DELETE_FILE_EVENT",
	NewLoadEvent:             "NEW_LOAD_EVENT",
	RandEvent:                "RAND_EVENT",
	UserVarEvent:             "USER_VAR_EVENT",
	FormatDescriptionEvent:   "FORMAT_DESCRIPTION_EVENT",
	XIDEvent:                 "XID_EVENT",
	BeginLoadQueryEvent:      "BEGIN_LOAD_QUERY_EVENT",
	ExecuteLoadQueryEvent:    "EXECUTE_LOAD_QUERY_EVENT",
	TableMapEvent:            "TABLE_MAP_EVENT",
	WriteRowsEventV0:         "WRITE_ROWS_EVENTv0",
	UpdateRowsEventV0:        "UPDATE_ROWS_EVENTv0",
	DeleteRowsEventV0:        "DELETE_ROWS_EVENTv0",
	WriteRowsEventV1:         "WRITE_ROWS_EVENTv1",
	UpdateRowsEventV1:        "UPDATE_ROWS_EVENTv1",
	DeleteRowsEventV1:        "DELETE_ROWS_EVENTv1",
	IncidentEvent:            "INCIDENT_EVENT",
	HeartbeatEvent:           "HEARTBEAT_EVENT",
	IgnorableEvent:           "IGNORABLE_EVENT",
	RowsQueryEvent:           "ROWS_QUERY_EVENT",
	WriteRowsEventV2:         "WRITE_ROWS_EVENTv2",
	UpdateRowsEventV2:        "UPDATE_ROWS_EVENTv2",
	DeleteRowsEventV2:        "DELETE_ROWS_EVENTv2",
	GTIDEvent:                "GTID_EVENT",
	AnonymousGTIDEvent:       "ANONYMOUS_GTID_EVENT",
	PreviousGTIDEvent:        "PREVIOUS_GTIDS_EVENT",
	WriteRowsEventV3:         "WRITE_ROWS_EVENTv3",
	UpdateRowsEventV3:        "UPDATE_ROWS_EVENTv3",
	DeleteRowsEventV3:        "DELETE_ROWS_EVENTv3",
	TransactionContextEvent:  "TRANSACTION_CONTEXT_EVENT",
	ViewChangeEvent:          "VIEW_CHANGE_EVENT",
	XAEvent:                  "XA_EVENT",
	BinlogCheckpointEvent:    "BINLOG_CHECKPOINT_EVENT",
	BinlogDumpGTIDEvent:      "BINLOG_DUMP_GTID_EVENT",
	BinlogDumpMGTIDEvent:     "BINLOG_DUMP_MGTID_EVENT",
	BinlogDumpBGTIDEvent:     "BINLOG_DUMP_BGTID_EVENT",
	BinlogTransaction:        "BINLOG_TRANSACTION_EVENT",
	RotateEventV2:            "ROTATE_EVENTv2",
	IntvarEventV2:            "INTVAR_EVENTv2",
	RandEventV2:              "RAND_EVENTv2",
	UserVarEventV2:           "USER_VAR_EVENTv2",
	FormatDescriptionEventV2: "FORMAT_DESCRIPTION_EVENTv2",
	XIDEventV2:               "XID_EVENTv2",
	BinlogCheckpointEventV2:  "BINLOG_CHECKPOINT_EVENTv2",
	IntvarEventV3:            "INTVAR_EVENTv3",
	HeartbeatEventV2:         "HEARTBEAT_EVENTv2",
	IgnorableEventV2:         "IGNORABLE_EVENTv2",
	RowsQueryEventV2:         "ROWS_QUERY_EVENTv2",
	TableMapEventV3:          "TABLE_MAP_EVENTv3",
}

EventType2Str mapping the name of binary log event type

View Source
var QStatusKey2Str = map[uint8]string{
	QFlags2Code:            "Q_FLAGS2_CODE",
	QSQLModeCode:           "Q_SQL_MODE_CODE",
	QCatalog:               "Q_CATALOG",
	QAutoIncrement:         "Q_AUTO_INCREMENT",
	QCharsetCode:           "Q_CHARSET_CODE",
	QTimeZoneCode:          "Q_TIME_ZONE_CODE",
	QCatalogNZCode:         "Q_CATALOG_NZ_CODE",
	QLCTimeNamesCode:       "Q_LC_TIME_NAMES_CODE",
	QCharsetDatabaseCode:   "Q_CHARSET_DATABASE_CODE",
	QTableMapForUpdateCode: "Q_TABLE_MAP_FOR_UPDATE_CODE",
	QMasterDataWrittenCode: "Q_MASTER_DATA_WRITTEN_CODE",
	QInvokers:              "Q_INVOKERS",
	QUpdatedDBNames:        "Q_UPDATED_DB_NAMES",
	QMicroseconds:          "Q_MICROSECONDS",
}

QStatusKey2Str is the name of status_vars

Functions

func ChecksumValidate

func ChecksumValidate(checksumType byte, expectedChecksum []byte, data []byte) bool

ChecksumValidate will validate binary log event checksum This information is from 'github.com/siddontang/go-mysql/replication/parser.go' mysql use zlib's CRC32 implementation, which uses polynomial 0xedb88320UL. reference: https://github.com/madler/zlib/blob/master/crc32.c https://github.com/madler/zlib/blob/master/doc/rfc1952.txt#L419

func FixedLengthInt

func FixedLengthInt(buf []byte) uint64

FixedLengthInt will turn byte to uint64 this function is from 'github.com/siddontang/go-mysql/replication/util.go'

func LengthEncodedInt

func LengthEncodedInt(b []byte) (num uint64, isNull bool, n int)

LengthEncodedInt will decode byte to uint64 this function is from 'github.com/siddontang/go-mysql/replication/util.go'

func LengthEnodedString

func LengthEnodedString(b []byte) ([]byte, bool, int, error)

LengthEnodedString will decode bytes

func ReadNBytes

func ReadNBytes(rd io.Reader, size int64) ([]byte, error)

ReadNBytes read n bytes from io.Reader

Types

type BaseEventBody

type BaseEventBody struct{}

BaseEventBody is base off all events

type BinEvent

type BinEvent struct {
	Header       *BinEventHeader
	Body         BinEventBody
	ChecksumType byte
	ChecksumVal  []byte
	DatabaseName string   // Kurt Larsen (ChaosHour) added this
	TableName    string   // Kurt Larsen (ChaosHour) added this
	ColumnCount  uint64   // Kurt Larsen (ChaosHour) added this
	ColumnTypes  []byte   // Kurt Larsen (ChaosHour) added this
	ColumnNames  []string // Kurt Larsen (ChaosHour) added this
}

BinEvent binary log event definition - Kurt Larsen (ChaosHour) added the following:

func (*BinEvent) String

func (event *BinEvent) String() string

String interface implement - Kurt Larsen (ChaosHour) added this:

func (*BinEvent) Validation

func (event *BinEvent) Validation(bin *BinaryLogInfo, header, body []byte) ([]byte, error)

Validation event validity check - Kurt Larsen (ChaosHour) added this:

type BinEventBody

type BinEventBody interface {
	// contains filtered or unexported methods
}

BinEventBody describe event body

type BinEventHeader

type BinEventHeader struct {
	Timestamp int64
	EventType uint8
	ServerID  int64
	EventSize int64
	LogPos    int64
	Flag      uint16
}

BinEventHeader binary log header definition https://dev.mysql.com/doc/internals/en/binlog-event-header.html

func (*BinEventHeader) String

func (header *BinEventHeader) String() string

String interface implement

func (*BinEventHeader) Type

func (header *BinEventHeader) Type() string

Type function will translate event type into string

type BinEventUnParsed

type BinEventUnParsed struct {
	BaseEventBody
	Data []byte
}

BinEventUnParsed save the event data when the event type not supported yet.

type BinFileDecoder

type BinFileDecoder struct {
	Path string // binary log path

	// binary log reading options
	Option *BinReaderOption

	// file object
	BinFile *os.File

	*BinaryLogInfo
	// contains filtered or unexported fields
}

BinFileDecoder will mapping a binary log file, decode binary log event

func NewBinFileDecoder

func NewBinFileDecoder(path string, options ...*BinReaderOption) (*BinFileDecoder, error)

NewBinFileDecoder return a BinFileDecoder with binary log file path

func (*BinFileDecoder) DecodeEvent

func (decoder *BinFileDecoder) DecodeEvent() (*BinEvent, error)

DecodeEvent will decode a single event from binary log

func (*BinFileDecoder) GetFilePos

func (decoder *BinFileDecoder) GetFilePos() (int64, error)

GetFilePos return current file position in the binary log file. Added by Kurt Larsen

func (*BinFileDecoder) GetFilePosRange

func (decoder *BinFileDecoder) GetFilePosRange() (int64, int64, error)

Get the StartPos and the EndPos of the binary log file. Added by Kurt Larsen

func (*BinFileDecoder) WalkEvent

func (decoder *BinFileDecoder) WalkEvent(f func(event *BinEvent) (isContinue bool, err error)) error

WalkEvent will walk all events for binary log which in io.Reader This function will return isFinish bool and err error.

type BinFmtDescEvent

type BinFmtDescEvent struct {
	BaseEventBody
	BinlogVersion     int
	MySQLVersion      string
	CreateTime        int64
	EventHeaderLength int64
	EventTypeHeader   []byte
	// contains filtered or unexported fields
}

BinFmtDescEvent is the definition of FORMAT_DESCRIPTION_EVENT https://dev.mysql.com/doc/internals/en/format-description-event.html

type BinIntvarEvent

type BinIntvarEvent struct {
	BaseEventBody
	Type  uint8
	Value uint64
}

BinIntvarEvent is the definition of INTVAR_EVENT https://dev.mysql.com/doc/internals/en/xid-event.html Transaction ID for 2PC, written whenever a COMMIT is expected.

type BinLogChecksumEvent

type BinLogChecksumEvent struct {
	BaseEventBody
	Flags    uint16
	Checksum uint32
}

Binlog_checksum_event https://dev.mysql.com/doc/internals/en/binlog-checksum-event.html The binlog_checksum_event is added to the binlog as last event to tell the reader what binlog checksum was used.

type BinLogVersionEvent

type BinLogVersionEvent struct {
	BaseEventBody
	Version uint16
}

Binlog_version_event https://dev.mysql.com/doc/internals/en/binlog-version-event.html The binlog_version_event is added to the binlog as last event to tell the reader what binlog version was used.

type BinPreGTIDsEvent

type BinPreGTIDsEvent struct{ BaseEventBody }

BinPreGTIDsEvent is the definition of PREVIOUS_GTIDS_EVENT TODO: PREVIOUS_GTIDS_EVENT

type BinQueryEvent

type BinQueryEvent struct {
	BaseEventBody
	SlaveProxyID  int64
	ExecutionTime int64
	ErrorCode     uint16

	StatusVars []byte
	Schema     string
	Query      string
	// contains filtered or unexported fields
}

BinQueryEvent is the definition of QUERY_EVENT https://dev.mysql.com/doc/internals/en/query-event.html

func (*BinQueryEvent) Statue

func (event *BinQueryEvent) Statue() error

Statue will format status_vars of QUERY_EVENT TODO decode QUERY_EVENT status_var

func (*BinQueryEvent) String

func (event *BinQueryEvent) String() string

TODO decode QUERY_EVENT status_var

type BinReaderOption

type BinReaderOption struct {
	StartPos  int64
	EndPos    int64
	StartTime time.Time
	EndTime   time.Time
}

BinReaderOption will describe the details to tell decoders when it should start and when stop. with time [start, end)

func (*BinReaderOption) Start

func (o *BinReaderOption) Start(header *BinEventHeader) bool

Start return bool of if start decoding

func (*BinReaderOption) Stop

func (o *BinReaderOption) Stop(header *BinEventHeader) bool

Stop return bool of if stop decoding

type BinRotateEvent

type BinRotateEvent struct {
	BaseEventBody
	Position uint64
	FileName string
}

BinRotateEvent is the definition of ROTATE_EVENT https://dev.mysql.com/doc/internals/en/rotate-event.html The rotate event is added to the binlog as last event to tell the reader what binlog to request next.

type BinRowsEvent

type BinRowsEvent struct {
	BaseEventBody
	// header
	Version int
	TableID uint64

	Flags uint16
	// if version == 2
	ExtraData []byte

	// body
	ColumnCount    uint64
	ColumnsBitmap1 []byte
	// if UPDATE_ROWS_EVENTv1 or v2
	ColumnsBitmap2 []byte
	// contains filtered or unexported fields
}

BinRowsEvent describe MySQL ROWS_EVENT https://dev.mysql.com/doc/internals/en/rows-event.html

func (*BinRowsEvent) Init

func (e *BinRowsEvent) Init(h *BinFmtDescEvent, eventType uint8) *BinRowsEvent

Init BinRowsEvent, adding version and table_id length

type BinRowsQueryEvent

type BinRowsQueryEvent struct {
	BaseEventBody
	SlaveProxyID  uint32
	ExecutionTime uint32
	SchemaName    string
	Query         string
}

Binlog::Rows_query_log_event - Kurt Larsen https://dev.mysql.com/doc/internals/en/rows-query-log-event.html The rows_query_log_event is added to the binlog as last event to tell the reader what query was executed.

type BinTableMapEvent

type BinTableMapEvent struct {
	BaseEventBody
	TableID uint64

	Flags         uint16
	Schema        string
	Table         string
	ColumnCount   uint64
	ColumnTypeDef []byte
	ColumnMetaDef []uint16
	NullBitmap    []byte
	// contains filtered or unexported fields
}

BinTableMapEvent is the definition of TABLE_MAP_EVENT https://dev.mysql.com/doc/internals/en/table-map-event.html

func (*BinTableMapEvent) Init

Init BinTableMapEvent tableIDLen

type BinXIDEvent

type BinXIDEvent struct {
	BaseEventBody
	XID uint64
}

BinXIDEvent is the definition of XID_EVENT https://dev.mysql.com/doc/internals/en/xid-event.html Transaction ID for 2PC, written whenever a COMMIT is expected.

type BinaryLogInfo

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

BinaryLogInfo is the base decoder of all types

Jump to

Keyboard shortcuts

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