generated from cybozu-go/neco-template
/
status.go
75 lines (63 loc) · 2.2 KB
/
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
package dbop
import (
"context"
"database/sql"
"errors"
"fmt"
"strings"
)
var statusGlobalVarsString = strings.Join(statusGlobalVars, ",")
func (o *operator) GetStatus(ctx context.Context) (*MySQLInstanceStatus, error) {
status := &MySQLInstanceStatus{}
globalVariablesStatus, err := o.getGlobalVariablesStatus(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get global variables: pod=%s, namespace=%s: %w", o.name, o.namespace, err)
}
status.GlobalVariables = *globalVariablesStatus
if err := o.db.Select(&status.ReplicaHosts, `SHOW SLAVE HOSTS`); err != nil {
return nil, fmt.Errorf("failed to get slave hosts: pod=%s, namespace=%s: %w", o.name, o.namespace, err)
}
replicaStatus, err := o.getReplicaStatus(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get replica status: pod=%s, namespace=%s: %w", o.name, o.namespace, err)
}
status.ReplicaStatus = replicaStatus
cloneStatus, err := o.getCloneStateStatus(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get clone status: pod=%s, namespace=%s: %w", o.name, o.namespace, err)
}
status.CloneStatus = cloneStatus
return status, nil
}
func (o *operator) getGlobalVariablesStatus(ctx context.Context) (*GlobalVariables, error) {
status := &GlobalVariables{}
err := o.db.GetContext(ctx, status, "SELECT "+statusGlobalVarsString)
if err != nil {
return nil, fmt.Errorf("failed to get mysql global variables: %w", err)
}
return status, nil
}
func (o *operator) getReplicaStatus(ctx context.Context) (*ReplicaStatus, error) {
status := &ReplicaStatus{}
err := o.db.GetContext(ctx, status, `SHOW SLAVE STATUS`)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
// slave status can be empty for non-replica servers
return nil, nil
}
return nil, fmt.Errorf("failed to get slave status: %w", err)
}
return status, nil
}
func (o *operator) getCloneStateStatus(ctx context.Context) (*CloneStatus, error) {
status := &CloneStatus{}
err := o.db.GetContext(ctx, status, `SELECT state FROM performance_schema.clone_status`)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
// clone status can be empty
return nil, nil
}
return nil, fmt.Errorf("failed to get ps.clone_status: %w", err)
}
return status, nil
}