MySQL 8.3.0
Source Code Documentation
sharded_kv_store.h
Go to the documentation of this file.
1/* Copyright (c) 2020, 2023, Oracle and/or its affiliates.
2
3This program is free software; you can redistribute it and/or modify it under
4the terms of the GNU General Public License, version 2.0, as published by the
5Free Software Foundation.
6
7This program is also distributed with certain software (including but not
8limited to OpenSSL) that is licensed under separate terms, as designated in a
9particular file or component or in included license documentation. The authors
10of MySQL hereby grant you an additional permission to link the program and
11your derivative works with the separately licensed software that they have
12included with MySQL.
13
14This program is distributed in the hope that it will be useful, but WITHOUT
15ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
17for more details.
18
19You should have received a copy of the GNU General Public License along with
20this program; if not, write to the Free Software Foundation, Inc.,
2151 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23/** @file storage/temptable/include/temptable/sharded_kv_store.h
24TempTable sharded key-value store implementation. */
25
26#ifndef TEMPTABLE_SHARDED_KV_STORE_H
27#define TEMPTABLE_SHARDED_KV_STORE_H
28
29#include <array>
30
34
35namespace temptable {
36
37/** Sharded key-value store, a convenience wrapper class around Key_value_store
38 * that creates N_SHARDS instances of Key_value_store and exposes a simple
39 * interface to further manipulate with them. User code, if it wishes so, can
40 * opt-in for different key-value implementation or different locking
41 * mechanisms. Defaults are provided as convenience.
42 *
43 * Mapping between a shard and an actual Key_value_store is done at the level of
44 * implementation detail of this class and it is done via modulo-arithmethic on
45 * THD (connection) identifier. It is guaranteed that the same shard (same
46 * Key_value_store instance) will always be returned given the same input: THD
47 * (connection) identifier. However, there is no guarantee that there will be
48 * only one such mapping (this function is not a bijection).
49 *
50 * In other words, due to the wrap-around property of modulo-arithmetic
51 * and depending on the actual value of N_SHARDS, it is very much possible to
52 * get a reference to the same Key_value_store instance for two different THD
53 * (connection) identifiers. Thread-safety guarantees are exercised at the
54 * level of Key_value_store instance but are controlled through the `Lock`, this
55 * class template parameter.
56 *
57 * Due to aforementioned modulo-arithmethic, and the questionable performance in
58 * its more general form, N_SHARDS must be a number which is a power of two.
59 * This enables us to implement a modulo operation in single bitwise
60 * instruction. Check whether N_SHARDS is a number which is power of two is run
61 * during the compile-time.
62 * */
63template <size_t N_SHARDS, typename Lock = std::shared_timed_mutex,
64 template <typename...> class KeyValueImpl = std::unordered_map>
66 : public Sharded_key_value_store_logger<Sharded_key_value_store<N_SHARDS>,
67 DEBUG_BUILD> {
68 /** Do not break encapsulation when using CRTP. */
71
72 /** Check whether all compile-time pre-conditions are set. */
73 static_assert(N_SHARDS && !(N_SHARDS & (N_SHARDS - 1)),
74 "N_SHARDS template parameter must be a power of two.");
75
76 /** Bitmask which enables us to implement modulo-arithmetic operation in
77 * single bitwise instruction. */
78 static constexpr size_t MODULO_MASK = N_SHARDS - 1;
79
80 /** In the event of inability to express ourselves with something like
81 * std::array<alignas<N> Key_value_store<...>> we have to fallback to this
82 * method.
83 * */
86 };
87
88 /** N_SHARDS instances of Key_value_store's. */
89 std::array<L1_dcache_aligned_kv_store, N_SHARDS> m_kv_store_shard;
90
91 public:
92 /** Returns a reference to one of the underlying Key_value_store instances.
93 *
94 * [in] THD (connection) identifier.
95 * @return A reference to Key_value_store instance. */
97 return m_kv_store_shard[thd_id & MODULO_MASK].shard;
98 }
99};
100
101} // namespace temptable
102
103#endif /* TEMPTABLE_SHARDED_KV_STORE_H */
Key-value store, a convenience wrapper class which models a thread-safe dictionary type.
Definition: kv_store.h:60
Sharded key-value store, a convenience wrapper class around Key_value_store that creates N_SHARDS ins...
Definition: sharded_kv_store.h:67
std::array< L1_dcache_aligned_kv_store, N_SHARDS > m_kv_store_shard
N_SHARDS instances of Key_value_store's.
Definition: sharded_kv_store.h:89
Key_value_store< Lock, KeyValueImpl > & operator[](size_t thd_id)
Returns a reference to one of the underlying Key_value_store instances.
Definition: sharded_kv_store.h:96
static constexpr size_t MODULO_MASK
Check whether all compile-time pre-conditions are set.
Definition: sharded_kv_store.h:78
TempTable key-value store implementation.
Definition: allocator.h:44
constexpr bool DEBUG_BUILD
Store build-type information into the constexpr expression.
Definition: constants.h:75
constexpr size_t L1_DCACHE_SIZE
Store L1-dcache size information into the constexpr expression.
Definition: constants.h:81
std::unordered_map< Key, Value, Hash, Key_equal, ut::allocator< std::pair< const Key, Value > > > unordered_map
Definition: ut0new.h:2897
TempTable sharded key-value store logger implementation.
TempTable constants.
In the event of inability to express ourselves with something like std::array<alignas<N> Key_value_st...
Definition: sharded_kv_store.h:84
Key_value_store< Lock, KeyValueImpl > shard
Definition: sharded_kv_store.h:85
Default Sharded_key_value_store logging facility which turns to no-op in non-debug builds.
Definition: sharded_kv_store_logger.h:41