-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
sst_writer.go
329 lines (288 loc) · 11.1 KB
/
sst_writer.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
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
// Copyright 2019 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package storage
import (
"bytes"
"context"
"io"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/util/uuid"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/pebble/sstable"
)
// SSTWriter writes SSTables.
type SSTWriter struct {
fw *sstable.Writer
f io.Writer
// DataSize tracks the total key and value bytes added so far.
DataSize int64
scratch []byte
}
var _ Writer = &SSTWriter{}
// writeCloseSyncer interface copied from pebble.sstable.
type writeCloseSyncer interface {
io.WriteCloser
Sync() error
}
// noopSyncCloser is used to wrap io.Writers for sstable.Writer so that callers
// can decide when to close/sync.
type noopSyncCloser struct {
io.Writer
}
func (noopSyncCloser) Sync() error {
return nil
}
func (noopSyncCloser) Close() error {
return nil
}
// MakeBackupSSTWriter creates a new SSTWriter tailored for backup SSTs which
// are typically only ever iterated in their entirety.
func MakeBackupSSTWriter(f io.Writer) SSTWriter {
opts := DefaultPebbleOptions().MakeWriterOptions(0)
// Don't need BlockPropertyCollectors for backups.
opts.BlockPropertyCollectors = nil
opts.TableFormat = sstable.TableFormatRocksDBv2
// Disable bloom filters since we only ever iterate backups.
opts.FilterPolicy = nil
// Bump up block size, since we almost never seek or do point lookups, so more
// block checksums and more index entries are just overhead and smaller blocks
// reduce compression ratio.
opts.BlockSize = 128 << 10
opts.MergerName = "nullptr"
sst := sstable.NewWriter(noopSyncCloser{f}, opts)
return SSTWriter{fw: sst, f: f}
}
// MakeIngestionSSTWriter creates a new SSTWriter tailored for ingestion SSTs.
// These SSTs have bloom filters enabled (as set in DefaultPebbleOptions) and
// format set to RocksDBv2.
func MakeIngestionSSTWriter(f writeCloseSyncer) SSTWriter {
opts := DefaultPebbleOptions().MakeWriterOptions(0)
// TODO(sumeer): we should use BlockPropertyCollectors here if the cluster
// version permits (which is also reflected in the store's roachpb.Version
// and pebble.FormatMajorVersion).
opts.BlockPropertyCollectors = nil
opts.TableFormat = sstable.TableFormatRocksDBv2
opts.MergerName = "nullptr"
sst := sstable.NewWriter(f, opts)
return SSTWriter{fw: sst, f: f}
}
// Finish finalizes the writer and returns the constructed file's contents,
// since the last call to Truncate (if any). At least one kv entry must have been added.
func (fw *SSTWriter) Finish() error {
if fw.fw == nil {
return errors.New("cannot call Finish on a closed writer")
}
if err := fw.fw.Close(); err != nil {
return err
}
fw.fw = nil
return nil
}
// ClearRawRange implements the Writer interface.
func (fw *SSTWriter) ClearRawRange(start, end roachpb.Key) error {
return fw.clearRange(MVCCKey{Key: start}, MVCCKey{Key: end})
}
// ClearMVCCRangeAndIntents implements the Writer interface.
func (fw *SSTWriter) ClearMVCCRangeAndIntents(start, end roachpb.Key) error {
panic("ClearMVCCRangeAndIntents is unsupported")
}
// ClearMVCCRange implements the Writer interface.
func (fw *SSTWriter) ClearMVCCRange(start, end MVCCKey) error {
return fw.clearRange(start, end)
}
func (fw *SSTWriter) clearRange(start, end MVCCKey) error {
if fw.fw == nil {
return errors.New("cannot call ClearRange on a closed writer")
}
fw.DataSize += int64(len(start.Key)) + int64(len(end.Key))
fw.scratch = EncodeKeyToBuf(fw.scratch[:0], start)
return fw.fw.DeleteRange(fw.scratch, EncodeKey(end))
}
// Put puts a kv entry into the sstable being built. An error is returned if it
// is not greater than any previously added entry (according to the comparator
// configured during writer creation). `Close` cannot have been called.
//
// TODO(sumeer): Put has been removed from the Writer interface, but there
// are many callers of this SSTWriter method. Fix those callers and remove.
func (fw *SSTWriter) Put(key MVCCKey, value []byte) error {
if fw.fw == nil {
return errors.New("cannot call Put on a closed writer")
}
fw.DataSize += int64(len(key.Key)) + int64(len(value))
fw.scratch = EncodeKeyToBuf(fw.scratch[:0], key)
return fw.fw.Set(fw.scratch, value)
}
// PutMVCC implements the Writer interface.
// An error is returned if it is not greater than any previously added entry
// (according to the comparator configured during writer creation). `Close`
// cannot have been called.
func (fw *SSTWriter) PutMVCC(key MVCCKey, value []byte) error {
if key.Timestamp.IsEmpty() {
panic("PutMVCC timestamp is empty")
}
return fw.put(key, value)
}
// PutUnversioned implements the Writer interface.
// An error is returned if it is not greater than any previously added entry
// (according to the comparator configured during writer creation). `Close`
// cannot have been called.
func (fw *SSTWriter) PutUnversioned(key roachpb.Key, value []byte) error {
return fw.put(MVCCKey{Key: key}, value)
}
// PutIntent implements the Writer interface.
// An error is returned if it is not greater than any previously added entry
// (according to the comparator configured during writer creation). `Close`
// cannot have been called.
func (fw *SSTWriter) PutIntent(
ctx context.Context, key roachpb.Key, value []byte, txnUUID uuid.UUID,
) error {
return fw.put(MVCCKey{Key: key}, value)
}
// PutEngineKey implements the Writer interface.
// An error is returned if it is not greater than any previously added entry
// (according to the comparator configured during writer creation). `Close`
// cannot have been called.
func (fw *SSTWriter) PutEngineKey(key EngineKey, value []byte) error {
if fw.fw == nil {
return errors.New("cannot call Put on a closed writer")
}
fw.DataSize += int64(len(key.Key)) + int64(len(value))
fw.scratch = key.EncodeToBuf(fw.scratch[:0])
return fw.fw.Set(fw.scratch, value)
}
// put puts a kv entry into the sstable being built. An error is returned if it
// is not greater than any previously added entry (according to the comparator
// configured during writer creation). `Close` cannot have been called.
func (fw *SSTWriter) put(key MVCCKey, value []byte) error {
if fw.fw == nil {
return errors.New("cannot call Put on a closed writer")
}
fw.DataSize += int64(len(key.Key)) + int64(len(value))
fw.scratch = EncodeKeyToBuf(fw.scratch[:0], key)
return fw.fw.Set(fw.scratch, value)
}
// ApplyBatchRepr implements the Writer interface.
func (fw *SSTWriter) ApplyBatchRepr(repr []byte, sync bool) error {
panic("unimplemented")
}
// ClearMVCC implements the Writer interface. An error is returned if it is
// not greater than any previous point key passed to this Writer (according to
// the comparator configured during writer creation). `Close` cannot have been
// called.
func (fw *SSTWriter) ClearMVCC(key MVCCKey) error {
if key.Timestamp.IsEmpty() {
panic("ClearMVCC timestamp is empty")
}
return fw.clear(key)
}
// ClearUnversioned implements the Writer interface. An error is returned if
// it is not greater than any previous point key passed to this Writer
// (according to the comparator configured during writer creation). `Close`
// cannot have been called.
func (fw *SSTWriter) ClearUnversioned(key roachpb.Key) error {
return fw.clear(MVCCKey{Key: key})
}
// ClearIntent implements the Writer interface. An error is returned if it is
// not greater than any previous point key passed to this Writer (according to
// the comparator configured during writer creation). `Close` cannot have been
// called.
func (fw *SSTWriter) ClearIntent(
key roachpb.Key, txnDidNotUpdateMeta bool, txnUUID uuid.UUID,
) error {
panic("ClearIntent is unsupported")
}
// ClearEngineKey implements the Writer interface. An error is returned if it is
// not greater than any previous point key passed to this Writer (according to
// the comparator configured during writer creation). `Close` cannot have been
// called.
func (fw *SSTWriter) ClearEngineKey(key EngineKey) error {
if fw.fw == nil {
return errors.New("cannot call Clear on a closed writer")
}
fw.scratch = key.EncodeToBuf(fw.scratch[:0])
fw.DataSize += int64(len(key.Key))
return fw.fw.Delete(fw.scratch)
}
// An error is returned if it is not greater than any previous point key
// passed to this Writer (according to the comparator configured during writer
// creation). `Close` cannot have been called.
func (fw *SSTWriter) clear(key MVCCKey) error {
if fw.fw == nil {
return errors.New("cannot call Clear on a closed writer")
}
fw.scratch = EncodeKeyToBuf(fw.scratch[:0], key)
fw.DataSize += int64(len(key.Key))
return fw.fw.Delete(fw.scratch)
}
// SingleClearEngineKey implements the Writer interface.
func (fw *SSTWriter) SingleClearEngineKey(key EngineKey) error {
panic("unimplemented")
}
// ClearIterRange implements the Writer interface.
func (fw *SSTWriter) ClearIterRange(iter MVCCIterator, start, end roachpb.Key) error {
panic("ClearIterRange is unsupported")
}
// Merge implements the Writer interface.
func (fw *SSTWriter) Merge(key MVCCKey, value []byte) error {
if fw.fw == nil {
return errors.New("cannot call Merge on a closed writer")
}
fw.DataSize += int64(len(key.Key)) + int64(len(value))
fw.scratch = EncodeKeyToBuf(fw.scratch[:0], key)
return fw.fw.Merge(fw.scratch, value)
}
// LogData implements the Writer interface.
func (fw *SSTWriter) LogData(data []byte) error {
// No-op.
return nil
}
// LogLogicalOp implements the Writer interface.
func (fw *SSTWriter) LogLogicalOp(op MVCCLogicalOpType, details MVCCLogicalOpDetails) {
// No-op.
}
// Close finishes and frees memory and other resources. Close is idempotent.
func (fw *SSTWriter) Close() {
if fw.fw == nil {
return
}
// pebble.Writer *does* return interesting errors from Close... but normally
// we already called its Close() in Finish() and we no-op here. Thus the only
// time we expect to be here is in a deferred Close(), in which case the caller
// probably is already returning some other error, so returning one from this
// method just makes for messy defers.
_ = fw.fw.Close()
fw.fw = nil
}
// MemFile is a file-like struct that buffers all data written to it in memory.
// Implements the writeCloseSyncer interface and is intended for use with
// SSTWriter.
type MemFile struct {
bytes.Buffer
}
// Close implements the writeCloseSyncer interface.
func (*MemFile) Close() error {
return nil
}
// Flush implements the same interface as the standard library's *bufio.Writer's
// Flush method. The Pebble sstable Writer tests whether files implement a Flush
// method. If not, it wraps the file with a bufio.Writer to buffer writes to the
// underlying file. This buffering is not necessary for an in-memory file. We
// signal this by implementing Flush as a noop.
func (*MemFile) Flush() error {
return nil
}
// Sync implements the writeCloseSyncer interface.
func (*MemFile) Sync() error {
return nil
}
// Data returns the in-memory buffer behind this MemFile.
func (f *MemFile) Data() []byte {
return f.Bytes()
}