agent

package
v2.6.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2019 License: Apache-2.0 Imports: 38 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultName is the default name of the command line.
	DefaultName = "mysql-agent"
)

Variables

View Source
var (
	// Version defines the version of agent
	Version = "1.0.0+git"

	// GitHash will be set during make
	GitHash = "Not provided (use make build instead of go build)"
	// BuildTS will be set during make
	BuildTS = "Not provided (use make build instead of go build)"
)

Functions

func Concatenate

func Concatenate(arrays ...[]clientv3.Op) []clientv3.Op

Concatenate creates a new array and concatenates all passed-in arrays together

func DoWithRetry

func DoWithRetry(f func() error, funcName string, retryTimes int, retryInterval time.Duration) error

DoWithRetry will execute f once, followed by `retryTimes` retry if returned err is not nil so given DoWithRetry(f, 2), f will be executed at most 3 times, 1 execution and 2 retries, with the last time error returned.

func InitLogger

func InitLogger(cfg *Config) error

InitLogger initializes Agent's logger.

func PrintVersionInfo

func PrintVersionInfo()

PrintVersionInfo show version info to log

func StartClean

func StartClean(cfg *Config) error

StartClean should be invoked by another process when agent exits with status code 3, is used to clean the data that should have been cleaned by agent gracefully shutdown.

Types

type Config

type Config struct {
	*flag.FlagSet

	// ClusterName distinguishes one Master-Slaves cluster from another
	ClusterName string `toml:"cluster-name" json:"cluster-name"`

	// LeaderLeaseTTL is the leader lease time to live, measured by second
	LeaderLeaseTTL int64 `toml:"leader-lease-ttl" json:"leader-lease-ttl"`
	// ShutdownThreshold is the time that costs when agent shutdown, measured by second
	ShutdownThreshold int64 `toml:"shutdown-threshold" json:"shutdown-threshold"`

	DataDir    string `toml:"data-dir" json:"data-dir"`
	NodeID     string `toml:"node-id" json:"node-id"`
	ListenAddr string `toml:"addr" json:"addr"`

	// OnlyFollow decides whether current node join in leader campaign
	// if onlyFollow is true, current node will NOT campaign leader
	// else, aka onlyFollow is false, node will participate in leader campaign
	OnlyFollow bool `toml:"only-follow" json:"only-follow"`

	EtcdURLs        string `toml:"etcd-urls" json:"etcd-urls"`
	EtcdDialTimeout time.Duration
	RefreshInterval int    `toml:"refresh-interval" json:"refresh-interval"`
	RegisterTTL     int    `toml:"register-ttl" json:"register-ttl"`
	EtcdRootPath    string `toml:"etcd-root-path" json:"etcd-root-path"`
	EtcdUsername    string `toml:"etcd-username" json:"etcd-username"`
	EtcdPassword    string `toml:"etcd-password" json:"etcd-password"`

	LogLevel    string `toml:"log-level" json:"log-level"`
	LogFile     string `toml:"log-file" json:"log-file"`
	ErrorLog    string `toml:"error-log" json:"error-log"`
	LogMaxSize  int    `toml:"log-max-size" json:"log-max-size"`
	LogMaxDays  int    `toml:"log-max-days" json:"log-max-days"`
	LogCompress bool   `toml:"log-compress" json:"log-compress"`

	ServiceType string         `toml:"service-type" json:"service-type"`
	DBConfig    types.DBConfig `toml:"db-config" json:"db-config"`

	InternalServiceHost string `toml:"internal-service-host" json:"internal-service-host"`
	ExternalServiceHost string `toml:"external-service-host" json:"external-service-host"`

	ForkProcessFile       string   `toml:"fork-process-file" json:"fork-process-file"`
	ForkProcessArgs       []string `toml:"fork-process-args" json:"fork-process-args"`
	ForkProcessWaitSecond int      `toml:"fork-process-wait-second" json:"fork-process-wait-second"`

	CampaignWaitTime time.Duration
	// contains filtered or unexported fields
}

Config holds the configuration of agent

func NewConfig

func NewConfig() *Config

NewConfig return an instance of configuration

func (*Config) Parse

func (cfg *Config) Parse(arguments []string) error

Parse parse all config from command-line flags, or configuration file.

type EtcdRegistry

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

EtcdRegistry wraps the reactions with etcd.

func NewEtcdRegistry

func NewEtcdRegistry(cli *etcd.Client, reqTimeout time.Duration) *EtcdRegistry

NewEtcdRegistry returns an EtcdRegistry client.

func (*EtcdRegistry) Close

func (r *EtcdRegistry) Close() error

Close closes the etcd client.

func (*EtcdRegistry) Node

func (r *EtcdRegistry) Node(pctx context.Context, nodeID string) (*NodeStatus, error)

Node returns node status in etcd by nodeID.

func (*EtcdRegistry) RefreshNode

func (r *EtcdRegistry) RefreshNode(pctx context.Context, nodeID string) error

RefreshNode keeps heartbeats with etcd.

func (*EtcdRegistry) RegisterNode

func (r *EtcdRegistry) RegisterNode(pctx context.Context, nodeID, internalHost, externalHost string, ttl int64) error

RegisterNode registers node in the etcd.

func (*EtcdRegistry) UnregisterNode

func (r *EtcdRegistry) UnregisterNode(pctx context.Context, nodeID string) error

UnregisterNode unregisters node from etcd.

type Leader

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

Leader represents the Leader info get from etcd

type LeaderMeta

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

LeaderMeta is the meta-info of the leader, which is persist in etcd, with no expiration

type LogForElection

type LogForElection struct {
	Term     uint64 `json:"term"`
	LastUUID string `json:"last_uuid"`
	LastGTID string `json:"last_gtid"`
	EndTxnID uint64 `json:"end_txn_id"`

	// Version is the schema version of LogForElection, used for backward compatibility
	Version int `json:"version"`
}

LogForElection describes the necessary information (log) for election

type Node

type Node interface {
	// ID returns node ID
	ID() string
	// Register registers node to etcd.
	Register(ctx context.Context) error
	// Unregister unregisters node from etcd.
	Unregister(ctx context.Context) error
	// Heartbeat refreshes node state in etcd.
	// key 'root/nodes/<nodeID/alive' will disappear after TTL time passed.
	Heartbeat(ctx context.Context) <-chan error
	// RowClient returns raw etcd client.
	RawClient() *etcd.Client
	// NodeStatus return one node status
	NodeStatus(ctx context.Context, nodeID string) (*NodeStatus, error)
}

Node defines actions with etcd.

func NewAgentNode

func NewAgentNode(cfg *Config) (Node, error)

NewAgentNode returns a MySQL agentNode that monitor MySQL server.

type NodeStatus

type NodeStatus struct {
	NodeID       string
	InternalHost string
	ExternalHost string
	IsAlive      bool     `json:",omitempty"`
	LatestPos    Position `json:",omitempty"`
}

NodeStatus describes the status information of a node in etcd.

type Position

type Position struct {
	File string
	Pos  string
	GTID string

	UUID string

	EndTxnID uint64 `json:",omitempty"`

	SlaveIORunning      bool `json:",omitempty"`
	SlaveSQLRunning     bool `json:",omitempty"`
	SecondsBehindMaster int  `json:",omitempty"`
}

Position describes the position of MySQL binlog.

type Server

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

Server maintains agent's status at runtime.

func NewServer

func NewServer(cfg *Config) (*Server, error)

NewServer returns an instance of agent server.

func (*Server) ChangeMaster

func (s *Server) ChangeMaster(w http.ResponseWriter, r *http.Request)

ChangeMaster triggers change master by endpoint

func (*Server) Close

func (s *Server) Close()

Close gracefully releases resource of agent server.

func (*Server) MasterCheck

func (s *Server) MasterCheck(w http.ResponseWriter, r *http.Request)

MasterCheck return status 200 iff current node is master else status 418

func (*Server) SetOnlyFollow

func (s *Server) SetOnlyFollow(w http.ResponseWriter, r *http.Request)

SetOnlyFollow sets the s.onlyFollow to true or false, depending on the param `onlyFollow` passed in

func (*Server) SetReadOnly

func (s *Server) SetReadOnly(w http.ResponseWriter, r *http.Request)

SetReadOnly sets mysql readonly

func (*Server) SetReadWrite

func (s *Server) SetReadWrite(w http.ResponseWriter, r *http.Request)

SetReadWrite sets mysql readwrite

func (*Server) SlaveCheck

func (s *Server) SlaveCheck(w http.ResponseWriter, r *http.Request)

SlaveCheck return status 200 iff current node is not master else status 418

func (*Server) Start

func (s *Server) Start() error

Start runs Server, and maintains heartbeat to etcd.

type ServiceManager

type ServiceManager interface {
	// SetReadOnly set service readonly
	SetReadOnly() error
	// SetReadWrite set service readwrite
	SetReadWrite() error
	// PromoteToMaster promotes a slave to master
	PromoteToMaster() error
	// RedirectMaster make a slave point to another master
	RedirectMaster(masterHost, masterPort string) error
	// SetReadOnlyManually sets service readonly, but only executed manually
	SetReadOnlyManually() (bool, error)

	LoadSlaveStatusFromDB() (*Position, error)
	LoadMasterStatusFromDB() (*Position, error)
	LoadReplicationInfoOfMaster() (masterUUID, executedGTID string, endTxnID uint64, err error)
	LoadReplicationInfoOfSlave() (masterUUID, executedGTID string, endTxnID uint64, err error)
	GetServerUUID() (string, error)

	Close() error
}

ServiceManager defines some functions to manage service

func NewMySQLServiceManager

func NewMySQLServiceManager(dbConfig types.DBConfig, timeout time.Duration) (ServiceManager, error)

NewMySQLServiceManager returns the instance of mysqlServiceManager

func NewPostgreSQLServiceManager

func NewPostgreSQLServiceManager(dbConfig types.DBConfig, timeout time.Duration) (ServiceManager, error)

NewPostgreSQLServiceManager returns the instance of mysqlServiceManager

type WarnHook

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

WarnHook is used to write log whose level higher than and equal to warn to another file

func (WarnHook) Fire

func (h WarnHook) Fire(e *log.Entry) error

Fire is used to write entry to log file

func (WarnHook) Levels

func (h WarnHook) Levels() []log.Level

Levels return the log level that fires the "Fire" function

Jump to

Keyboard shortcuts

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