|
| 1 | +package storage |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "os" |
| 9 | + "path" |
| 10 | + "sync" |
| 11 | + |
| 12 | + "github.com/pingcap/errors" |
| 13 | + berrors "github.com/pingcap/tidb/br/pkg/errors" |
| 14 | + "go.uber.org/multierr" |
| 15 | +) |
| 16 | + |
| 17 | +// Effect is an side effect that happens in the batch storage. |
| 18 | +type Effect any |
| 19 | + |
| 20 | +// EffPut is the side effect of a call to `WriteFile`. |
| 21 | +type EffPut struct { |
| 22 | + File string `json:"file"` |
| 23 | + Content []byte `json:"content"` |
| 24 | +} |
| 25 | + |
| 26 | +// EffDeleteFiles is the side effect of a call to `DeleteFiles`. |
| 27 | +type EffDeleteFiles struct { |
| 28 | + Files []string `json:"files"` |
| 29 | +} |
| 30 | + |
| 31 | +// EffDeleteFile is the side effect of a call to `DeleteFile`. |
| 32 | +type EffDeleteFile string |
| 33 | + |
| 34 | +// EffRename is the side effect of a call to `Rename`. |
| 35 | +type EffRename struct { |
| 36 | + From string `json:"from"` |
| 37 | + To string `json:"to"` |
| 38 | +} |
| 39 | + |
| 40 | +// JSONEffects converts a slices of effects into json. |
| 41 | +// The json will be a tagged union: `{"type": $go_type_name, "effect": $effect}` |
| 42 | +func JSONEffects(es []Effect, output io.Writer) error { |
| 43 | + type Typed struct { |
| 44 | + Type string `json:"type"` |
| 45 | + Eff Effect `json:"effect"` |
| 46 | + } |
| 47 | + |
| 48 | + out := make([]Typed, 0, len(es)) |
| 49 | + for _, eff := range es { |
| 50 | + out = append(out, Typed{ |
| 51 | + Type: fmt.Sprintf("%T", eff), |
| 52 | + Eff: eff, |
| 53 | + }) |
| 54 | + } |
| 55 | + |
| 56 | + return json.NewEncoder(output).Encode(out) |
| 57 | +} |
| 58 | + |
| 59 | +func SaveJSONEffectsToTmp(es []Effect) (string, error) { |
| 60 | + // Save the json to a subdir so user can redirect the output path by symlinking... |
| 61 | + tmp, err := os.CreateTemp(path.Join(os.TempDir(), "tidb_br"), "br-effects-*.json") |
| 62 | + if err != nil { |
| 63 | + return "", errors.Trace(err) |
| 64 | + } |
| 65 | + if err := JSONEffects(es, tmp); err != nil { |
| 66 | + return "", err |
| 67 | + } |
| 68 | + if err := tmp.Sync(); err != nil { |
| 69 | + return "", errors.Trace(err) |
| 70 | + } |
| 71 | + if err := tmp.Close(); err != nil { |
| 72 | + return "", errors.Trace(err) |
| 73 | + } |
| 74 | + return tmp.Name(), nil |
| 75 | +} |
| 76 | + |
| 77 | +// Batched is a wrapper of an external storage that suspends all write operations ("effects"). |
| 78 | +// If `Close()` without calling `Commit()`, nothing will happen in the underlying external storage. |
| 79 | +// In that case, we have done a "dry run". |
| 80 | +// |
| 81 | +// You may use `ReadOnlyEffects()` to get the history of the effects. |
| 82 | +// But don't modify the returned slice! |
| 83 | +// |
| 84 | +// You may use `Commit()` to execute all suspended effects. |
| 85 | +type Batched struct { |
| 86 | + ExternalStorage |
| 87 | + effectsMu sync.Mutex |
| 88 | + // It will be one of: |
| 89 | + // EffPut, EffDeleteFiles, EffDeleteFile, EffRename |
| 90 | + effects []Effect |
| 91 | +} |
| 92 | + |
| 93 | +// Batch wraps an external storage instance to a batched version. |
| 94 | +func Batch(s ExternalStorage) *Batched { |
| 95 | + return &Batched{ExternalStorage: s} |
| 96 | +} |
| 97 | + |
| 98 | +// Fetch all effects from the batched storage. |
| 99 | +// |
| 100 | +// **The returned slice should not be modified.** |
| 101 | +func (d *Batched) ReadOnlyEffects() []Effect { |
| 102 | + d.effectsMu.Lock() |
| 103 | + defer d.effectsMu.Unlock() |
| 104 | + return d.effects |
| 105 | +} |
| 106 | + |
| 107 | +// CleanEffects cleans all suspended effects. |
| 108 | +func (d *Batched) CleanEffects() { |
| 109 | + d.effectsMu.Lock() |
| 110 | + defer d.effectsMu.Unlock() |
| 111 | + d.effects = nil |
| 112 | +} |
| 113 | + |
| 114 | +func (d *Batched) DeleteFiles(ctx context.Context, names []string) error { |
| 115 | + d.effectsMu.Lock() |
| 116 | + defer d.effectsMu.Unlock() |
| 117 | + d.effects = append(d.effects, EffDeleteFiles{Files: names}) |
| 118 | + return nil |
| 119 | +} |
| 120 | + |
| 121 | +func (d *Batched) DeleteFile(ctx context.Context, name string) error { |
| 122 | + d.effectsMu.Lock() |
| 123 | + defer d.effectsMu.Unlock() |
| 124 | + d.effects = append(d.effects, EffDeleteFile(name)) |
| 125 | + return nil |
| 126 | +} |
| 127 | + |
| 128 | +func (d *Batched) WriteFile(ctx context.Context, name string, data []byte) error { |
| 129 | + d.effectsMu.Lock() |
| 130 | + defer d.effectsMu.Unlock() |
| 131 | + d.effects = append(d.effects, EffPut{File: name, Content: data}) |
| 132 | + return nil |
| 133 | +} |
| 134 | + |
| 135 | +func (d *Batched) Rename(ctx context.Context, oldName, newName string) error { |
| 136 | + d.effectsMu.Lock() |
| 137 | + defer d.effectsMu.Unlock() |
| 138 | + d.effects = append(d.effects, EffRename{From: oldName, To: newName}) |
| 139 | + return nil |
| 140 | +} |
| 141 | + |
| 142 | +func (d *Batched) Create(ctx context.Context, path string, option *WriterOption) (ExternalFileWriter, error) { |
| 143 | + return nil, errors.Annotatef(berrors.ErrStorageUnknown, "ExternalStorage.Create isn't allowed in batch mode for now.") |
| 144 | +} |
| 145 | + |
| 146 | +// Commit performs all effects recorded so long in the REAL external storage. |
| 147 | +// This will cleanup all of the suspended effects. |
| 148 | +func (d *Batched) Commit(ctx context.Context) error { |
| 149 | + d.effectsMu.Lock() |
| 150 | + defer d.effectsMu.Unlock() |
| 151 | + |
| 152 | + var err error |
| 153 | + for _, eff := range d.effects { |
| 154 | + switch e := eff.(type) { |
| 155 | + case EffPut: |
| 156 | + err = multierr.Combine(d.ExternalStorage.WriteFile(ctx, e.File, e.Content), err) |
| 157 | + case EffDeleteFiles: |
| 158 | + err = multierr.Combine(d.ExternalStorage.DeleteFiles(ctx, e.Files), err) |
| 159 | + case EffDeleteFile: |
| 160 | + err = multierr.Combine(d.ExternalStorage.DeleteFile(ctx, string(e)), err) |
| 161 | + case EffRename: |
| 162 | + err = multierr.Combine(d.ExternalStorage.Rename(ctx, e.From, e.To), err) |
| 163 | + default: |
| 164 | + return errors.Annotatef(berrors.ErrStorageUnknown, "Unknown effect type %T", eff) |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + d.effects = nil |
| 169 | + |
| 170 | + return nil |
| 171 | +} |
0 commit comments