forked from wal-g/wal-g
/
redis.go
99 lines (88 loc) · 2.09 KB
/
redis.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
package helpers
import (
"context"
"fmt"
"github.com/go-redis/redis"
"github.com/wal-g/tracelog"
"math/rand"
"strconv"
"time"
)
type RedisCtl struct {
host string
port int
binPath string
confPath string
*redis.Client
ctx context.Context
}
func NewRedisCtl(ctx context.Context, host string, port int, password, binPath, confPath string) (*RedisCtl, error) {
expHost, expPort, err := ExposedHostPort(ctx, host, port)
if err != nil {
return nil, fmt.Errorf("Expose host failed: %v\n", err)
}
client := redis.NewClient(&redis.Options{
Addr: expHost + ":" + strconv.Itoa(expPort),
Password: password,
DB: 0,
})
return &RedisCtl{
host,
port,
binPath,
confPath,
client,
ctx,
}, nil
}
func (rc *RedisCtl) Addr() string {
return rc.Options().Addr
}
func (rc *RedisCtl) Host() string {
return rc.host
}
type Strings struct {
arraylist []string
}
func (rc *RedisCtl) WriteTestData(mark string, docsCount int) error {
var rows []interface{}
for k := 1; k <= docsCount; k++ {
var data interface{}
switch rand.Intn(3) {
case 1:
data = "string_val"
case 2:
data = 100500
case 3:
data = Strings{[]string{"hello", "there"}}
}
rows = append(rows, fmt.Sprintf("%s_key_num%d", mark, k), data)
}
status := rc.MSet(rows...)
tracelog.DebugLogger.Printf("WriteTestData result: %v", status)
if status.Err() != nil {
return fmt.Errorf("failed to write test data to redis: %w", status.Err())
}
return nil
}
func (rc *RedisCtl) PushBackup() (string, error) {
exec, err := rc.runCmd([]string{"backup-push"})
if err != nil {
return "", err
}
return BackupNameFromCreate(exec.Combined()), nil
}
func (rc *RedisCtl) runCmd(run []string) (ExecResult, error) {
command := []string{rc.binPath, "--config", rc.confPath}
command = append(command, run...)
exc, err := RunCommandStrict(rc.ctx, rc.host, command)
return exc, err
}
func (rc *RedisCtl) PurgeRetain(keepNumber int) error {
_, err := rc.runCmd([]string{
"delete",
"--retain-count", strconv.Itoa(keepNumber),
"--retain-after", time.Now().Format("2006-01-02T15:04:05Z"),
"--confirm"})
return err
}