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