|
| 1 | +// Copyright 2019 PingCAP, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package compact |
| 15 | + |
| 16 | +import ( |
| 17 | + "bytes" |
| 18 | + "context" |
| 19 | + "encoding/json" |
| 20 | + "io" |
| 21 | + "os" |
| 22 | + "os/exec" |
| 23 | + "path/filepath" |
| 24 | + "strconv" |
| 25 | + |
| 26 | + "github.com/pingcap/errors" |
| 27 | + "github.com/pingcap/tidb-operator/cmd/backup-manager/app/compact/options" |
| 28 | + backuputil "github.com/pingcap/tidb-operator/cmd/backup-manager/app/util" |
| 29 | + "github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1" |
| 30 | + pkgutil "github.com/pingcap/tidb-operator/pkg/backup/util" |
| 31 | + listers "github.com/pingcap/tidb-operator/pkg/client/listers/pingcap/v1alpha1" |
| 32 | + "github.com/pingcap/tidb-operator/pkg/controller" |
| 33 | + "github.com/pingcap/tidb-operator/pkg/util" |
| 34 | + "k8s.io/klog/v2" |
| 35 | +) |
| 36 | + |
| 37 | +const ( |
| 38 | + messageCompactionDone = "Finishing compaction." |
| 39 | + messageCompactAborted = "Compaction aborted." |
| 40 | +) |
| 41 | + |
| 42 | +// logLine is line of JSON log. |
| 43 | +// It just extracted the message from the JSON and keeps the origin json bytes. |
| 44 | +// So you may extract fields from it by `json.Unmarshal(l.Raw, ...)`. |
| 45 | +type logLine struct { |
| 46 | + Message string `json:"Message"` |
| 47 | + Raw json.RawMessage `json:"-"` |
| 48 | +} |
| 49 | + |
| 50 | +// Manager mainly used to manage backup related work |
| 51 | +type Manager struct { |
| 52 | + compact *v1alpha1.CompactBackup |
| 53 | + resourceLister listers.CompactBackupLister |
| 54 | + statusUpdater controller.CompactStatusUpdaterInterface |
| 55 | + options options.CompactOpts |
| 56 | +} |
| 57 | + |
| 58 | +// NewManager return a Manager |
| 59 | +func NewManager( |
| 60 | + lister listers.CompactBackupLister, |
| 61 | + statusUpdater controller.CompactStatusUpdaterInterface, |
| 62 | + compactOpts options.CompactOpts) *Manager { |
| 63 | + compact, err := lister.CompactBackups(compactOpts.Namespace).Get(compactOpts.ResourceName) |
| 64 | + if err != nil { |
| 65 | + klog.Errorf("can't find compact %s:%s CRD object, err: %v", compactOpts.Namespace, compactOpts.ResourceName, err) |
| 66 | + return nil |
| 67 | + } |
| 68 | + return &Manager{ |
| 69 | + compact, |
| 70 | + lister, |
| 71 | + statusUpdater, |
| 72 | + compactOpts, |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func (cm *Manager) brBin() string { |
| 77 | + return filepath.Join(util.BRBinPath, "br") |
| 78 | +} |
| 79 | + |
| 80 | +func (cm *Manager) kvCtlBin() string { |
| 81 | + return filepath.Join(util.KVCTLBinPath, "tikv-ctl") |
| 82 | +} |
| 83 | + |
| 84 | +// ProcessBackup used to process the backup logic |
| 85 | +func (cm *Manager) ProcessCompact() error { |
| 86 | + var err error |
| 87 | + ctx, cancel := backuputil.GetContextForTerminationSignals(cm.options.ResourceName) |
| 88 | + defer cancel() |
| 89 | + |
| 90 | + compact, err := cm.resourceLister.CompactBackups(cm.options.Namespace).Get(cm.options.ResourceName) |
| 91 | + defer func() { |
| 92 | + cm.statusUpdater.OnFinish(ctx, cm.compact, err) |
| 93 | + }() |
| 94 | + if err != nil { |
| 95 | + return errors.New("backup not found") |
| 96 | + } |
| 97 | + if err = options.ParseCompactOptions(compact, &cm.options); err != nil { |
| 98 | + return errors.Annotate(err, "failed to parse compact options") |
| 99 | + } |
| 100 | + |
| 101 | + b64, err := cm.base64ifyStorage(ctx) |
| 102 | + if err != nil { |
| 103 | + return errors.Annotate(err, "failed to base64ify storage") |
| 104 | + } |
| 105 | + return cm.runCompaction(ctx, b64) |
| 106 | +} |
| 107 | + |
| 108 | +func (cm *Manager) base64ifyStorage(ctx context.Context) (string, error) { |
| 109 | + brCmd, err := cm.base64ifyCmd(ctx) |
| 110 | + if err != nil { |
| 111 | + return "", err |
| 112 | + } |
| 113 | + out, err := brCmd.Output() |
| 114 | + if err != nil { |
| 115 | + eerr, ok := err.(*exec.ExitError) |
| 116 | + if !ok { |
| 117 | + return "", errors.Annotatef(err, "failed to execute BR with args %v", brCmd.Args) |
| 118 | + } |
| 119 | + klog.Warningf("Failed to execute base64ify; stderr = %s", string(eerr.Stderr)) |
| 120 | + return "", errors.Annotatef(err, "failed to execute BR with args %v", brCmd.Args) |
| 121 | + } |
| 122 | + out = bytes.Trim(out, "\r\n \t") |
| 123 | + return string(out), nil |
| 124 | +} |
| 125 | + |
| 126 | +func (cm *Manager) base64ifyCmd(ctx context.Context) (*exec.Cmd, error) { |
| 127 | + br := cm.brBin() |
| 128 | + args := []string{ |
| 129 | + "operator", |
| 130 | + "base64ify", |
| 131 | + } |
| 132 | + StorageOpts, err := pkgutil.GenStorageArgsForFlag(cm.compact.Spec.StorageProvider, "storage") |
| 133 | + if err != nil { |
| 134 | + return nil, err |
| 135 | + } |
| 136 | + args = append(args, StorageOpts...) |
| 137 | + return exec.CommandContext(ctx, br, args...), nil |
| 138 | +} |
| 139 | + |
| 140 | +func (cm *Manager) runCompaction(ctx context.Context, base64Storage string) (err error) { |
| 141 | + cmd := cm.compactCmd(ctx, base64Storage) |
| 142 | + |
| 143 | + // tikvLog is used to capture the log from tikv-ctl, which is sent to stderr by default |
| 144 | + tikvLog, err := cmd.StderrPipe() |
| 145 | + if err != nil { |
| 146 | + return errors.Annotate(err, "failed to create stderr pipe for compact") |
| 147 | + } |
| 148 | + if err := cmd.Start(); err != nil { |
| 149 | + return errors.Annotate(err, "failed to start compact") |
| 150 | + } |
| 151 | + |
| 152 | + cm.statusUpdater.OnStart(ctx, cm.compact) |
| 153 | + err = cm.processCompactionLogs(ctx, io.TeeReader(tikvLog, os.Stdout)) |
| 154 | + if err != nil { |
| 155 | + return err |
| 156 | + } |
| 157 | + |
| 158 | + return cmd.Wait() |
| 159 | +} |
| 160 | + |
| 161 | +func (cm *Manager) compactCmd(ctx context.Context, base64Storage string) *exec.Cmd { |
| 162 | + ctl := cm.kvCtlBin() |
| 163 | + // You should not change the log configuration here, it should sync with the upstream setting |
| 164 | + args := []string{ |
| 165 | + "--log-level", |
| 166 | + "INFO", |
| 167 | + "--log-format", |
| 168 | + "json", |
| 169 | + "compact-log-backup", |
| 170 | + "--storage-base64", |
| 171 | + base64Storage, |
| 172 | + "--from", |
| 173 | + strconv.FormatUint(cm.options.FromTS, 10), |
| 174 | + "--until", |
| 175 | + strconv.FormatUint(cm.options.UntilTS, 10), |
| 176 | + "-N", |
| 177 | + strconv.FormatUint(cm.options.Concurrency, 10), |
| 178 | + } |
| 179 | + return exec.CommandContext(ctx, ctl, args...) |
| 180 | +} |
| 181 | + |
| 182 | +func (cm *Manager) processCompactionLogs(ctx context.Context, logStream io.Reader) error { |
| 183 | + dec := json.NewDecoder(logStream) |
| 184 | + |
| 185 | + for dec.More() { |
| 186 | + if ctx.Err() != nil { |
| 187 | + return ctx.Err() |
| 188 | + } |
| 189 | + |
| 190 | + var raw json.RawMessage |
| 191 | + if err := dec.Decode(&raw); err != nil { |
| 192 | + return errors.Annotate(err, "failed to decode raw log line") |
| 193 | + } |
| 194 | + |
| 195 | + var line logLine |
| 196 | + if err := json.Unmarshal(raw, &line); err != nil { |
| 197 | + return errors.Annotate(err, "failed to decode the line of log") |
| 198 | + } |
| 199 | + line.Raw = raw |
| 200 | + |
| 201 | + if err := cm.processLogLine(ctx, line); err != nil { |
| 202 | + return err |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + return nil |
| 207 | +} |
| 208 | + |
| 209 | +func (cm *Manager) processLogLine(ctx context.Context, l logLine) error { |
| 210 | + switch l.Message { |
| 211 | + case messageCompactionDone: |
| 212 | + var prog controller.Progress |
| 213 | + if err := json.Unmarshal(l.Raw, &prog); err != nil { |
| 214 | + return errors.Annotatef(err, "failed to decode progress message: %s", string(l.Raw)) |
| 215 | + } |
| 216 | + cm.statusUpdater.OnProgress(ctx, cm.compact, prog) |
| 217 | + return nil |
| 218 | + case messageCompactAborted: |
| 219 | + errContainer := struct { |
| 220 | + Err string `json:"err"` |
| 221 | + }{} |
| 222 | + if err := json.Unmarshal(l.Raw, &errContainer); err != nil { |
| 223 | + return errors.Annotatef(err, "failed to decode error message: %s", string(l.Raw)) |
| 224 | + } |
| 225 | + return errors.New(errContainer.Err) |
| 226 | + default: |
| 227 | + return nil |
| 228 | + } |
| 229 | +} |
0 commit comments