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