| 1 | /* |
| 2 | Copyright (c) 2014, 2026, Oracle and/or its affiliates. |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU General Public License, version 2.0, |
| 6 | as published by the Free Software Foundation. |
| 7 | |
| 8 | This program is designed to work with certain software (including |
| 9 | but not limited to OpenSSL) that is licensed under separate terms, |
| 10 | as designated in a particular file or component or in included license |
| 11 | documentation. The authors of MySQL hereby grant you an additional |
| 12 | permission to link the program and your derivative works with the |
| 13 | separately licensed software that they have either included with |
| 14 | the program or referenced in the documentation. |
| 15 | |
| 16 | This program is distributed in the hope that it will be useful, |
| 17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | GNU General Public License, version 2.0, for more details. |
| 20 | |
| 21 | You should have received a copy of the GNU General Public License |
| 22 | along with this program; if not, write to the Free Software |
| 23 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 24 | |
| 25 | #include <gtest/gtest.h> |
| 26 | #include <stddef.h> |
| 27 | #include <sys/types.h> |
| 28 | |
| 29 | #include "lex_string.h" |
| 30 | #include "sql/item.h" |
| 31 | #include "sql/opt_costmodel.h" |
| 32 | #include "unittest/gunit/fake_table.h" |
| 33 | #include "unittest/gunit/test_utils.h" |
| 34 | |
| 35 | namespace costconstants_unittest { |
| 36 | |
| 37 | using my_testing::Server_initializer; |
| 38 | |
| 39 | /* |
| 40 | Default values for cost constants. These needs to be updated when |
| 41 | cost constants in opt_costconstans.h are changed. |
| 42 | */ |
| 43 | |
| 44 | // Default value for Server_cost_constants::ROW_EVALUATE_COST |
| 45 | const double default_row_evaluate_cost = 0.1; |
| 46 | |
| 47 | // Default value for Server_cost_constants::KEY_COMPARE_COST |
| 48 | const double default_key_compare_cost = 0.05; |
| 49 | |
| 50 | // Default value for Server_cost_constants::HEAP_TEMPTABLE_CREATE_COST |
| 51 | const double default_memory_temptable_create_cost = 1.0; |
| 52 | |
| 53 | // Default value for Server_cost_constants::HEAP_TEMPTABLE_ROW_COST |
| 54 | const double default_memory_temptable_row_cost = 0.1; |
| 55 | |
| 56 | // Default value for Server_cost_constants::DISK_TEMPTABLE_CREATE_COST |
| 57 | const double default_disk_temptable_create_cost = 20.0; |
| 58 | |
| 59 | // Default value for Server_cost_constants::DISK_TEMPTABLE_ROW_COST |
| 60 | const double default_disk_temptable_row_cost = 0.5; |
| 61 | |
| 62 | // Default value SE_cost_constants::MEMORY_BLOCK_READ_COST |
| 63 | const double default_memory_block_read_cost = 0.25; |
| 64 | |
| 65 | // Default value SE_cost_constants::IO_BLOCK_READ_COST |
| 66 | const double default_io_block_read_cost = 1.0; |
| 67 | |
| 68 | class CostConstantsTest : public ::testing::Test { |
| 69 | protected: |
| 70 | void SetUp() override { |
| 71 | initializer.SetUp(); |
| 72 | |
| 73 | // Initilize some storage engines |
| 74 | LEX_CSTRING engine_name0 = {STRING_WITH_LEN("InnoDB0")}; |
| 75 | LEX_CSTRING engine_name1 = {STRING_WITH_LEN("InnoDB1")}; |
| 76 | LEX_CSTRING engine_name2 = {STRING_WITH_LEN("InnoDB2")}; |
| 77 | LEX_CSTRING engine_name4 = {STRING_WITH_LEN("InnoDB4")}; |
| 78 | LEX_CSTRING engine_name7 = {STRING_WITH_LEN("InnoDB7")}; |
| 79 | |
| 80 | insert_hton2plugin(0, new st_plugin_int())->name = engine_name0; |
| 81 | insert_hton2plugin(1, new st_plugin_int())->name = engine_name1; |
| 82 | insert_hton2plugin(2, new st_plugin_int())->name = engine_name2; |
| 83 | insert_hton2plugin(4, new st_plugin_int())->name = engine_name4; |
| 84 | insert_hton2plugin(7, new st_plugin_int())->name = engine_name7; |
| 85 | } |
| 86 | |
| 87 | void TearDown() override { |
| 88 | initializer.TearDown(); |
| 89 | delete remove_hton2plugin(0); |
| 90 | delete remove_hton2plugin(1); |
| 91 | delete remove_hton2plugin(2); |
| 92 | delete remove_hton2plugin(4); |
| 93 | delete remove_hton2plugin(7); |
| 94 | } |
| 95 | |
| 96 | THD *thd() { return initializer.thd(); } |
| 97 | |
| 98 | Server_initializer initializer; |
| 99 | }; |
| 100 | |
| 101 | /* |
| 102 | Class for making it possible to test protected member functions |
| 103 | of the SE_cost_constants class. |
| 104 | */ |
| 105 | class Testable_SE_cost_constants : public SE_cost_constants { |
| 106 | public: |
| 107 | /* |
| 108 | Wrapper function that allows testing of the protected update() function. |
| 109 | */ |
| 110 | cost_constant_error test_update_func(const LEX_CSTRING &name, |
| 111 | const double value) { |
| 112 | return update(name, value); |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | Wrapper function that allows testing of the protected update() function. |
| 117 | */ |
| 118 | cost_constant_error test_update_default_func(const LEX_CSTRING &name, |
| 119 | const double value) { |
| 120 | return update_default(name, value); |
| 121 | } |
| 122 | }; |
| 123 | |
| 124 | /* |
| 125 | Storage engines can extend the cost model by adding new cost |
| 126 | constants. This is done by making a subclass of the |
| 127 | SE_cost_constant class. To make a limited test for this, a |
| 128 | fictional storage engine called TapeEngine is used. The following |
| 129 | class adds a new cost constant called TAPE_IO_COST. Note that this |
| 130 | should inherit from SE_cost_constants, but to make it easier to write |
| 131 | a unit test, we inherit from Testable_SE_cost_constants. |
| 132 | */ |
| 133 | class TapeEngine_cost_constants : public Testable_SE_cost_constants { |
| 134 | public: |
| 135 | TapeEngine_cost_constants() |
| 136 | : m_tape_io_cost(200.0), m_tape_io_cost_default(true) {} |
| 137 | |
| 138 | double tape_io_cost() const { return m_tape_io_cost; } |
| 139 | |
| 140 | protected: |
| 141 | cost_constant_error set(const LEX_CSTRING &name, const double value, |
| 142 | bool default_value) { |
| 143 | // Process TAPE_IO_COST here |
| 144 | if (!my_strcasecmp(&my_charset_utf8mb3_general_ci, "TAPE_IO_COST", |
| 145 | name.str)) { |
| 146 | update_cost_value(&m_tape_io_cost, &m_tape_io_cost_default, value, |
| 147 | default_value); |
| 148 | return COST_CONSTANT_OK; |
| 149 | } |
| 150 | |
| 151 | // If the cost constant name was not recognized here, call the parent |
| 152 | return Testable_SE_cost_constants::set(name, value, default_value); |
| 153 | } |
| 154 | |
| 155 | private: |
| 156 | double m_tape_io_cost; |
| 157 | bool m_tape_io_cost_default; |
| 158 | }; |
| 159 | |
| 160 | /* |
| 161 | Class for making it easier to test protected member function of the |
| 162 | Cost_model_class. To make it possible to call some of the functions |
| 163 | that relies on storage engine plugins being loaded, without having |
| 164 | the plugins loaded, this class re-implements a fake version of one |
| 165 | of the utility functions. |
| 166 | */ |
| 167 | class Testable_Cost_model_constants : public Cost_model_constants { |
| 168 | public: |
| 169 | /* |
| 170 | Wrapper function that allows testing of the protected inc_ref_count() |
| 171 | function. |
| 172 | */ |
| 173 | void test_inc_ref_count() { inc_ref_count(); } |
| 174 | |
| 175 | /* |
| 176 | Wrapper function that allows testing of the protected dec_ref_count() |
| 177 | function. |
| 178 | */ |
| 179 | unsigned int test_dec_ref_count() { return dec_ref_count(); } |
| 180 | |
| 181 | private: |
| 182 | /** |
| 183 | The implementation of Cost_model_constants relies on using the |
| 184 | installed plugins for looking up the handler's slot id based on the |
| 185 | name of the storage engine. For unit tests this does not work since |
| 186 | no storage engines are installed. We fake this here by overriding the |
| 187 | function that looks up the handler's slot id. |
| 188 | |
| 189 | This function supports two storage engines, "Karius" installed in slot |
| 190 | 4 and "Baktus" installed in slot 7. |
| 191 | */ |
| 192 | |
| 193 | uint find_handler_slot_from_name(THD *, const LEX_CSTRING &name) const { |
| 194 | if (my_strcasecmp(&my_charset_utf8mb3_general_ci, "Karius", name.str) == 0) |
| 195 | return 4; |
| 196 | |
| 197 | if (my_strcasecmp(&my_charset_utf8mb3_general_ci, "Baktus", name.str) == 0) |
| 198 | return 7; |
| 199 | |
| 200 | // There is no handler for a storage engine with the given name |
| 201 | return HA_SLOT_UNDEF; |
| 202 | } |
| 203 | }; |
| 204 | |
| 205 | /** |
| 206 | Validates that a cost constant object for server cost constants has |
| 207 | the expected default values. |
| 208 | */ |
| 209 | |
| 210 | static void validate_default_server_cost_constants( |
| 211 | const Server_cost_constants *cost) { |
| 212 | EXPECT_EQ(cost->row_evaluate_cost(), default_row_evaluate_cost); |
| 213 | EXPECT_EQ(cost->key_compare_cost(), default_key_compare_cost); |
| 214 | EXPECT_EQ(cost->memory_temptable_create_cost(), |
| 215 | default_memory_temptable_create_cost); |
| 216 | EXPECT_EQ(cost->memory_temptable_row_cost(), |
| 217 | default_memory_temptable_row_cost); |
| 218 | EXPECT_EQ(cost->disk_temptable_create_cost(), |
| 219 | default_disk_temptable_create_cost); |
| 220 | EXPECT_EQ(cost->disk_temptable_row_cost(), default_disk_temptable_row_cost); |
| 221 | } |
| 222 | |
| 223 | /* |
| 224 | Test the Server_cost_constants interface. |
| 225 | */ |
| 226 | TEST_F(CostConstantsTest, CostConstantsServer) { |
| 227 | Server_cost_constants server_constants; |
| 228 | |
| 229 | // Validate expected default values for cost constants |
| 230 | validate_default_server_cost_constants(&server_constants); |
| 231 | |
| 232 | /* |
| 233 | Test updating values for cost constants |
| 234 | */ |
| 235 | const double new_value = 3.14; |
| 236 | |
| 237 | // row_evaluate_cost |
| 238 | const LEX_CSTRING row_evaluate_name = {STRING_WITH_LEN("ROW_EVALUATE_COST")}; |
| 239 | EXPECT_EQ(server_constants.row_evaluate_cost(), default_row_evaluate_cost); |
| 240 | EXPECT_EQ(server_constants.set(row_evaluate_name, new_value), |
| 241 | COST_CONSTANT_OK); |
| 242 | EXPECT_EQ(server_constants.row_evaluate_cost(), new_value); |
| 243 | |
| 244 | // key_compare_cost |
| 245 | const LEX_CSTRING key_compare_name = {STRING_WITH_LEN("KEY_COMPARE_COST")}; |
| 246 | EXPECT_EQ(server_constants.key_compare_cost(), default_key_compare_cost); |
| 247 | EXPECT_EQ(server_constants.set(key_compare_name, new_value), |
| 248 | COST_CONSTANT_OK); |
| 249 | EXPECT_EQ(server_constants.key_compare_cost(), new_value); |
| 250 | |
| 251 | // memory_temptable_create_cost |
| 252 | const LEX_CSTRING memory_temptable_create_name = { |
| 253 | STRING_WITH_LEN("MEMORY_TEMPTABLE_CREATE_COST")}; |
| 254 | EXPECT_EQ(server_constants.memory_temptable_create_cost(), |
| 255 | default_memory_temptable_create_cost); |
| 256 | EXPECT_EQ(server_constants.set(memory_temptable_create_name, new_value), |
| 257 | COST_CONSTANT_OK); |
| 258 | EXPECT_EQ(server_constants.memory_temptable_create_cost(), new_value); |
| 259 | |
| 260 | // memory_temptable_row_cost |
| 261 | const LEX_CSTRING memory_temptable_row_name = { |
| 262 | STRING_WITH_LEN("MEMORY_TEMPTABLE_ROW_COST")}; |
| 263 | EXPECT_EQ(server_constants.memory_temptable_row_cost(), |
| 264 | default_memory_temptable_row_cost); |
| 265 | EXPECT_EQ(server_constants.set(memory_temptable_row_name, new_value), |
| 266 | COST_CONSTANT_OK); |
| 267 | EXPECT_EQ(server_constants.memory_temptable_row_cost(), new_value); |
| 268 | |
| 269 | // disk_temptable_create_cost |
| 270 | const LEX_CSTRING disk_temptable_create_name = { |
| 271 | STRING_WITH_LEN("DISK_TEMPTABLE_CREATE_COST")}; |
| 272 | EXPECT_EQ(server_constants.disk_temptable_create_cost(), |
| 273 | default_disk_temptable_create_cost); |
| 274 | EXPECT_EQ(server_constants.set(disk_temptable_create_name, new_value), |
| 275 | COST_CONSTANT_OK); |
| 276 | EXPECT_EQ(server_constants.disk_temptable_create_cost(), new_value); |
| 277 | |
| 278 | // disk_temptable_row_cost |
| 279 | const LEX_CSTRING disk_temptable_row_name = { |
| 280 | STRING_WITH_LEN("DISK_TEMPTABLE_ROW_COST")}; |
| 281 | EXPECT_EQ(server_constants.disk_temptable_row_cost(), |
| 282 | default_disk_temptable_row_cost); |
| 283 | EXPECT_EQ(server_constants.set(disk_temptable_row_name, new_value), |
| 284 | COST_CONSTANT_OK); |
| 285 | EXPECT_EQ(server_constants.disk_temptable_row_cost(), new_value); |
| 286 | |
| 287 | /* |
| 288 | Test with upper, lower and mixed case for cost constant name. |
| 289 | */ |
| 290 | const LEX_CSTRING name_upper = {STRING_WITH_LEN("ROW_EVALUATE_COST")}; |
| 291 | const LEX_CSTRING name_lower = {STRING_WITH_LEN("row_evaluate_cost")}; |
| 292 | const LEX_CSTRING name_mixed = {STRING_WITH_LEN("rOw_EvAlUaTe_CoSt")}; |
| 293 | const LEX_CSTRING name_space = {STRING_WITH_LEN("ROW_EVALUATE_COST ")}; |
| 294 | |
| 295 | const double value1 = 2.74; |
| 296 | const double value2 = 3.14; |
| 297 | |
| 298 | EXPECT_EQ(server_constants.set(name_upper, value1), COST_CONSTANT_OK); |
| 299 | EXPECT_EQ(server_constants.row_evaluate_cost(), value1); |
| 300 | EXPECT_EQ(server_constants.set(name_lower, value2), COST_CONSTANT_OK); |
| 301 | EXPECT_EQ(server_constants.row_evaluate_cost(), value2); |
| 302 | EXPECT_EQ(server_constants.set(name_mixed, value1), COST_CONSTANT_OK); |
| 303 | EXPECT_EQ(server_constants.row_evaluate_cost(), value1); |
| 304 | |
| 305 | EXPECT_EQ(server_constants.set(name_space, value2), UNKNOWN_COST_NAME); |
| 306 | EXPECT_EQ(server_constants.row_evaluate_cost(), value1); |
| 307 | |
| 308 | /* |
| 309 | Test with an unknown cost constant name. |
| 310 | */ |
| 311 | const LEX_CSTRING unknown_name = {STRING_WITH_LEN("UNKNOWN_COST")}; |
| 312 | EXPECT_EQ(server_constants.set(unknown_name, value1), UNKNOWN_COST_NAME); |
| 313 | |
| 314 | /* |
| 315 | Test with illegal cost values: negative or zero should not be allowed. |
| 316 | */ |
| 317 | EXPECT_EQ(server_constants.row_evaluate_cost(), value1); |
| 318 | EXPECT_EQ(server_constants.set(row_evaluate_name, -1.0), INVALID_COST_VALUE); |
| 319 | EXPECT_EQ(server_constants.row_evaluate_cost(), value1); |
| 320 | EXPECT_EQ(server_constants.set(row_evaluate_name, 0.0), INVALID_COST_VALUE); |
| 321 | EXPECT_EQ(server_constants.row_evaluate_cost(), value1); |
| 322 | } |
| 323 | |
| 324 | /* |
| 325 | Test the SE_cost_constants interface. |
| 326 | */ |
| 327 | TEST_F(CostConstantsTest, CostConstantsStorageEngine) { |
| 328 | SE_cost_constants se_constants; |
| 329 | |
| 330 | // Validate expected default values for cost constants |
| 331 | EXPECT_EQ(se_constants.memory_block_read_cost(), |
| 332 | default_memory_block_read_cost); |
| 333 | EXPECT_EQ(se_constants.io_block_read_cost(), default_io_block_read_cost); |
| 334 | |
| 335 | /* |
| 336 | Test updating values for cost constants |
| 337 | */ |
| 338 | Testable_SE_cost_constants se_constants2; |
| 339 | |
| 340 | const double new_value1 = 2.74; |
| 341 | const double new_value2 = 3.14; |
| 342 | |
| 343 | /* |
| 344 | Test memory_block_read_cost |
| 345 | */ |
| 346 | const LEX_CSTRING memory_block_read_name = { |
| 347 | STRING_WITH_LEN("MEMORY_BLOCK_READ_COST")}; |
| 348 | |
| 349 | // Update the default value, first time |
| 350 | EXPECT_EQ(se_constants2.test_update_default_func(memory_block_read_name, |
| 351 | new_value1), |
| 352 | COST_CONSTANT_OK); |
| 353 | EXPECT_EQ(se_constants2.memory_block_read_cost(), new_value1); |
| 354 | |
| 355 | // Update the default value, second time |
| 356 | EXPECT_EQ(se_constants2.test_update_default_func(memory_block_read_name, |
| 357 | new_value2), |
| 358 | COST_CONSTANT_OK); |
| 359 | EXPECT_EQ(se_constants2.memory_block_read_cost(), new_value2); |
| 360 | |
| 361 | // Update with a engine specific value, first time |
| 362 | EXPECT_EQ(se_constants2.test_update_func(memory_block_read_name, new_value1), |
| 363 | COST_CONSTANT_OK); |
| 364 | EXPECT_EQ(se_constants2.memory_block_read_cost(), new_value1); |
| 365 | |
| 366 | // Update with a engine specific value, second time |
| 367 | EXPECT_EQ(se_constants2.test_update_func(memory_block_read_name, new_value2), |
| 368 | COST_CONSTANT_OK); |
| 369 | EXPECT_EQ(se_constants2.memory_block_read_cost(), new_value2); |
| 370 | |
| 371 | // Update the default value, this should not change the value |
| 372 | EXPECT_EQ(se_constants2.test_update_default_func(memory_block_read_name, |
| 373 | new_value1), |
| 374 | COST_CONSTANT_OK); |
| 375 | EXPECT_EQ(se_constants2.memory_block_read_cost(), new_value2); |
| 376 | |
| 377 | /* |
| 378 | Test with upper, lower and mixed case for cost constant name. |
| 379 | */ |
| 380 | const LEX_CSTRING mem_name_upper = { |
| 381 | STRING_WITH_LEN("MEMORY_BLOCK_READ_COST")}; |
| 382 | const LEX_CSTRING mem_name_lower = { |
| 383 | STRING_WITH_LEN("memory_block_read_cost")}; |
| 384 | const LEX_CSTRING mem_name_mixed = { |
| 385 | STRING_WITH_LEN("mEmOrY_bLoCk_ReAd_CoSt")}; |
| 386 | |
| 387 | EXPECT_EQ(se_constants2.test_update_func(mem_name_upper, new_value1), |
| 388 | COST_CONSTANT_OK); |
| 389 | EXPECT_EQ(se_constants2.memory_block_read_cost(), new_value1); |
| 390 | EXPECT_EQ(se_constants2.test_update_func(mem_name_lower, new_value2), |
| 391 | COST_CONSTANT_OK); |
| 392 | EXPECT_EQ(se_constants2.memory_block_read_cost(), new_value2); |
| 393 | EXPECT_EQ(se_constants2.test_update_func(mem_name_mixed, new_value1), |
| 394 | COST_CONSTANT_OK); |
| 395 | EXPECT_EQ(se_constants2.memory_block_read_cost(), new_value1); |
| 396 | |
| 397 | /* |
| 398 | Test io_block_read_cost |
| 399 | */ |
| 400 | const LEX_CSTRING io_block_read_name = { |
| 401 | STRING_WITH_LEN("IO_BLOCK_READ_COST")}; |
| 402 | |
| 403 | // Update the default value, first time |
| 404 | EXPECT_EQ( |
| 405 | se_constants2.test_update_default_func(io_block_read_name, new_value1), |
| 406 | COST_CONSTANT_OK); |
| 407 | EXPECT_EQ(se_constants2.io_block_read_cost(), new_value1); |
| 408 | |
| 409 | // Update the default value, second time |
| 410 | EXPECT_EQ( |
| 411 | se_constants2.test_update_default_func(io_block_read_name, new_value2), |
| 412 | COST_CONSTANT_OK); |
| 413 | EXPECT_EQ(se_constants2.io_block_read_cost(), new_value2); |
| 414 | |
| 415 | // Update with a engine specific value, first time |
| 416 | EXPECT_EQ(se_constants2.test_update_func(io_block_read_name, new_value1), |
| 417 | COST_CONSTANT_OK); |
| 418 | EXPECT_EQ(se_constants2.io_block_read_cost(), new_value1); |
| 419 | |
| 420 | // Update with a engine specific value, second time |
| 421 | EXPECT_EQ(se_constants2.test_update_func(io_block_read_name, new_value2), |
| 422 | COST_CONSTANT_OK); |
| 423 | EXPECT_EQ(se_constants2.io_block_read_cost(), new_value2); |
| 424 | |
| 425 | // Update the default value, this should not change the value |
| 426 | EXPECT_EQ( |
| 427 | se_constants2.test_update_default_func(io_block_read_name, new_value1), |
| 428 | COST_CONSTANT_OK); |
| 429 | EXPECT_EQ(se_constants2.io_block_read_cost(), new_value2); |
| 430 | |
| 431 | /* |
| 432 | Test with upper, lower and mixed case for cost constant name. |
| 433 | */ |
| 434 | const LEX_CSTRING io_name_upper = {STRING_WITH_LEN("IO_BLOCK_READ_COST")}; |
| 435 | const LEX_CSTRING io_name_lower = {STRING_WITH_LEN("io_block_read_cost")}; |
| 436 | const LEX_CSTRING io_name_mixed = {STRING_WITH_LEN("iO_bLoCk_ReAd_CoSt")}; |
| 437 | |
| 438 | EXPECT_EQ(se_constants2.test_update_func(io_name_upper, new_value1), |
| 439 | COST_CONSTANT_OK); |
| 440 | EXPECT_EQ(se_constants2.io_block_read_cost(), new_value1); |
| 441 | EXPECT_EQ(se_constants2.test_update_func(io_name_lower, new_value2), |
| 442 | COST_CONSTANT_OK); |
| 443 | EXPECT_EQ(se_constants2.io_block_read_cost(), new_value2); |
| 444 | EXPECT_EQ(se_constants2.test_update_func(io_name_mixed, new_value1), |
| 445 | COST_CONSTANT_OK); |
| 446 | EXPECT_EQ(se_constants2.io_block_read_cost(), new_value1); |
| 447 | |
| 448 | /* |
| 449 | Test with an unknown cost constant name. |
| 450 | */ |
| 451 | const LEX_CSTRING unknown_name = {STRING_WITH_LEN("UNKNOWN_COST")}; |
| 452 | EXPECT_EQ(se_constants2.test_update_func(unknown_name, new_value2), |
| 453 | UNKNOWN_COST_NAME); |
| 454 | EXPECT_EQ(se_constants2.io_block_read_cost(), new_value1); |
| 455 | |
| 456 | /* |
| 457 | Test with illegal cost values: negative and zero is illegal. |
| 458 | */ |
| 459 | EXPECT_EQ(se_constants2.io_block_read_cost(), new_value1); |
| 460 | EXPECT_EQ(se_constants2.test_update_func(io_block_read_name, -1.0), |
| 461 | INVALID_COST_VALUE); |
| 462 | EXPECT_EQ(se_constants2.io_block_read_cost(), new_value1); |
| 463 | EXPECT_EQ(se_constants2.test_update_func(io_block_read_name, 0.0), |
| 464 | INVALID_COST_VALUE); |
| 465 | EXPECT_EQ(se_constants2.io_block_read_cost(), new_value1); |
| 466 | |
| 467 | /* |
| 468 | Test with cost constants for a storage engine that has added a new |
| 469 | storage engine specific cost constant named TAPE_IO_COST. |
| 470 | */ |
| 471 | TapeEngine_cost_constants tape_constants; |
| 472 | |
| 473 | // Validate expected default values for cost constants |
| 474 | EXPECT_EQ(tape_constants.io_block_read_cost(), default_io_block_read_cost); |
| 475 | EXPECT_EQ(tape_constants.tape_io_cost(), 200.0); |
| 476 | |
| 477 | // change io_block_read_cost |
| 478 | EXPECT_EQ(tape_constants.io_block_read_cost(), default_io_block_read_cost); |
| 479 | EXPECT_EQ(tape_constants.test_update_func(io_block_read_name, new_value1), |
| 480 | COST_CONSTANT_OK); |
| 481 | EXPECT_EQ(tape_constants.io_block_read_cost(), new_value1); |
| 482 | |
| 483 | // change tape_io_cost |
| 484 | const LEX_CSTRING tape_io_name = {STRING_WITH_LEN("TAPE_IO_COST")}; |
| 485 | EXPECT_EQ(tape_constants.tape_io_cost(), 200.0); |
| 486 | |
| 487 | // Change default value for tape_io_cost |
| 488 | EXPECT_EQ(tape_constants.test_update_default_func(tape_io_name, new_value1), |
| 489 | COST_CONSTANT_OK); |
| 490 | EXPECT_EQ(tape_constants.tape_io_cost(), new_value1); |
| 491 | |
| 492 | // Change engine specific value for tape_io_cost |
| 493 | EXPECT_EQ(tape_constants.test_update_func(tape_io_name, new_value2), |
| 494 | COST_CONSTANT_OK); |
| 495 | EXPECT_EQ(tape_constants.tape_io_cost(), new_value2); |
| 496 | |
| 497 | // Test with an unknown cost constant name. |
| 498 | EXPECT_EQ(tape_constants.test_update_func(unknown_name, new_value1), |
| 499 | UNKNOWN_COST_NAME); |
| 500 | } |
| 501 | |
| 502 | /* |
| 503 | Test the Cost model constants interface. |
| 504 | */ |
| 505 | TEST_F(CostConstantsTest, CostConstants) { |
| 506 | /* |
| 507 | Test ref counter. |
| 508 | */ |
| 509 | { |
| 510 | Testable_Cost_model_constants cost_constants_ref_test; |
| 511 | |
| 512 | cost_constants_ref_test.test_inc_ref_count(); |
| 513 | cost_constants_ref_test.test_inc_ref_count(); |
| 514 | EXPECT_EQ(cost_constants_ref_test.test_dec_ref_count(), 1U); |
| 515 | EXPECT_EQ(cost_constants_ref_test.test_dec_ref_count(), 0U); |
| 516 | } |
| 517 | |
| 518 | /* |
| 519 | Test default server cost constants. |
| 520 | */ |
| 521 | Cost_model_constants cost_constants; |
| 522 | |
| 523 | const Server_cost_constants *server_const = |
| 524 | cost_constants.get_server_cost_constants(); |
| 525 | |
| 526 | validate_default_server_cost_constants(server_const); |
| 527 | |
| 528 | /* |
| 529 | Test default table cost constants. |
| 530 | */ |
| 531 | Fake_TABLE table(1, false); |
| 532 | |
| 533 | const SE_cost_constants *se_const = |
| 534 | cost_constants.get_se_cost_constants(&table); |
| 535 | |
| 536 | // Validate expected default values for cost constants |
| 537 | EXPECT_EQ(se_const->memory_block_read_cost(), default_memory_block_read_cost); |
| 538 | EXPECT_EQ(se_const->io_block_read_cost(), default_io_block_read_cost); |
| 539 | |
| 540 | /* |
| 541 | Test updating server constants. |
| 542 | */ |
| 543 | const double new_value1 = 2.74; |
| 544 | const double new_value2 = 3.14; |
| 545 | const double new_value3 = 5.00; |
| 546 | |
| 547 | const LEX_CSTRING row_evaluate_name = {STRING_WITH_LEN("ROW_EVALUATE_COST")}; |
| 548 | EXPECT_EQ( |
| 549 | cost_constants.update_server_cost_constant(row_evaluate_name, new_value1), |
| 550 | COST_CONSTANT_OK); |
| 551 | server_const = cost_constants.get_server_cost_constants(); |
| 552 | EXPECT_EQ(server_const->row_evaluate_cost(), new_value1); |
| 553 | |
| 554 | const LEX_CSTRING unknown_name = {STRING_WITH_LEN("UNKNOWN_COST")}; |
| 555 | EXPECT_EQ( |
| 556 | cost_constants.update_server_cost_constant(unknown_name, new_value2), |
| 557 | UNKNOWN_COST_NAME); |
| 558 | |
| 559 | // Test with illegal cost constant values |
| 560 | EXPECT_EQ(server_const->row_evaluate_cost(), new_value1); |
| 561 | EXPECT_EQ(cost_constants.update_server_cost_constant(row_evaluate_name, -1.0), |
| 562 | INVALID_COST_VALUE); |
| 563 | EXPECT_EQ(server_const->row_evaluate_cost(), new_value1); |
| 564 | EXPECT_EQ(cost_constants.update_server_cost_constant(row_evaluate_name, 0.0), |
| 565 | INVALID_COST_VALUE); |
| 566 | EXPECT_EQ(server_const->row_evaluate_cost(), new_value1); |
| 567 | |
| 568 | /* |
| 569 | Test updating table cost constants. |
| 570 | */ |
| 571 | const LEX_CSTRING default_name = {STRING_WITH_LEN("default")}; |
| 572 | const LEX_CSTRING io_block_read_name = { |
| 573 | STRING_WITH_LEN("IO_BLOCK_READ_COST")}; |
| 574 | EXPECT_EQ(cost_constants.update_engine_cost_constant( |
| 575 | nullptr, default_name, 0, io_block_read_name, new_value1), |
| 576 | COST_CONSTANT_OK); |
| 577 | |
| 578 | // Verify that the cost constant is updated |
| 579 | se_const = cost_constants.get_se_cost_constants(&table); |
| 580 | EXPECT_EQ(se_const->io_block_read_cost(), new_value1); |
| 581 | |
| 582 | /* |
| 583 | Do a second update of the same default value. |
| 584 | */ |
| 585 | EXPECT_EQ(cost_constants.update_engine_cost_constant( |
| 586 | nullptr, default_name, 0, io_block_read_name, new_value2), |
| 587 | COST_CONSTANT_OK); |
| 588 | |
| 589 | // Verify that the cost constant is updated |
| 590 | se_const = cost_constants.get_se_cost_constants(&table); |
| 591 | EXPECT_EQ(se_const->io_block_read_cost(), new_value2); |
| 592 | |
| 593 | /* |
| 594 | Test with illegal cost constant values. |
| 595 | */ |
| 596 | EXPECT_EQ(cost_constants.update_engine_cost_constant( |
| 597 | nullptr, default_name, 0, io_block_read_name, -1.0), |
| 598 | INVALID_COST_VALUE); |
| 599 | EXPECT_EQ(se_const->io_block_read_cost(), new_value2); |
| 600 | EXPECT_EQ(cost_constants.update_engine_cost_constant(nullptr, default_name, 0, |
| 601 | io_block_read_name, 0.0), |
| 602 | INVALID_COST_VALUE); |
| 603 | EXPECT_EQ(se_const->io_block_read_cost(), new_value2); |
| 604 | |
| 605 | /* |
| 606 | Test updating with a none existing cost constant. |
| 607 | */ |
| 608 | const LEX_CSTRING lunch_cost_name = {STRING_WITH_LEN("LUNCH_COST")}; |
| 609 | EXPECT_EQ(cost_constants.update_engine_cost_constant( |
| 610 | nullptr, default_name, 0, lunch_cost_name, new_value1), |
| 611 | UNKNOWN_COST_NAME); |
| 612 | |
| 613 | /* |
| 614 | Test handling of illegal storage category number. |
| 615 | */ |
| 616 | EXPECT_EQ(cost_constants.update_engine_cost_constant( |
| 617 | nullptr, default_name, 100, io_block_read_name, new_value1), |
| 618 | INVALID_DEVICE_TYPE); |
| 619 | |
| 620 | // Verify that the cost constant is not updated |
| 621 | se_const = cost_constants.get_se_cost_constants(&table); |
| 622 | EXPECT_EQ(se_const->io_block_read_cost(), new_value2); |
| 623 | |
| 624 | /* |
| 625 | Create two table objects that are stored in different storage engines. |
| 626 | */ |
| 627 | Fake_TABLE table_se1(1, false); |
| 628 | table_se1.file->ht->slot = 1; |
| 629 | Fake_TABLE table_se2(1, false); |
| 630 | table_se2.file->ht->slot = 2; |
| 631 | |
| 632 | Cost_model_constants cost_constants2; |
| 633 | |
| 634 | const SE_cost_constants *se_cost1 = |
| 635 | cost_constants2.get_se_cost_constants(&table_se1); |
| 636 | const SE_cost_constants *se_cost2 = |
| 637 | cost_constants2.get_se_cost_constants(&table_se2); |
| 638 | |
| 639 | // Verify that both tables have the same default value |
| 640 | EXPECT_EQ(se_cost1->io_block_read_cost(), default_io_block_read_cost); |
| 641 | EXPECT_EQ(se_cost2->io_block_read_cost(), default_io_block_read_cost); |
| 642 | |
| 643 | // Update the default value and verify that the cost constant for both |
| 644 | // tables are changed. |
| 645 | EXPECT_EQ(cost_constants2.update_engine_cost_constant( |
| 646 | nullptr, default_name, 0, io_block_read_name, new_value1), |
| 647 | COST_CONSTANT_OK); |
| 648 | EXPECT_EQ(se_cost1->io_block_read_cost(), new_value1); |
| 649 | EXPECT_EQ(se_cost2->io_block_read_cost(), new_value1); |
| 650 | |
| 651 | /* |
| 652 | Create a cost model constants set that has cost constants for the |
| 653 | two storage engines named "Karius" and "Baktus". We use this instead |
| 654 | of the real Cost_model_constant class since that relies on that the |
| 655 | plugins for storage engines have been initialized. |
| 656 | */ |
| 657 | Testable_Cost_model_constants cost_constants3; |
| 658 | |
| 659 | const LEX_CSTRING karius_name = {STRING_WITH_LEN("Karius")}; |
| 660 | |
| 661 | // Create two tables that use these storage engines |
| 662 | Fake_TABLE table_karius(1, false); |
| 663 | table_karius.file->ht->slot = 4; // Karius is in ht->slot 4 |
| 664 | Fake_TABLE table_baktus(1, false); |
| 665 | table_baktus.file->ht->slot = 7; // Baktus is in ht->slot 7 |
| 666 | |
| 667 | // Get cost constants to use for these tables |
| 668 | const SE_cost_constants *se_cost_karius = |
| 669 | cost_constants3.get_se_cost_constants(&table_karius); |
| 670 | const SE_cost_constants *se_cost_baktus = |
| 671 | cost_constants3.get_se_cost_constants(&table_baktus); |
| 672 | |
| 673 | // Verify that both tables have the same default value |
| 674 | EXPECT_EQ(se_cost_karius->io_block_read_cost(), default_io_block_read_cost); |
| 675 | EXPECT_EQ(se_cost_baktus->io_block_read_cost(), default_io_block_read_cost); |
| 676 | |
| 677 | // Update the default value and verify that the cost constant for both |
| 678 | // tables are changed. |
| 679 | EXPECT_EQ(cost_constants3.update_engine_cost_constant( |
| 680 | nullptr, default_name, 0, io_block_read_name, new_value1), |
| 681 | COST_CONSTANT_OK); |
| 682 | EXPECT_EQ(se_cost_karius->io_block_read_cost(), new_value1); |
| 683 | EXPECT_EQ(se_cost_baktus->io_block_read_cost(), new_value1); |
| 684 | |
| 685 | // Update one of the storage engines with a new cost value and verify that |
| 686 | // only this engine got the new cost value |
| 687 | EXPECT_EQ(cost_constants3.update_engine_cost_constant( |
| 688 | nullptr, karius_name, 0, io_block_read_name, new_value2), |
| 689 | COST_CONSTANT_OK); |
| 690 | EXPECT_EQ(se_cost_karius->io_block_read_cost(), new_value2); |
| 691 | EXPECT_EQ(se_cost_baktus->io_block_read_cost(), new_value1); |
| 692 | |
| 693 | // Do a new update of the default value and verify that only the storage |
| 694 | // engine that still is using the default value is changed. |
| 695 | EXPECT_EQ(cost_constants3.update_engine_cost_constant( |
| 696 | nullptr, default_name, 0, io_block_read_name, new_value3), |
| 697 | COST_CONSTANT_OK); |
| 698 | EXPECT_EQ(se_cost_karius->io_block_read_cost(), new_value2); |
| 699 | EXPECT_EQ(se_cost_baktus->io_block_read_cost(), new_value3); |
| 700 | } |
| 701 | |
| 702 | } // namespace costconstants_unittest |