forked from cockroachdb/cockroach
/
export.go
163 lines (143 loc) · 4.75 KB
/
export.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
// Copyright 2016 The Cockroach Authors.
//
// Licensed under the Cockroach Community Licence (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://github.com/cockroachdb/cockroach/blob/master/ccl/LICENSE
package storageccl
import (
"crypto/sha512"
"fmt"
"io"
"os"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/pkg/ccl/storageccl/engineccl"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"github.com/cockroachdb/cockroach/pkg/storage"
"github.com/cockroachdb/cockroach/pkg/storage/engine"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/tracing"
"github.com/pkg/errors"
)
func init() {
storage.SetExportCmd(storage.Command{
DeclareKeys: storage.DefaultDeclareKeys,
Eval: evalExport,
})
}
// evalExport dumps the requested keys into files of non-overlapping key ranges
// in a format suitable for bulk ingest.
func evalExport(
ctx context.Context, batch engine.ReadWriter, cArgs storage.CommandArgs, resp roachpb.Response,
) (pd storage.EvalResult, retErr error) {
args := cArgs.Args.(*roachpb.ExportRequest)
h := cArgs.Header
r := cArgs.Repl
reply := resp.(*roachpb.ExportResponse)
ctx, span := tracing.ChildSpan(ctx, fmt.Sprintf("Export [%s,%s)", args.Key, args.EndKey))
defer tracing.FinishSpan(span)
// If the startTime is zero, then we're doing a full backup and the gc
// threshold is irrelevant. Otherwise, make sure startTime is after the gc
// threshold. If it's not, the mvcc tombstones could have been deleted and
// the resulting RocksDB tombstones compacted, which means we'd miss
// deletions in the incremental backup.
gcThreshold := r.GCThreshold()
if args.StartTime != (hlc.Timestamp{}) {
if !gcThreshold.Less(args.StartTime) {
return storage.EvalResult{}, errors.Errorf("start timestamp %v must be after replica GC threshold %v", args.StartTime, gcThreshold)
}
}
if err := beginLimitedRequest(ctx); err != nil {
return storage.EvalResult{}, err
}
defer endLimitedRequest()
exportStore, err := MakeExportStorage(ctx, args.Storage)
if err != nil {
return storage.EvalResult{}, err
}
defer exportStore.Close()
filename := fmt.Sprintf("%d.sst", parser.GenerateUniqueInt(r.NodeID()))
writer, err := exportStore.PutFile(ctx, filename)
if err != nil {
return storage.EvalResult{}, err
}
localPath := writer.LocalFile()
defer writer.Cleanup()
sstWriter := engine.MakeRocksDBSstFileWriter()
sst := &sstWriter
if err := sst.Open(localPath); err != nil {
return storage.EvalResult{}, err
}
defer func() {
if sst != nil {
if closeErr := sst.Close(); closeErr != nil {
log.Warningf(ctx, "could not close sst writer %s: %+v", localPath, closeErr)
}
}
}()
// TODO(dan): Move all this iteration into cpp to avoid the cgo calls.
// TODO(dan): Consider checking ctx periodically during the MVCCIterate call.
var entries int64
iter := engineccl.NewMVCCIncrementalIterator(batch)
defer iter.Close()
iter.Reset(args.Key, args.EndKey, args.StartTime, h.Timestamp)
for ; iter.Valid(); iter.Next() {
key, value := iter.Key(), iter.Value()
if log.V(3) {
log.Infof(ctx, "Export %+v %+v", key, value)
}
entries++
if err := sst.Add(engine.MVCCKeyValue{Key: key, Value: value}); err != nil {
return storage.EvalResult{}, errors.Wrapf(err, "adding key %s", key)
}
}
if err := iter.Error(); err != nil {
// The error may be a WriteIntentError. In which case, returning it will
// cause this command to be retried.
return storage.EvalResult{}, err
}
if entries == 0 {
// The defer above `Close`s sst if it's not nil, which in turn finishes
// the SSTable. However, our SSTable library errors if there is not at
// least one entry, so set `sst = nil` to avoid the `Close` and the log
// spam when it fails.
sst = nil
reply.Files = []roachpb.ExportResponse_File{}
return storage.EvalResult{}, nil
}
if err := sst.Close(); err != nil {
return storage.EvalResult{}, err
}
size := sst.DataSize
sst = nil
// Compute the checksum before we upload and remove the local file.
checksum, err := sha512ChecksumFile(localPath)
if err != nil {
return storage.EvalResult{}, err
}
if err := writer.Finish(); err != nil {
return storage.EvalResult{}, err
}
reply.Files = []roachpb.ExportResponse_File{{
Span: args.Span,
Path: filename,
DataSize: size,
Sha512: checksum,
}}
return storage.EvalResult{}, nil
}
func sha512ChecksumFile(path string) ([]byte, error) {
h := sha512.New()
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
if _, err := io.Copy(h, f); err != nil {
return nil, err
}
return h.Sum(nil), nil
}