This repository was archived by the owner on Aug 19, 2026. It is now read-only.
forked from redpanda-data/redpanda
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecord_utils.cc
More file actions
340 lines (313 loc) · 11 KB
/
Copy pathrecord_utils.cc
File metadata and controls
340 lines (313 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Copyright 2020 Redpanda Data, Inc.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.md
//
// 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
#include "model/record_utils.h"
#include "hashing/crc32c.h"
#include "model/record.h"
#include "utils/vint.h"
#include <type_traits>
namespace model {
template<typename T, typename = std::enable_if_t<std::is_integral_v<T>, T>>
void crc_extend_cpu_to_le(crc::crc32c& crc, T i) {
auto j = ss::cpu_to_le(i);
crc.extend(j);
}
template<typename... T>
void crc_extend_all_cpu_to_le(crc::crc32c& crc, T... t) {
((crc_extend_cpu_to_le(crc, t)), ...);
}
/// \brief uint32_t because that's what crc32c uses
/// it is *only* record_batch_header.header_crc;
uint32_t internal_header_only_crc(const record_batch_header& header) {
auto c = crc::crc32c();
crc_extend_all_cpu_to_le(
c,
/*Additional fields*/
header.size_bytes,
header.base_offset(),
static_cast<std::underlying_type_t<record_batch_type>>(header.type),
header.crc,
/*Below are same fields as kafka - but at no cost on x86 since they are
hashed as little endian*/
header.attrs.value(),
header.last_offset_delta,
header.first_timestamp.value(),
header.max_timestamp.value(),
header.producer_id,
header.producer_epoch,
header.base_sequence,
header.record_count);
return c.value();
}
template<typename T, typename = std::enable_if_t<std::is_integral_v<T>, T>>
void crc_extend_cpu_to_be(crc::crc32c& crc, T i) {
auto j = ss::cpu_to_be(i);
crc.extend(j);
}
template<typename... T>
void crc_extend_all_cpu_to_be(crc::crc32c& crc, T... t) {
((crc_extend_cpu_to_be(crc, t)), ...);
}
void crc_record_batch_header(
crc::crc32c& crc, const record_batch_header& header) {
crc_extend_all_cpu_to_be(
crc,
header.attrs.value(),
header.last_offset_delta,
header.first_timestamp.value(),
header.max_timestamp.value(),
header.producer_id,
header.producer_epoch,
header.base_sequence,
header.record_count);
}
uint32_t
crc_record_batch(const record_batch_header& hdr, const iobuf& records) {
auto crc = crc::crc32c();
crc_record_batch_header(crc, hdr);
crc_extend_iobuf(crc, records);
return crc.value();
}
uint32_t crc_record_batch(const record_batch& b) {
return crc_record_batch(b.header(), b.data());
}
template<typename Parser, typename ParserData>
static chunked_vector<model::record_header>
parse_record_headers(Parser& parser, ParserData parser_data) {
chunked_vector<model::record_header> headers;
auto [header_count, _] = parser.read_varlong();
/**
* If records buffer is corrupted, it may result in reading very large
* number, check if it is the case and throw an exception.
*/
if (static_cast<size_t>(header_count) > parser.bytes_left()) [[unlikely]] {
throw std::out_of_range(
fmt::format(
"Expected {} headers, but only {} bytes left",
header_count,
parser.bytes_left()));
}
headers.reserve(header_count);
for (int i = 0; i < header_count; ++i) {
auto [key_length, kv] = parser.read_varlong();
iobuf key;
if (key_length > 0) {
key = parser_data(parser, key_length);
}
auto [value_length, vv] = parser.read_varlong();
iobuf value;
if (value_length > 0) {
value = parser_data(parser, value_length);
}
headers.emplace_back(
model::record_header(
key_length, std::move(key), value_length, std::move(value)));
}
return headers;
}
template<typename Parser, typename ParserData>
static model::record do_parse_one_record_from_buffer(
Parser& parser,
int32_t record_size,
model::record_attributes::type attr,
ParserData parser_data) {
auto [timestamp_delta, tv] = parser.read_varlong();
auto [offset_delta, ov] = parser.read_varlong();
auto [key_length, kv] = parser.read_varlong();
iobuf key;
if (key_length > 0) {
if (key_length > record_size) [[unlikely]] {
throw std::out_of_range(
fmt::format(
"Expected key length {} but record has only {} bytes in total",
key_length,
record_size));
}
key = parser_data(parser, key_length);
}
auto [value_length, vv] = parser.read_varlong();
iobuf value;
if (value_length > 0) {
if (value_length > record_size) [[unlikely]] {
throw std::out_of_range(
fmt::format(
"Expected value length {} but record has only {} bytes in "
"total",
value_length,
record_size));
}
value = parser_data(parser, value_length);
}
auto headers = parse_record_headers(parser, parser_data);
return model::record(
record_size,
model::record_attributes(attr),
static_cast<int64_t>(timestamp_delta),
static_cast<int32_t>(offset_delta),
key_length,
std::move(key),
value_length,
std::move(value),
std::move(headers));
}
static std::pair<int64_t, model::record_attributes::type>
parse_record_meta_from_buffer(iobuf_parser_base& parser) {
/*
* require that record attributes be unaffected by endianness. all of the
* other record fields are properly handled by virtue of their types being
* either blobs or variable length integers.
*/
static_assert(
sizeof(model::record_attributes::type) == 1,
"model attributes expected to be one byte");
auto [record_size, rv] = parser.read_varlong();
if (static_cast<size_t>(record_size) > parser.bytes_left()) [[unlikely]] {
throw std::out_of_range(
fmt::format(
"Expected record size {} but only {} bytes left",
record_size,
parser.bytes_left()));
}
auto attr = parser.consume_type<model::record_attributes::type>();
return std::make_pair(record_size, attr);
}
model::record parse_one_record_from_buffer(iobuf_parser& parser) {
auto [record_size, attr] = parse_record_meta_from_buffer(parser);
return do_parse_one_record_from_buffer(
parser, record_size, attr, [](iobuf_parser& parser, int64_t len) {
return parser.share(len);
});
}
model::record parse_one_record_copy_from_buffer(iobuf_const_parser& parser) {
auto [record_size, attr] = parse_record_meta_from_buffer(parser);
return do_parse_one_record_from_buffer(
parser, record_size, attr, [](iobuf_const_parser& parser, int64_t len) {
return parser.copy(len);
});
}
static inline void append_vint_to_iobuf(iobuf& b, int64_t v) {
auto vb = vint::to_bytes(v);
b.append(vb.data(), vb.size());
}
void append_record_to_buffer(iobuf& a, const model::record& r) {
a.reserve_memory(vint::max_length * 6);
append_vint_to_iobuf(a, r.size_bytes());
const auto attrs = ss::cpu_to_be(r.attributes().value());
// NOLINTNEXTLINE
a.append(reinterpret_cast<const char*>(&attrs), sizeof(attrs));
append_vint_to_iobuf(a, r.timestamp_delta());
append_vint_to_iobuf(a, r.offset_delta());
auto key_bytes = std::max(r.key_size(), 0);
auto val_bytes = std::max(r.value_size(), 0);
a.reserve_memory(key_bytes + val_bytes);
append_vint_to_iobuf(a, r.key_size());
if (r.key_size() > 0) {
for (auto& f : r.key()) {
a.append(f.get(), f.size());
}
}
append_vint_to_iobuf(a, r.value_size());
if (r.value_size() > 0) {
for (auto& f : r.value()) {
a.append(f.get(), f.size());
}
}
auto& hdrs = r.headers();
append_vint_to_iobuf(a, hdrs.size());
for (auto& h : hdrs) {
append_vint_to_iobuf(a, h.key_size());
a.reserve_memory(h.memory_usage());
if (h.key_size() > 0) {
for (auto& f : h.key()) {
a.append(f.get(), f.size());
}
}
append_vint_to_iobuf(a, h.value_size());
if (h.value_size() > 0) {
for (auto& f : h.value()) {
a.append(f.get(), f.size());
}
}
}
}
model::record_metadata parse_record_metadata_from_buffer(
iobuf_const_parser& p, bool fully_parse_record) {
auto [record_size, attr] = parse_record_meta_from_buffer(p);
auto [timestamp_delta, tv] = p.read_varlong();
auto [offset_delta, ov] = p.read_varlong();
if (!fully_parse_record) {
auto total_bytes_read = 1 + tv + ov;
if (record_size <= total_bytes_read) [[unlikely]] {
throw std::out_of_range(
fmt::format(
"Expected record size {} to be greater than bytes read {}",
record_size,
total_bytes_read));
}
p.skip(record_size - total_bytes_read);
} else {
auto start = p.bytes_consumed() - (tv + ov);
auto [key_length, kv] = p.read_varlong();
if (key_length > record_size) [[unlikely]] {
throw std::out_of_range(
fmt::format(
"Expected key length {} but record has only {} bytes in total",
key_length,
record_size));
}
if (key_length > 0) {
p.skip(key_length);
}
auto [value_length, vv] = p.read_varlong();
if (value_length > record_size) [[unlikely]] {
throw std::out_of_range(
fmt::format(
"Expected value length {} but record has only {} bytes in "
"total",
value_length,
record_size));
}
if (value_length > 0) {
p.skip(value_length);
}
auto [header_count, hcv] = p.read_varlong();
if (static_cast<size_t>(header_count) > p.bytes_left()) [[unlikely]] {
throw std::out_of_range(
fmt::format(
"Expected {} headers, but only {} bytes left",
header_count,
p.bytes_left()));
}
for (int64_t i = 0; i < header_count; ++i) {
auto [hk_len, hkv] = p.read_varlong();
if (hk_len > 0) {
p.skip(hk_len);
}
auto [hv_len, hvv] = p.read_varlong();
if (hv_len > 0) {
p.skip(hv_len);
}
}
// record_size includes the 1-byte attr already consumed above.
auto bytes_parsed = p.bytes_consumed() - start + 1;
if (bytes_parsed != static_cast<size_t>(record_size)) [[unlikely]] {
throw std::out_of_range(
fmt::format(
"Record size mismatch: expected {} but parsed {} bytes",
record_size,
bytes_parsed));
}
}
return {
static_cast<int32_t>(record_size),
model::record_attributes(attr),
static_cast<int64_t>(timestamp_delta),
static_cast<int32_t>(offset_delta),
};
}
} // namespace model