mysqlutil

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2025 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// GroupInfoUnset status of the group info: no data set
	GroupInfoUnset = "Unset"

	// GroupInfoInconsistent status of the group info: nodesinfos is not consistent between nodes
	GroupInfoInconsistent = "Inconsistent"

	// GroupInfoConsistent status of the group info: nodeinfos is complete and consistent between nodes
	GroupInfoConsistent = "Consistent"

	// GroupInfoPartial status of the group info: data is not complete (some nodes didn't respond) but group is avaiable
	GroupInfoPartial = "Partial"

	// GroupInfoUnavailable status of the group info: data is not complete (some nodes didn't respond) but group is unavailable
	GroupInfoUnavailable = "Unavailable"
)
View Source
const (
	// MysqlPrimaryRole mysql role primary
	MysqlPrimaryRole = "PRIMARY"
	// MysqlSecondaryRole mysql role secondary
	MysqlSecondaryRole = "SECONDARY"
	// MysqlUnknownRole mysql role unknown
	MysqlUnknownRole = "UNKNOWN"
)
View Source
const (
	MysqlOnlineState      = "ONLINE"
	MysqlOfflineState     = "OFFLINE"
	MysqlUnreachableState = "UNREACHABLE"
	MysqlRecoveringState  = "RECOVERING"
	MysqlErrorState       = "ERROR"
	MysqlMissingState     = "MISSING"
)
View Source
const (
	// DefaultMysqlPort define the default MySQL Port
	DefaultMysqlPort = 3306
	// MysqlSourceRole mysql role source
	MysqlSourceRole = "Source"
	// MysqlReplicaRole mysql role replica
	MysqlReplicaRole = "Replica"
)
View Source
const (

	// ErrNotFound cannot find a node to connect to
	ErrNotFound = "unable to find a node to connect"
)

Variables

This section is empty.

Functions

func ScanMap

func ScanMap(rows *sql.Rows) (map[string]sql.NullString, error)

Types

type AdminConnections

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

AdminConnections connection map for mysql replication cluster currently the admin connection is not threadSafe since it is only use in the Events thread.

func (*AdminConnections) Add

func (cnx *AdminConnections) Add(addr string) error

Add connect to the given address and register the client connection to the map

func (*AdminConnections) AddAll

func (cnx *AdminConnections) AddAll(addrs []string)

AddAll connect to the given list of addresses and register them in the map fail silently

func (*AdminConnections) Get

func (cnx *AdminConnections) Get(addr string) (IClient, error)

Get returns a client connection for the given adress, connects if the connection is not in the map yet

func (*AdminConnections) GetAll

func (cnx *AdminConnections) GetAll() map[string]IClient

GetAll returns a map of all clients per address

func (*AdminConnections) GetSelected

func (cnx *AdminConnections) GetSelected(addrs []string) map[string]IClient

GetSelected returns a map of clients based on the input addresses

func (*AdminConnections) Reconnect

func (cnx *AdminConnections) Reconnect(addr string) error

Reconnect force a reconnection on the given address is the adress is not part of the map, act like Add

func (*AdminConnections) Remove

func (cnx *AdminConnections) Remove(addr string)

Remove disconnect and remove the client connection from the map

func (*AdminConnections) ReplaceAll

func (cnx *AdminConnections) ReplaceAll(addrs []string)

ReplaceAll clear the pool and re-populate it with new connections fail silently

func (*AdminConnections) Reset

func (cnx *AdminConnections) Reset()

Reset close all connections and clear the connection map

type AdminOptions

type AdminOptions struct {
	ConnectionTimeout int
	Username          string
	Password          string
}

AdminOptions optional options for redis admin

type Client

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

Client structure representing a client connection to mysql

func (*Client) Close

func (c *Client) Close() error

Close closes the connection.

func (*Client) Exec

func (c *Client) Exec(ctx context.Context, query string, args ...any) (sql.Result, error)

func (*Client) Query

func (c *Client) Query(ctx context.Context, query string, args ...any) (*sql.Rows, error)

func (*Client) QueryRow

func (c *Client) QueryRow(ctx context.Context, input interface{}, query string, args ...any) error

type GroupAdmin

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

GroupAdmin wraps redis cluster admin logic

func (*GroupAdmin) Close

func (a *GroupAdmin) Close()

Close used to close all possible resources instance by the ReplicationAdmin

func (*GroupAdmin) Connections

func (a *GroupAdmin) Connections() IAdminConnections

Connections returns the connection map of all clients

func (*GroupAdmin) EnsureGroupSeeds

func (a *GroupAdmin) EnsureGroupSeeds(ctx context.Context, addr, seeds, allowList string) error

func (*GroupAdmin) GetGroupInfos

func (a *GroupAdmin) GetGroupInfos(ctx context.Context, memberCount int) *GroupInfos

GetGroupInfos return the ReplicationNodes infos for all nodes

func (*GroupAdmin) JoinGroup

func (a *GroupAdmin) JoinGroup(ctx context.Context, addr, replicationUser, replicationPassword string) error

func (*GroupAdmin) SetupGroup

func (a *GroupAdmin) SetupGroup(ctx context.Context, addr, replicationUser, replicationPassword string) error

func (*GroupAdmin) StartGroupReplication

func (a *GroupAdmin) StartGroupReplication(ctx context.Context, addr string) error

StartGroupReplication stop group replication

func (*GroupAdmin) StopGroupReplication

func (a *GroupAdmin) StopGroupReplication(ctx context.Context, addr string) error

StopGroupReplication stop group replication

type GroupInfos

type GroupInfos struct {
	Infos  map[string]*GroupNode
	Status string
}

GroupInfos represents the node infos for all nodes of the cluster

func NewGroupInfos

func NewGroupInfos() *GroupInfos

NewGroupInfos returns an instance of GroupInfos

func (*GroupInfos) ElectPrimary

func (g *GroupInfos) ElectPrimary() string

type GroupNode

type GroupNode struct {
	*GroupNodeInfo
	ReadOnly      bool
	SuperReadOnly bool
	GtidExecuted  string
}

func NewDefaultGroupNode

func NewDefaultGroupNode() *GroupNode

NewDefaultGroupNode builds and returns new defaultNode instance

func (*GroupNode) GetRole

GetRole return the Mysql Replication Node GetRole

type GroupNodeInfo

type GroupNodeInfo struct {
	ID    string
	Host  string
	Port  int
	Role  string
	State string
}

func NewDefaultGroupNodeInfo

func NewDefaultGroupNodeInfo() *GroupNodeInfo

NewDefaultGroupNodeInfo builds and returns new default GroupNodeInfo instance

type IAdminConnections

type IAdminConnections interface {
	// AddAll connect to the given list of addresses and
	// register them in the map
	// fail silently
	AddAll(addrs []string)

	// Add connect to the given address and
	// register the client connection to the pool
	Add(addr string) error

	// Reconnect force a reconnection on the given address
	// if the adress is not part of the map, act like Add
	Reconnect(addr string) error

	// Remove disconnect and remove the client connection from the map
	Remove(addr string)

	// ReplaceAll clear the map and re-populate it with new connections
	// fail silently
	ReplaceAll(addrs []string)

	// Reset close all connections and clear the connection map
	Reset()

	// Get returns a client connection for the given address,
	// connects if the connection is not in the map yet
	Get(addr string) (IClient, error)

	// GetAll returns a map of all clients per address
	GetAll() map[string]IClient

	//GetSelected returns a map of clients based on the input addresses
	GetSelected(addrs []string) map[string]IClient
}

IAdminConnections interface representing the map of admin connections to mysql nodes

func NewAdminConnections

func NewAdminConnections(addrs []string, options *AdminOptions, log logr.Logger) IAdminConnections

NewAdminConnections returns and instance of AdminConnectionsInterface

type IClient

type IClient interface {
	// Close closes the connection.
	Close() error

	// Exec calls the given query like
	Exec(ctx context.Context, query string, args ...any) (sql.Result, error)
	Query(ctx context.Context, query string, args ...any) (*sql.Rows, error)
	QueryRow(ctx context.Context, input interface{}, query string, args ...any) error
}

IClient mysql client interface

func NewClient

func NewClient(addr, username, password string, timeout int) (IClient, error)

NewClient build a client connection and connect to a mysql

type IGroupAdmin

type IGroupAdmin interface {
	// Connections returns the connection map of all clients
	Connections() IAdminConnections

	// Close the admin connections
	Close()

	// GetGroupInfos return the GroupInfos for all nodes
	GetGroupInfos(ctx context.Context, memberCount int) *GroupInfos

	SetupGroup(ctx context.Context, addr, replicationUser, replicationPassword string) error

	JoinGroup(ctx context.Context, addr, replicationUser, replicationPassword string) error

	EnsureGroupSeeds(ctx context.Context, addr, seeds, allowList string) error

	StopGroupReplication(ctx context.Context, addr string) error
}

IGroupAdmin mysql group replication admin interface

func NewGroupAdmin

func NewGroupAdmin(addrs []string, options *AdminOptions, log logr.Logger) IGroupAdmin

NewGroupAdmin returns new AdminInterface instance at the same time it connects to all Redis ReplicationNodes thanks to the address list

type IReplicationAdmin

type IReplicationAdmin interface {
	// Connections returns the connection map of all clients
	Connections() IAdminConnections

	// Close the admin connections
	Close()

	// GetReplicationStatus return the ReplicationNodes infos for all nodes
	GetReplicationStatus(ctx context.Context) *ReplicationInfo

	// SetSemiSyncSourceON set mysql variables semi_sync_source_enabled to ON
	SetSemiSyncSourceON(ctx context.Context, addr string) error

	// SetSemiSyncSourceOFF set mysql variables semi_sync_source_enabled to OFF
	SetSemiSyncSourceOFF(ctx context.Context, addr string) error

	// SetSemiSyncReplicaON set mysql variables semi_sync_replica_enabled to ON
	SetSemiSyncReplicaON(ctx context.Context, addr string) error

	// SetSemiSyncReplicaOFF set mysql variables semi_sync_replica_enabled to OFF
	SetSemiSyncReplicaOFF(ctx context.Context, addr string) error

	// SetAllNodeReadOnly set all mysql node read_only super_read_only to true
	SetAllNodeReadOnly(ctx context.Context) error

	// SetReadOnly set mysql variables read_only to flag
	SetReadOnly(ctx context.Context, addr string, flag bool) error

	// SetSuperReadOnly set mysql variables super_read_only to flag
	SetSuperReadOnly(ctx context.Context, addr string, flag bool) error

	// ResetReplica set mysql reset replica all
	ResetReplica(ctx context.Context, addr string) error

	// WaitSourcePos wait mysql source position
	WaitSourcePos(ctx context.Context, addr, SourceLogFile, ReadSourceLogPos string) error

	// SetReplica set mysql replica source and start replica
	SetReplica(ctx context.Context, addr, sourceHost, sourcePort, replicationUser, replicationPwd string) error

	// StopReplica stop mysql replica process
	StopReplica(ctx context.Context, addr string) error

	// StartReplica start mysql replica process
	StartReplica(ctx context.Context, addr string) error

	// GetUser get mysql users list
	GetUser(ctx context.Context, addr, host string, filter []string) (Users, error)

	// GetVersion return mysql version
	GetVersion(ctx context.Context, addr string) (string, error)
}

IReplicationAdmin mysql admin interface

func NewReplicationAdmin

func NewReplicationAdmin(addrs []string, options *AdminOptions, log logr.Logger) IReplicationAdmin

NewReplicationAdmin returns new AdminInterface instance at the same time it connects to all Redis ReplicationNodes thanks to the addrs list

type ReplicationAdmin

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

ReplicationAdmin wraps redis cluster admin logic

func (*ReplicationAdmin) Close

func (a *ReplicationAdmin) Close()

Close used to close all possible resources instanciate by the ReplicationAdmin

func (*ReplicationAdmin) Connections

func (a *ReplicationAdmin) Connections() IAdminConnections

Connections returns the connection map of all clients

func (*ReplicationAdmin) GetReplicationStatus

func (a *ReplicationAdmin) GetReplicationStatus(ctx context.Context) *ReplicationInfo

GetReplicationStatus return the ReplicationNodes infos for all nodes

func (*ReplicationAdmin) GetUser

func (a *ReplicationAdmin) GetUser(ctx context.Context, addr, host string, filters []string) (Users, error)

func (*ReplicationAdmin) GetVersion

func (a *ReplicationAdmin) GetVersion(ctx context.Context, addr string) (string, error)

func (*ReplicationAdmin) ResetReplica

func (a *ReplicationAdmin) ResetReplica(ctx context.Context, addr string) error

func (*ReplicationAdmin) SetAllNodeReadOnly

func (a *ReplicationAdmin) SetAllNodeReadOnly(ctx context.Context) error

func (*ReplicationAdmin) SetReadOnly

func (a *ReplicationAdmin) SetReadOnly(ctx context.Context, addr string, flag bool) error

func (*ReplicationAdmin) SetReplica

func (a *ReplicationAdmin) SetReplica(ctx context.Context, addr, sourceHost, sourcePort, replicationUser, replicationPwd string) error

func (*ReplicationAdmin) SetSemiSyncReplicaOFF

func (a *ReplicationAdmin) SetSemiSyncReplicaOFF(ctx context.Context, addr string) error

func (*ReplicationAdmin) SetSemiSyncReplicaON

func (a *ReplicationAdmin) SetSemiSyncReplicaON(ctx context.Context, addr string) error

func (*ReplicationAdmin) SetSemiSyncSourceOFF

func (a *ReplicationAdmin) SetSemiSyncSourceOFF(ctx context.Context, addr string) error

func (*ReplicationAdmin) SetSemiSyncSourceON

func (a *ReplicationAdmin) SetSemiSyncSourceON(ctx context.Context, addr string) error

func (*ReplicationAdmin) SetSuperReadOnly

func (a *ReplicationAdmin) SetSuperReadOnly(ctx context.Context, addr string, flag bool) error

func (*ReplicationAdmin) StartReplica

func (a *ReplicationAdmin) StartReplica(ctx context.Context, addr string) error

func (*ReplicationAdmin) StopReplica

func (a *ReplicationAdmin) StopReplica(ctx context.Context, addr string) error

func (*ReplicationAdmin) WaitSourcePos

func (a *ReplicationAdmin) WaitSourcePos(ctx context.Context, addr, SourceLogFile, ReadSourceLogPos string) error

type ReplicationInfo

type ReplicationInfo struct {
	Nodes map[string]*ReplicationNode
}

func NewReplicationInfo

func NewReplicationInfo() *ReplicationInfo

type ReplicationNode

type ReplicationNode struct {
	Host                   string
	Port                   int
	Role                   string
	Ready                  bool
	ReadOnly               bool
	SuperReadOnly          bool
	SourceHost             string
	SourcePort             string
	ReplicaIO              string
	ReplicaSQL             string
	ReadSourceLogPos       string
	ExecSourceLogPos       string
	SourceLogFile          string
	SecondsBehindSource    int
	SemiSyncSourceEnabled  bool
	SemiSyncReplicaEnabled bool
}

func DecodeNode

func DecodeNode(result map[string]sql.NullString, addr string, log logr.Logger) *ReplicationNode

DecodeNode decode from the cmd output the MySQL nodes info. Second argument is the node on which we are connected to request info

func NewDefaultReplicationNode

func NewDefaultReplicationNode() *ReplicationNode

NewDefaultReplicationNode builds and returns new defaultNode instance

func (*ReplicationNode) GetExecSourceLogPos

func (n *ReplicationNode) GetExecSourceLogPos() int

func (*ReplicationNode) GetReadSourceLogPos

func (n *ReplicationNode) GetReadSourceLogPos() int

func (*ReplicationNode) GetRole

GetRole return the Mysql Replication ReplicationNode GetRole

func (*ReplicationNode) GetSourcePort

func (n *ReplicationNode) GetSourcePort() int

func (*ReplicationNode) HostPort

func (n *ReplicationNode) HostPort() string

HostPort returns join Host Port string

type ReplicationNodes

type ReplicationNodes []*ReplicationNode

ReplicationNodes represent a ReplicationNode slice

type User

type User struct {
	Username       string
	Authentication string
}

type Users

type Users map[string]*User

func NewDefaultUsers

func NewDefaultUsers() Users

func (Users) TransferToAPI

func (u Users) TransferToAPI() []string

Jump to

Keyboard shortcuts

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