MySQL  8.0.16
Source Code Documentation
opt_costconstantcache.h
Go to the documentation of this file.
1 #ifndef OPT_COSTCONSTANTCACHE_INCLUDED
2 #define OPT_COSTCONSTANTCACHE_INCLUDED
3 
4 /*
5  Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License, version 2.0,
9  as published by the Free Software Foundation.
10 
11  This program is also distributed with certain software (including
12  but not limited to OpenSSL) that is licensed under separate terms,
13  as designated in a particular file or component or in included license
14  documentation. The authors of MySQL hereby grant you an additional
15  permission to link the program and your derivative works with the
16  separately licensed software that they have included with MySQL.
17 
18  This program is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  GNU General Public License, version 2.0, for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with this program; if not, write to the Free Software
25  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
26 
27 #include <stddef.h>
28 
29 #include "my_dbug.h"
31 #include "mysql/psi/mysql_mutex.h"
32 #include "sql/opt_costconstants.h" // Cost_model_constants
33 
34 /**
35  This class implements a cache for "cost constant sets". This cache
36  is responsible for creating the set of cost constant, giving new
37  sessions access to the latest versions of the cost constants, and
38  for re-reading the cost constant tables in the case where these have
39  been updated.
40 
41  The cost constant cache keeps a copy of the current set of cost
42  constants. Each time a new session initializes its Cost_model_server
43  object (by calling Cost_model_server::init() in lex_start()), the
44  Cost_model_server object will request the cost constant cache to
45  give it the current version of the cost constants. This is done by
46  calling Cost_constant_cache::get_cost_constants(). This function
47  will just return a pointer to the current set of cost constants. As
48  time goes, new cost constant sets might be created and added to the
49  cost constant cache. In order to know when a cost constant set can
50  be deleted, reference counting is used. Each time a session asks for
51  the cost constants, the reference counter is incremented. When the
52  session releases the cost constant set by calling
53  @c release_cost_constants(), the reference counter will be
54  decremented. When the reference counter becomes zero, the cost
55  constant set is deleted.
56 */
57 
59  public:
60  /**
61  Creates an empty cost constant cache. To initialize it with default
62  cost constants, @c init() must be called. To use cost constants from
63  the cost constant tables, @c reload() must be called.
64  */
66 
67  /**
68  Destructor for the cost constant cache. Before the cost constant cache
69  is deleted, @c close() must have been called.
70  */
72 
73  /**
74  Initialize the cost module.
75 
76  The cost constants will be initialized with the default values found in
77  the source code. To start using the cost constant values found in
78  the configuration tables, the @c reload() function must be called.
79  */
80 
81  void init();
82 
83  /**
84  Close the cost constant cache.
85 
86  All resources owned by the cost constant cache are released.
87  */
88 
89  void close();
90 
91  /**
92  Reload all cost constants from the configuration tables.
93  */
94 
95  void reload();
96 
97  /**
98  Get the currently used set of cost constants.
99 
100  This function will just return a pointer to a shared version of the
101  cost constants. For tracking of how many sessions that is using the
102  set and to be able to know when it is safe to delete the cost constant
103  object, reference counting is used. This function will increase the
104  ref count for the returned cost constant object. To decrease the reference
105  counter when the cost constants are no longer used,
106  @c release_cost_constants() must be called.
107 
108  @note To ensure that the reference counter is only incremented once for
109  each session that uses the cost constant set, this function should only
110  be called once per session.
111 
112  @return pointer to the cost constants
113  */
114 
117 
118  // Increase the ref count on the cost constant object
120 
122 
123  return current_cost_constants;
124  }
125 
126  /**
127  Releases the cost constant set.
128 
129  This will decrement the reference counter on the cost constant set and
130  if nobody is using it, it will be deleted. This function should be
131  called each time a client (a session) no longer has any use for a
132  cost constant set that it has previously gotten from calling
133  @c get_cost_constants()
134 
135  @param cost_constants pointer to the cost constant set
136  */
137 
138  void release_cost_constants(const Cost_model_constants *cost_constants) {
139  DBUG_ASSERT(cost_constants != NULL);
140 
141  /*
142  The reason for using a const cast here is to be able to keep
143  the cost constant object const outside of this module.
144  */
145  Cost_model_constants *cost =
146  const_cast<Cost_model_constants *>(cost_constants);
147 
149 
150  const unsigned int ref_count = cost->dec_ref_count();
151 
153 
154  // If none is using these cost constants then delete them
155  if (ref_count == 0) delete cost;
156  }
157 
158  private:
159  /**
160  Create default cost constants.
161 
162  This will create cost constants based on default values defined in the
163  source code.
164  */
165 
167 
168  /**
169  Replace the current cost constants with a new set of cost constants.
170 
171  @param new_cost_constants the new cost constants
172  */
173 
174  void update_current_cost_constants(Cost_model_constants *new_cost_constants);
175 
176  /**
177  The current set of cost constants that will be used by new sessions.
178  */
180 
181  /**
182  Mutex protecting the pointer to the current cost constant set and
183  reference counting on all cost constant sets.
184  */
186 
187  bool m_inited;
188 };
189 
190 /**
191  Initializes the optimizer cost module. This should be done during
192  startup from mysqld.cc.
193 */
194 
195 void init_optimizer_cost_module(bool enable_plugins);
196 
197 /**
198  Deletes the optimizer cost module. This should be called when
199  the server stops to release allocated resources.
200 */
201 
203 
204 /**
205  Reloads the optimizer cost constants from the cost constant tables.
206 
207  @note In order to read the cost constant tables, a THD is needed. This
208  function will create a new temporary THD that will be used for
209  this. In case the caller already has a THD this will not be used.
210 */
212 
213 #endif /* OPT_COSTCONSTANTCACHE_INCLUDED */
void close()
Close the cost constant cache.
Definition: opt_costconstantcache.cc:89
Set of all cost constants used by the server and all storage engines.
Definition: opt_costconstants.h:415
Instrumentation helpers for mutexes.
This class implements a cache for "cost constant sets".
Definition: opt_costconstantcache.h:58
void inc_ref_count()
Increment the reference counter for this cost constant set.
Definition: opt_costconstants.h:490
Sergei Dialog Client Authentication NULL
Definition: dialog.cc:352
void delete_optimizer_cost_module()
Deletes the optimizer cost module.
Definition: opt_costconstantcache.cc:449
const Cost_model_constants * get_cost_constants()
Get the currently used set of cost constants.
Definition: opt_costconstantcache.h:115
void reload_optimizer_cost_constants()
Reloads the optimizer cost constants from the cost constant tables.
Definition: opt_costconstantcache.cc:457
Cost_constant_cache()
Creates an empty cost constant cache.
Definition: opt_costconstantcache.cc:61
void init()
Initialize the cost module.
Definition: opt_costconstantcache.cc:70
void update_current_cost_constants(Cost_model_constants *new_cost_constants)
Replace the current cost constants with a new set of cost constants.
Definition: opt_costconstantcache.cc:136
Cost_model_constants * current_cost_constants
The current set of cost constants that will be used by new sessions.
Definition: opt_costconstantcache.h:179
void init_optimizer_cost_module(bool enable_plugins)
Initializes the optimizer cost module.
Definition: opt_costconstantcache.cc:438
#define DBUG_ASSERT(A)
Definition: my_dbug.h:128
Cost_model_constants * create_defaults() const
Create default cost constants.
Definition: opt_costconstantcache.cc:129
bool m_inited
Definition: opt_costconstantcache.h:187
~Cost_constant_cache()
Destructor for the cost constant cache.
Definition: opt_costconstantcache.cc:64
void reload()
Reload all cost constants from the configuration tables.
Definition: opt_costconstantcache.cc:113
An instrumented mutex structure.
Definition: mysql_mutex_bits.h:49
ABI for instrumented mutexes.
#define mysql_mutex_unlock(M)
Definition: mysql_mutex.h:53
#define mysql_mutex_lock(M)
Definition: mysql_mutex.h:44
unsigned int dec_ref_count()
Decrement the reference counter for this cost constant set.
Definition: opt_costconstants.h:501
void release_cost_constants(const Cost_model_constants *cost_constants)
Releases the cost constant set.
Definition: opt_costconstantcache.h:138
mysql_mutex_t LOCK_cost_const
Mutex protecting the pointer to the current cost constant set and reference counting on all cost cons...
Definition: opt_costconstantcache.h:185