/
rethinkdb.go
247 lines (199 loc) · 7.31 KB
/
rethinkdb.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package rethinkdb
import (
"context"
"fmt"
"log/slog"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"time"
"errors"
"github.com/metal-stack/backup-restore-sidecar/cmd/internal/probe"
"github.com/metal-stack/backup-restore-sidecar/cmd/internal/utils"
"github.com/metal-stack/backup-restore-sidecar/pkg/constants"
"golang.org/x/sync/errgroup"
r "gopkg.in/rethinkdb/rethinkdb-go.v6"
)
const (
connectionTimeout = 1 * time.Second
restoreDatabaseStartupTimeout = 30 * time.Second
rethinkDBCmd = "rethinkdb"
rethinkDBDumpCmd = "rethinkdb-dump"
rethinkDBRestoreCmd = "rethinkdb-restore"
)
var (
rethinkDBBackupFilePath = filepath.Join(constants.BackupDir, "rethinkdb")
rethinkDBRestoreFilePath = filepath.Join(constants.RestoreDir, "rethinkdb")
)
// RethinkDB implements the database interface
type RethinkDB struct {
datadir string
url string
passwordFile string
log *slog.Logger
executor *utils.CmdExecutor
}
// New instantiates a new rethinkdb database
func New(log *slog.Logger, datadir string, url string, passwordFile string) *RethinkDB {
return &RethinkDB{
log: log,
datadir: datadir,
url: url,
passwordFile: passwordFile,
executor: utils.NewExecutor(log),
}
}
// Check indicates whether a restore of the database is required or not.
func (db *RethinkDB) Check(_ context.Context) (bool, error) {
empty, err := utils.IsEmpty(db.datadir)
if err != nil {
return false, err
}
if empty {
db.log.Info("data directory is empty")
return true, err
}
return false, nil
}
// Backup takes a backup of the database
func (db *RethinkDB) Backup(ctx context.Context) error {
if err := os.RemoveAll(constants.BackupDir); err != nil {
return fmt.Errorf("could not clean backup directory: %w", err)
}
if err := os.MkdirAll(constants.BackupDir, 0777); err != nil {
return fmt.Errorf("could not create backup directory: %w", err)
}
args := []string{"-f", rethinkDBBackupFilePath}
if db.passwordFile != "" {
args = append(args, "--password-file="+db.passwordFile)
}
if db.url != "" {
args = append(args, "--connect="+db.url)
}
out, err := db.executor.ExecuteCommandWithOutput(ctx, rethinkDBDumpCmd, nil, args...)
fmt.Println(out)
if err != nil {
return fmt.Errorf("error running backup command: %w", err)
}
if strings.Contains(out, "0 rows exported from 0 tables, with 0 secondary indexes, and 0 hook functions") {
return errors.New("the database is empty, taking a backup is not yet possible")
}
if _, err := os.Stat(rethinkDBBackupFilePath); os.IsNotExist(err) {
return fmt.Errorf("backup file was not created: %s", rethinkDBBackupFilePath)
}
db.log.Debug("successfully took backup of rethinkdb database")
return nil
}
// Recover restores a database backup
func (db *RethinkDB) Recover(ctx context.Context) error {
if _, err := os.Stat(rethinkDBRestoreFilePath); os.IsNotExist(err) {
return fmt.Errorf("restore file not present: %s", rethinkDBRestoreFilePath)
}
passwordRaw, err := os.ReadFile(db.passwordFile)
if err != nil {
return fmt.Errorf("unable to read rethinkdb password file at %s: %w", db.passwordFile, err)
}
// rethinkdb requires to be running when restoring a backup.
// however, if we let the real database container start, we cannot interrupt it anymore in case
// an issue occurs during the restoration. therefore, we spin up an own instance of rethinkdb
// inside the sidecar against which we can restore.
var (
cmd *exec.Cmd
g, _ = errgroup.WithContext(ctx)
rethinkdbCtx, cancelRethinkdb = context.WithCancel(ctx) // cancel sends a KILL signal to the process
// IMPORTANT: when the recovery goes wrong, the database directory MUST be cleaned up
// otherwise on pod restart the database directory is not empty anymore and
// the backup-restore-sidecar will assume it's a fresh database and let the
// database start without restored data, which can mess up things big time
handleFailedRecovery = func(restoreErr error) error {
db.log.Error("trying to handle failed database recovery", "error", restoreErr)
// kill the rethinkdb process
cancelRethinkdb()
db.log.Info("waiting for async rethinkdb go routine to stop")
err := g.Wait()
if err != nil {
db.log.Error("rethinkdb go routine finished with error", "error", err)
}
if err := os.RemoveAll(db.datadir); err != nil {
db.log.Error("unable to cleanup database data directory after failed recovery attempt, high risk of starting with fresh database on container restart", "err", err)
} else {
db.log.Info("cleaned up database data directory after failed recovery attempt to prevent start of fresh database")
}
return restoreErr
}
)
defer cancelRethinkdb()
g.Go(func() error {
args := []string{"--bind", "all", "--driver-port", "1", "--directory", db.datadir, "--initial-password", strings.TrimSpace(string(passwordRaw))}
db.log.Debug("execute rethinkdb", "args", args)
cmd = exec.CommandContext(rethinkdbCtx, rethinkDBCmd, args...) // nolint:gosec
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
return fmt.Errorf("unable to run rethinkdb: %w", err)
}
db.log.Info("rethinkdb process finished")
return nil
})
db.log.Info("waiting for rethinkdb database to come up")
probeCtx, probeCancel := context.WithTimeout(ctx, restoreDatabaseStartupTimeout)
defer probeCancel()
restoreDB := New(db.log, db.datadir, "localhost:1", db.passwordFile)
err = probe.Start(probeCtx, restoreDB.log, restoreDB)
if err != nil {
return handleFailedRecovery(fmt.Errorf("rethinkdb did not come up: %w", err))
}
args := []string{}
if db.url != "" {
args = append(args, "--connect="+restoreDB.url)
}
if db.passwordFile != "" {
args = append(args, "--password-file="+db.passwordFile)
}
args = append(args, rethinkDBRestoreFilePath)
out, err := db.executor.ExecuteCommandWithOutput(ctx, rethinkDBRestoreCmd, nil, args...)
fmt.Println(out)
if err != nil {
return handleFailedRecovery(fmt.Errorf("error running restore command: %w", err))
}
if err := cmd.Process.Signal(syscall.SIGTERM); err != nil {
db.log.Error("failed to send sigterm signal to rethinkdb, killing it", "error", err)
cancelRethinkdb()
}
err = g.Wait()
if err != nil {
db.log.Error("rethinkdb process not properly terminated, but restore was successful", "error", err)
} else {
db.log.Info("successfully restored rethinkdb database")
}
return nil
}
// Probe figures out if the database is running and available for taking backups.
func (db *RethinkDB) Probe(ctx context.Context) error {
passwordRaw, err := os.ReadFile(db.passwordFile)
if err != nil {
return fmt.Errorf("unable to read rethinkdb password file at %s: %w", db.passwordFile, err)
}
session, err := r.Connect(r.ConnectOpts{
Addresses: []string{db.url},
Username: "admin",
Password: strings.TrimSpace(string(passwordRaw)),
MaxIdle: 10,
MaxOpen: 20,
})
if err != nil {
return fmt.Errorf("cannot create rethinkdb client: %w", err)
}
_, err = r.DB("rethinkdb").Table("server_status").Run(session)
if err != nil {
return fmt.Errorf("error retrieving rethinkdb server status: %w", err)
}
return nil
}
// Upgrade performs an upgrade of the database in case a newer version of the database is detected.
func (db *RethinkDB) Upgrade(_ context.Context) error {
return nil
}