MySQL 9.0.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
32#include "sql/mdl.h"
33
34/**
35 Class which is used to store MDL locks associated with XA transactions
36 in prepared state which clients have disconnected. These locks are not
37 associated with any particular THD/thread. Later they will be retrieved
38 by the thread which attempts to process the prepared XA transaction.
39
40 This is singleton class. It contains map where each element represents
41 MDL_context of disconnected prepared XA transaction and holds related
42 metadata locks.
43*/
44
46 private:
47 /**
48 Key for uniquely identifying MDL_context in the MDL_context_backup map.
49 */
50 typedef std::basic_string<uchar> MDL_context_backup_key;
51
53
54 typedef std::map<
55 MDL_context_backup_key, std::unique_ptr<MDL_context_backup>,
56 std::less<MDL_context_backup_key>,
58 std::unique_ptr<MDL_context_backup>>>>
59 Element_map_type; // Real map type.
60
61 /* Singleton. No explicit Objects */
63
64 /* Singleton Object */
66
67 public:
68 /* Not copyable. */
70
71 /* Not assignable. */
72 void operator=(const MDL_context_backup_manager &) = delete;
73
74 /**
75 Initialize member variables and singleton object
76 */
77
78 static bool init();
79
80 /**
81 Return singleton object
82 */
83
85
86 /**
87 Cleanup and delete singleton object
88 */
89
90 static void destroy();
91
92 private:
93 /**
94 destroy mutex and clear backup map
95 */
96
98
99 static void init_psi_keys(void);
100
101 public:
102 /**
103 Create backup from given MDL_context by cloning all transactional
104 locks to backup context and adds to backup context manager collection.
105 This method is used during user's session disconnect for storing MDL
106 locks acquired by prepared XA transaction(s).
107
108 This function does not set error codes beyond what is set by the
109 functions it calls.
110
111 @param[in] context MDL_context from which backup is created.
112 @param[in] key Key to identity MDL_context
113 @param[in] keylen Key Length
114
115 @retval true Error, e.g. Fail to create backup object, fail
116 to clone locks.
117 @retval false Success or a backup already exist for this key.
118 */
119
120 bool create_backup(const MDL_context *context, const uchar *key,
121 const size_t keylen);
122
123 /**
124 Create backup MDL_context, process request on it and add to backup context
125 manager collection. This method is called during server start up in order
126 to acquire MDL locks held by prepared XA transactions existed before server
127 shutdown.
128
129 This function does not set error codes beyond what is set by the
130 functions it calls.
131
132 @param[in] mdl_requests Requests need to be processed and backed up.
133 @param[in] key Key to identity MDL_context
134 @param[in] keylen Key Length
135
136 @retval true Error, e.g. Fail to create backup object, fail
137 to clone locks.
138 @retval false Success or a backup already exist for this key.
139 */
140
141 bool create_backup(MDL_request_list *mdl_requests, const uchar *key,
142 const size_t keylen);
143
144 /**
145 Restore locks from backup to given MDL_context.
146
147 This function does not set error codes beyond what is set by the
148 functions it calls.
149
150 @param[out] mdl_context MDL_context to which backup is restored.
151 @param[in] key Key to identity MDL_context
152 @param[in] keylen Key Length
153
154 @retval true Error, e.g. There is no element in the
155 collection matching given key, fail
156 to restore locks.
157 @retval false Success
158 */
159
160 bool restore_backup(MDL_context *mdl_context, const uchar *key,
161 const size_t keylen);
162
163 /**
164 Delete backup context and release associated locks.
165
166 @param[in] key Key to identity MDL_context
167 @param[in] keylen Key Length
168 */
169
170 void delete_backup(const uchar *key, const size_t keylen);
171
172 private:
173 // Collection for holding MDL_context_backup elements
175
176 // Mutex to protect m_backup_map
178
179 /**
180 Check for presence of a record with specified key
181
182 @param[in] key_obj Key to identity MDL_context
183
184 @return true if there is a record for specified key, else false
185 */
186 bool check_key_exist(const MDL_context_backup_key &key_obj);
187};
188
189#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:45
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:174
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:65
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:52
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:177
static MDL_context_backup_manager & instance()
Return singleton object.
Definition: mdl_context_backup.cc:136
std::basic_string< uchar > MDL_context_backup_key
Key for uniquely identifying MDL_context in the MDL_context_backup map.
Definition: mdl_context_backup.h:50
void operator=(const MDL_context_backup_manager &)=delete
Context of the owner of metadata locks.
Definition: mdl.h:1412
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:2893
required string key
Definition: replication_asynchronous_connection_failover.proto:60
An instrumented mutex structure.
Definition: mysql_mutex_bits.h:50