-
Notifications
You must be signed in to change notification settings - Fork 489
/
clean.go
259 lines (222 loc) · 8.01 KB
/
clean.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
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright 2019 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package clean
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"os/exec"
"time"
"github.com/pingcap/tidb-operator/cmd/backup-manager/app/constants"
"github.com/pingcap/tidb-operator/cmd/backup-manager/app/util"
"github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1"
bkutil "github.com/pingcap/tidb-operator/pkg/backup/util"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/klog/v2"
)
var (
defaultBackoff = wait.Backoff{
Duration: 100 * time.Millisecond,
Factor: 2.0,
Jitter: 0,
Steps: 8,
Cap: time.Second,
}
)
const (
metaFile = "/backupmeta"
CloudAPIConcurrency = 3
)
// Options contains the input arguments to the backup command
type Options struct {
Namespace string
BackupName string
}
func (bo *Options) String() string {
return fmt.Sprintf("%s/%s", bo.Namespace, bo.BackupName)
}
// cleanBackupMetaWithVolSnapshots clean snapshot and the backup meta
func (bo *Options) cleanBackupMetaWithVolSnapshots(ctx context.Context, backup *v1alpha1.Backup) error {
backend, err := bkutil.NewStorageBackend(backup.Spec.StorageProvider, &bkutil.StorageCredential{})
if err != nil {
return err
}
defer backend.Close()
err = bo.deleteSnapshotsAndBackupMeta(ctx, backup)
if err != nil {
klog.Errorf("For backup %s clean, failed to clean backup: %s", bo, err)
}
return err
}
func (bo *Options) deleteSnapshotsAndBackupMeta(ctx context.Context, backup *v1alpha1.Backup) error {
//1. get backupmeta and fetch the snapshot information
//rclone copy remote:/bukect/backup/backupmeta /backupmeta
opts := util.GetOptions(backup.Spec.StorageProvider)
if err := bo.copyRemoteBackupMetaToLocal(ctx, backup.Status.BackupPath, opts); err != nil {
klog.Warningf("rclone copy remote backupmeta to local failure, err: %s. it possible that bucket or backup folder is deleted already. a mannual check is require", err)
return nil
}
defer func() {
_ = os.Remove(metaFile)
}()
contents, err := os.ReadFile(metaFile)
if errors.Is(err, os.ErrNotExist) {
klog.Errorf("read metadata file %s failed, err: %s, a manual check or delete action required.", metaFile, err)
return err
} else if err != nil { // will retry it
klog.Errorf("read metadata file %s failed, err: %s", metaFile, err)
return err
}
metaInfo := &bkutil.EBSBasedBRMeta{}
if err = json.Unmarshal(contents, metaInfo); err != nil {
klog.Errorf("rclone copy remote backupmeta to local failure.")
return err
}
//2. delete the snapshot
if err = bo.deleteVolumeSnapshots(metaInfo); err != nil {
klog.Errorf("delete volume snapshot failure, a mannual check or delete aciton require.")
return err
}
//3. delete the backupmeta info
if err = bo.cleanRemoteBackupData(ctx, backup.Status.BackupPath, opts); err != nil {
return err
}
return nil
}
func (bo *Options) deleteVolumeSnapshots(meta *bkutil.EBSBasedBRMeta) error {
newVolumeIDMap := make(map[string]string)
for i := range meta.TiKVComponent.Stores {
store := meta.TiKVComponent.Stores[i]
for j := range store.Volumes {
vol := store.Volumes[j]
newVolumeIDMap[vol.ID] = vol.SnapshotID
}
}
ec2Session, err := bkutil.NewEC2Session(CloudAPIConcurrency)
if err != nil {
klog.Errorf("new a ec2 session failure.")
return err
}
if err = ec2Session.DeleteSnapshots(newVolumeIDMap); err != nil {
klog.Errorf("delete snapshot failure.")
return err
}
return nil
}
// CleanBRRemoteBackupData clean the backup data from remote
func (bo *Options) CleanBRRemoteBackupData(ctx context.Context, backup *v1alpha1.Backup) error {
opt := backup.GetCleanOption()
backend, err := bkutil.NewStorageBackend(backup.Spec.StorageProvider, &bkutil.StorageCredential{})
if err != nil {
return err
}
defer backend.Close()
round := 0
return util.RetryOnError(ctx, opt.RetryCount, 0, util.RetriableOnAnyError, func() error {
round++
err := bo.cleanBRRemoteBackupDataOnce(ctx, backend, opt, round)
if err != nil {
klog.Errorf("For backup %s clean %d, failed to clean backup: %s", bo, round, err)
}
return err
})
}
func (bo *Options) cleanBRRemoteBackupDataOnce(ctx context.Context, backend *bkutil.StorageBackend, opt v1alpha1.CleanOption, round int) error {
klog.Infof("For backup %s clean %d, start to clean backup with opt: %+v", bo, round, opt)
iter := backend.ListPage(nil)
backoff := defaultBackoff
index := 0
count, deletedCount, failedCount := 0, 0, 0
for {
needBackoff := false
index++
logPrefix := fmt.Sprintf("For backup %s clean %d-%d", bo, round, index)
objs, err := iter.Next(ctx, int(opt.PageSize))
if err == io.EOF {
break
}
if err != nil {
return err
}
klog.Infof("%s, try to delete %d objects", logPrefix, len(objs))
result := backend.BatchDeleteObjects(ctx, objs, opt.BatchDeleteOption)
count += len(objs)
deletedCount += len(result.Deleted)
failedCount += len(result.Errors)
if len(result.Deleted) != 0 {
klog.Infof("%s, delete %d objects successfully", logPrefix, len(result.Deleted))
for _, obj := range result.Deleted {
klog.V(4).Infof("%s, delete object %s successfully", logPrefix, obj)
}
}
if len(result.Errors) != 0 {
klog.Errorf("%s, delete %d objects failed", logPrefix, len(result.Errors))
for _, oerr := range result.Errors {
klog.V(4).Infof("%s, delete object %s failed: %s", logPrefix, oerr.Key, oerr.Err)
}
needBackoff = true
}
if len(result.Deleted)+len(result.Errors) < len(objs) {
klog.Errorf("%s, sum of deleted and failed objects %d is less than expected", logPrefix, len(result.Deleted)+len(result.Errors))
needBackoff = true
}
if opt.BackoffEnabled {
if needBackoff {
time.Sleep(backoff.Step())
} else {
backoff = defaultBackoff // reset backoff
}
}
}
klog.Infof("For backup %s clean %d, clean backup finished, total:%d deleted:%d failed:%d", bo, round, count, deletedCount, failedCount)
if deletedCount < count {
return fmt.Errorf("some objects failed to be deleted")
}
objs, err := backend.ListPage(nil).Next(ctx, int(opt.PageSize))
if err != nil && err != io.EOF {
return err
}
if len(objs) != 0 {
return fmt.Errorf("some objects are missing to be deleted")
}
return nil
}
func (bo *Options) cleanRemoteBackupData(ctx context.Context, bucket string, opts []string) error {
destBucket := util.NormalizeBucketURI(bucket)
args := util.ConstructRcloneArgs(constants.RcloneConfigArg, opts, "delete", destBucket, "", true)
output, err := exec.CommandContext(ctx, "rclone", args...).CombinedOutput()
if err != nil {
return fmt.Errorf("cluster %s, execute rclone delete command failed, output: %s, err: %v", bo, string(output), err)
}
args = util.ConstructRcloneArgs(constants.RcloneConfigArg, opts, "delete", fmt.Sprintf("%s.tmp", destBucket), "", true)
output, err = exec.CommandContext(ctx, "rclone", args...).CombinedOutput()
if err != nil {
return fmt.Errorf("cluster %s, execute rclone delete command failed, output: %s, err: %v", bo, string(output), err)
}
klog.Infof("cluster %s backup %s was deleted successfully", bo, bucket)
return nil
}
// copy remote backupmeta to local
func (bo *Options) copyRemoteBackupMetaToLocal(ctx context.Context, bucket string, opts []string) error {
destBucket := util.NormalizeBucketURI(bucket)
args := util.ConstructRcloneArgs(constants.RcloneConfigArg, opts, "copy", fmt.Sprintf("%s/backupmeta", destBucket), "/", true)
output, err := exec.CommandContext(ctx, "rclone", args...).CombinedOutput()
if err != nil {
return fmt.Errorf("cluster %s, execute rclone copy command failed, output: %s, err: %v", bo, string(output), err)
}
klog.Infof("cluster %s backup %s was copy successfully", bo, bucket)
return nil
}