github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/kv/kvserver/kvserverbase/bulk_adder.go (about) 1 // Copyright 2018 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package kvserverbase 12 13 import ( 14 "context" 15 "fmt" 16 17 "github.com/cockroachdb/cockroach/pkg/kv" 18 "github.com/cockroachdb/cockroach/pkg/roachpb" 19 "github.com/cockroachdb/cockroach/pkg/util/hlc" 20 ) 21 22 // BulkAdderOptions is used to configure the behavior of a BulkAdder. 23 type BulkAdderOptions struct { 24 // Name is used in logging messages to identify this adder or the process on 25 // behalf of which it is adding data. 26 Name string 27 28 // SSTSize is the size at which an SST will be flushed and a new one started. 29 // SSTs are also split during a buffer flush to avoid spanning range bounds so 30 // they may be smaller than this limit. 31 SSTSize func() int64 32 33 // SplitAndScatterAfter is the number of bytes which if added without hitting 34 // an existing split will cause the adder to split and scatter the next span. 35 SplitAndScatterAfter func() int64 36 37 // MinBufferSize is the initial size of the BulkAdder buffer. It indicates the 38 // amount of memory we require to be able to buffer data before flushing for 39 // SST creation. 40 MinBufferSize int64 41 42 // BufferSize is the maximum size we can grow the BulkAdder buffer to. 43 MaxBufferSize func() int64 44 45 // StepBufferSize is the increment in which we will attempt to grow the 46 // BulkAdder buffer if the memory monitor permits. 47 StepBufferSize int64 48 49 // SkipLocalDuplicates configures handling of duplicate keys within a local 50 // sorted batch. When true if the same key/value pair is added more than once 51 // subsequent additions will be ignored instead of producing an error. If an 52 // attempt to add the same key has a differnet value, it is always an error. 53 // Once a batch is flushed – explicitly or automatically – local duplicate 54 // detection does not apply. 55 SkipDuplicates bool 56 57 // DisallowShadowing controls whether shadowing of existing keys is permitted 58 // when the SSTables produced by this adder are ingested. 59 DisallowShadowing bool 60 } 61 62 // BulkAdderFactory describes a factory function for BulkAdders. 63 type BulkAdderFactory func( 64 ctx context.Context, db *kv.DB, timestamp hlc.Timestamp, opts BulkAdderOptions, 65 ) (BulkAdder, error) 66 67 // BulkAdder describes a bulk-adding helper that can be used to add lots of KVs. 68 type BulkAdder interface { 69 // Add adds a KV pair to the adder's buffer, potentially flushing if needed. 70 Add(ctx context.Context, key roachpb.Key, value []byte) error 71 // Flush explicitly flushes anything remaining in the adder's buffer. 72 Flush(ctx context.Context) error 73 // IsEmpty returns whether or not this BulkAdder has data buffered. 74 IsEmpty() bool 75 // CurrentBufferFill returns how full the configured buffer is. 76 CurrentBufferFill() float32 77 // GetSummary returns a summary of rows/bytes/etc written by this batcher. 78 GetSummary() roachpb.BulkOpSummary 79 // Close closes the underlying buffers/writers. 80 Close(ctx context.Context) 81 // SetOnFlush sets a callback function called after flushing the buffer. 82 SetOnFlush(func()) 83 } 84 85 // DuplicateKeyError represents a failed attempt to ingest the same key twice 86 // using a BulkAdder within the same batch. 87 type DuplicateKeyError struct { 88 Key roachpb.Key 89 Value []byte 90 } 91 92 func (d *DuplicateKeyError) Error() string { 93 return fmt.Sprintf("duplicate key: %s", d.Key) 94 }