generated from cybozu-go/neco-template
/
mysql_status.go
166 lines (154 loc) · 7.08 KB
/
mysql_status.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package server
import (
"context"
"database/sql"
"errors"
"fmt"
"strconv"
"time"
)
// MySQLGlobalVariablesStatus defines the observed global variable state of a MySQL instance
type MySQLGlobalVariablesStatus struct {
ReadOnly bool `db:"@@read_only"`
SuperReadOnly bool `db:"@@super_read_only"`
RplSemiSyncMasterWaitForSlaveCount int `db:"@@rpl_semi_sync_master_wait_for_slave_count"`
CloneValidDonorList sql.NullString `db:"@@clone_valid_donor_list"`
}
// MySQLCloneStateStatus defines the observed clone state of a MySQL instance
type MySQLCloneStateStatus struct {
State sql.NullString `db:"state"`
}
// MySQLPrimaryStatus defines the observed state of a primary
type MySQLPrimaryStatus struct {
ExecutedGtidSet string `db:"Executed_Gtid_Set"`
// All of variables from here are NOT used in MOCO's reconcile
File string `db:"File"`
Position string `db:"Position"`
BinlogDoDB string `db:"Binlog_Do_DB"`
BinlogIgnoreDB string `db:"Binlog_Ignore_DB"`
}
// MySQLReplicaStatus defines the observed state of a replica
type MySQLReplicaStatus struct {
LastIOErrno int `db:"Last_IO_Errno"`
LastIOError string `db:"Last_IO_Error"`
LastSQLErrno int `db:"Last_SQL_Errno"`
LastSQLError string `db:"Last_SQL_Error"`
MasterHost string `db:"Master_Host"`
RetrievedGtidSet string `db:"Retrieved_Gtid_Set"`
ExecutedGtidSet string `db:"Executed_Gtid_Set"`
SlaveIORunning string `db:"Slave_IO_Running"`
SlaveSQLRunning string `db:"Slave_SQL_Running"`
// All of variables from here are NOT used in MOCO's reconcile
SlaveIOState string `db:"Slave_IO_State"`
MasterUser string `db:"Master_User"`
MasterPort int `db:"Master_Port"`
ConnectRetry int `db:"Connect_Retry"`
MasterLogFile string `db:"Master_Log_File"`
ReadMasterLogPos int `db:"Read_Master_Log_Pos"`
RelayLogFile string `db:"Relay_Log_File"`
RelayLogPos int `db:"Relay_Log_Pos"`
RelayMasterLogFile string `db:"Relay_Master_Log_File"`
ReplicateDoDB string `db:"Replicate_Do_DB"`
ReplicateIgnoreDB string `db:"Replicate_Ignore_DB"`
ReplicateDoTable string `db:"Replicate_Do_Table"`
ReplicateIgnoreTable string `db:"Replicate_Ignore_Table"`
ReplicateWildDoTable string `db:"Replicate_Wild_Do_Table"`
ReplicateWildIgnoreTable string `db:"Replicate_Wild_Ignore_Table"`
LastErrno int `db:"Last_Errno"`
LastError string `db:"Last_Error"`
SkipCounter int `db:"Skip_Counter"`
ExecMasterLogPos int `db:"Exec_Master_Log_Pos"`
RelayLogSpace int `db:"Relay_Log_Space"`
UntilCondition string `db:"Until_Condition"`
UntilLogFile string `db:"Until_Log_File"`
UntilLogPos int `db:"Until_Log_Pos"`
MasterSSLAllowed string `db:"Master_SSL_Allowed"`
MasterSSLCAFile string `db:"Master_SSL_CA_File"`
MasterSSLCAPath string `db:"Master_SSL_CA_Path"`
MasterSSLCert string `db:"Master_SSL_Cert"`
MasterSSLCipher string `db:"Master_SSL_Cipher"`
MasterSSLKey string `db:"Master_SSL_Key"`
SecondsBehindMaster sql.NullInt64 `db:"Seconds_Behind_Master"`
MasterSSLVerifyServerCert string `db:"Master_SSL_Verify_Server_Cert"`
ReplicateIgnoreServerIds string `db:"Replicate_Ignore_Server_Ids"`
MasterServerID int `db:"Master_Server_Id"`
MasterUUID string `db:"Master_UUID"`
MasterInfoFile string `db:"Master_Info_File"`
SQLDelay int `db:"SQL_Delay"`
SQLRemainingDelay sql.NullInt64 `db:"SQL_Remaining_Delay"`
SlaveSQLRunningState string `db:"Slave_SQL_Running_State"`
MasterRetryCount int `db:"Master_Retry_Count"`
MasterBind string `db:"Master_Bind"`
LastIOErrorTimestamp string `db:"Last_IO_Error_Timestamp"`
LastSQLErrorTimestamp string `db:"Last_SQL_Error_Timestamp"`
MasterSSLCrl string `db:"Master_SSL_Crl"`
MasterSSLCrlpath string `db:"Master_SSL_Crlpath"`
AutoPosition string `db:"Auto_Position"`
ReplicateRewriteDB string `db:"Replicate_Rewrite_DB"`
ChannelName string `db:"Channel_Name"`
MasterTLSVersion string `db:"Master_TLS_Version"`
Masterpublickeypath string `db:"Master_public_key_path"`
Getmasterpublickey string `db:"Get_master_public_key"`
NetworkNamespace string `db:"Network_Namespace"`
}
func (a *Agent) GetMySQLGlobalVariable(ctx context.Context) (*MySQLGlobalVariablesStatus, error) {
status := &MySQLGlobalVariablesStatus{}
err := a.db.GetContext(ctx, status, `SELECT @@read_only, @@super_read_only, @@rpl_semi_sync_master_wait_for_slave_count, @@clone_valid_donor_list`)
if err != nil {
return nil, fmt.Errorf("failed to get global variable: %w", err)
}
return status, nil
}
func (a *Agent) GetMySQLCloneStateStatus(ctx context.Context) (*MySQLCloneStateStatus, error) {
status := &MySQLCloneStateStatus{}
err := a.db.GetContext(ctx, status, `SELECT state FROM performance_schema.clone_status`)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return &MySQLCloneStateStatus{}, nil
}
return nil, err
}
return status, nil
}
func (a *Agent) GetMySQLPrimaryStatus(ctx context.Context) (*MySQLPrimaryStatus, error) {
status := &MySQLPrimaryStatus{}
if err := a.db.GetContext(ctx, status, `SHOW MASTER STATUS`); err != nil {
return nil, fmt.Errorf("failed to show master status: %w", err)
}
return status, nil
}
func (a *Agent) GetMySQLReplicaStatus(ctx context.Context) (*MySQLReplicaStatus, error) {
status := &MySQLReplicaStatus{}
if err := a.db.GetContext(ctx, status, `SHOW SLAVE STATUS`); err != nil {
return nil, fmt.Errorf("failed to show slave status: %w", err)
}
return status, nil
}
func (a *Agent) GetTransactionTimestamps(ctx context.Context) (queued, applied time.Time, uptime time.Duration, err error) {
err = a.db.GetContext(ctx, &queued, `
SELECT MAX(LAST_QUEUED_TRANSACTION_ORIGINAL_COMMIT_TIMESTAMP)
FROM performance_schema.replication_connection_status`)
if err != nil {
return
}
err = a.db.GetContext(ctx, &applied, `
SELECT MAX(LAST_APPLIED_TRANSACTION_ORIGINAL_COMMIT_TIMESTAMP)
FROM performance_schema.replication_applier_status_by_worker`)
if err != nil {
return
}
var uptime_seconds_string string
err = a.db.GetContext(ctx, &uptime_seconds_string, `
SELECT VARIABLE_VALUE
FROM performance_schema.global_status
WHERE VARIABLE_NAME='Uptime'`)
if err != nil {
return
}
uptime_seconds, err := strconv.Atoi(uptime_seconds_string)
if err != nil {
return
}
uptime = time.Second * time.Duration(uptime_seconds)
return
}