Skip to content

Commit 4874647

Browse files
Merge branch 'myr-handler-buffers' into myr-range-locking
2 parents e763c6b + 59f1fb8 commit 4874647

File tree

4 files changed

+61
-36
lines changed

4 files changed

+61
-36
lines changed

storage/rocksdb/ha_rocksdb.cc

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9351,15 +9351,22 @@ int ha_rocksdb::convert_record_from_storage_format(
93519351
: rc;
93529352
}
93539353

9354+
static constexpr std::size_t get_next_aligned(std::size_t &offset) {
9355+
constexpr auto bits_below_alignment = alignof(std::max_align_t) - 1;
9356+
constexpr auto bits_above_alignment = ~bits_below_alignment;
9357+
9358+
const auto padding =
9359+
((offset + bits_below_alignment) & bits_above_alignment) - offset;
9360+
offset += padding;
9361+
9362+
return padding;
9363+
}
9364+
93549365
int ha_rocksdb::alloc_key_buffers(const TABLE &table_arg,
93559366
const Rdb_tbl_def &tbl_def_arg) {
93569367
DBUG_ENTER_FUNC();
93579368

93589369
std::shared_ptr<Rdb_key_def> *const kd_arr = tbl_def_arg.m_key_descr_arr;
9359-
9360-
uint max_packed_sk_len = 0;
9361-
uint pack_key_len = 0;
9362-
93639370
m_pk_descr = kd_arr[pk_index(table_arg, tbl_def_arg)];
93649371

93659372
// move this into get_table_handler() ??
@@ -9368,12 +9375,11 @@ int ha_rocksdb::alloc_key_buffers(const TABLE &table_arg,
93689375
return rtn;
93699376
}
93709377

9371-
pack_key_len = m_pk_descr->max_storage_fmt_length();
9372-
m_pk_packed_tuple = reinterpret_cast<uchar *>(
9373-
my_malloc(PSI_NOT_INSTRUMENTED, pack_key_len, MYF(0)));
9378+
const auto pack_key_len = m_pk_descr->max_storage_fmt_length();
9379+
auto buf_size = static_cast<std::size_t>(pack_key_len);
93749380

93759381
/* Sometimes, we may use m_sk_packed_tuple for storing packed PK */
9376-
max_packed_sk_len = pack_key_len;
9382+
auto max_packed_sk_len = pack_key_len;
93779383
for (uint i = 0; i < table_arg.s->keys; i++) {
93789384
/* Primary key was processed above */
93799385
if (i == table_arg.s->primary_key) continue;
@@ -9390,48 +9396,57 @@ int ha_rocksdb::alloc_key_buffers(const TABLE &table_arg,
93909396
}
93919397
}
93929398

9393-
m_sk_packed_tuple = reinterpret_cast<uchar *>(
9394-
my_malloc(PSI_NOT_INSTRUMENTED, max_packed_sk_len, MYF(0)));
9395-
m_sk_packed_tuple_old = reinterpret_cast<uchar *>(
9396-
my_malloc(PSI_NOT_INSTRUMENTED, max_packed_sk_len, MYF(0)));
9397-
m_sk_packed_tuple_updated = reinterpret_cast<uchar *>(
9398-
my_malloc(PSI_NOT_INSTRUMENTED, max_packed_sk_len, MYF(0)));
9399-
m_end_key_packed_tuple = reinterpret_cast<uchar *>(
9400-
my_malloc(PSI_NOT_INSTRUMENTED, max_packed_sk_len, MYF(0)));
9401-
m_pack_buffer = reinterpret_cast<uchar *>(
9402-
my_malloc(PSI_NOT_INSTRUMENTED, max_packed_sk_len, MYF(0)));
9399+
const auto pad1 [[maybe_unused]] = get_next_aligned(buf_size);
9400+
const auto m_sk_packed_tuple_offset = buf_size;
9401+
buf_size += max_packed_sk_len;
9402+
9403+
const auto pad2 [[maybe_unused]] = get_next_aligned(buf_size);
9404+
const auto m_sk_packed_tuple_old_offset = buf_size;
9405+
buf_size += max_packed_sk_len;
9406+
9407+
const auto pad3 [[maybe_unused]] = get_next_aligned(buf_size);
9408+
const auto m_sk_packed_tuple_updated_offset = buf_size;
9409+
buf_size += max_packed_sk_len;
94039410

9404-
if (m_pk_packed_tuple == nullptr || m_sk_packed_tuple == nullptr ||
9405-
m_sk_packed_tuple_old == nullptr ||
9406-
m_sk_packed_tuple_updated == nullptr ||
9407-
m_end_key_packed_tuple == nullptr || m_pack_buffer == nullptr) {
9408-
// One or more of the above allocations failed. Clean up and exit
9411+
const auto pad4 [[maybe_unused]] = get_next_aligned(buf_size);
9412+
const auto m_end_key_packed_tuple_offset = buf_size;
9413+
buf_size += max_packed_sk_len;
9414+
9415+
const auto pad5 [[maybe_unused]] = get_next_aligned(buf_size);
9416+
const auto m_pack_buffer_offset = buf_size;
9417+
buf_size += max_packed_sk_len;
9418+
9419+
buffers.reset(static_cast<uchar *>(
9420+
my_malloc(PSI_NOT_INSTRUMENTED, buf_size, MYF(0))));
9421+
if (buffers == nullptr) {
94099422
free_key_buffers();
94109423

94119424
DBUG_RETURN(HA_ERR_OUT_OF_MEM);
94129425
}
94139426

9427+
m_pk_packed_tuple = buffers.get();
9428+
MEM_NOACCESS(m_pk_packed_tuple + pack_key_len, pad1);
9429+
m_sk_packed_tuple = buffers.get() + m_sk_packed_tuple_offset;
9430+
MEM_NOACCESS(m_sk_packed_tuple + max_packed_sk_len, pad2);
9431+
m_sk_packed_tuple_old = buffers.get() + m_sk_packed_tuple_old_offset;
9432+
MEM_NOACCESS(m_sk_packed_tuple_old + max_packed_sk_len, pad3);
9433+
m_sk_packed_tuple_updated = buffers.get() + m_sk_packed_tuple_updated_offset;
9434+
MEM_NOACCESS(m_sk_packed_tuple_updated + max_packed_sk_len, pad4);
9435+
m_end_key_packed_tuple = buffers.get() + m_end_key_packed_tuple_offset;
9436+
MEM_NOACCESS(m_end_key_packed_tuple + max_packed_sk_len, pad5);
9437+
m_pack_buffer = buffers.get() + m_pack_buffer_offset;
9438+
94149439
DBUG_RETURN(HA_EXIT_SUCCESS);
94159440
}
94169441

94179442
void ha_rocksdb::free_key_buffers() {
9418-
my_free(m_pk_packed_tuple);
94199443
m_pk_packed_tuple = nullptr;
9420-
9421-
my_free(m_sk_packed_tuple);
94229444
m_sk_packed_tuple = nullptr;
9423-
9424-
my_free(m_sk_packed_tuple_old);
94259445
m_sk_packed_tuple_old = nullptr;
9426-
9427-
my_free(m_sk_packed_tuple_updated);
94289446
m_sk_packed_tuple_updated = nullptr;
9429-
9430-
my_free(m_end_key_packed_tuple);
94319447
m_end_key_packed_tuple = nullptr;
9432-
9433-
my_free(m_pack_buffer);
94349448
m_pack_buffer = nullptr;
9449+
buffers.reset();
94359450

94369451
release_blob_buffer();
94379452
}

storage/rocksdb/ha_rocksdb.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,14 @@ class ha_rocksdb : public my_core::handler, public blob_buffer {
195195
*/
196196
mutable bool m_pk_can_be_decoded;
197197

198+
// The common buffer for m_pk_packed_tuple, m_sk_packed_tuple,
199+
// m_sk_packed_tuple_old, m_sk_packed_tuple_updated, m_end_key_packed_tuple,
200+
// & m_pack_buffer.
201+
unique_ptr_my_free<uchar[]> buffers;
202+
// ^^ todo: change it to 'char[]'?
203+
198204
uchar *m_pk_packed_tuple; /* Buffer for storing PK in StorageFormat */
199-
// ^^ todo: change it to 'char*'? TODO: ^ can we join this with last_rowkey?
205+
// TODO: ^ can we join this with last_rowkey?
200206

201207
/*
202208
Temporary buffers for storing the key part of the Key/Value pair

storage/rocksdb/rdb_datadic.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,6 +1320,8 @@ uint Rdb_key_def::pack_record(const TABLE *const tbl, uchar *const pack_buffer,
13201320
assert_IMP(should_store_row_debug_checksums,
13211321
(m_index_type == INDEX_TYPE_SECONDARY));
13221322

1323+
MEM_UNDEFINED(pack_buffer, max_storage_fmt_length());
1324+
13231325
uchar *tuple = packed_tuple;
13241326
size_t unpack_start_pos = size_t(-1);
13251327
size_t unpack_len_pos = size_t(-1);
@@ -1471,6 +1473,8 @@ uint Rdb_key_def::pack_record(const TABLE *const tbl, uchar *const pack_buffer,
14711473

14721474
assert(is_storage_available(tuple - packed_tuple, 0));
14731475

1476+
MEM_NOACCESS(pack_buffer, max_storage_fmt_length());
1477+
14741478
return tuple - packed_tuple;
14751479
}
14761480

storage/rocksdb/rdb_iterator.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ int Rdb_iterator_base::convert_iterator_status() const {
418418
if (s.ok() || s.IsNotFound()) return HA_ERR_END_OF_FILE;
419419

420420
auto &tx = *get_tx_from_thd(m_thd);
421-
return rdb_tx_set_status_error(tx, s, *m_kd, m_tbl_def);
421+
return rdb_tx_set_status_error(tx, s, m_kd, m_tbl_def);
422422
}
423423

424424
int Rdb_iterator_base::get(const rocksdb::Slice *key,

0 commit comments

Comments
 (0)