-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollback.go
102 lines (82 loc) · 2.47 KB
/
rollback.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
package exporter
import (
"bytes"
"context"
"fmt"
"mobserver/internal/metric"
"mobserver/internal/mongoutils"
"os/exec"
"strings"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/mongo"
)
type rollbackCollector struct {
ctx context.Context
base *baseCollector
}
func newRollbackCollector(client *mongo.Client, logger *logrus.Logger) prometheus.Collector {
return &rollbackCollector{
ctx: context.Background(),
base: newBaseCollector(client, logger),
}
}
func (c *rollbackCollector) Describe(ch chan<- *prometheus.Desc) {
c.base.Describe(ch, c.collect)
}
func (c *rollbackCollector) Collect(ch chan<- prometheus.Metric) {
c.base.Collect(ch)
}
func (c *rollbackCollector) collect(ch chan<- prometheus.Metric) {
// Rollback info represents a map of collection UUIDs
// to a boolean value indicating whether a rollback is in progress.
rollbackInfo, err := c.getRollbackStatus()
if err != nil {
c.base.logger.Errorf("Failed to get rollback status: %v", err)
return
}
for _, mt := range metric.RollbackStatusToPromMetrics(rollbackInfo) {
ch <- mt
}
}
func (c *rollbackCollector) getRollbackStatus() (map[string]bool, error) {
var out bytes.Buffer
var stderr bytes.Buffer
cmdLineOpts, err := mongoutils.GetCmdLineOpts(c.ctx, c.base.client)
if err != nil {
return nil, fmt.Errorf("failed to get command line options: %v", err)
}
rollbackDir := cmdLineOpts.Parsed.Storage.DBPath + "/rollback"
res := make(map[string]bool)
cmd := exec.Command("bash", "-c", fmt.Sprintf("[ -d %s ]", rollbackDir))
if err := cmd.Run(); err != nil {
// If the rollback directory does not exist, there is no rollback in progress.
return res, nil
}
cmd = exec.Command("bash", "-c", fmt.Sprintf("ls -l %s | grep -v total | awk '{print $NF}'", rollbackDir))
cmd.Stdout = &out
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return nil, fmt.Errorf("failed to get rollback status: %v", stderr.String())
}
collUUIDs := strings.Split(out.String(), "\n")
if len(collUUIDs) == 0 {
return res, nil
}
collInfo, err := mongoutils.GetAllDatabasesAndCollections(c.ctx, c.base.client)
if err != nil {
return nil, fmt.Errorf("failed to get collection UUIDs: %v", err)
}
for _, uid := range collUUIDs {
if uid == "" {
continue
}
if ns, ok := collInfo[uid]; !ok {
c.base.logger.Warnf("Unknown collection UUID: %s", uid)
res["unknown.unknown"] = true
} else {
res[ns] = true
}
}
return res, nil
}