MySQL 9.2.0
Source Code Documentation
mdl_context_backup.h
Go to the documentation of this file.
1/* Copyright (c) 2018, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef MDL_CONTEXT_BACKUP_H
25#define MDL_CONTEXT_BACKUP_H
26
27#include <cstring>
28#include <map>
29#include <memory>
30
31#include "my_char_traits.h"
33#include "sql/mdl.h"
34
35/**
36 Class which is used to store MDL locks associated with XA transactions
37 in prepared state which clients have disconnected. These locks are not
38 associated with any particular THD/thread. Later they will be retrieved
39 by the thread which attempts to process the prepared XA transaction.
40
41 This is singleton class. It contains map where each element represents
42 MDL_context of disconnected prepared XA transaction and holds related
43 metadata locks.
44*/
45
47 private:
48 /**
49 Key for uniquely identifying MDL_context in the MDL_context_backup map.
50 */
52 std::basic_string<uchar, my_char_traits<uchar>>;
53
55
56 typedef std::map<
57 MDL_context_backup_key, std::unique_ptr<MDL_context_backup>,
58 std::less<MDL_context_backup_key>,
60 std::unique_ptr<MDL_context_backup>>>>
61 Element_map_type; // Real map type.
62
63 /* Singleton. No explicit Objects */
65
66 /* Singleton Object */
68
69 public:
70 /* Not copyable. */
72
73 /* Not assignable. */
74 void operator=(const MDL_context_backup_manager &) = delete;
75
76 /**
77 Initialize member variables and singleton object
78 */
79
80 static bool init();
81
82 /**
83 Return singleton object
84 */
85
87
88 /**
89 Cleanup and delete singleton object
90 */
91
92 static void destroy();
93
94 private:
95 /**
96 destroy mutex and clear backup map
97 */
98
100
101 static void init_psi_keys(void);
102
103 public:
104 /**
105 Create backup from given MDL_context by cloning all transactional
106 locks to backup context and adds to backup context manager collection.
107 This method is used during user's session disconnect for storing MDL
108 locks acquired by prepared XA transaction(s).
109
110 This function does not set error codes beyond what is set by the
111 functions it calls.
112
113 @param[in] context MDL_context from which backup is created.
114 @param[in] key Key to identity MDL_context
115 @param[in] keylen Key Length
116
117 @retval true Error, e.g. Fail to create backup object, fail
118 to clone locks.
119 @retval false Success or a backup already exist for this key.
120 */
121
122 bool create_backup(const MDL_context *context, const uchar *key,
123 const size_t keylen);
124
125 /**
126 Create backup MDL_context, process request on it and add to backup context
127 manager collection. This method is called during server start up in order
128 to acquire MDL locks held by prepared XA transactions existed before server
129 shutdown.
130
131 This function does not set error codes beyond what is set by the
132 functions it calls.
133
134 @param[in] mdl_requests Requests need to be processed and backed up.
135 @param[in] key Key to identity MDL_context
136 @param[in] keylen Key Length
137
138 @retval true Error, e.g. Fail to create backup object, fail
139 to clone locks.
140 @retval false Success or a backup already exist for this key.
141 */
142
143 bool create_backup(MDL_request_list *mdl_requests, const uchar *key,
144 const size_t keylen);
145
146 /**
147 Restore locks from backup to given MDL_context.
148
149 This function does not set error codes beyond what is set by the
150 functions it calls.
151
152 @param[out] mdl_context MDL_context to which backup is restored.
153 @param[in] key Key to identity MDL_context
154 @param[in] keylen Key Length
155
156 @retval true Error, e.g. There is no element in the
157 collection matching given key, fail
158 to restore locks.
159 @retval false Success
160 */
161
162 bool restore_backup(MDL_context *mdl_context, const uchar *key,
163 const size_t keylen);
164
165 /**
166 Delete backup context and release associated locks.
167
168 @param[in] key Key to identity MDL_context
169 @param[in] keylen Key Length
170 */
171
172 void delete_backup(const uchar *key, const size_t keylen);
173
174 private:
175 // Collection for holding MDL_context_backup elements
177
178 // Mutex to protect m_backup_map
180
181 /**
182 Check for presence of a record with specified key
183
184 @param[in] key_obj Key to identity MDL_context
185
186 @return true if there is a record for specified key, else false
187 */
188 bool check_key_exist(const MDL_context_backup_key &key_obj);
189};
190
191#endif // MDL_CONTEXT_BACKUP_H
Intrusive parameterized list.
Definition: sql_plist.h:75
Wrapper around MDL_context class which allows to store it in backup manager's collection.
Definition: mdl_context_backup.cc:37
Class which is used to store MDL locks associated with XA transactions in prepared state which client...
Definition: mdl_context_backup.h:46
bool check_key_exist(const MDL_context_backup_key &key_obj)
Check for presence of a record with specified key.
Definition: mdl_context_backup.cc:151
static void init_psi_keys(void)
Definition: mdl_context_backup.cc:106
~MDL_context_backup_manager()
destroy mutex and clear backup map
Definition: mdl_context_backup.cc:147
void delete_backup(const uchar *key, const size_t keylen)
Delete backup context and release associated locks.
Definition: mdl_context_backup.cc:251
Element_map_type m_backup_map
Definition: mdl_context_backup.h:176
static bool init()
Initialize member variables and singleton object.
Definition: mdl_context_backup.cc:129
static void destroy()
Cleanup and delete singleton object.
Definition: mdl_context_backup.cc:141
MDL_context_backup_manager(const MDL_context_backup_manager &)=delete
bool restore_backup(MDL_context *mdl_context, const uchar *key, const size_t keylen)
Restore locks from backup to given MDL_context.
Definition: mdl_context_backup.cc:233
MDL_context_backup_manager(PSI_memory_key key)
Definition: mdl_context_backup.cc:116
static MDL_context_backup_manager * m_single
Definition: mdl_context_backup.h:67
std::map< MDL_context_backup_key, std::unique_ptr< MDL_context_backup >, std::less< MDL_context_backup_key >, Malloc_allocator< std::pair< const MDL_context_backup_key, std::unique_ptr< MDL_context_backup > > > > Element_map_type
Definition: mdl_context_backup.h:54
bool create_backup(const MDL_context *context, const uchar *key, const size_t keylen)
Create backup from given MDL_context by cloning all transactional locks to backup context and adds to...
Definition: mdl_context_backup.cc:157
mysql_mutex_t m_LOCK_mdl_context_backup
Definition: mdl_context_backup.h:179
std::basic_string< uchar, my_char_traits< uchar > > MDL_context_backup_key
Key for uniquely identifying MDL_context in the MDL_context_backup map.
Definition: mdl_context_backup.h:52
static MDL_context_backup_manager & instance()
Return singleton object.
Definition: mdl_context_backup.cc:136
void operator=(const MDL_context_backup_manager &)=delete
Context of the owner of metadata locks.
Definition: mdl.h:1415
Malloc_allocator is a C++ STL memory allocator based on my_malloc/my_free.
Definition: malloc_allocator.h:63
unsigned int PSI_memory_key
Instrumented memory key.
Definition: psi_memory_bits.h:49
unsigned char uchar
Definition: my_inttypes.h:52
std::map< Key, Value, Compare, ut::allocator< std::pair< const Key, Value > > > map
Specialization of map which uses ut_allocator.
Definition: ut0new.h:2894
required string key
Definition: replication_asynchronous_connection_failover.proto:60
An instrumented mutex structure.
Definition: mysql_mutex_bits.h:50