-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathhelper.go
251 lines (226 loc) · 8.27 KB
/
helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Copyright 2018 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package row
import (
"context"
"fmt"
"sort"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/settings"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catalogkeys"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/rowenc"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/cockroach/pkg/util/encoding"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/log/eventpb"
)
const (
// maxRowSizeFloor is the lower bound for sql.mutations.max_row_size.{warn|error}.
maxRowSizeFloor = 1 << 10
// maxRowSizeCeil is the upper bound for sql.mutations.max_row_size.{warn|error}.
maxRowSizeCeil = 512 << 20
)
var maxRowSizeWarn = settings.RegisterByteSizeSetting(
"sql.mutations.max_row_size.warn",
"maximum size of row (or column family if multiple column families are in use) that SQL can "+
"write to the KV store, above which an event is logged to SQL_PERF or SQL_INTERNAL_PERF "+
"(setting to 0 disables large row logging)",
kvserver.MaxCommandSizeDefault,
func(size int64) error {
if size != 0 && size < maxRowSizeFloor {
return fmt.Errorf(
"cannot set sql.mutations.max_row_size.warn to %v, must be 0 or >= %v",
size, maxRowSizeFloor,
)
} else if size > maxRowSizeCeil {
return fmt.Errorf(
"cannot set sql.mutations.max_row_size.warn to %v, must be <= %v",
size, maxRowSizeCeil,
)
}
return nil
},
).WithPublic()
// rowHelper has the common methods for table row manipulations.
type rowHelper struct {
Codec keys.SQLCodec
TableDesc catalog.TableDescriptor
// Secondary indexes.
Indexes []catalog.Index
indexEntries []rowenc.IndexEntry
// Computed during initialization for pretty-printing.
primIndexValDirs []encoding.Direction
secIndexValDirs [][]encoding.Direction
// Computed and cached.
primaryIndexKeyPrefix []byte
primaryIndexKeyCols catalog.TableColSet
primaryIndexValueCols catalog.TableColSet
sortedColumnFamilies map[descpb.FamilyID][]descpb.ColumnID
maxRowSizeWarn uint32
internal bool
}
func newRowHelper(
codec keys.SQLCodec,
desc catalog.TableDescriptor,
indexes []catalog.Index,
sv *settings.Values,
internal bool,
) rowHelper {
rh := rowHelper{Codec: codec, TableDesc: desc, Indexes: indexes, internal: internal}
// Pre-compute the encoding directions of the index key values for
// pretty-printing in traces.
rh.primIndexValDirs = catalogkeys.IndexKeyValDirs(rh.TableDesc.GetPrimaryIndex())
rh.secIndexValDirs = make([][]encoding.Direction, len(rh.Indexes))
for i := range rh.Indexes {
rh.secIndexValDirs[i] = catalogkeys.IndexKeyValDirs(rh.Indexes[i])
}
rh.maxRowSizeWarn = uint32(maxRowSizeWarn.Get(sv))
return rh
}
// encodeIndexes encodes the primary and secondary index keys. The
// secondaryIndexEntries are only valid until the next call to encodeIndexes or
// encodeSecondaryIndexes. includeEmpty details whether the results should
// include empty secondary index k/v pairs.
func (rh *rowHelper) encodeIndexes(
colIDtoRowIndex catalog.TableColMap,
values []tree.Datum,
ignoreIndexes util.FastIntSet,
includeEmpty bool,
) (primaryIndexKey []byte, secondaryIndexEntries []rowenc.IndexEntry, err error) {
primaryIndexKey, err = rh.encodePrimaryIndex(colIDtoRowIndex, values)
if err != nil {
return nil, nil, err
}
secondaryIndexEntries, err = rh.encodeSecondaryIndexes(colIDtoRowIndex, values, ignoreIndexes, includeEmpty)
if err != nil {
return nil, nil, err
}
return primaryIndexKey, secondaryIndexEntries, nil
}
// encodePrimaryIndex encodes the primary index key.
func (rh *rowHelper) encodePrimaryIndex(
colIDtoRowIndex catalog.TableColMap, values []tree.Datum,
) (primaryIndexKey []byte, err error) {
if rh.primaryIndexKeyPrefix == nil {
rh.primaryIndexKeyPrefix = rowenc.MakeIndexKeyPrefix(rh.Codec, rh.TableDesc,
rh.TableDesc.GetPrimaryIndexID())
}
primaryIndexKey, _, err = rowenc.EncodeIndexKey(
rh.TableDesc, rh.TableDesc.GetPrimaryIndex(), colIDtoRowIndex, values, rh.primaryIndexKeyPrefix)
return primaryIndexKey, err
}
// encodeSecondaryIndexes encodes the secondary index keys based on a row's
// values.
//
// The secondaryIndexEntries are only valid until the next call to encodeIndexes
// or encodeSecondaryIndexes, when they are overwritten.
//
// This function will not encode index entries for any index with an ID in
// ignoreIndexes.
//
// includeEmpty details whether the results should include empty secondary index
// k/v pairs.
func (rh *rowHelper) encodeSecondaryIndexes(
colIDtoRowIndex catalog.TableColMap,
values []tree.Datum,
ignoreIndexes util.FastIntSet,
includeEmpty bool,
) (secondaryIndexEntries []rowenc.IndexEntry, err error) {
if cap(rh.indexEntries) < len(rh.Indexes) {
rh.indexEntries = make([]rowenc.IndexEntry, 0, len(rh.Indexes))
}
rh.indexEntries = rh.indexEntries[:0]
for i := range rh.Indexes {
index := rh.Indexes[i]
if !ignoreIndexes.Contains(int(index.GetID())) {
entries, err := rowenc.EncodeSecondaryIndex(rh.Codec, rh.TableDesc, index, colIDtoRowIndex, values, includeEmpty)
if err != nil {
return nil, err
}
rh.indexEntries = append(rh.indexEntries, entries...)
}
}
return rh.indexEntries, nil
}
// skipColumnNotInPrimaryIndexValue returns true if the value at column colID
// does not need to be encoded, either because it is already part of the primary
// key, or because it is not part of the primary index altogether. Composite
// datums are considered too, so a composite datum in a PK will return false.
func (rh *rowHelper) skipColumnNotInPrimaryIndexValue(
colID descpb.ColumnID, value tree.Datum,
) (bool, error) {
if rh.primaryIndexKeyCols.Empty() {
rh.primaryIndexKeyCols = rh.TableDesc.GetPrimaryIndex().CollectKeyColumnIDs()
rh.primaryIndexValueCols = rh.TableDesc.GetPrimaryIndex().CollectPrimaryStoredColumnIDs()
}
if !rh.primaryIndexKeyCols.Contains(colID) {
return !rh.primaryIndexValueCols.Contains(colID), nil
}
if cdatum, ok := value.(tree.CompositeDatum); ok {
// Composite columns are encoded in both the key and the value.
return !cdatum.IsComposite(), nil
}
// Skip primary key columns as their values are encoded in the key of
// each family. Family 0 is guaranteed to exist and acts as a
// sentinel.
return true, nil
}
func (rh *rowHelper) sortedColumnFamily(famID descpb.FamilyID) ([]descpb.ColumnID, bool) {
if rh.sortedColumnFamilies == nil {
rh.sortedColumnFamilies = make(map[descpb.FamilyID][]descpb.ColumnID, rh.TableDesc.NumFamilies())
_ = rh.TableDesc.ForeachFamily(func(family *descpb.ColumnFamilyDescriptor) error {
colIDs := append([]descpb.ColumnID{}, family.ColumnIDs...)
sort.Sort(descpb.ColumnIDs(colIDs))
rh.sortedColumnFamilies[family.ID] = colIDs
return nil
})
}
colIDs, ok := rh.sortedColumnFamilies[famID]
return colIDs, ok
}
func (rh *rowHelper) checkRowSize(
ctx context.Context,
key *roachpb.Key,
value *roachpb.Value,
primIndex bool,
secIndex int,
family descpb.FamilyID,
) error {
size := uint32(len(*key)) + uint32(len(value.RawBytes))
if rh.maxRowSizeWarn != 0 && size > rh.maxRowSizeWarn {
valDirs := rh.primIndexValDirs
index := rh.TableDesc.GetPrimaryIndex()
if !primIndex {
valDirs = rh.secIndexValDirs[secIndex]
index = rh.Indexes[secIndex]
}
details := eventpb.CommonLargeRowDetails{
RowSize: size,
MaxRowSize: rh.maxRowSizeWarn,
TableID: uint32(rh.TableDesc.GetID()),
IndexID: uint32(index.GetID()),
FamilyID: uint32(family),
Key: keys.PrettyPrint(valDirs, *key),
}
var event eventpb.EventPayload
if rh.internal {
event = &eventpb.LargeRowInternal{CommonLargeRowDetails: details}
} else {
event = &eventpb.LargeRow{CommonLargeRowDetails: details}
}
log.StructuredEvent(ctx, event)
}
return nil
}