Line data Source code
1 : // Copyright 2011 The LevelDB-Go and Pebble Authors. All rights reserved. Use
2 : // of this source code is governed by a BSD-style license that can be found in
3 : // the LICENSE file.
4 :
5 : package sstable
6 :
7 : import (
8 : "bytes"
9 : "encoding/binary"
10 : "fmt"
11 : "math"
12 : "runtime"
13 : "sort"
14 : "sync"
15 :
16 : "github.com/cespare/xxhash/v2"
17 : "github.com/cockroachdb/errors"
18 : "github.com/cockroachdb/pebble/internal/base"
19 : "github.com/cockroachdb/pebble/internal/bytealloc"
20 : "github.com/cockroachdb/pebble/internal/cache"
21 : "github.com/cockroachdb/pebble/internal/crc"
22 : "github.com/cockroachdb/pebble/internal/invariants"
23 : "github.com/cockroachdb/pebble/internal/keyspan"
24 : "github.com/cockroachdb/pebble/internal/private"
25 : "github.com/cockroachdb/pebble/internal/rangekey"
26 : "github.com/cockroachdb/pebble/objstorage"
27 : )
28 :
29 : // encodedBHPEstimatedSize estimates the size of the encoded BlockHandleWithProperties.
30 : // It would also be nice to account for the length of the data block properties here,
31 : // but isn't necessary since this is an estimate.
32 : const encodedBHPEstimatedSize = binary.MaxVarintLen64 * 2
33 :
34 : var errWriterClosed = errors.New("pebble: writer is closed")
35 :
36 : // WriterMetadata holds info about a finished sstable.
37 : type WriterMetadata struct {
38 : Size uint64
39 : SmallestPoint InternalKey
40 : // LargestPoint, LargestRangeKey, LargestRangeDel should not be accessed
41 : // before Writer.Close is called, because they may only be set on
42 : // Writer.Close.
43 : LargestPoint InternalKey
44 : SmallestRangeDel InternalKey
45 : LargestRangeDel InternalKey
46 : SmallestRangeKey InternalKey
47 : LargestRangeKey InternalKey
48 : HasPointKeys bool
49 : HasRangeDelKeys bool
50 : HasRangeKeys bool
51 : SmallestSeqNum uint64
52 : LargestSeqNum uint64
53 : Properties Properties
54 : }
55 :
56 : // SetSmallestPointKey sets the smallest point key to the given key.
57 : // NB: this method set the "absolute" smallest point key. Any existing key is
58 : // overridden.
59 2 : func (m *WriterMetadata) SetSmallestPointKey(k InternalKey) {
60 2 : m.SmallestPoint = k
61 2 : m.HasPointKeys = true
62 2 : }
63 :
64 : // SetSmallestRangeDelKey sets the smallest rangedel key to the given key.
65 : // NB: this method set the "absolute" smallest rangedel key. Any existing key is
66 : // overridden.
67 2 : func (m *WriterMetadata) SetSmallestRangeDelKey(k InternalKey) {
68 2 : m.SmallestRangeDel = k
69 2 : m.HasRangeDelKeys = true
70 2 : }
71 :
72 : // SetSmallestRangeKey sets the smallest range key to the given key.
73 : // NB: this method set the "absolute" smallest range key. Any existing key is
74 : // overridden.
75 2 : func (m *WriterMetadata) SetSmallestRangeKey(k InternalKey) {
76 2 : m.SmallestRangeKey = k
77 2 : m.HasRangeKeys = true
78 2 : }
79 :
80 : // SetLargestPointKey sets the largest point key to the given key.
81 : // NB: this method set the "absolute" largest point key. Any existing key is
82 : // overridden.
83 2 : func (m *WriterMetadata) SetLargestPointKey(k InternalKey) {
84 2 : m.LargestPoint = k
85 2 : m.HasPointKeys = true
86 2 : }
87 :
88 : // SetLargestRangeDelKey sets the largest rangedel key to the given key.
89 : // NB: this method set the "absolute" largest rangedel key. Any existing key is
90 : // overridden.
91 2 : func (m *WriterMetadata) SetLargestRangeDelKey(k InternalKey) {
92 2 : m.LargestRangeDel = k
93 2 : m.HasRangeDelKeys = true
94 2 : }
95 :
96 : // SetLargestRangeKey sets the largest range key to the given key.
97 : // NB: this method set the "absolute" largest range key. Any existing key is
98 : // overridden.
99 2 : func (m *WriterMetadata) SetLargestRangeKey(k InternalKey) {
100 2 : m.LargestRangeKey = k
101 2 : m.HasRangeKeys = true
102 2 : }
103 :
104 2 : func (m *WriterMetadata) updateSeqNum(seqNum uint64) {
105 2 : if m.SmallestSeqNum > seqNum {
106 2 : m.SmallestSeqNum = seqNum
107 2 : }
108 2 : if m.LargestSeqNum < seqNum {
109 2 : m.LargestSeqNum = seqNum
110 2 : }
111 : }
112 :
113 : // Writer is a table writer.
114 : type Writer struct {
115 : writable objstorage.Writable
116 : meta WriterMetadata
117 : err error
118 : // cacheID and fileNum are used to remove blocks written to the sstable from
119 : // the cache, providing a defense in depth against bugs which cause cache
120 : // collisions.
121 : cacheID uint64
122 : fileNum base.DiskFileNum
123 : // The following fields are copied from Options.
124 : blockSize int
125 : blockSizeThreshold int
126 : indexBlockSize int
127 : indexBlockSizeThreshold int
128 : compare Compare
129 : split Split
130 : formatKey base.FormatKey
131 : compression Compression
132 : separator Separator
133 : successor Successor
134 : tableFormat TableFormat
135 : isStrictObsolete bool
136 : writingToLowestLevel bool
137 : cache *cache.Cache
138 : restartInterval int
139 : checksumType ChecksumType
140 : // disableKeyOrderChecks disables the checks that keys are added to an
141 : // sstable in order. It is intended for internal use only in the construction
142 : // of invalid sstables for testing. See tool/make_test_sstables.go.
143 : disableKeyOrderChecks bool
144 : // With two level indexes, the index/filter of a SST file is partitioned into
145 : // smaller blocks with an additional top-level index on them. When reading an
146 : // index/filter, only the top-level index is loaded into memory. The two level
147 : // index/filter then uses the top-level index to load on demand into the block
148 : // cache the partitions that are required to perform the index/filter query.
149 : //
150 : // Two level indexes are enabled automatically when there is more than one
151 : // index block.
152 : //
153 : // This is useful when there are very large index blocks, which generally occurs
154 : // with the usage of large keys. With large index blocks, the index blocks fight
155 : // the data blocks for block cache space and the index blocks are likely to be
156 : // re-read many times from the disk. The top level index, which has a much
157 : // smaller memory footprint, can be used to prevent the entire index block from
158 : // being loaded into the block cache.
159 : twoLevelIndex bool
160 : // Internal flag to allow creation of range-del-v1 format blocks. Only used
161 : // for testing. Note that v2 format blocks are backwards compatible with v1
162 : // format blocks.
163 : rangeDelV1Format bool
164 : indexBlock *indexBlockBuf
165 : rangeDelBlock blockWriter
166 : rangeKeyBlock blockWriter
167 : topLevelIndexBlock blockWriter
168 : props Properties
169 : blockPropCollectors []BlockPropertyCollector
170 : obsoleteCollector obsoleteKeyBlockPropertyCollector
171 : blockPropsEncoder blockPropertiesEncoder
172 : // filter accumulates the filter block. If populated, the filter ingests
173 : // either the output of w.split (i.e. a prefix extractor) if w.split is not
174 : // nil, or the full keys otherwise.
175 : filter filterWriter
176 : indexPartitions []indexBlockAndBlockProperties
177 :
178 : // indexBlockAlloc is used to bulk-allocate byte slices used to store index
179 : // blocks in indexPartitions. These live until the index finishes.
180 : indexBlockAlloc []byte
181 : // indexSepAlloc is used to bulk-allocate index block separator slices stored
182 : // in indexPartitions. These live until the index finishes.
183 : indexSepAlloc bytealloc.A
184 :
185 : // To allow potentially overlapping (i.e. un-fragmented) range keys spans to
186 : // be added to the Writer, a keyspan.Fragmenter is used to retain the keys
187 : // and values, emitting fragmented, coalesced spans as appropriate. Range
188 : // keys must be added in order of their start user-key.
189 : fragmenter keyspan.Fragmenter
190 : rangeKeyEncoder rangekey.Encoder
191 : rangeKeysBySuffix keyspan.KeysBySuffix
192 : rangeKeySpan keyspan.Span
193 : rkBuf []byte
194 : // dataBlockBuf consists of the state which is currently owned by and used by
195 : // the Writer client goroutine. This state can be handed off to other goroutines.
196 : dataBlockBuf *dataBlockBuf
197 : // blockBuf consists of the state which is owned by and used by the Writer client
198 : // goroutine.
199 : blockBuf blockBuf
200 :
201 : coordination coordinationState
202 :
203 : // Information (other than the byte slice) about the last point key, to
204 : // avoid extracting it again.
205 : lastPointKeyInfo pointKeyInfo
206 :
207 : // For value blocks.
208 : shortAttributeExtractor base.ShortAttributeExtractor
209 : requiredInPlaceValueBound UserKeyPrefixBound
210 : // When w.tableFormat >= TableFormatPebblev3, valueBlockWriter is nil iff
211 : // WriterOptions.DisableValueBlocks was true.
212 : valueBlockWriter *valueBlockWriter
213 : }
214 :
215 : type pointKeyInfo struct {
216 : trailer uint64
217 : // Only computed when w.valueBlockWriter is not nil.
218 : userKeyLen int
219 : // prefixLen uses w.split, if not nil. Only computed when w.valueBlockWriter
220 : // is not nil.
221 : prefixLen int
222 : // True iff the point was marked obsolete.
223 : isObsolete bool
224 : }
225 :
226 : type coordinationState struct {
227 : parallelismEnabled bool
228 :
229 : // writeQueue is used to write data blocks to disk. The writeQueue is primarily
230 : // used to maintain the order in which data blocks must be written to disk. For
231 : // this reason, every single data block write must be done through the writeQueue.
232 : writeQueue *writeQueue
233 :
234 : sizeEstimate dataBlockEstimates
235 : }
236 :
237 2 : func (c *coordinationState) init(parallelismEnabled bool, writer *Writer) {
238 2 : c.parallelismEnabled = parallelismEnabled
239 2 : // useMutex is false regardless of parallelismEnabled, because we do not do
240 2 : // parallel compression yet.
241 2 : c.sizeEstimate.useMutex = false
242 2 :
243 2 : // writeQueueSize determines the size of the write queue, or the number
244 2 : // of items which can be added to the queue without blocking. By default, we
245 2 : // use a writeQueue size of 0, since we won't be doing any block writes in
246 2 : // parallel.
247 2 : writeQueueSize := 0
248 2 : if parallelismEnabled {
249 2 : writeQueueSize = runtime.GOMAXPROCS(0)
250 2 : }
251 2 : c.writeQueue = newWriteQueue(writeQueueSize, writer)
252 : }
253 :
254 : // sizeEstimate is a general purpose helper for estimating two kinds of sizes:
255 : // A. The compressed sstable size, which is useful for deciding when to start
256 : //
257 : // a new sstable during flushes or compactions. In practice, we use this in
258 : // estimating the data size (excluding the index).
259 : //
260 : // B. The size of index blocks to decide when to start a new index block.
261 : //
262 : // There are some terminology peculiarities which are due to the origin of
263 : // sizeEstimate for use case A with parallel compression enabled (for which
264 : // the code has not been merged). Specifically this relates to the terms
265 : // "written" and "compressed".
266 : // - The notion of "written" for case A is sufficiently defined by saying that
267 : // the data block is compressed. Waiting for the actual data block write to
268 : // happen can result in unnecessary estimation, when we already know how big
269 : // it will be in compressed form. Additionally, with the forthcoming value
270 : // blocks containing older MVCC values, these compressed block will be held
271 : // in-memory until late in the sstable writing, and we do want to accurately
272 : // account for them without waiting for the actual write.
273 : // For case B, "written" means that the index entry has been fully
274 : // generated, and has been added to the uncompressed block buffer for that
275 : // index block. It does not include actually writing a potentially
276 : // compressed index block.
277 : // - The notion of "compressed" is to differentiate between a "inflight" size
278 : // and the actual size, and is handled via computing a compression ratio
279 : // observed so far (defaults to 1).
280 : // For case A, this is actual data block compression, so the "inflight" size
281 : // is uncompressed blocks (that are no longer being written to) and the
282 : // "compressed" size is after they have been compressed.
283 : // For case B the inflight size is for a key-value pair in the index for
284 : // which the value size (the encoded size of the BlockHandleWithProperties)
285 : // is not accurately known, while the compressed size is the size of that
286 : // entry when it has been added to the (in-progress) index ssblock.
287 : //
288 : // Usage: To update state, one can optionally provide an inflight write value
289 : // using addInflight (used for case B). When something is "written" the state
290 : // can be updated using either writtenWithDelta or writtenWithTotal, which
291 : // provide the actual delta size or the total size (latter must be
292 : // monotonically non-decreasing). If there were no calls to addInflight, there
293 : // isn't any real estimation happening here. So case A does not do any real
294 : // estimation. However, when we introduce parallel compression, there will be
295 : // estimation in that the client goroutine will call addInFlight and the
296 : // compression goroutines will call writtenWithDelta.
297 : type sizeEstimate struct {
298 : // emptySize is the size when there is no inflight data, and numEntries is 0.
299 : // emptySize is constant once set.
300 : emptySize uint64
301 :
302 : // inflightSize is the estimated size of some inflight data which hasn't
303 : // been written yet.
304 : inflightSize uint64
305 :
306 : // totalSize is the total size of the data which has already been written.
307 : totalSize uint64
308 :
309 : // numWrittenEntries is the total number of entries which have already been
310 : // written.
311 : numWrittenEntries uint64
312 : // numInflightEntries is the total number of entries which are inflight, and
313 : // haven't been written.
314 : numInflightEntries uint64
315 :
316 : // maxEstimatedSize stores the maximum result returned from sizeEstimate.size.
317 : // It ensures that values returned from subsequent calls to Writer.EstimatedSize
318 : // never decrease.
319 : maxEstimatedSize uint64
320 :
321 : // We assume that the entries added to the sizeEstimate can be compressed.
322 : // For this reason, we keep track of a compressedSize and an uncompressedSize
323 : // to compute a compression ratio for the inflight entries. If the entries
324 : // aren't being compressed, then compressedSize and uncompressedSize must be
325 : // equal.
326 : compressedSize uint64
327 : uncompressedSize uint64
328 : }
329 :
330 2 : func (s *sizeEstimate) init(emptySize uint64) {
331 2 : s.emptySize = emptySize
332 2 : }
333 :
334 2 : func (s *sizeEstimate) size() uint64 {
335 2 : ratio := float64(1)
336 2 : if s.uncompressedSize > 0 {
337 2 : ratio = float64(s.compressedSize) / float64(s.uncompressedSize)
338 2 : }
339 2 : estimatedInflightSize := uint64(float64(s.inflightSize) * ratio)
340 2 : total := s.totalSize + estimatedInflightSize
341 2 : if total > s.maxEstimatedSize {
342 2 : s.maxEstimatedSize = total
343 2 : } else {
344 2 : total = s.maxEstimatedSize
345 2 : }
346 :
347 2 : if total == 0 {
348 2 : return s.emptySize
349 2 : }
350 :
351 2 : return total
352 : }
353 :
354 2 : func (s *sizeEstimate) numTotalEntries() uint64 {
355 2 : return s.numWrittenEntries + s.numInflightEntries
356 2 : }
357 :
358 2 : func (s *sizeEstimate) addInflight(size int) {
359 2 : s.numInflightEntries++
360 2 : s.inflightSize += uint64(size)
361 2 : }
362 :
363 2 : func (s *sizeEstimate) writtenWithTotal(newTotalSize uint64, inflightSize int) {
364 2 : finalEntrySize := int(newTotalSize - s.totalSize)
365 2 : s.writtenWithDelta(finalEntrySize, inflightSize)
366 2 : }
367 :
368 2 : func (s *sizeEstimate) writtenWithDelta(finalEntrySize int, inflightSize int) {
369 2 : if inflightSize > 0 {
370 2 : // This entry was previously inflight, so we should decrement inflight
371 2 : // entries and update the "compression" stats for future estimation.
372 2 : s.numInflightEntries--
373 2 : s.inflightSize -= uint64(inflightSize)
374 2 : s.uncompressedSize += uint64(inflightSize)
375 2 : s.compressedSize += uint64(finalEntrySize)
376 2 : }
377 2 : s.numWrittenEntries++
378 2 : s.totalSize += uint64(finalEntrySize)
379 : }
380 :
381 2 : func (s *sizeEstimate) clear() {
382 2 : *s = sizeEstimate{emptySize: s.emptySize}
383 2 : }
384 :
385 : type indexBlockBuf struct {
386 : // block will only be accessed from the writeQueue.
387 : block blockWriter
388 :
389 : size struct {
390 : useMutex bool
391 : mu sync.Mutex
392 : estimate sizeEstimate
393 : }
394 :
395 : // restartInterval matches indexBlockBuf.block.restartInterval. We store it twice, because the `block`
396 : // must only be accessed from the writeQueue goroutine.
397 : restartInterval int
398 : }
399 :
400 2 : func (i *indexBlockBuf) clear() {
401 2 : i.block.clear()
402 2 : if i.size.useMutex {
403 2 : i.size.mu.Lock()
404 2 : defer i.size.mu.Unlock()
405 2 : }
406 2 : i.size.estimate.clear()
407 2 : i.restartInterval = 0
408 : }
409 :
410 : var indexBlockBufPool = sync.Pool{
411 2 : New: func() interface{} {
412 2 : return &indexBlockBuf{}
413 2 : },
414 : }
415 :
416 : const indexBlockRestartInterval = 1
417 :
418 2 : func newIndexBlockBuf(useMutex bool) *indexBlockBuf {
419 2 : i := indexBlockBufPool.Get().(*indexBlockBuf)
420 2 : i.size.useMutex = useMutex
421 2 : i.restartInterval = indexBlockRestartInterval
422 2 : i.block.restartInterval = indexBlockRestartInterval
423 2 : i.size.estimate.init(emptyBlockSize)
424 2 : return i
425 2 : }
426 :
427 : func (i *indexBlockBuf) shouldFlush(
428 : sep InternalKey, valueLen, targetBlockSize, sizeThreshold int,
429 2 : ) bool {
430 2 : if i.size.useMutex {
431 2 : i.size.mu.Lock()
432 2 : defer i.size.mu.Unlock()
433 2 : }
434 :
435 2 : nEntries := i.size.estimate.numTotalEntries()
436 2 : return shouldFlush(
437 2 : sep, valueLen, i.restartInterval, int(i.size.estimate.size()),
438 2 : int(nEntries), targetBlockSize, sizeThreshold)
439 : }
440 :
441 2 : func (i *indexBlockBuf) add(key InternalKey, value []byte, inflightSize int) {
442 2 : i.block.add(key, value)
443 2 : size := i.block.estimatedSize()
444 2 : if i.size.useMutex {
445 2 : i.size.mu.Lock()
446 2 : defer i.size.mu.Unlock()
447 2 : }
448 2 : i.size.estimate.writtenWithTotal(uint64(size), inflightSize)
449 : }
450 :
451 2 : func (i *indexBlockBuf) finish() []byte {
452 2 : b := i.block.finish()
453 2 : return b
454 2 : }
455 :
456 2 : func (i *indexBlockBuf) addInflight(inflightSize int) {
457 2 : if i.size.useMutex {
458 2 : i.size.mu.Lock()
459 2 : defer i.size.mu.Unlock()
460 2 : }
461 2 : i.size.estimate.addInflight(inflightSize)
462 : }
463 :
464 2 : func (i *indexBlockBuf) estimatedSize() uint64 {
465 2 : if i.size.useMutex {
466 2 : i.size.mu.Lock()
467 2 : defer i.size.mu.Unlock()
468 2 : }
469 :
470 : // Make sure that the size estimation works as expected when parallelism
471 : // is disabled.
472 2 : if invariants.Enabled && !i.size.useMutex {
473 2 : if i.size.estimate.inflightSize != 0 {
474 0 : panic("unexpected inflight entry in index block size estimation")
475 : }
476 :
477 : // NB: The i.block should only be accessed from the writeQueue goroutine,
478 : // when parallelism is enabled. We break that invariant here, but that's
479 : // okay since parallelism is disabled.
480 2 : if i.size.estimate.size() != uint64(i.block.estimatedSize()) {
481 0 : panic("index block size estimation sans parallelism is incorrect")
482 : }
483 : }
484 2 : return i.size.estimate.size()
485 : }
486 :
487 : // sizeEstimate is used for sstable size estimation. sizeEstimate can be
488 : // accessed by the Writer client and compressionQueue goroutines. Fields
489 : // should only be read/updated through the functions defined on the
490 : // *sizeEstimate type.
491 : type dataBlockEstimates struct {
492 : // If we don't do block compression in parallel, then we don't need to take
493 : // the performance hit of synchronizing using this mutex.
494 : useMutex bool
495 : mu sync.Mutex
496 :
497 : estimate sizeEstimate
498 : }
499 :
500 : // inflightSize is the uncompressed block size estimate which has been
501 : // previously provided to addInflightDataBlock(). If addInflightDataBlock()
502 : // has not been called, this must be set to 0. compressedSize is the
503 : // compressed size of the block.
504 2 : func (d *dataBlockEstimates) dataBlockCompressed(compressedSize int, inflightSize int) {
505 2 : if d.useMutex {
506 0 : d.mu.Lock()
507 0 : defer d.mu.Unlock()
508 0 : }
509 2 : d.estimate.writtenWithDelta(compressedSize+blockTrailerLen, inflightSize)
510 : }
511 :
512 : // size is an estimated size of datablock data which has been written to disk.
513 2 : func (d *dataBlockEstimates) size() uint64 {
514 2 : if d.useMutex {
515 0 : d.mu.Lock()
516 0 : defer d.mu.Unlock()
517 0 : }
518 : // If there is no parallel compression, there should not be any inflight bytes.
519 2 : if invariants.Enabled && !d.useMutex {
520 2 : if d.estimate.inflightSize != 0 {
521 0 : panic("unexpected inflight entry in data block size estimation")
522 : }
523 : }
524 2 : return d.estimate.size()
525 : }
526 :
527 : // Avoid linter unused error.
528 : var _ = (&dataBlockEstimates{}).addInflightDataBlock
529 :
530 : // NB: unused since no parallel compression.
531 0 : func (d *dataBlockEstimates) addInflightDataBlock(size int) {
532 0 : if d.useMutex {
533 0 : d.mu.Lock()
534 0 : defer d.mu.Unlock()
535 0 : }
536 :
537 0 : d.estimate.addInflight(size)
538 : }
539 :
540 : var writeTaskPool = sync.Pool{
541 2 : New: func() interface{} {
542 2 : t := &writeTask{}
543 2 : t.compressionDone = make(chan bool, 1)
544 2 : return t
545 2 : },
546 : }
547 :
548 : type checksummer struct {
549 : checksumType ChecksumType
550 : xxHasher *xxhash.Digest
551 : }
552 :
553 2 : func (c *checksummer) checksum(block []byte, blockType []byte) (checksum uint32) {
554 2 : // Calculate the checksum.
555 2 : switch c.checksumType {
556 2 : case ChecksumTypeCRC32c:
557 2 : checksum = crc.New(block).Update(blockType).Value()
558 1 : case ChecksumTypeXXHash64:
559 1 : if c.xxHasher == nil {
560 1 : c.xxHasher = xxhash.New()
561 1 : } else {
562 1 : c.xxHasher.Reset()
563 1 : }
564 1 : c.xxHasher.Write(block)
565 1 : c.xxHasher.Write(blockType)
566 1 : checksum = uint32(c.xxHasher.Sum64())
567 0 : default:
568 0 : panic(errors.Newf("unsupported checksum type: %d", c.checksumType))
569 : }
570 2 : return checksum
571 : }
572 :
573 : type blockBuf struct {
574 : // tmp is a scratch buffer, large enough to hold either footerLen bytes,
575 : // blockTrailerLen bytes, (5 * binary.MaxVarintLen64) bytes, and most
576 : // likely large enough for a block handle with properties.
577 : tmp [blockHandleLikelyMaxLen]byte
578 : // compressedBuf is the destination buffer for compression. It is re-used over the
579 : // lifetime of the blockBuf, avoiding the allocation of a temporary buffer for each block.
580 : compressedBuf []byte
581 : checksummer checksummer
582 : }
583 :
584 2 : func (b *blockBuf) clear() {
585 2 : // We can't assign b.compressedBuf[:0] to compressedBuf because snappy relies
586 2 : // on the length of the buffer, and not the capacity to determine if it needs
587 2 : // to make an allocation.
588 2 : *b = blockBuf{
589 2 : compressedBuf: b.compressedBuf, checksummer: b.checksummer,
590 2 : }
591 2 : }
592 :
593 : // A dataBlockBuf holds all the state required to compress and write a data block to disk.
594 : // A dataBlockBuf begins its lifecycle owned by the Writer client goroutine. The Writer
595 : // client goroutine adds keys to the sstable, writing directly into a dataBlockBuf's blockWriter
596 : // until the block is full. Once a dataBlockBuf's block is full, the dataBlockBuf may be passed
597 : // to other goroutines for compression and file I/O.
598 : type dataBlockBuf struct {
599 : blockBuf
600 : dataBlock blockWriter
601 :
602 : // uncompressed is a reference to a byte slice which is owned by the dataBlockBuf. It is the
603 : // next byte slice to be compressed. The uncompressed byte slice will be backed by the
604 : // dataBlock.buf.
605 : uncompressed []byte
606 : // compressed is a reference to a byte slice which is owned by the dataBlockBuf. It is the
607 : // compressed byte slice which must be written to disk. The compressed byte slice may be
608 : // backed by the dataBlock.buf, or the dataBlockBuf.compressedBuf, depending on whether
609 : // we use the result of the compression.
610 : compressed []byte
611 :
612 : // We're making calls to BlockPropertyCollectors from the Writer client goroutine. We need to
613 : // pass the encoded block properties over to the write queue. To prevent copies, and allocations,
614 : // we give each dataBlockBuf, a blockPropertiesEncoder.
615 : blockPropsEncoder blockPropertiesEncoder
616 : // dataBlockProps is set when Writer.finishDataBlockProps is called. The dataBlockProps slice is
617 : // a shallow copy of the internal buffer of the dataBlockBuf.blockPropsEncoder.
618 : dataBlockProps []byte
619 :
620 : // sepScratch is reusable scratch space for computing separator keys.
621 : sepScratch []byte
622 : }
623 :
624 2 : func (d *dataBlockBuf) clear() {
625 2 : d.blockBuf.clear()
626 2 : d.dataBlock.clear()
627 2 :
628 2 : d.uncompressed = nil
629 2 : d.compressed = nil
630 2 : d.dataBlockProps = nil
631 2 : d.sepScratch = d.sepScratch[:0]
632 2 : }
633 :
634 : var dataBlockBufPool = sync.Pool{
635 2 : New: func() interface{} {
636 2 : return &dataBlockBuf{}
637 2 : },
638 : }
639 :
640 2 : func newDataBlockBuf(restartInterval int, checksumType ChecksumType) *dataBlockBuf {
641 2 : d := dataBlockBufPool.Get().(*dataBlockBuf)
642 2 : d.dataBlock.restartInterval = restartInterval
643 2 : d.checksummer.checksumType = checksumType
644 2 : return d
645 2 : }
646 :
647 2 : func (d *dataBlockBuf) finish() {
648 2 : d.uncompressed = d.dataBlock.finish()
649 2 : }
650 :
651 2 : func (d *dataBlockBuf) compressAndChecksum(c Compression) {
652 2 : d.compressed = compressAndChecksum(d.uncompressed, c, &d.blockBuf)
653 2 : }
654 :
655 : func (d *dataBlockBuf) shouldFlush(
656 : key InternalKey, valueLen, targetBlockSize, sizeThreshold int,
657 2 : ) bool {
658 2 : return shouldFlush(
659 2 : key, valueLen, d.dataBlock.restartInterval, d.dataBlock.estimatedSize(),
660 2 : d.dataBlock.nEntries, targetBlockSize, sizeThreshold)
661 2 : }
662 :
663 : type indexBlockAndBlockProperties struct {
664 : nEntries int
665 : // sep is the last key added to this block, for computing a separator later.
666 : sep InternalKey
667 : properties []byte
668 : // block is the encoded block produced by blockWriter.finish.
669 : block []byte
670 : }
671 :
672 : // Set sets the value for the given key. The sequence number is set to 0.
673 : // Intended for use to externally construct an sstable before ingestion into a
674 : // DB. For a given Writer, the keys passed to Set must be in strictly increasing
675 : // order.
676 : //
677 : // TODO(peter): untested
678 2 : func (w *Writer) Set(key, value []byte) error {
679 2 : if w.err != nil {
680 0 : return w.err
681 0 : }
682 2 : if w.isStrictObsolete {
683 0 : return errors.Errorf("use AddWithForceObsolete")
684 0 : }
685 : // forceObsolete is false based on the assumption that no RANGEDELs in the
686 : // sstable delete the added points.
687 2 : return w.addPoint(base.MakeInternalKey(key, 0, InternalKeyKindSet), value, false)
688 : }
689 :
690 : // Delete deletes the value for the given key. The sequence number is set to
691 : // 0. Intended for use to externally construct an sstable before ingestion into
692 : // a DB.
693 : //
694 : // TODO(peter): untested
695 1 : func (w *Writer) Delete(key []byte) error {
696 1 : if w.err != nil {
697 0 : return w.err
698 0 : }
699 1 : if w.isStrictObsolete {
700 0 : return errors.Errorf("use AddWithForceObsolete")
701 0 : }
702 : // forceObsolete is false based on the assumption that no RANGEDELs in the
703 : // sstable delete the added points.
704 1 : return w.addPoint(base.MakeInternalKey(key, 0, InternalKeyKindDelete), nil, false)
705 : }
706 :
707 : // DeleteRange deletes all of the keys (and values) in the range [start,end)
708 : // (inclusive on start, exclusive on end). The sequence number is set to
709 : // 0. Intended for use to externally construct an sstable before ingestion into
710 : // a DB.
711 : //
712 : // TODO(peter): untested
713 2 : func (w *Writer) DeleteRange(start, end []byte) error {
714 2 : if w.err != nil {
715 0 : return w.err
716 0 : }
717 2 : return w.addTombstone(base.MakeInternalKey(start, 0, InternalKeyKindRangeDelete), end)
718 : }
719 :
720 : // Merge adds an action to the DB that merges the value at key with the new
721 : // value. The details of the merge are dependent upon the configured merge
722 : // operator. The sequence number is set to 0. Intended for use to externally
723 : // construct an sstable before ingestion into a DB.
724 : //
725 : // TODO(peter): untested
726 0 : func (w *Writer) Merge(key, value []byte) error {
727 0 : if w.err != nil {
728 0 : return w.err
729 0 : }
730 0 : if w.isStrictObsolete {
731 0 : return errors.Errorf("use AddWithForceObsolete")
732 0 : }
733 : // forceObsolete is false based on the assumption that no RANGEDELs in the
734 : // sstable that delete the added points. If the user configured this writer
735 : // to be strict-obsolete, addPoint will reject the addition of this MERGE.
736 0 : return w.addPoint(base.MakeInternalKey(key, 0, InternalKeyKindMerge), value, false)
737 : }
738 :
739 : // Add adds a key/value pair to the table being written. For a given Writer,
740 : // the keys passed to Add must be in increasing order. The exception to this
741 : // rule is range deletion tombstones. Range deletion tombstones need to be
742 : // added ordered by their start key, but they can be added out of order from
743 : // point entries. Additionally, range deletion tombstones must be fragmented
744 : // (i.e. by keyspan.Fragmenter).
745 2 : func (w *Writer) Add(key InternalKey, value []byte) error {
746 2 : if w.isStrictObsolete {
747 0 : return errors.Errorf("use AddWithForceObsolete")
748 0 : }
749 2 : return w.AddWithForceObsolete(key, value, false)
750 : }
751 :
752 : // AddWithForceObsolete must be used when writing a strict-obsolete sstable.
753 : //
754 : // forceObsolete indicates whether the caller has determined that this key is
755 : // obsolete even though it may be the latest point key for this userkey. This
756 : // should be set to true for keys obsoleted by RANGEDELs, and is required for
757 : // strict-obsolete sstables.
758 : //
759 : // Note that there are two properties, S1 and S2 (see comment in format.go)
760 : // that strict-obsolete ssts must satisfy. S2, due to RANGEDELs, is solely the
761 : // responsibility of the caller. S1 is solely the responsibility of the
762 : // callee.
763 2 : func (w *Writer) AddWithForceObsolete(key InternalKey, value []byte, forceObsolete bool) error {
764 2 : if w.err != nil {
765 0 : return w.err
766 0 : }
767 :
768 2 : switch key.Kind() {
769 2 : case InternalKeyKindRangeDelete:
770 2 : return w.addTombstone(key, value)
771 : case base.InternalKeyKindRangeKeyDelete,
772 : base.InternalKeyKindRangeKeySet,
773 0 : base.InternalKeyKindRangeKeyUnset:
774 0 : w.err = errors.Errorf(
775 0 : "pebble: range keys must be added via one of the RangeKey* functions")
776 0 : return w.err
777 : }
778 2 : return w.addPoint(key, value, forceObsolete)
779 : }
780 :
781 2 : func (w *Writer) makeAddPointDecisionV2(key InternalKey) error {
782 2 : prevTrailer := w.lastPointKeyInfo.trailer
783 2 : w.lastPointKeyInfo.trailer = key.Trailer
784 2 : if w.dataBlockBuf.dataBlock.nEntries == 0 {
785 2 : return nil
786 2 : }
787 2 : if !w.disableKeyOrderChecks {
788 2 : prevPointUserKey := w.dataBlockBuf.dataBlock.getCurUserKey()
789 2 : cmpUser := w.compare(prevPointUserKey, key.UserKey)
790 2 : if cmpUser > 0 || (cmpUser == 0 && prevTrailer <= key.Trailer) {
791 1 : return errors.Errorf(
792 1 : "pebble: keys must be added in strictly increasing order: %s, %s",
793 1 : InternalKey{UserKey: prevPointUserKey, Trailer: prevTrailer}.Pretty(w.formatKey),
794 1 : key.Pretty(w.formatKey))
795 1 : }
796 : }
797 2 : return nil
798 : }
799 :
800 : // REQUIRES: at least one point has been written to the Writer.
801 2 : func (w *Writer) getLastPointUserKey() []byte {
802 2 : if w.dataBlockBuf.dataBlock.nEntries == 0 {
803 0 : panic(errors.AssertionFailedf("no point keys added to writer"))
804 : }
805 2 : return w.dataBlockBuf.dataBlock.getCurUserKey()
806 : }
807 :
808 : // REQUIRES: w.tableFormat >= TableFormatPebblev3
809 : func (w *Writer) makeAddPointDecisionV3(
810 : key InternalKey, valueLen int,
811 2 : ) (setHasSamePrefix bool, writeToValueBlock bool, isObsolete bool, err error) {
812 2 : prevPointKeyInfo := w.lastPointKeyInfo
813 2 : w.lastPointKeyInfo.userKeyLen = len(key.UserKey)
814 2 : w.lastPointKeyInfo.prefixLen = w.split(key.UserKey)
815 2 : w.lastPointKeyInfo.trailer = key.Trailer
816 2 : w.lastPointKeyInfo.isObsolete = false
817 2 : if !w.meta.HasPointKeys {
818 2 : return false, false, false, nil
819 2 : }
820 2 : keyKind := base.TrailerKind(key.Trailer)
821 2 : prevPointUserKey := w.getLastPointUserKey()
822 2 : prevPointKey := InternalKey{UserKey: prevPointUserKey, Trailer: prevPointKeyInfo.trailer}
823 2 : prevKeyKind := base.TrailerKind(prevPointKeyInfo.trailer)
824 2 : considerWriteToValueBlock := prevKeyKind == InternalKeyKindSet &&
825 2 : keyKind == InternalKeyKindSet
826 2 : if considerWriteToValueBlock && !w.requiredInPlaceValueBound.IsEmpty() {
827 1 : keyPrefix := key.UserKey[:w.lastPointKeyInfo.prefixLen]
828 1 : cmpUpper := w.compare(
829 1 : w.requiredInPlaceValueBound.Upper, keyPrefix)
830 1 : if cmpUpper <= 0 {
831 1 : // Common case for CockroachDB. Make it empty since all future keys in
832 1 : // this sstable will also have cmpUpper <= 0.
833 1 : w.requiredInPlaceValueBound = UserKeyPrefixBound{}
834 1 : } else if w.compare(keyPrefix, w.requiredInPlaceValueBound.Lower) >= 0 {
835 1 : considerWriteToValueBlock = false
836 1 : }
837 : }
838 : // cmpPrefix is initialized iff considerWriteToValueBlock.
839 2 : var cmpPrefix int
840 2 : var cmpUser int
841 2 : if considerWriteToValueBlock {
842 2 : // Compare the prefixes.
843 2 : cmpPrefix = w.compare(prevPointUserKey[:prevPointKeyInfo.prefixLen],
844 2 : key.UserKey[:w.lastPointKeyInfo.prefixLen])
845 2 : cmpUser = cmpPrefix
846 2 : if cmpPrefix == 0 {
847 2 : // Need to compare suffixes to compute cmpUser.
848 2 : cmpUser = w.compare(prevPointUserKey[prevPointKeyInfo.prefixLen:],
849 2 : key.UserKey[w.lastPointKeyInfo.prefixLen:])
850 2 : }
851 2 : } else {
852 2 : cmpUser = w.compare(prevPointUserKey, key.UserKey)
853 2 : }
854 : // Ensure that no one adds a point key kind without considering the obsolete
855 : // handling for that kind.
856 2 : switch keyKind {
857 : case InternalKeyKindSet, InternalKeyKindSetWithDelete, InternalKeyKindMerge,
858 2 : InternalKeyKindDelete, InternalKeyKindSingleDelete, InternalKeyKindDeleteSized:
859 0 : default:
860 0 : panic(errors.AssertionFailedf("unexpected key kind %s", keyKind.String()))
861 : }
862 : // If same user key, then the current key is obsolete if any of the
863 : // following is true:
864 : // C1 The prev key was obsolete.
865 : // C2 The prev key was not a MERGE. When the previous key is a MERGE we must
866 : // preserve SET* and MERGE since their values will be merged into the
867 : // previous key. We also must preserve DEL* since there may be an older
868 : // SET*/MERGE in a lower level that must not be merged with the MERGE --
869 : // if we omit the DEL* that lower SET*/MERGE will become visible.
870 : //
871 : // Regardless of whether it is the same user key or not
872 : // C3 The current key is some kind of point delete, and we are writing to
873 : // the lowest level, then it is also obsolete. The correctness of this
874 : // relies on the same user key not spanning multiple sstables in a level.
875 : //
876 : // C1 ensures that for a user key there is at most one transition from
877 : // !obsolete to obsolete. Consider a user key k, for which the first n keys
878 : // are not obsolete. We consider the various value of n:
879 : //
880 : // n = 0: This happens due to forceObsolete being set by the caller, or due
881 : // to C3. forceObsolete must only be set due a RANGEDEL, and that RANGEDEL
882 : // must also delete all the lower seqnums for the same user key. C3 triggers
883 : // due to a point delete and that deletes all the lower seqnums for the same
884 : // user key.
885 : //
886 : // n = 1: This is the common case. It happens when the first key is not a
887 : // MERGE, or the current key is some kind of point delete.
888 : //
889 : // n > 1: This is due to a sequence of MERGE keys, potentially followed by a
890 : // single non-MERGE key.
891 2 : isObsoleteC1AndC2 := cmpUser == 0 &&
892 2 : (prevPointKeyInfo.isObsolete || prevKeyKind != InternalKeyKindMerge)
893 2 : isObsoleteC3 := w.writingToLowestLevel &&
894 2 : (keyKind == InternalKeyKindDelete || keyKind == InternalKeyKindSingleDelete ||
895 2 : keyKind == InternalKeyKindDeleteSized)
896 2 : isObsolete = isObsoleteC1AndC2 || isObsoleteC3
897 2 : // TODO(sumeer): storing isObsolete SET and SETWITHDEL in value blocks is
898 2 : // possible, but requires some care in documenting and checking invariants.
899 2 : // There is code that assumes nothing in value blocks because of single MVCC
900 2 : // version (those should be ok). We have to ensure setHasSamePrefix is
901 2 : // correctly initialized here etc.
902 2 :
903 2 : if !w.disableKeyOrderChecks &&
904 2 : (cmpUser > 0 || (cmpUser == 0 && prevPointKeyInfo.trailer <= key.Trailer)) {
905 1 : return false, false, false, errors.Errorf(
906 1 : "pebble: keys must be added in strictly increasing order: %s, %s",
907 1 : prevPointKey.Pretty(w.formatKey), key.Pretty(w.formatKey))
908 1 : }
909 2 : if !considerWriteToValueBlock {
910 2 : return false, false, isObsolete, nil
911 2 : }
912 : // NB: it is possible that cmpUser == 0, i.e., these two SETs have identical
913 : // user keys (because of an open snapshot). This should be the rare case.
914 2 : setHasSamePrefix = cmpPrefix == 0
915 2 : // Use of 0 here is somewhat arbitrary. Given the minimum 3 byte encoding of
916 2 : // valueHandle, this should be > 3. But tiny values are common in test and
917 2 : // unlikely in production, so we use 0 here for better test coverage.
918 2 : const tinyValueThreshold = 0
919 2 : // NB: setting WriterOptions.DisableValueBlocks does not disable the
920 2 : // setHasSamePrefix optimization.
921 2 : considerWriteToValueBlock = setHasSamePrefix && valueLen > tinyValueThreshold && w.valueBlockWriter != nil
922 2 : return setHasSamePrefix, considerWriteToValueBlock, isObsolete, nil
923 : }
924 :
925 2 : func (w *Writer) addPoint(key InternalKey, value []byte, forceObsolete bool) error {
926 2 : if w.isStrictObsolete && key.Kind() == InternalKeyKindMerge {
927 1 : return errors.Errorf("MERGE not supported in a strict-obsolete sstable")
928 1 : }
929 2 : var err error
930 2 : var setHasSameKeyPrefix, writeToValueBlock, addPrefixToValueStoredWithKey bool
931 2 : var isObsolete bool
932 2 : maxSharedKeyLen := len(key.UserKey)
933 2 : if w.tableFormat >= TableFormatPebblev3 {
934 2 : // maxSharedKeyLen is limited to the prefix of the preceding key. If the
935 2 : // preceding key was in a different block, then the blockWriter will
936 2 : // ignore this maxSharedKeyLen.
937 2 : maxSharedKeyLen = w.lastPointKeyInfo.prefixLen
938 2 : setHasSameKeyPrefix, writeToValueBlock, isObsolete, err =
939 2 : w.makeAddPointDecisionV3(key, len(value))
940 2 : addPrefixToValueStoredWithKey = base.TrailerKind(key.Trailer) == InternalKeyKindSet
941 2 : } else {
942 2 : err = w.makeAddPointDecisionV2(key)
943 2 : }
944 2 : if err != nil {
945 1 : return err
946 1 : }
947 2 : isObsolete = w.tableFormat >= TableFormatPebblev4 && (isObsolete || forceObsolete)
948 2 : w.lastPointKeyInfo.isObsolete = isObsolete
949 2 : var valueStoredWithKey []byte
950 2 : var prefix valuePrefix
951 2 : var valueStoredWithKeyLen int
952 2 : if writeToValueBlock {
953 2 : vh, err := w.valueBlockWriter.addValue(value)
954 2 : if err != nil {
955 0 : return err
956 0 : }
957 2 : n := encodeValueHandle(w.blockBuf.tmp[:], vh)
958 2 : valueStoredWithKey = w.blockBuf.tmp[:n]
959 2 : valueStoredWithKeyLen = len(valueStoredWithKey) + 1
960 2 : var attribute base.ShortAttribute
961 2 : if w.shortAttributeExtractor != nil {
962 1 : // TODO(sumeer): for compactions, it is possible that the input sstable
963 1 : // already has this value in the value section and so we have already
964 1 : // extracted the ShortAttribute. Avoid extracting it again. This will
965 1 : // require changing the Writer.Add interface.
966 1 : if attribute, err = w.shortAttributeExtractor(
967 1 : key.UserKey, w.lastPointKeyInfo.prefixLen, value); err != nil {
968 0 : return err
969 0 : }
970 : }
971 2 : prefix = makePrefixForValueHandle(setHasSameKeyPrefix, attribute)
972 2 : } else {
973 2 : valueStoredWithKey = value
974 2 : valueStoredWithKeyLen = len(value)
975 2 : if addPrefixToValueStoredWithKey {
976 2 : valueStoredWithKeyLen++
977 2 : }
978 2 : prefix = makePrefixForInPlaceValue(setHasSameKeyPrefix)
979 : }
980 :
981 2 : if err := w.maybeFlush(key, valueStoredWithKeyLen); err != nil {
982 1 : return err
983 1 : }
984 :
985 2 : for i := range w.blockPropCollectors {
986 2 : v := value
987 2 : if addPrefixToValueStoredWithKey {
988 2 : // Values for SET are not required to be in-place, and in the future may
989 2 : // not even be read by the compaction, so pass nil values. Block
990 2 : // property collectors in such Pebble DB's must not look at the value.
991 2 : v = nil
992 2 : }
993 2 : if err := w.blockPropCollectors[i].Add(key, v); err != nil {
994 1 : w.err = err
995 1 : return err
996 1 : }
997 : }
998 2 : if w.tableFormat >= TableFormatPebblev4 {
999 2 : w.obsoleteCollector.AddPoint(isObsolete)
1000 2 : }
1001 :
1002 2 : w.maybeAddToFilter(key.UserKey)
1003 2 : w.dataBlockBuf.dataBlock.addWithOptionalValuePrefix(
1004 2 : key, isObsolete, valueStoredWithKey, maxSharedKeyLen, addPrefixToValueStoredWithKey, prefix,
1005 2 : setHasSameKeyPrefix)
1006 2 :
1007 2 : w.meta.updateSeqNum(key.SeqNum())
1008 2 :
1009 2 : if !w.meta.HasPointKeys {
1010 2 : k := w.dataBlockBuf.dataBlock.getCurKey()
1011 2 : // NB: We need to ensure that SmallestPoint.UserKey is set, so we create
1012 2 : // an InternalKey which is semantically identical to the key, but won't
1013 2 : // have a nil UserKey. We do this, because key.UserKey could be nil, and
1014 2 : // we don't want SmallestPoint.UserKey to be nil.
1015 2 : //
1016 2 : // todo(bananabrick): Determine if it's okay to have a nil SmallestPoint
1017 2 : // .UserKey now that we don't rely on a nil UserKey to determine if the
1018 2 : // key has been set or not.
1019 2 : w.meta.SetSmallestPointKey(k.Clone())
1020 2 : }
1021 :
1022 2 : w.props.NumEntries++
1023 2 : switch key.Kind() {
1024 2 : case InternalKeyKindDelete, InternalKeyKindSingleDelete:
1025 2 : w.props.NumDeletions++
1026 2 : w.props.RawPointTombstoneKeySize += uint64(len(key.UserKey))
1027 2 : case InternalKeyKindDeleteSized:
1028 2 : var size uint64
1029 2 : if len(value) > 0 {
1030 2 : var n int
1031 2 : size, n = binary.Uvarint(value)
1032 2 : if n <= 0 {
1033 0 : w.err = errors.Newf("%s key's value (%x) does not parse as uvarint",
1034 0 : errors.Safe(key.Kind().String()), value)
1035 0 : return w.err
1036 0 : }
1037 : }
1038 2 : w.props.NumDeletions++
1039 2 : w.props.NumSizedDeletions++
1040 2 : w.props.RawPointTombstoneKeySize += uint64(len(key.UserKey))
1041 2 : w.props.RawPointTombstoneValueSize += size
1042 2 : case InternalKeyKindMerge:
1043 2 : w.props.NumMergeOperands++
1044 : }
1045 2 : w.props.RawKeySize += uint64(key.Size())
1046 2 : w.props.RawValueSize += uint64(len(value))
1047 2 : return nil
1048 : }
1049 :
1050 1 : func (w *Writer) prettyTombstone(k InternalKey, value []byte) fmt.Formatter {
1051 1 : return keyspan.Span{
1052 1 : Start: k.UserKey,
1053 1 : End: value,
1054 1 : Keys: []keyspan.Key{{Trailer: k.Trailer}},
1055 1 : }.Pretty(w.formatKey)
1056 1 : }
1057 :
1058 2 : func (w *Writer) addTombstone(key InternalKey, value []byte) error {
1059 2 : if !w.disableKeyOrderChecks && !w.rangeDelV1Format && w.rangeDelBlock.nEntries > 0 {
1060 2 : // Check that tombstones are being added in fragmented order. If the two
1061 2 : // tombstones overlap, their start and end keys must be identical.
1062 2 : prevKey := w.rangeDelBlock.getCurKey()
1063 2 : switch c := w.compare(prevKey.UserKey, key.UserKey); {
1064 0 : case c > 0:
1065 0 : w.err = errors.Errorf("pebble: keys must be added in order: %s, %s",
1066 0 : prevKey.Pretty(w.formatKey), key.Pretty(w.formatKey))
1067 0 : return w.err
1068 2 : case c == 0:
1069 2 : prevValue := w.rangeDelBlock.curValue
1070 2 : if w.compare(prevValue, value) != 0 {
1071 1 : w.err = errors.Errorf("pebble: overlapping tombstones must be fragmented: %s vs %s",
1072 1 : w.prettyTombstone(prevKey, prevValue),
1073 1 : w.prettyTombstone(key, value))
1074 1 : return w.err
1075 1 : }
1076 2 : if prevKey.SeqNum() <= key.SeqNum() {
1077 1 : w.err = errors.Errorf("pebble: keys must be added in strictly increasing order: %s, %s",
1078 1 : prevKey.Pretty(w.formatKey), key.Pretty(w.formatKey))
1079 1 : return w.err
1080 1 : }
1081 2 : default:
1082 2 : prevValue := w.rangeDelBlock.curValue
1083 2 : if w.compare(prevValue, key.UserKey) > 0 {
1084 1 : w.err = errors.Errorf("pebble: overlapping tombstones must be fragmented: %s vs %s",
1085 1 : w.prettyTombstone(prevKey, prevValue),
1086 1 : w.prettyTombstone(key, value))
1087 1 : return w.err
1088 1 : }
1089 : }
1090 : }
1091 :
1092 2 : if key.Trailer == InternalKeyRangeDeleteSentinel {
1093 0 : w.err = errors.Errorf("pebble: cannot add range delete sentinel: %s", key.Pretty(w.formatKey))
1094 0 : return w.err
1095 0 : }
1096 :
1097 2 : w.meta.updateSeqNum(key.SeqNum())
1098 2 :
1099 2 : switch {
1100 1 : case w.rangeDelV1Format:
1101 1 : // Range tombstones are not fragmented in the v1 (i.e. RocksDB) range
1102 1 : // deletion block format, so we need to track the largest range tombstone
1103 1 : // end key as every range tombstone is added.
1104 1 : //
1105 1 : // Note that writing the v1 format is only supported for tests.
1106 1 : if w.props.NumRangeDeletions == 0 {
1107 1 : w.meta.SetSmallestRangeDelKey(key.Clone())
1108 1 : w.meta.SetLargestRangeDelKey(base.MakeRangeDeleteSentinelKey(value).Clone())
1109 1 : } else {
1110 1 : if base.InternalCompare(w.compare, w.meta.SmallestRangeDel, key) > 0 {
1111 1 : w.meta.SetSmallestRangeDelKey(key.Clone())
1112 1 : }
1113 1 : end := base.MakeRangeDeleteSentinelKey(value)
1114 1 : if base.InternalCompare(w.compare, w.meta.LargestRangeDel, end) < 0 {
1115 1 : w.meta.SetLargestRangeDelKey(end.Clone())
1116 1 : }
1117 : }
1118 :
1119 2 : default:
1120 2 : // Range tombstones are fragmented in the v2 range deletion block format,
1121 2 : // so the start key of the first range tombstone added will be the smallest
1122 2 : // range tombstone key. The largest range tombstone key will be determined
1123 2 : // in Writer.Close() as the end key of the last range tombstone added.
1124 2 : if w.props.NumRangeDeletions == 0 {
1125 2 : w.meta.SetSmallestRangeDelKey(key.Clone())
1126 2 : }
1127 : }
1128 :
1129 2 : w.props.NumEntries++
1130 2 : w.props.NumDeletions++
1131 2 : w.props.NumRangeDeletions++
1132 2 : w.props.RawKeySize += uint64(key.Size())
1133 2 : w.props.RawValueSize += uint64(len(value))
1134 2 : w.rangeDelBlock.add(key, value)
1135 2 : return nil
1136 : }
1137 :
1138 : // RangeKeySet sets a range between start (inclusive) and end (exclusive) with
1139 : // the given suffix to the given value. The resulting range key is given the
1140 : // sequence number zero, with the expectation that the resulting sstable will be
1141 : // ingested.
1142 : //
1143 : // Keys must be added to the table in increasing order of start key. Spans are
1144 : // not required to be fragmented. The same suffix may not be set or unset twice
1145 : // over the same keyspan, because it would result in inconsistent state. Both
1146 : // the Set and Unset would share the zero sequence number, and a key cannot be
1147 : // both simultaneously set and unset.
1148 1 : func (w *Writer) RangeKeySet(start, end, suffix, value []byte) error {
1149 1 : return w.addRangeKeySpan(keyspan.Span{
1150 1 : Start: w.tempRangeKeyCopy(start),
1151 1 : End: w.tempRangeKeyCopy(end),
1152 1 : Keys: []keyspan.Key{
1153 1 : {
1154 1 : Trailer: base.MakeTrailer(0, base.InternalKeyKindRangeKeySet),
1155 1 : Suffix: w.tempRangeKeyCopy(suffix),
1156 1 : Value: w.tempRangeKeyCopy(value),
1157 1 : },
1158 1 : },
1159 1 : })
1160 1 : }
1161 :
1162 : // RangeKeyUnset un-sets a range between start (inclusive) and end (exclusive)
1163 : // with the given suffix. The resulting range key is given the
1164 : // sequence number zero, with the expectation that the resulting sstable will be
1165 : // ingested.
1166 : //
1167 : // Keys must be added to the table in increasing order of start key. Spans are
1168 : // not required to be fragmented. The same suffix may not be set or unset twice
1169 : // over the same keyspan, because it would result in inconsistent state. Both
1170 : // the Set and Unset would share the zero sequence number, and a key cannot be
1171 : // both simultaneously set and unset.
1172 1 : func (w *Writer) RangeKeyUnset(start, end, suffix []byte) error {
1173 1 : return w.addRangeKeySpan(keyspan.Span{
1174 1 : Start: w.tempRangeKeyCopy(start),
1175 1 : End: w.tempRangeKeyCopy(end),
1176 1 : Keys: []keyspan.Key{
1177 1 : {
1178 1 : Trailer: base.MakeTrailer(0, base.InternalKeyKindRangeKeyUnset),
1179 1 : Suffix: w.tempRangeKeyCopy(suffix),
1180 1 : },
1181 1 : },
1182 1 : })
1183 1 : }
1184 :
1185 : // RangeKeyDelete deletes a range between start (inclusive) and end (exclusive).
1186 : //
1187 : // Keys must be added to the table in increasing order of start key. Spans are
1188 : // not required to be fragmented.
1189 1 : func (w *Writer) RangeKeyDelete(start, end []byte) error {
1190 1 : return w.addRangeKeySpan(keyspan.Span{
1191 1 : Start: w.tempRangeKeyCopy(start),
1192 1 : End: w.tempRangeKeyCopy(end),
1193 1 : Keys: []keyspan.Key{
1194 1 : {Trailer: base.MakeTrailer(0, base.InternalKeyKindRangeKeyDelete)},
1195 1 : },
1196 1 : })
1197 1 : }
1198 :
1199 : // AddRangeKey adds a range key set, unset, or delete key/value pair to the
1200 : // table being written.
1201 : //
1202 : // Range keys must be supplied in strictly ascending order of start key (i.e.
1203 : // user key ascending, sequence number descending, and key type descending).
1204 : // Ranges added must also be supplied in fragmented span order - i.e. other than
1205 : // spans that are perfectly aligned (same start and end keys), spans may not
1206 : // overlap. Range keys may be added out of order relative to point keys and
1207 : // range deletions.
1208 2 : func (w *Writer) AddRangeKey(key InternalKey, value []byte) error {
1209 2 : if w.err != nil {
1210 0 : return w.err
1211 0 : }
1212 2 : return w.addRangeKey(key, value)
1213 : }
1214 :
1215 1 : func (w *Writer) addRangeKeySpan(span keyspan.Span) error {
1216 1 : if w.compare(span.Start, span.End) >= 0 {
1217 0 : return errors.Errorf(
1218 0 : "pebble: start key must be strictly less than end key",
1219 0 : )
1220 0 : }
1221 1 : if w.fragmenter.Start() != nil && w.compare(w.fragmenter.Start(), span.Start) > 0 {
1222 1 : return errors.Errorf("pebble: spans must be added in order: %s > %s",
1223 1 : w.formatKey(w.fragmenter.Start()), w.formatKey(span.Start))
1224 1 : }
1225 : // Add this span to the fragmenter.
1226 1 : w.fragmenter.Add(span)
1227 1 : return w.err
1228 : }
1229 :
1230 1 : func (w *Writer) encodeRangeKeySpan(span keyspan.Span) {
1231 1 : // This method is the emit function of the Fragmenter.
1232 1 : //
1233 1 : // NB: The span should only contain range keys and be internally consistent
1234 1 : // (eg, no duplicate suffixes, no additional keys after a RANGEKEYDEL).
1235 1 : //
1236 1 : // We use w.rangeKeysBySuffix and w.rangeKeySpan to avoid allocations.
1237 1 :
1238 1 : // Sort the keys by suffix. Iteration doesn't *currently* depend on it, but
1239 1 : // we may want to in the future.
1240 1 : w.rangeKeysBySuffix.Cmp = w.compare
1241 1 : w.rangeKeysBySuffix.Keys = span.Keys
1242 1 : sort.Sort(&w.rangeKeysBySuffix)
1243 1 :
1244 1 : w.rangeKeySpan = span
1245 1 : w.rangeKeySpan.Keys = w.rangeKeysBySuffix.Keys
1246 1 : w.err = firstError(w.err, w.rangeKeyEncoder.Encode(&w.rangeKeySpan))
1247 1 : }
1248 :
1249 2 : func (w *Writer) addRangeKey(key InternalKey, value []byte) error {
1250 2 : if !w.disableKeyOrderChecks && w.rangeKeyBlock.nEntries > 0 {
1251 2 : prevStartKey := w.rangeKeyBlock.getCurKey()
1252 2 : prevEndKey, _, ok := rangekey.DecodeEndKey(prevStartKey.Kind(), w.rangeKeyBlock.curValue)
1253 2 : if !ok {
1254 0 : // We panic here as we should have previously decoded and validated this
1255 0 : // key and value when it was first added to the range key block.
1256 0 : panic(errors.Errorf("pebble: invalid end key for span: %s",
1257 0 : prevStartKey.Pretty(w.formatKey)))
1258 : }
1259 :
1260 2 : curStartKey := key
1261 2 : curEndKey, _, ok := rangekey.DecodeEndKey(curStartKey.Kind(), value)
1262 2 : if !ok {
1263 0 : w.err = errors.Errorf("pebble: invalid end key for span: %s",
1264 0 : curStartKey.Pretty(w.formatKey))
1265 0 : return w.err
1266 0 : }
1267 :
1268 : // Start keys must be strictly increasing.
1269 2 : if base.InternalCompare(w.compare, prevStartKey, curStartKey) >= 0 {
1270 1 : w.err = errors.Errorf(
1271 1 : "pebble: range keys starts must be added in increasing order: %s, %s",
1272 1 : prevStartKey.Pretty(w.formatKey), key.Pretty(w.formatKey))
1273 1 : return w.err
1274 1 : }
1275 :
1276 : // Start keys are increasing. If the start user keys are equal, the
1277 : // end keys must be equal (i.e. aligned spans).
1278 2 : if w.compare(prevStartKey.UserKey, curStartKey.UserKey) == 0 {
1279 2 : if w.compare(prevEndKey, curEndKey) != 0 {
1280 1 : w.err = errors.Errorf("pebble: overlapping range keys must be fragmented: %s, %s",
1281 1 : prevStartKey.Pretty(w.formatKey),
1282 1 : curStartKey.Pretty(w.formatKey))
1283 1 : return w.err
1284 1 : }
1285 2 : } else if w.compare(prevEndKey, curStartKey.UserKey) > 0 {
1286 1 : // If the start user keys are NOT equal, the spans must be disjoint (i.e.
1287 1 : // no overlap).
1288 1 : // NOTE: the inequality excludes zero, as we allow the end key of the
1289 1 : // lower span be the same as the start key of the upper span, because
1290 1 : // the range end key is considered an exclusive bound.
1291 1 : w.err = errors.Errorf("pebble: overlapping range keys must be fragmented: %s, %s",
1292 1 : prevStartKey.Pretty(w.formatKey),
1293 1 : curStartKey.Pretty(w.formatKey))
1294 1 : return w.err
1295 1 : }
1296 : }
1297 :
1298 : // TODO(travers): Add an invariant-gated check to ensure that suffix-values
1299 : // are sorted within coalesced spans.
1300 :
1301 : // Range-keys and point-keys are intended to live in "parallel" keyspaces.
1302 : // However, we track a single seqnum in the table metadata that spans both of
1303 : // these keyspaces.
1304 : // TODO(travers): Consider tracking range key seqnums separately.
1305 2 : w.meta.updateSeqNum(key.SeqNum())
1306 2 :
1307 2 : // Range tombstones are fragmented, so the start key of the first range key
1308 2 : // added will be the smallest. The largest range key is determined in
1309 2 : // Writer.Close() as the end key of the last range key added to the block.
1310 2 : if w.props.NumRangeKeys() == 0 {
1311 2 : w.meta.SetSmallestRangeKey(key.Clone())
1312 2 : }
1313 :
1314 : // Update block properties.
1315 2 : w.props.RawRangeKeyKeySize += uint64(key.Size())
1316 2 : w.props.RawRangeKeyValueSize += uint64(len(value))
1317 2 : switch key.Kind() {
1318 2 : case base.InternalKeyKindRangeKeyDelete:
1319 2 : w.props.NumRangeKeyDels++
1320 2 : case base.InternalKeyKindRangeKeySet:
1321 2 : w.props.NumRangeKeySets++
1322 2 : case base.InternalKeyKindRangeKeyUnset:
1323 2 : w.props.NumRangeKeyUnsets++
1324 0 : default:
1325 0 : panic(errors.Errorf("pebble: invalid range key type: %s", key.Kind()))
1326 : }
1327 :
1328 2 : for i := range w.blockPropCollectors {
1329 2 : if err := w.blockPropCollectors[i].Add(key, value); err != nil {
1330 0 : return err
1331 0 : }
1332 : }
1333 :
1334 : // Add the key to the block.
1335 2 : w.rangeKeyBlock.add(key, value)
1336 2 : return nil
1337 : }
1338 :
1339 : // tempRangeKeyBuf returns a slice of length n from the Writer's rkBuf byte
1340 : // slice. Any byte written to the returned slice is retained for the lifetime of
1341 : // the Writer.
1342 1 : func (w *Writer) tempRangeKeyBuf(n int) []byte {
1343 1 : if cap(w.rkBuf)-len(w.rkBuf) < n {
1344 1 : size := len(w.rkBuf) + 2*n
1345 1 : if size < 2*cap(w.rkBuf) {
1346 1 : size = 2 * cap(w.rkBuf)
1347 1 : }
1348 1 : buf := make([]byte, len(w.rkBuf), size)
1349 1 : copy(buf, w.rkBuf)
1350 1 : w.rkBuf = buf
1351 : }
1352 1 : b := w.rkBuf[len(w.rkBuf) : len(w.rkBuf)+n]
1353 1 : w.rkBuf = w.rkBuf[:len(w.rkBuf)+n]
1354 1 : return b
1355 : }
1356 :
1357 : // tempRangeKeyCopy returns a copy of the provided slice, stored in the Writer's
1358 : // range key buffer.
1359 1 : func (w *Writer) tempRangeKeyCopy(k []byte) []byte {
1360 1 : if len(k) == 0 {
1361 1 : return nil
1362 1 : }
1363 1 : buf := w.tempRangeKeyBuf(len(k))
1364 1 : copy(buf, k)
1365 1 : return buf
1366 : }
1367 :
1368 2 : func (w *Writer) maybeAddToFilter(key []byte) {
1369 2 : if w.filter != nil {
1370 2 : prefix := key[:w.split(key)]
1371 2 : w.filter.addKey(prefix)
1372 2 : }
1373 : }
1374 :
1375 2 : func (w *Writer) flush(key InternalKey) error {
1376 2 : // We're finishing a data block.
1377 2 : err := w.finishDataBlockProps(w.dataBlockBuf)
1378 2 : if err != nil {
1379 1 : return err
1380 1 : }
1381 2 : w.dataBlockBuf.finish()
1382 2 : w.dataBlockBuf.compressAndChecksum(w.compression)
1383 2 : // Since dataBlockEstimates.addInflightDataBlock was never called, the
1384 2 : // inflightSize is set to 0.
1385 2 : w.coordination.sizeEstimate.dataBlockCompressed(len(w.dataBlockBuf.compressed), 0)
1386 2 :
1387 2 : // Determine if the index block should be flushed. Since we're accessing the
1388 2 : // dataBlockBuf.dataBlock.curKey here, we have to make sure that once we start
1389 2 : // to pool the dataBlockBufs, the curKey isn't used by the Writer once the
1390 2 : // dataBlockBuf is added back to a sync.Pool. In this particular case, the
1391 2 : // byte slice which supports "sep" will eventually be copied when "sep" is
1392 2 : // added to the index block.
1393 2 : prevKey := w.dataBlockBuf.dataBlock.getCurKey()
1394 2 : sep := w.indexEntrySep(prevKey, key, w.dataBlockBuf)
1395 2 : // We determine that we should flush an index block from the Writer client
1396 2 : // goroutine, but we actually finish the index block from the writeQueue.
1397 2 : // When we determine that an index block should be flushed, we need to call
1398 2 : // BlockPropertyCollector.FinishIndexBlock. But block property collector
1399 2 : // calls must happen sequentially from the Writer client. Therefore, we need
1400 2 : // to determine that we are going to flush the index block from the Writer
1401 2 : // client.
1402 2 : shouldFlushIndexBlock := supportsTwoLevelIndex(w.tableFormat) && w.indexBlock.shouldFlush(
1403 2 : sep, encodedBHPEstimatedSize, w.indexBlockSize, w.indexBlockSizeThreshold,
1404 2 : )
1405 2 :
1406 2 : var indexProps []byte
1407 2 : var flushableIndexBlock *indexBlockBuf
1408 2 : if shouldFlushIndexBlock {
1409 2 : flushableIndexBlock = w.indexBlock
1410 2 : w.indexBlock = newIndexBlockBuf(w.coordination.parallelismEnabled)
1411 2 : // Call BlockPropertyCollector.FinishIndexBlock, since we've decided to
1412 2 : // flush the index block.
1413 2 : indexProps, err = w.finishIndexBlockProps()
1414 2 : if err != nil {
1415 1 : return err
1416 1 : }
1417 : }
1418 :
1419 : // We've called BlockPropertyCollector.FinishDataBlock, and, if necessary,
1420 : // BlockPropertyCollector.FinishIndexBlock. Since we've decided to finish
1421 : // the data block, we can call
1422 : // BlockPropertyCollector.AddPrevDataBlockToIndexBlock.
1423 2 : w.addPrevDataBlockToIndexBlockProps()
1424 2 :
1425 2 : // Schedule a write.
1426 2 : writeTask := writeTaskPool.Get().(*writeTask)
1427 2 : // We're setting compressionDone to indicate that compression of this block
1428 2 : // has already been completed.
1429 2 : writeTask.compressionDone <- true
1430 2 : writeTask.buf = w.dataBlockBuf
1431 2 : writeTask.indexEntrySep = sep
1432 2 : writeTask.currIndexBlock = w.indexBlock
1433 2 : writeTask.indexInflightSize = sep.Size() + encodedBHPEstimatedSize
1434 2 : writeTask.finishedIndexProps = indexProps
1435 2 : writeTask.flushableIndexBlock = flushableIndexBlock
1436 2 :
1437 2 : // The writeTask corresponds to an unwritten index entry.
1438 2 : w.indexBlock.addInflight(writeTask.indexInflightSize)
1439 2 :
1440 2 : w.dataBlockBuf = nil
1441 2 : if w.coordination.parallelismEnabled {
1442 2 : w.coordination.writeQueue.add(writeTask)
1443 2 : } else {
1444 2 : err = w.coordination.writeQueue.addSync(writeTask)
1445 2 : }
1446 2 : w.dataBlockBuf = newDataBlockBuf(w.restartInterval, w.checksumType)
1447 2 :
1448 2 : return err
1449 : }
1450 :
1451 2 : func (w *Writer) maybeFlush(key InternalKey, valueLen int) error {
1452 2 : if !w.dataBlockBuf.shouldFlush(key, valueLen, w.blockSize, w.blockSizeThreshold) {
1453 2 : return nil
1454 2 : }
1455 :
1456 2 : err := w.flush(key)
1457 2 :
1458 2 : if err != nil {
1459 1 : w.err = err
1460 1 : return err
1461 1 : }
1462 :
1463 2 : return nil
1464 : }
1465 :
1466 : // dataBlockBuf.dataBlockProps set by this method must be encoded before any future use of the
1467 : // dataBlockBuf.blockPropsEncoder, since the properties slice will get reused by the
1468 : // blockPropsEncoder.
1469 2 : func (w *Writer) finishDataBlockProps(buf *dataBlockBuf) error {
1470 2 : if len(w.blockPropCollectors) == 0 {
1471 2 : return nil
1472 2 : }
1473 2 : var err error
1474 2 : buf.blockPropsEncoder.resetProps()
1475 2 : for i := range w.blockPropCollectors {
1476 2 : scratch := buf.blockPropsEncoder.getScratchForProp()
1477 2 : if scratch, err = w.blockPropCollectors[i].FinishDataBlock(scratch); err != nil {
1478 1 : return err
1479 1 : }
1480 2 : if len(scratch) > 0 {
1481 2 : buf.blockPropsEncoder.addProp(shortID(i), scratch)
1482 2 : }
1483 : }
1484 :
1485 2 : buf.dataBlockProps = buf.blockPropsEncoder.unsafeProps()
1486 2 : return nil
1487 : }
1488 :
1489 : // The BlockHandleWithProperties returned by this method must be encoded before any future use of
1490 : // the Writer.blockPropsEncoder, since the properties slice will get reused by the blockPropsEncoder.
1491 : // maybeAddBlockPropertiesToBlockHandle should only be called if block is being written synchronously
1492 : // with the Writer client.
1493 : func (w *Writer) maybeAddBlockPropertiesToBlockHandle(
1494 : bh BlockHandle,
1495 2 : ) (BlockHandleWithProperties, error) {
1496 2 : err := w.finishDataBlockProps(w.dataBlockBuf)
1497 2 : if err != nil {
1498 0 : return BlockHandleWithProperties{}, err
1499 0 : }
1500 2 : return BlockHandleWithProperties{BlockHandle: bh, Props: w.dataBlockBuf.dataBlockProps}, nil
1501 : }
1502 :
1503 2 : func (w *Writer) indexEntrySep(prevKey, key InternalKey, dataBlockBuf *dataBlockBuf) InternalKey {
1504 2 : // Make a rough guess that we want key-sized scratch to compute the separator.
1505 2 : if cap(dataBlockBuf.sepScratch) < key.Size() {
1506 2 : dataBlockBuf.sepScratch = make([]byte, 0, key.Size()*2)
1507 2 : }
1508 :
1509 2 : var sep InternalKey
1510 2 : if key.UserKey == nil && key.Trailer == 0 {
1511 2 : sep = prevKey.Successor(w.compare, w.successor, dataBlockBuf.sepScratch[:0])
1512 2 : } else {
1513 2 : sep = prevKey.Separator(w.compare, w.separator, dataBlockBuf.sepScratch[:0], key)
1514 2 : }
1515 2 : return sep
1516 : }
1517 :
1518 : // addIndexEntry adds an index entry for the specified key and block handle.
1519 : // addIndexEntry can be called from both the Writer client goroutine, and the
1520 : // writeQueue goroutine. If the flushIndexBuf != nil, then the indexProps, as
1521 : // they're used when the index block is finished.
1522 : //
1523 : // Invariant:
1524 : // 1. addIndexEntry must not store references to the sep InternalKey, the tmp
1525 : // byte slice, bhp.Props. That is, these must be either deep copied or
1526 : // encoded.
1527 : // 2. addIndexEntry must not hold references to the flushIndexBuf, and the writeTo
1528 : // indexBlockBufs.
1529 : func (w *Writer) addIndexEntry(
1530 : sep InternalKey,
1531 : bhp BlockHandleWithProperties,
1532 : tmp []byte,
1533 : flushIndexBuf *indexBlockBuf,
1534 : writeTo *indexBlockBuf,
1535 : inflightSize int,
1536 : indexProps []byte,
1537 2 : ) error {
1538 2 : if bhp.Length == 0 {
1539 0 : // A valid blockHandle must be non-zero.
1540 0 : // In particular, it must have a non-zero length.
1541 0 : return nil
1542 0 : }
1543 :
1544 2 : encoded := encodeBlockHandleWithProperties(tmp, bhp)
1545 2 :
1546 2 : if flushIndexBuf != nil {
1547 2 : if cap(w.indexPartitions) == 0 {
1548 2 : w.indexPartitions = make([]indexBlockAndBlockProperties, 0, 32)
1549 2 : }
1550 : // Enable two level indexes if there is more than one index block.
1551 2 : w.twoLevelIndex = true
1552 2 : if err := w.finishIndexBlock(flushIndexBuf, indexProps); err != nil {
1553 0 : return err
1554 0 : }
1555 : }
1556 :
1557 2 : writeTo.add(sep, encoded, inflightSize)
1558 2 : return nil
1559 : }
1560 :
1561 2 : func (w *Writer) addPrevDataBlockToIndexBlockProps() {
1562 2 : for i := range w.blockPropCollectors {
1563 2 : w.blockPropCollectors[i].AddPrevDataBlockToIndexBlock()
1564 2 : }
1565 : }
1566 :
1567 : // addIndexEntrySync adds an index entry for the specified key and block handle.
1568 : // Writer.addIndexEntry is only called synchronously once Writer.Close is called.
1569 : // addIndexEntrySync should only be called if we're sure that index entries
1570 : // aren't being written asynchronously.
1571 : //
1572 : // Invariant:
1573 : // 1. addIndexEntrySync must not store references to the prevKey, key InternalKey's,
1574 : // the tmp byte slice. That is, these must be either deep copied or encoded.
1575 : //
1576 : // TODO: Improve coverage of this method. e.g. tests passed without the line
1577 : // `w.twoLevelIndex = true` previously.
1578 : func (w *Writer) addIndexEntrySync(
1579 : prevKey, key InternalKey, bhp BlockHandleWithProperties, tmp []byte,
1580 2 : ) error {
1581 2 : return w.addIndexEntrySep(w.indexEntrySep(prevKey, key, w.dataBlockBuf), bhp, tmp)
1582 2 : }
1583 :
1584 : func (w *Writer) addIndexEntrySep(
1585 : sep InternalKey, bhp BlockHandleWithProperties, tmp []byte,
1586 2 : ) error {
1587 2 : shouldFlush := supportsTwoLevelIndex(
1588 2 : w.tableFormat) && w.indexBlock.shouldFlush(
1589 2 : sep, encodedBHPEstimatedSize, w.indexBlockSize, w.indexBlockSizeThreshold,
1590 2 : )
1591 2 : var flushableIndexBlock *indexBlockBuf
1592 2 : var props []byte
1593 2 : var err error
1594 2 : if shouldFlush {
1595 2 : flushableIndexBlock = w.indexBlock
1596 2 : w.indexBlock = newIndexBlockBuf(w.coordination.parallelismEnabled)
1597 2 : w.twoLevelIndex = true
1598 2 : // Call BlockPropertyCollector.FinishIndexBlock, since we've decided to
1599 2 : // flush the index block.
1600 2 : props, err = w.finishIndexBlockProps()
1601 2 : if err != nil {
1602 0 : return err
1603 0 : }
1604 : }
1605 :
1606 2 : err = w.addIndexEntry(sep, bhp, tmp, flushableIndexBlock, w.indexBlock, 0, props)
1607 2 : if flushableIndexBlock != nil {
1608 2 : flushableIndexBlock.clear()
1609 2 : indexBlockBufPool.Put(flushableIndexBlock)
1610 2 : }
1611 2 : w.addPrevDataBlockToIndexBlockProps()
1612 2 : return err
1613 : }
1614 :
1615 : func shouldFlush(
1616 : key InternalKey,
1617 : valueLen int,
1618 : restartInterval, estimatedBlockSize, numEntries, targetBlockSize, sizeThreshold int,
1619 2 : ) bool {
1620 2 : if numEntries == 0 {
1621 2 : return false
1622 2 : }
1623 :
1624 2 : if estimatedBlockSize >= targetBlockSize {
1625 2 : return true
1626 2 : }
1627 :
1628 : // The block is currently smaller than the target size.
1629 2 : if estimatedBlockSize <= sizeThreshold {
1630 2 : // The block is smaller than the threshold size at which we'll consider
1631 2 : // flushing it.
1632 2 : return false
1633 2 : }
1634 :
1635 2 : newSize := estimatedBlockSize + key.Size() + valueLen
1636 2 : if numEntries%restartInterval == 0 {
1637 2 : newSize += 4
1638 2 : }
1639 2 : newSize += 4 // varint for shared prefix length
1640 2 : newSize += uvarintLen(uint32(key.Size())) // varint for unshared key bytes
1641 2 : newSize += uvarintLen(uint32(valueLen)) // varint for value size
1642 2 : // Flush if the block plus the new entry is larger than the target size.
1643 2 : return newSize > targetBlockSize
1644 : }
1645 :
1646 2 : func cloneKeyWithBuf(k InternalKey, a bytealloc.A) (bytealloc.A, InternalKey) {
1647 2 : if len(k.UserKey) == 0 {
1648 0 : return a, k
1649 0 : }
1650 2 : a, keyCopy := a.Copy(k.UserKey)
1651 2 : return a, InternalKey{UserKey: keyCopy, Trailer: k.Trailer}
1652 : }
1653 :
1654 : // Invariants: The byte slice returned by finishIndexBlockProps is heap-allocated
1655 : //
1656 : // and has its own lifetime, independent of the Writer and the blockPropsEncoder,
1657 : //
1658 : // and it is safe to:
1659 : // 1. Reuse w.blockPropsEncoder without first encoding the byte slice returned.
1660 : // 2. Store the byte slice in the Writer since it is a copy and not supported by
1661 : // an underlying buffer.
1662 2 : func (w *Writer) finishIndexBlockProps() ([]byte, error) {
1663 2 : w.blockPropsEncoder.resetProps()
1664 2 : for i := range w.blockPropCollectors {
1665 2 : scratch := w.blockPropsEncoder.getScratchForProp()
1666 2 : var err error
1667 2 : if scratch, err = w.blockPropCollectors[i].FinishIndexBlock(scratch); err != nil {
1668 1 : return nil, err
1669 1 : }
1670 2 : if len(scratch) > 0 {
1671 2 : w.blockPropsEncoder.addProp(shortID(i), scratch)
1672 2 : }
1673 : }
1674 2 : return w.blockPropsEncoder.props(), nil
1675 : }
1676 :
1677 : // finishIndexBlock finishes the current index block and adds it to the top
1678 : // level index block. This is only used when two level indexes are enabled.
1679 : //
1680 : // Invariants:
1681 : // 1. The props slice passed into finishedIndexBlock must not be a
1682 : // owned by any other struct, since it will be stored in the Writer.indexPartitions
1683 : // slice.
1684 : // 2. None of the buffers owned by indexBuf will be shallow copied and stored elsewhere.
1685 : // That is, it must be safe to reuse indexBuf after finishIndexBlock has been called.
1686 2 : func (w *Writer) finishIndexBlock(indexBuf *indexBlockBuf, props []byte) error {
1687 2 : part := indexBlockAndBlockProperties{
1688 2 : nEntries: indexBuf.block.nEntries, properties: props,
1689 2 : }
1690 2 : w.indexSepAlloc, part.sep = cloneKeyWithBuf(
1691 2 : indexBuf.block.getCurKey(), w.indexSepAlloc,
1692 2 : )
1693 2 : bk := indexBuf.finish()
1694 2 : if len(w.indexBlockAlloc) < len(bk) {
1695 2 : // Allocate enough bytes for approximately 16 index blocks.
1696 2 : w.indexBlockAlloc = make([]byte, len(bk)*16)
1697 2 : }
1698 2 : n := copy(w.indexBlockAlloc, bk)
1699 2 : part.block = w.indexBlockAlloc[:n:n]
1700 2 : w.indexBlockAlloc = w.indexBlockAlloc[n:]
1701 2 : w.indexPartitions = append(w.indexPartitions, part)
1702 2 : return nil
1703 : }
1704 :
1705 2 : func (w *Writer) writeTwoLevelIndex() (BlockHandle, error) {
1706 2 : props, err := w.finishIndexBlockProps()
1707 2 : if err != nil {
1708 0 : return BlockHandle{}, err
1709 0 : }
1710 : // Add the final unfinished index.
1711 2 : if err = w.finishIndexBlock(w.indexBlock, props); err != nil {
1712 0 : return BlockHandle{}, err
1713 0 : }
1714 :
1715 2 : for i := range w.indexPartitions {
1716 2 : b := &w.indexPartitions[i]
1717 2 : w.props.NumDataBlocks += uint64(b.nEntries)
1718 2 :
1719 2 : data := b.block
1720 2 : w.props.IndexSize += uint64(len(data))
1721 2 : bh, err := w.writeBlock(data, w.compression, &w.blockBuf)
1722 2 : if err != nil {
1723 0 : return BlockHandle{}, err
1724 0 : }
1725 2 : bhp := BlockHandleWithProperties{
1726 2 : BlockHandle: bh,
1727 2 : Props: b.properties,
1728 2 : }
1729 2 : encoded := encodeBlockHandleWithProperties(w.blockBuf.tmp[:], bhp)
1730 2 : w.topLevelIndexBlock.add(b.sep, encoded)
1731 : }
1732 :
1733 : // NB: RocksDB includes the block trailer length in the index size
1734 : // property, though it doesn't include the trailer in the top level
1735 : // index size property.
1736 2 : w.props.IndexPartitions = uint64(len(w.indexPartitions))
1737 2 : w.props.TopLevelIndexSize = uint64(w.topLevelIndexBlock.estimatedSize())
1738 2 : w.props.IndexSize += w.props.TopLevelIndexSize + blockTrailerLen
1739 2 :
1740 2 : return w.writeBlock(w.topLevelIndexBlock.finish(), w.compression, &w.blockBuf)
1741 : }
1742 :
1743 2 : func compressAndChecksum(b []byte, compression Compression, blockBuf *blockBuf) []byte {
1744 2 : // Compress the buffer, discarding the result if the improvement isn't at
1745 2 : // least 12.5%.
1746 2 : blockType, compressed := compressBlock(compression, b, blockBuf.compressedBuf)
1747 2 : if blockType != noCompressionBlockType && cap(compressed) > cap(blockBuf.compressedBuf) {
1748 2 : blockBuf.compressedBuf = compressed[:cap(compressed)]
1749 2 : }
1750 2 : if len(compressed) < len(b)-len(b)/8 {
1751 2 : b = compressed
1752 2 : } else {
1753 2 : blockType = noCompressionBlockType
1754 2 : }
1755 :
1756 2 : blockBuf.tmp[0] = byte(blockType)
1757 2 :
1758 2 : // Calculate the checksum.
1759 2 : checksum := blockBuf.checksummer.checksum(b, blockBuf.tmp[:1])
1760 2 : binary.LittleEndian.PutUint32(blockBuf.tmp[1:5], checksum)
1761 2 : return b
1762 : }
1763 :
1764 2 : func (w *Writer) writeCompressedBlock(block []byte, blockTrailerBuf []byte) (BlockHandle, error) {
1765 2 : bh := BlockHandle{Offset: w.meta.Size, Length: uint64(len(block))}
1766 2 :
1767 2 : if w.cacheID != 0 && w.fileNum != 0 {
1768 2 : // Remove the block being written from the cache. This provides defense in
1769 2 : // depth against bugs which cause cache collisions.
1770 2 : //
1771 2 : // TODO(peter): Alternatively, we could add the uncompressed value to the
1772 2 : // cache.
1773 2 : w.cache.Delete(w.cacheID, w.fileNum, bh.Offset)
1774 2 : }
1775 :
1776 : // Write the bytes to the file.
1777 2 : if err := w.writable.Write(block); err != nil {
1778 0 : return BlockHandle{}, err
1779 0 : }
1780 2 : w.meta.Size += uint64(len(block))
1781 2 : if err := w.writable.Write(blockTrailerBuf[:blockTrailerLen]); err != nil {
1782 0 : return BlockHandle{}, err
1783 0 : }
1784 2 : w.meta.Size += blockTrailerLen
1785 2 :
1786 2 : return bh, nil
1787 : }
1788 :
1789 : // Write implements io.Writer. This is analogous to writeCompressedBlock for
1790 : // blocks that already incorporate the trailer, and don't need the callee to
1791 : // return a BlockHandle.
1792 2 : func (w *Writer) Write(blockWithTrailer []byte) (n int, err error) {
1793 2 : offset := w.meta.Size
1794 2 : if w.cacheID != 0 && w.fileNum != 0 {
1795 2 : // Remove the block being written from the cache. This provides defense in
1796 2 : // depth against bugs which cause cache collisions.
1797 2 : //
1798 2 : // TODO(peter): Alternatively, we could add the uncompressed value to the
1799 2 : // cache.
1800 2 : w.cache.Delete(w.cacheID, w.fileNum, offset)
1801 2 : }
1802 2 : w.meta.Size += uint64(len(blockWithTrailer))
1803 2 : if err := w.writable.Write(blockWithTrailer); err != nil {
1804 0 : return 0, err
1805 0 : }
1806 2 : return len(blockWithTrailer), nil
1807 : }
1808 :
1809 : func (w *Writer) writeBlock(
1810 : b []byte, compression Compression, blockBuf *blockBuf,
1811 2 : ) (BlockHandle, error) {
1812 2 : b = compressAndChecksum(b, compression, blockBuf)
1813 2 : return w.writeCompressedBlock(b, blockBuf.tmp[:])
1814 2 : }
1815 :
1816 : // assertFormatCompatibility ensures that the features present on the table are
1817 : // compatible with the table format version.
1818 2 : func (w *Writer) assertFormatCompatibility() error {
1819 2 : // PebbleDBv1: block properties.
1820 2 : if len(w.blockPropCollectors) > 0 && w.tableFormat < TableFormatPebblev1 {
1821 1 : return errors.Newf(
1822 1 : "table format version %s is less than the minimum required version %s for block properties",
1823 1 : w.tableFormat, TableFormatPebblev1,
1824 1 : )
1825 1 : }
1826 :
1827 : // PebbleDBv2: range keys.
1828 2 : if w.props.NumRangeKeys() > 0 && w.tableFormat < TableFormatPebblev2 {
1829 1 : return errors.Newf(
1830 1 : "table format version %s is less than the minimum required version %s for range keys",
1831 1 : w.tableFormat, TableFormatPebblev2,
1832 1 : )
1833 1 : }
1834 :
1835 : // PebbleDBv3: value blocks.
1836 2 : if (w.props.NumValueBlocks > 0 || w.props.NumValuesInValueBlocks > 0 ||
1837 2 : w.props.ValueBlocksSize > 0) && w.tableFormat < TableFormatPebblev3 {
1838 0 : return errors.Newf(
1839 0 : "table format version %s is less than the minimum required version %s for value blocks",
1840 0 : w.tableFormat, TableFormatPebblev3)
1841 0 : }
1842 :
1843 : // PebbleDBv4: DELSIZED tombstones.
1844 2 : if w.props.NumSizedDeletions > 0 && w.tableFormat < TableFormatPebblev4 {
1845 0 : return errors.Newf(
1846 0 : "table format version %s is less than the minimum required version %s for sized deletion tombstones",
1847 0 : w.tableFormat, TableFormatPebblev4)
1848 0 : }
1849 2 : return nil
1850 : }
1851 :
1852 : // Close finishes writing the table and closes the underlying file that the
1853 : // table was written to.
1854 2 : func (w *Writer) Close() (err error) {
1855 2 : defer func() {
1856 2 : if w.valueBlockWriter != nil {
1857 2 : releaseValueBlockWriter(w.valueBlockWriter)
1858 2 : // Defensive code in case Close gets called again. We don't want to put
1859 2 : // the same object to a sync.Pool.
1860 2 : w.valueBlockWriter = nil
1861 2 : }
1862 2 : if w.writable != nil {
1863 1 : w.writable.Abort()
1864 1 : w.writable = nil
1865 1 : }
1866 : // Record any error in the writer (so we can exit early if Close is called
1867 : // again).
1868 2 : if err != nil {
1869 1 : w.err = err
1870 1 : }
1871 : }()
1872 :
1873 : // finish must be called before we check for an error, because finish will
1874 : // block until every single task added to the writeQueue has been processed,
1875 : // and an error could be encountered while any of those tasks are processed.
1876 2 : if err := w.coordination.writeQueue.finish(); err != nil {
1877 1 : return err
1878 1 : }
1879 :
1880 2 : if w.err != nil {
1881 1 : return w.err
1882 1 : }
1883 :
1884 : // The w.meta.LargestPointKey is only used once the Writer is closed, so it is safe to set it
1885 : // when the Writer is closed.
1886 : //
1887 : // The following invariants ensure that setting the largest key at this point of a Writer close
1888 : // is correct:
1889 : // 1. Keys must only be added to the Writer in an increasing order.
1890 : // 2. The current w.dataBlockBuf is guaranteed to have the latest key added to the Writer. This
1891 : // must be true, because a w.dataBlockBuf is only switched out when a dataBlock is flushed,
1892 : // however, if a dataBlock is flushed, then we add a key to the new w.dataBlockBuf in the
1893 : // addPoint function after the flush occurs.
1894 2 : if w.dataBlockBuf.dataBlock.nEntries >= 1 {
1895 2 : w.meta.SetLargestPointKey(w.dataBlockBuf.dataBlock.getCurKey().Clone())
1896 2 : }
1897 :
1898 : // Finish the last data block, or force an empty data block if there
1899 : // aren't any data blocks at all.
1900 2 : if w.dataBlockBuf.dataBlock.nEntries > 0 || w.indexBlock.block.nEntries == 0 {
1901 2 : bh, err := w.writeBlock(w.dataBlockBuf.dataBlock.finish(), w.compression, &w.dataBlockBuf.blockBuf)
1902 2 : if err != nil {
1903 0 : return err
1904 0 : }
1905 2 : bhp, err := w.maybeAddBlockPropertiesToBlockHandle(bh)
1906 2 : if err != nil {
1907 0 : return err
1908 0 : }
1909 2 : prevKey := w.dataBlockBuf.dataBlock.getCurKey()
1910 2 : if err := w.addIndexEntrySync(prevKey, InternalKey{}, bhp, w.dataBlockBuf.tmp[:]); err != nil {
1911 0 : return err
1912 0 : }
1913 : }
1914 2 : w.props.DataSize = w.meta.Size
1915 2 :
1916 2 : // Write the filter block.
1917 2 : var metaindex rawBlockWriter
1918 2 : metaindex.restartInterval = 1
1919 2 : if w.filter != nil {
1920 2 : b, err := w.filter.finish()
1921 2 : if err != nil {
1922 0 : return err
1923 0 : }
1924 2 : bh, err := w.writeBlock(b, NoCompression, &w.blockBuf)
1925 2 : if err != nil {
1926 0 : return err
1927 0 : }
1928 2 : n := encodeBlockHandle(w.blockBuf.tmp[:], bh)
1929 2 : metaindex.add(InternalKey{UserKey: []byte(w.filter.metaName())}, w.blockBuf.tmp[:n])
1930 2 : w.props.FilterPolicyName = w.filter.policyName()
1931 2 : w.props.FilterSize = bh.Length
1932 : }
1933 :
1934 2 : var indexBH BlockHandle
1935 2 : if w.twoLevelIndex {
1936 2 : w.props.IndexType = twoLevelIndex
1937 2 : // Write the two level index block.
1938 2 : indexBH, err = w.writeTwoLevelIndex()
1939 2 : if err != nil {
1940 0 : return err
1941 0 : }
1942 2 : } else {
1943 2 : w.props.IndexType = binarySearchIndex
1944 2 : // NB: RocksDB includes the block trailer length in the index size
1945 2 : // property, though it doesn't include the trailer in the filter size
1946 2 : // property.
1947 2 : w.props.IndexSize = uint64(w.indexBlock.estimatedSize()) + blockTrailerLen
1948 2 : w.props.NumDataBlocks = uint64(w.indexBlock.block.nEntries)
1949 2 :
1950 2 : // Write the single level index block.
1951 2 : indexBH, err = w.writeBlock(w.indexBlock.finish(), w.compression, &w.blockBuf)
1952 2 : if err != nil {
1953 0 : return err
1954 0 : }
1955 : }
1956 :
1957 : // Write the range-del block. The block handle must added to the meta index block
1958 : // after the properties block has been written. This is because the entries in the
1959 : // metaindex block must be sorted by key.
1960 2 : var rangeDelBH BlockHandle
1961 2 : if w.props.NumRangeDeletions > 0 {
1962 2 : if !w.rangeDelV1Format {
1963 2 : // Because the range tombstones are fragmented in the v2 format, the end
1964 2 : // key of the last added range tombstone will be the largest range
1965 2 : // tombstone key. Note that we need to make this into a range deletion
1966 2 : // sentinel because sstable boundaries are inclusive while the end key of
1967 2 : // a range deletion tombstone is exclusive. A Clone() is necessary as
1968 2 : // rangeDelBlock.curValue is the same slice that will get passed
1969 2 : // into w.writer, and some implementations of vfs.File mutate the
1970 2 : // slice passed into Write(). Also, w.meta will often outlive the
1971 2 : // blockWriter, and so cloning curValue allows the rangeDelBlock's
1972 2 : // internal buffer to get gc'd.
1973 2 : k := base.MakeRangeDeleteSentinelKey(w.rangeDelBlock.curValue).Clone()
1974 2 : w.meta.SetLargestRangeDelKey(k)
1975 2 : }
1976 2 : rangeDelBH, err = w.writeBlock(w.rangeDelBlock.finish(), NoCompression, &w.blockBuf)
1977 2 : if err != nil {
1978 0 : return err
1979 0 : }
1980 : }
1981 :
1982 : // Write the range-key block, flushing any remaining spans from the
1983 : // fragmenter first.
1984 2 : w.fragmenter.Finish()
1985 2 :
1986 2 : var rangeKeyBH BlockHandle
1987 2 : if w.props.NumRangeKeys() > 0 {
1988 2 : key := w.rangeKeyBlock.getCurKey()
1989 2 : kind := key.Kind()
1990 2 : endKey, _, ok := rangekey.DecodeEndKey(kind, w.rangeKeyBlock.curValue)
1991 2 : if !ok {
1992 0 : return errors.Newf("invalid end key: %s", w.rangeKeyBlock.curValue)
1993 0 : }
1994 2 : k := base.MakeExclusiveSentinelKey(kind, endKey).Clone()
1995 2 : w.meta.SetLargestRangeKey(k)
1996 2 : // TODO(travers): The lack of compression on the range key block matches the
1997 2 : // lack of compression on the range-del block. Revisit whether we want to
1998 2 : // enable compression on this block.
1999 2 : rangeKeyBH, err = w.writeBlock(w.rangeKeyBlock.finish(), NoCompression, &w.blockBuf)
2000 2 : if err != nil {
2001 0 : return err
2002 0 : }
2003 : }
2004 :
2005 2 : if w.valueBlockWriter != nil {
2006 2 : vbiHandle, vbStats, err := w.valueBlockWriter.finish(w, w.meta.Size)
2007 2 : if err != nil {
2008 0 : return err
2009 0 : }
2010 2 : w.props.NumValueBlocks = vbStats.numValueBlocks
2011 2 : w.props.NumValuesInValueBlocks = vbStats.numValuesInValueBlocks
2012 2 : w.props.ValueBlocksSize = vbStats.valueBlocksAndIndexSize
2013 2 : if vbStats.numValueBlocks > 0 {
2014 2 : n := encodeValueBlocksIndexHandle(w.blockBuf.tmp[:], vbiHandle)
2015 2 : metaindex.add(InternalKey{UserKey: []byte(metaValueIndexName)}, w.blockBuf.tmp[:n])
2016 2 : }
2017 : }
2018 :
2019 : // Add the range key block handle to the metaindex block. Note that we add the
2020 : // block handle to the metaindex block before the other meta blocks as the
2021 : // metaindex block entries must be sorted, and the range key block name sorts
2022 : // before the other block names.
2023 2 : if w.props.NumRangeKeys() > 0 {
2024 2 : n := encodeBlockHandle(w.blockBuf.tmp[:], rangeKeyBH)
2025 2 : metaindex.add(InternalKey{UserKey: []byte(metaRangeKeyName)}, w.blockBuf.tmp[:n])
2026 2 : }
2027 :
2028 2 : {
2029 2 : // Finish and record the prop collectors if props are not yet recorded.
2030 2 : // Pre-computed props might have been copied by specialized sst creators
2031 2 : // like suffix replacer or a span copier.
2032 2 : if len(w.props.UserProperties) == 0 {
2033 2 : userProps := make(map[string]string)
2034 2 : for i := range w.blockPropCollectors {
2035 2 : scratch := w.blockPropsEncoder.getScratchForProp()
2036 2 : // Place the shortID in the first byte.
2037 2 : scratch = append(scratch, byte(i))
2038 2 : buf, err := w.blockPropCollectors[i].FinishTable(scratch)
2039 2 : if err != nil {
2040 1 : return err
2041 1 : }
2042 2 : var prop string
2043 2 : if len(buf) > 0 {
2044 2 : prop = string(buf)
2045 2 : }
2046 : // NB: The property is populated in the map even if it is the
2047 : // empty string, since the presence in the map is what indicates
2048 : // that the block property collector was used when writing.
2049 2 : userProps[w.blockPropCollectors[i].Name()] = prop
2050 : }
2051 2 : if len(userProps) > 0 {
2052 2 : w.props.UserProperties = userProps
2053 2 : }
2054 : }
2055 :
2056 : // Write the properties block.
2057 2 : var raw rawBlockWriter
2058 2 : // The restart interval is set to infinity because the properties block
2059 2 : // is always read sequentially and cached in a heap located object. This
2060 2 : // reduces table size without a significant impact on performance.
2061 2 : raw.restartInterval = propertiesBlockRestartInterval
2062 2 : w.props.CompressionOptions = rocksDBCompressionOptions
2063 2 : w.props.save(w.tableFormat, &raw)
2064 2 : bh, err := w.writeBlock(raw.finish(), NoCompression, &w.blockBuf)
2065 2 : if err != nil {
2066 0 : return err
2067 0 : }
2068 2 : n := encodeBlockHandle(w.blockBuf.tmp[:], bh)
2069 2 : metaindex.add(InternalKey{UserKey: []byte(metaPropertiesName)}, w.blockBuf.tmp[:n])
2070 : }
2071 :
2072 : // Add the range deletion block handle to the metaindex block.
2073 2 : if w.props.NumRangeDeletions > 0 {
2074 2 : n := encodeBlockHandle(w.blockBuf.tmp[:], rangeDelBH)
2075 2 : // The v2 range-del block encoding is backwards compatible with the v1
2076 2 : // encoding. We add meta-index entries for both the old name and the new
2077 2 : // name so that old code can continue to find the range-del block and new
2078 2 : // code knows that the range tombstones in the block are fragmented and
2079 2 : // sorted.
2080 2 : metaindex.add(InternalKey{UserKey: []byte(metaRangeDelName)}, w.blockBuf.tmp[:n])
2081 2 : if !w.rangeDelV1Format {
2082 2 : metaindex.add(InternalKey{UserKey: []byte(metaRangeDelV2Name)}, w.blockBuf.tmp[:n])
2083 2 : }
2084 : }
2085 :
2086 : // Write the metaindex block. It might be an empty block, if the filter
2087 : // policy is nil. NoCompression is specified because a) RocksDB never
2088 : // compresses the meta-index block and b) RocksDB has some code paths which
2089 : // expect the meta-index block to not be compressed.
2090 2 : metaindexBH, err := w.writeBlock(metaindex.blockWriter.finish(), NoCompression, &w.blockBuf)
2091 2 : if err != nil {
2092 0 : return err
2093 0 : }
2094 :
2095 : // Write the table footer.
2096 2 : footer := footer{
2097 2 : format: w.tableFormat,
2098 2 : checksum: w.blockBuf.checksummer.checksumType,
2099 2 : metaindexBH: metaindexBH,
2100 2 : indexBH: indexBH,
2101 2 : }
2102 2 : encoded := footer.encode(w.blockBuf.tmp[:])
2103 2 : if err := w.writable.Write(footer.encode(w.blockBuf.tmp[:])); err != nil {
2104 0 : return err
2105 0 : }
2106 2 : w.meta.Size += uint64(len(encoded))
2107 2 : w.meta.Properties = w.props
2108 2 :
2109 2 : // Check that the features present in the table are compatible with the format
2110 2 : // configured for the table.
2111 2 : if err = w.assertFormatCompatibility(); err != nil {
2112 1 : return err
2113 1 : }
2114 :
2115 2 : if err := w.writable.Finish(); err != nil {
2116 1 : w.writable = nil
2117 1 : return err
2118 1 : }
2119 2 : w.writable = nil
2120 2 :
2121 2 : w.dataBlockBuf.clear()
2122 2 : dataBlockBufPool.Put(w.dataBlockBuf)
2123 2 : w.dataBlockBuf = nil
2124 2 : w.indexBlock.clear()
2125 2 : indexBlockBufPool.Put(w.indexBlock)
2126 2 : w.indexBlock = nil
2127 2 :
2128 2 : // Make any future calls to Set or Close return an error.
2129 2 : w.err = errWriterClosed
2130 2 : return nil
2131 : }
2132 :
2133 : // EstimatedSize returns the estimated size of the sstable being written if a
2134 : // call to Finish() was made without adding additional keys.
2135 2 : func (w *Writer) EstimatedSize() uint64 {
2136 2 : return w.coordination.sizeEstimate.size() +
2137 2 : uint64(w.dataBlockBuf.dataBlock.estimatedSize()) +
2138 2 : w.indexBlock.estimatedSize()
2139 2 : }
2140 :
2141 : // Metadata returns the metadata for the finished sstable. Only valid to call
2142 : // after the sstable has been finished.
2143 2 : func (w *Writer) Metadata() (*WriterMetadata, error) {
2144 2 : if w.writable != nil {
2145 0 : return nil, errors.New("pebble: writer is not closed")
2146 0 : }
2147 2 : return &w.meta, nil
2148 : }
2149 :
2150 : // WriterOption provide an interface to do work on Writer while it is being
2151 : // opened.
2152 : type WriterOption interface {
2153 : // writerApply is called on the writer during opening in order to set
2154 : // internal parameters.
2155 : writerApply(*Writer)
2156 : }
2157 :
2158 : // PreviousPointKeyOpt is a WriterOption that provides access to the last
2159 : // point key written to the writer while building a sstable.
2160 : type PreviousPointKeyOpt struct {
2161 : w *Writer
2162 : }
2163 :
2164 : // UnsafeKey returns the last point key written to the writer to which this
2165 : // option was passed during creation. The returned key points directly into
2166 : // a buffer belonging to the Writer. The value's lifetime ends the next time a
2167 : // point key is added to the Writer.
2168 : // Invariant: UnsafeKey isn't and shouldn't be called after the Writer is closed.
2169 2 : func (o PreviousPointKeyOpt) UnsafeKey() base.InternalKey {
2170 2 : if o.w == nil {
2171 0 : return base.InvalidInternalKey
2172 0 : }
2173 :
2174 2 : if o.w.dataBlockBuf.dataBlock.nEntries >= 1 {
2175 2 : // o.w.dataBlockBuf.dataBlock.curKey is guaranteed to point to the last point key
2176 2 : // which was added to the Writer.
2177 2 : return o.w.dataBlockBuf.dataBlock.getCurKey()
2178 2 : }
2179 0 : return base.InternalKey{}
2180 : }
2181 :
2182 2 : func (o *PreviousPointKeyOpt) writerApply(w *Writer) {
2183 2 : o.w = w
2184 2 : }
2185 :
2186 : // NewWriter returns a new table writer for the file. Closing the writer will
2187 : // close the file.
2188 2 : func NewWriter(writable objstorage.Writable, o WriterOptions, extraOpts ...WriterOption) *Writer {
2189 2 : o = o.ensureDefaults()
2190 2 : w := &Writer{
2191 2 : writable: writable,
2192 2 : meta: WriterMetadata{
2193 2 : SmallestSeqNum: math.MaxUint64,
2194 2 : },
2195 2 : blockSize: o.BlockSize,
2196 2 : blockSizeThreshold: (o.BlockSize*o.BlockSizeThreshold + 99) / 100,
2197 2 : indexBlockSize: o.IndexBlockSize,
2198 2 : indexBlockSizeThreshold: (o.IndexBlockSize*o.BlockSizeThreshold + 99) / 100,
2199 2 : compare: o.Comparer.Compare,
2200 2 : split: o.Comparer.Split,
2201 2 : formatKey: o.Comparer.FormatKey,
2202 2 : compression: o.Compression,
2203 2 : separator: o.Comparer.Separator,
2204 2 : successor: o.Comparer.Successor,
2205 2 : tableFormat: o.TableFormat,
2206 2 : isStrictObsolete: o.IsStrictObsolete,
2207 2 : writingToLowestLevel: o.WritingToLowestLevel,
2208 2 : cache: o.Cache,
2209 2 : restartInterval: o.BlockRestartInterval,
2210 2 : checksumType: o.Checksum,
2211 2 : indexBlock: newIndexBlockBuf(o.Parallelism),
2212 2 : rangeDelBlock: blockWriter{
2213 2 : restartInterval: 1,
2214 2 : },
2215 2 : rangeKeyBlock: blockWriter{
2216 2 : restartInterval: 1,
2217 2 : },
2218 2 : topLevelIndexBlock: blockWriter{
2219 2 : restartInterval: 1,
2220 2 : },
2221 2 : fragmenter: keyspan.Fragmenter{
2222 2 : Cmp: o.Comparer.Compare,
2223 2 : Format: o.Comparer.FormatKey,
2224 2 : },
2225 2 : }
2226 2 : if w.tableFormat >= TableFormatPebblev3 {
2227 2 : w.shortAttributeExtractor = o.ShortAttributeExtractor
2228 2 : w.requiredInPlaceValueBound = o.RequiredInPlaceValueBound
2229 2 : if !o.DisableValueBlocks {
2230 2 : w.valueBlockWriter = newValueBlockWriter(
2231 2 : w.blockSize, w.blockSizeThreshold, w.compression, w.checksumType, func(compressedSize int) {
2232 2 : w.coordination.sizeEstimate.dataBlockCompressed(compressedSize, 0)
2233 2 : })
2234 : }
2235 : }
2236 :
2237 2 : w.dataBlockBuf = newDataBlockBuf(w.restartInterval, w.checksumType)
2238 2 :
2239 2 : w.blockBuf = blockBuf{
2240 2 : checksummer: checksummer{checksumType: o.Checksum},
2241 2 : }
2242 2 :
2243 2 : w.coordination.init(o.Parallelism, w)
2244 2 :
2245 2 : if writable == nil {
2246 0 : w.err = errors.New("pebble: nil writable")
2247 0 : return w
2248 0 : }
2249 :
2250 : // Note that WriterOptions are applied in two places; the ones with a
2251 : // preApply() method are applied here. The rest are applied down below after
2252 : // default properties are set.
2253 2 : type preApply interface{ preApply() }
2254 2 : for _, opt := range extraOpts {
2255 2 : if _, ok := opt.(preApply); ok {
2256 2 : opt.writerApply(w)
2257 2 : }
2258 : }
2259 :
2260 2 : if o.FilterPolicy != nil {
2261 2 : switch o.FilterType {
2262 2 : case TableFilter:
2263 2 : w.filter = newTableFilterWriter(o.FilterPolicy)
2264 0 : default:
2265 0 : panic(fmt.Sprintf("unknown filter type: %v", o.FilterType))
2266 : }
2267 : }
2268 :
2269 2 : w.props.ComparerName = o.Comparer.Name
2270 2 : w.props.CompressionName = o.Compression.String()
2271 2 : w.props.MergerName = o.MergerName
2272 2 : w.props.PropertyCollectorNames = "[]"
2273 2 : w.props.ExternalFormatVersion = rocksDBExternalFormatVersion
2274 2 :
2275 2 : if len(o.BlockPropertyCollectors) > 0 || w.tableFormat >= TableFormatPebblev4 {
2276 2 : var buf bytes.Buffer
2277 2 : buf.WriteString("[")
2278 2 : numBlockPropertyCollectors := len(o.BlockPropertyCollectors)
2279 2 : if w.tableFormat >= TableFormatPebblev4 {
2280 2 : numBlockPropertyCollectors++
2281 2 : }
2282 : // shortID is a uint8, so we cannot exceed that number of block
2283 : // property collectors.
2284 2 : if numBlockPropertyCollectors > math.MaxUint8 {
2285 0 : w.err = errors.New("pebble: too many block property collectors")
2286 0 : return w
2287 0 : }
2288 2 : if numBlockPropertyCollectors > 0 {
2289 2 : w.blockPropCollectors = make([]BlockPropertyCollector, numBlockPropertyCollectors)
2290 2 : }
2291 2 : if len(o.BlockPropertyCollectors) > 0 {
2292 2 : // The shortID assigned to a collector is the same as its index in
2293 2 : // this slice.
2294 2 : for i := range o.BlockPropertyCollectors {
2295 2 : w.blockPropCollectors[i] = o.BlockPropertyCollectors[i]()
2296 2 : if i > 0 {
2297 1 : buf.WriteString(",")
2298 1 : }
2299 2 : buf.WriteString(w.blockPropCollectors[i].Name())
2300 : }
2301 : }
2302 2 : if w.tableFormat >= TableFormatPebblev4 {
2303 2 : if numBlockPropertyCollectors > 1 {
2304 2 : buf.WriteString(",")
2305 2 : }
2306 2 : w.blockPropCollectors[numBlockPropertyCollectors-1] = &w.obsoleteCollector
2307 2 : buf.WriteString(w.obsoleteCollector.Name())
2308 : }
2309 2 : buf.WriteString("]")
2310 2 : w.props.PropertyCollectorNames = buf.String()
2311 : }
2312 :
2313 : // Apply the remaining WriterOptions that do not have a preApply() method.
2314 2 : for _, opt := range extraOpts {
2315 2 : if _, ok := opt.(preApply); ok {
2316 2 : continue
2317 : }
2318 2 : opt.writerApply(w)
2319 : }
2320 :
2321 : // Initialize the range key fragmenter and encoder.
2322 2 : w.fragmenter.Emit = w.encodeRangeKeySpan
2323 2 : w.rangeKeyEncoder.Emit = w.addRangeKey
2324 2 : return w
2325 : }
2326 :
2327 : // internalGetProperties is a private, internal-use-only function that takes a
2328 : // Writer and returns a pointer to its Properties, allowing direct mutation.
2329 : // It's used by internal Pebble flushes and compactions to set internal
2330 : // properties. It gets installed in private.
2331 2 : func internalGetProperties(w *Writer) *Properties {
2332 2 : return &w.props
2333 2 : }
2334 :
2335 2 : func init() {
2336 2 : private.SSTableWriterDisableKeyOrderChecks = func(i interface{}) {
2337 1 : w := i.(*Writer)
2338 1 : w.disableKeyOrderChecks = true
2339 1 : }
2340 2 : private.SSTableInternalProperties = internalGetProperties
2341 : }
|