-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathasync_replication.go
More file actions
329 lines (272 loc) · 7.78 KB
/
async_replication.go
File metadata and controls
329 lines (272 loc) · 7.78 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package main
import (
"context"
"log"
"os"
"path/filepath"
"github.com/pkg/errors"
"github.com/sjmudd/stopwatch"
"k8s.io/apimachinery/pkg/util/sets"
apiv1alpha1 "github.com/percona/percona-server-mysql-operator/api/v1alpha1"
database "github.com/percona/percona-server-mysql-operator/cmd/db"
mysqldb "github.com/percona/percona-server-mysql-operator/pkg/db"
"github.com/percona/percona-server-mysql-operator/pkg/mysql"
)
func bootstrapAsyncReplication(ctx context.Context) error {
timer := stopwatch.NewNamedStopwatch()
err := timer.AddMany([]string{"clone", "total"})
if err != nil {
return errors.Wrap(err, "add timers")
}
timer.Start("total")
defer func() {
timer.Stop("total")
log.Printf("bootstrap finished in %f seconds", timer.ElapsedSeconds("total"))
}()
svc := os.Getenv("SERVICE_NAME_UNREADY")
mysqlSvc := os.Getenv("SERVICE_NAME")
peers, err := lookup(svc)
if err != nil {
return errors.Wrap(err, "lookup")
}
log.Printf("Peers: %v", sets.List(peers))
fqdn, err := getFQDN(mysqlSvc)
if err != nil {
return errors.Wrap(err, "get FQDN")
}
log.Printf("FQDN: %s", fqdn)
primary, replicas, err := getTopology(ctx, fqdn, peers)
if err != nil {
return errors.Wrap(err, "select donor")
}
log.Printf("Primary: %s Replicas: %v", primary, replicas)
podHostname, err := os.Hostname()
if err != nil {
return errors.Wrap(err, "get hostname")
}
podIp, err := getPodIP(podHostname)
if err != nil {
return errors.Wrap(err, "get pod IP")
}
log.Printf("PodIP: %s", podIp)
primaryIp, err := getPodIP(primary)
if err != nil {
return errors.Wrap(err, "get primary IP")
}
log.Printf("PrimaryIP: %s", primaryIp)
donor, err := selectDonor(ctx, fqdn, primary, replicas)
if err != nil {
return errors.Wrap(err, "select donor")
}
log.Printf("Donor: %s", donor)
log.Printf("Opening connection to %s", podIp)
operatorPass, err := getSecret(apiv1alpha1.UserOperator)
if err != nil {
return errors.Wrapf(err, "get %s password", apiv1alpha1.UserOperator)
}
params := database.DBParams{
User: apiv1alpha1.UserOperator,
Pass: operatorPass,
Host: podIp,
}
readTimeout, err := getReadTimeout()
if err != nil {
return errors.Wrap(err, "get read timeout")
}
params.ReadTimeout = readTimeout
db, err := database.NewDatabase(ctx, params)
if err != nil {
return errors.Wrap(err, "connect to database")
}
defer db.Close()
if err := db.StopReplication(ctx); err != nil {
return err
}
switch {
case donor == "":
if err := db.ResetReplication(ctx); err != nil {
return err
}
log.Printf("Can't find a donor, we're on our own.")
return nil
case donor == fqdn:
if err := db.ResetReplication(ctx); err != nil {
return err
}
log.Printf("I'm the donor and therefore the primary.")
return nil
case primary == fqdn || primaryIp == podIp:
if err := db.ResetReplication(ctx); err != nil {
return err
}
log.Printf("I'm the primary.")
return nil
}
cloneLock := filepath.Join(mysql.DataMountPath, "clone.lock")
requireClone, err := isCloneRequired(cloneLock)
if err != nil {
return errors.Wrap(err, "check if clone is required")
}
log.Printf("Clone required: %t", requireClone)
if requireClone {
log.Println("Checking if a clone in progress")
inProgress, err := db.CloneInProgress(ctx)
if err != nil {
return errors.Wrap(err, "check if a clone in progress")
}
log.Printf("Clone in progress: %t", inProgress)
if inProgress {
return nil
}
if err := db.DisableSuperReadonly(ctx); err != nil {
return errors.Wrap(err, "disable super read only")
}
timer.Start("clone")
log.Printf("Cloning from %s", donor)
err = db.Clone(ctx, donor, string(apiv1alpha1.UserOperator), operatorPass, mysql.DefaultAdminPort)
timer.Stop("clone")
if err != nil && !errors.Is(err, database.ErrRestartAfterClone) {
return errors.Wrapf(err, "clone from donor %s", donor)
}
err = createCloneLock(cloneLock)
if err != nil {
return errors.Wrap(err, "create clone lock")
}
log.Println("Clone finished. Restarting container...")
// We return with 1 to restart container
os.Exit(1)
}
if !requireClone {
if err := deleteCloneLock(cloneLock); err != nil {
return errors.Wrap(err, "delete clone lock")
}
}
rStatus, _, err := db.ReplicationStatus(ctx)
if err != nil {
return errors.Wrap(err, "check replication status")
}
if rStatus == mysqldb.ReplicationStatusNotInitiated {
log.Println("configuring replication")
replicaPass, err := getSecret(apiv1alpha1.UserReplication)
if err != nil {
return errors.Wrapf(err, "get %s password", apiv1alpha1.UserReplication)
}
if err := db.StopReplication(ctx); err != nil {
return errors.Wrap(err, "stop replication")
}
if err := db.StartReplication(ctx, primary, replicaPass, mysql.DefaultPort); err != nil {
return errors.Wrap(err, "start replication")
}
}
if err := db.EnableSuperReadonly(ctx); err != nil {
return errors.Wrap(err, "enable super read only")
}
return nil
}
func getTopology(ctx context.Context, fqdn string, peers sets.Set[string]) (string, []string, error) {
replicas := sets.New[string]()
primary := ""
operatorPass, err := getSecret(apiv1alpha1.UserOperator)
if err != nil {
return "", nil, errors.Wrapf(err, "get %s password", apiv1alpha1.UserOperator)
}
for _, peer := range sets.List(peers) {
params := database.DBParams{
User: apiv1alpha1.UserOperator,
Pass: operatorPass,
Host: peer,
}
readTimeout, err := getReadTimeout()
if err != nil {
return "", nil, errors.Wrap(err, "get read timeout")
}
params.ReadTimeout = readTimeout
db, err := database.NewDatabase(ctx, params)
if err != nil {
return "", nil, errors.Wrapf(err, "connect to %s", peer)
}
defer db.Close()
status, source, err := db.ReplicationStatus(ctx)
if err != nil {
return "", nil, errors.Wrap(err, "check replication status")
}
replicaHost, err := db.ReportHost(ctx)
if err != nil {
return "", nil, errors.Wrap(err, "get report_host")
}
if replicaHost == "" {
continue
}
replicas.Insert(replicaHost)
if status == mysqldb.ReplicationStatusActive {
primary = source
}
}
if primary == "" && peers.Len() == 1 {
primary = sets.List(peers)[0]
} else if primary == "" {
for _, r := range sets.List(replicas) {
// We should set primary to the first replica, which is not the bootstrapped pod.
// The bootstrapped pod can't be a primary.
// Even if it was a primary before, orchestrator will promote another replica "as result of DeadMaster".
if r != fqdn {
primary = r
break
}
}
}
if replicas.Len() > 0 {
replicas.Delete(primary)
}
return primary, sets.List(replicas), nil
}
func selectDonor(ctx context.Context, fqdn, primary string, replicas []string) (string, error) {
donor := ""
operatorPass, err := getSecret(apiv1alpha1.UserOperator)
if err != nil {
return "", errors.Wrapf(err, "get %s password", apiv1alpha1.UserOperator)
}
for _, replica := range replicas {
params := database.DBParams{
User: apiv1alpha1.UserOperator,
Pass: operatorPass,
Host: replica,
}
readTimeout, err := getReadTimeout()
if err != nil {
return "", errors.Wrap(err, "get read timeout")
}
params.ReadTimeout = readTimeout
db, err := database.NewDatabase(ctx, params)
if err != nil {
continue
}
db.Close()
if fqdn != replica {
donor = replica
break
}
}
if donor == "" && fqdn != primary {
donor = primary
}
return donor, nil
}
func isCloneRequired(file string) (bool, error) {
_, err := os.Stat(file)
if err != nil {
if os.IsNotExist(err) {
return true, nil
}
return false, errors.Wrapf(err, "stat %s", file)
}
return false, nil
}
func createCloneLock(file string) error {
_, err := os.Create(file)
return errors.Wrapf(err, "create %s", file)
}
func deleteCloneLock(file string) error {
err := os.Remove(file)
return errors.Wrapf(err, "remove %s", file)
}