File: opt_costconstants-t.cc

package info (click to toggle)
mysql-8.0 8.0.43-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,924 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,181; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,194; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (702 lines) | stat: -rw-r--r-- 26,982 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
/*
   Copyright (c) 2014, 2025, Oracle and/or its affiliates.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License, version 2.0,
   as published by the Free Software Foundation.

   This program is designed to work with certain software (including
   but not limited to OpenSSL) that is licensed under separate terms,
   as designated in a particular file or component or in included license
   documentation.  The authors of MySQL hereby grant you an additional
   permission to link the program and your derivative works with the
   separately licensed software that they have either included with
   the program or referenced in the documentation.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License, version 2.0, for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */

#include <gtest/gtest.h>
#include <stddef.h>
#include <sys/types.h>

#include "lex_string.h"
#include "sql/item.h"
#include "sql/opt_costmodel.h"
#include "unittest/gunit/fake_table.h"
#include "unittest/gunit/test_utils.h"

namespace costconstants_unittest {

using my_testing::Server_initializer;

/*
  Default values for cost constants. These needs to be updated when
  cost constants in opt_costconstans.h are changed.
*/

// Default value for Server_cost_constants::ROW_EVALUATE_COST
const double default_row_evaluate_cost = 0.1;

// Default value for Server_cost_constants::KEY_COMPARE_COST
const double default_key_compare_cost = 0.05;

// Default value for Server_cost_constants::HEAP_TEMPTABLE_CREATE_COST
const double default_memory_temptable_create_cost = 1.0;

// Default value for Server_cost_constants::HEAP_TEMPTABLE_ROW_COST
const double default_memory_temptable_row_cost = 0.1;

// Default value for Server_cost_constants::DISK_TEMPTABLE_CREATE_COST
const double default_disk_temptable_create_cost = 20.0;

// Default value for Server_cost_constants::DISK_TEMPTABLE_ROW_COST
const double default_disk_temptable_row_cost = 0.5;

//  Default value SE_cost_constants::MEMORY_BLOCK_READ_COST
const double default_memory_block_read_cost = 0.25;

//  Default value SE_cost_constants::IO_BLOCK_READ_COST
const double default_io_block_read_cost = 1.0;

class CostConstantsTest : public ::testing::Test {
 protected:
  void SetUp() override {
    initializer.SetUp();

    // Initilize some storage engines
    LEX_CSTRING engine_name0 = {STRING_WITH_LEN("InnoDB0")};
    LEX_CSTRING engine_name1 = {STRING_WITH_LEN("InnoDB1")};
    LEX_CSTRING engine_name2 = {STRING_WITH_LEN("InnoDB2")};
    LEX_CSTRING engine_name4 = {STRING_WITH_LEN("InnoDB4")};
    LEX_CSTRING engine_name7 = {STRING_WITH_LEN("InnoDB7")};

    insert_hton2plugin(0, new st_plugin_int())->name = engine_name0;
    insert_hton2plugin(1, new st_plugin_int())->name = engine_name1;
    insert_hton2plugin(2, new st_plugin_int())->name = engine_name2;
    insert_hton2plugin(4, new st_plugin_int())->name = engine_name4;
    insert_hton2plugin(7, new st_plugin_int())->name = engine_name7;
  }

  void TearDown() override {
    initializer.TearDown();
    delete remove_hton2plugin(0);
    delete remove_hton2plugin(1);
    delete remove_hton2plugin(2);
    delete remove_hton2plugin(4);
    delete remove_hton2plugin(7);
  }

  THD *thd() { return initializer.thd(); }

  Server_initializer initializer;
};

/*
  Class for making it possible to test protected member functions
  of the SE_cost_constants class.
*/
class Testable_SE_cost_constants : public SE_cost_constants {
 public:
  /*
    Wrapper function that allows testing of the protected update() function.
  */
  cost_constant_error test_update_func(const LEX_CSTRING &name,
                                       const double value) {
    return update(name, value);
  }

  /*
    Wrapper function that allows testing of the protected update() function.
  */
  cost_constant_error test_update_default_func(const LEX_CSTRING &name,
                                               const double value) {
    return update_default(name, value);
  }
};

/*
  Storage engines can extend the cost model by adding new cost
  constants.  This is done by making a subclass of the
  SE_cost_constant class.  To make a limited test for this, a
  fictional storage engine called TapeEngine is used. The following
  class adds a new cost constant called TAPE_IO_COST.  Note that this
  should inherit from SE_cost_constants, but to make it easier to write
  a unit test, we inherit from Testable_SE_cost_constants.
*/
class TapeEngine_cost_constants : public Testable_SE_cost_constants {
 public:
  TapeEngine_cost_constants()
      : m_tape_io_cost(200.0), m_tape_io_cost_default(true) {}

  double tape_io_cost() const { return m_tape_io_cost; }

 protected:
  cost_constant_error set(const LEX_CSTRING &name, const double value,
                          bool default_value) {
    // Process TAPE_IO_COST here
    if (!my_strcasecmp(&my_charset_utf8mb3_general_ci, "TAPE_IO_COST",
                       name.str)) {
      update_cost_value(&m_tape_io_cost, &m_tape_io_cost_default, value,
                        default_value);
      return COST_CONSTANT_OK;
    }

    // If the cost constant name was not recognized here, call the parent
    return Testable_SE_cost_constants::set(name, value, default_value);
  }

 private:
  double m_tape_io_cost;
  bool m_tape_io_cost_default;
};

/*
  Class for making it easier to test protected member function of the
  Cost_model_class. To make it possible to call some of the functions
  that relies on storage engine plugins being loaded, without having
  the plugins loaded, this class re-implements a fake version of one
  of the utility functions.
*/
class Testable_Cost_model_constants : public Cost_model_constants {
 public:
  /*
    Wrapper function that allows testing of the protected inc_ref_count()
    function.
  */
  void test_inc_ref_count() { inc_ref_count(); }

  /*
    Wrapper function that allows testing of the protected dec_ref_count()
    function.
  */
  unsigned int test_dec_ref_count() { return dec_ref_count(); }

 private:
  /**
    The implementation of Cost_model_constants relies on using the
    installed plugins for looking up the handler's slot id based on the
    name of the storage engine. For unit tests this does not work since
    no storage engines are installed. We fake this here by overriding the
    function that looks up the handler's slot id.

    This function supports two storage engines, "Karius" installed in slot
    4 and "Baktus" installed in slot 7.
  */

  uint find_handler_slot_from_name(THD *, const LEX_CSTRING &name) const {
    if (my_strcasecmp(&my_charset_utf8mb3_general_ci, "Karius", name.str) == 0)
      return 4;

    if (my_strcasecmp(&my_charset_utf8mb3_general_ci, "Baktus", name.str) == 0)
      return 7;

    // There is no handler for a storage engine with the given name
    return HA_SLOT_UNDEF;
  }
};

/**
  Validates that a cost constant object for server cost constants has
  the expected default values.
*/

static void validate_default_server_cost_constants(
    const Server_cost_constants *cost) {
  EXPECT_EQ(cost->row_evaluate_cost(), default_row_evaluate_cost);
  EXPECT_EQ(cost->key_compare_cost(), default_key_compare_cost);
  EXPECT_EQ(cost->memory_temptable_create_cost(),
            default_memory_temptable_create_cost);
  EXPECT_EQ(cost->memory_temptable_row_cost(),
            default_memory_temptable_row_cost);
  EXPECT_EQ(cost->disk_temptable_create_cost(),
            default_disk_temptable_create_cost);
  EXPECT_EQ(cost->disk_temptable_row_cost(), default_disk_temptable_row_cost);
}

/*
  Test the Server_cost_constants interface.
*/
TEST_F(CostConstantsTest, CostConstantsServer) {
  Server_cost_constants server_constants;

  // Validate expected default values for cost constants
  validate_default_server_cost_constants(&server_constants);

  /*
    Test updating values for cost constants
  */
  const double new_value = 3.14;

  // row_evaluate_cost
  const LEX_CSTRING row_evaluate_name = {STRING_WITH_LEN("ROW_EVALUATE_COST")};
  EXPECT_EQ(server_constants.row_evaluate_cost(), default_row_evaluate_cost);
  EXPECT_EQ(server_constants.set(row_evaluate_name, new_value),
            COST_CONSTANT_OK);
  EXPECT_EQ(server_constants.row_evaluate_cost(), new_value);

  // key_compare_cost
  const LEX_CSTRING key_compare_name = {STRING_WITH_LEN("KEY_COMPARE_COST")};
  EXPECT_EQ(server_constants.key_compare_cost(), default_key_compare_cost);
  EXPECT_EQ(server_constants.set(key_compare_name, new_value),
            COST_CONSTANT_OK);
  EXPECT_EQ(server_constants.key_compare_cost(), new_value);

  // memory_temptable_create_cost
  const LEX_CSTRING memory_temptable_create_name = {
      STRING_WITH_LEN("MEMORY_TEMPTABLE_CREATE_COST")};
  EXPECT_EQ(server_constants.memory_temptable_create_cost(),
            default_memory_temptable_create_cost);
  EXPECT_EQ(server_constants.set(memory_temptable_create_name, new_value),
            COST_CONSTANT_OK);
  EXPECT_EQ(server_constants.memory_temptable_create_cost(), new_value);

  // memory_temptable_row_cost
  const LEX_CSTRING memory_temptable_row_name = {
      STRING_WITH_LEN("MEMORY_TEMPTABLE_ROW_COST")};
  EXPECT_EQ(server_constants.memory_temptable_row_cost(),
            default_memory_temptable_row_cost);
  EXPECT_EQ(server_constants.set(memory_temptable_row_name, new_value),
            COST_CONSTANT_OK);
  EXPECT_EQ(server_constants.memory_temptable_row_cost(), new_value);

  // disk_temptable_create_cost
  const LEX_CSTRING disk_temptable_create_name = {
      STRING_WITH_LEN("DISK_TEMPTABLE_CREATE_COST")};
  EXPECT_EQ(server_constants.disk_temptable_create_cost(),
            default_disk_temptable_create_cost);
  EXPECT_EQ(server_constants.set(disk_temptable_create_name, new_value),
            COST_CONSTANT_OK);
  EXPECT_EQ(server_constants.disk_temptable_create_cost(), new_value);

  // disk_temptable_row_cost
  const LEX_CSTRING disk_temptable_row_name = {
      STRING_WITH_LEN("DISK_TEMPTABLE_ROW_COST")};
  EXPECT_EQ(server_constants.disk_temptable_row_cost(),
            default_disk_temptable_row_cost);
  EXPECT_EQ(server_constants.set(disk_temptable_row_name, new_value),
            COST_CONSTANT_OK);
  EXPECT_EQ(server_constants.disk_temptable_row_cost(), new_value);

  /*
    Test with upper, lower and mixed case for cost constant name.
  */
  const LEX_CSTRING name_upper = {STRING_WITH_LEN("ROW_EVALUATE_COST")};
  const LEX_CSTRING name_lower = {STRING_WITH_LEN("row_evaluate_cost")};
  const LEX_CSTRING name_mixed = {STRING_WITH_LEN("rOw_EvAlUaTe_CoSt")};
  const LEX_CSTRING name_space = {STRING_WITH_LEN("ROW_EVALUATE_COST ")};

  const double value1 = 2.74;
  const double value2 = 3.14;

  EXPECT_EQ(server_constants.set(name_upper, value1), COST_CONSTANT_OK);
  EXPECT_EQ(server_constants.row_evaluate_cost(), value1);
  EXPECT_EQ(server_constants.set(name_lower, value2), COST_CONSTANT_OK);
  EXPECT_EQ(server_constants.row_evaluate_cost(), value2);
  EXPECT_EQ(server_constants.set(name_mixed, value1), COST_CONSTANT_OK);
  EXPECT_EQ(server_constants.row_evaluate_cost(), value1);

  EXPECT_EQ(server_constants.set(name_space, value2), UNKNOWN_COST_NAME);
  EXPECT_EQ(server_constants.row_evaluate_cost(), value1);

  /*
    Test with an unknown cost constant name.
  */
  const LEX_CSTRING unknown_name = {STRING_WITH_LEN("UNKNOWN_COST")};
  EXPECT_EQ(server_constants.set(unknown_name, value1), UNKNOWN_COST_NAME);

  /*
    Test with illegal cost values: negative or zero should not be allowed.
  */
  EXPECT_EQ(server_constants.row_evaluate_cost(), value1);
  EXPECT_EQ(server_constants.set(row_evaluate_name, -1.0), INVALID_COST_VALUE);
  EXPECT_EQ(server_constants.row_evaluate_cost(), value1);
  EXPECT_EQ(server_constants.set(row_evaluate_name, 0.0), INVALID_COST_VALUE);
  EXPECT_EQ(server_constants.row_evaluate_cost(), value1);
}

/*
  Test the SE_cost_constants interface.
*/
TEST_F(CostConstantsTest, CostConstantsStorageEngine) {
  SE_cost_constants se_constants;

  // Validate expected default values for cost constants
  EXPECT_EQ(se_constants.memory_block_read_cost(),
            default_memory_block_read_cost);
  EXPECT_EQ(se_constants.io_block_read_cost(), default_io_block_read_cost);

  /*
    Test updating values for cost constants
  */
  Testable_SE_cost_constants se_constants2;

  const double new_value1 = 2.74;
  const double new_value2 = 3.14;

  /*
    Test memory_block_read_cost
  */
  const LEX_CSTRING memory_block_read_name = {
      STRING_WITH_LEN("MEMORY_BLOCK_READ_COST")};

  // Update the default value, first time
  EXPECT_EQ(se_constants2.test_update_default_func(memory_block_read_name,
                                                   new_value1),
            COST_CONSTANT_OK);
  EXPECT_EQ(se_constants2.memory_block_read_cost(), new_value1);

  // Update the default value, second time
  EXPECT_EQ(se_constants2.test_update_default_func(memory_block_read_name,
                                                   new_value2),
            COST_CONSTANT_OK);
  EXPECT_EQ(se_constants2.memory_block_read_cost(), new_value2);

  // Update with a engine specific value, first time
  EXPECT_EQ(se_constants2.test_update_func(memory_block_read_name, new_value1),
            COST_CONSTANT_OK);
  EXPECT_EQ(se_constants2.memory_block_read_cost(), new_value1);

  // Update with a engine specific value, second time
  EXPECT_EQ(se_constants2.test_update_func(memory_block_read_name, new_value2),
            COST_CONSTANT_OK);
  EXPECT_EQ(se_constants2.memory_block_read_cost(), new_value2);

  // Update the default value, this should not change the value
  EXPECT_EQ(se_constants2.test_update_default_func(memory_block_read_name,
                                                   new_value1),
            COST_CONSTANT_OK);
  EXPECT_EQ(se_constants2.memory_block_read_cost(), new_value2);

  /*
    Test with upper, lower and mixed case for cost constant name.
  */
  const LEX_CSTRING mem_name_upper = {
      STRING_WITH_LEN("MEMORY_BLOCK_READ_COST")};
  const LEX_CSTRING mem_name_lower = {
      STRING_WITH_LEN("memory_block_read_cost")};
  const LEX_CSTRING mem_name_mixed = {
      STRING_WITH_LEN("mEmOrY_bLoCk_ReAd_CoSt")};

  EXPECT_EQ(se_constants2.test_update_func(mem_name_upper, new_value1),
            COST_CONSTANT_OK);
  EXPECT_EQ(se_constants2.memory_block_read_cost(), new_value1);
  EXPECT_EQ(se_constants2.test_update_func(mem_name_lower, new_value2),
            COST_CONSTANT_OK);
  EXPECT_EQ(se_constants2.memory_block_read_cost(), new_value2);
  EXPECT_EQ(se_constants2.test_update_func(mem_name_mixed, new_value1),
            COST_CONSTANT_OK);
  EXPECT_EQ(se_constants2.memory_block_read_cost(), new_value1);

  /*
    Test io_block_read_cost
  */
  const LEX_CSTRING io_block_read_name = {
      STRING_WITH_LEN("IO_BLOCK_READ_COST")};

  // Update the default value, first time
  EXPECT_EQ(
      se_constants2.test_update_default_func(io_block_read_name, new_value1),
      COST_CONSTANT_OK);
  EXPECT_EQ(se_constants2.io_block_read_cost(), new_value1);

  // Update the default value, second time
  EXPECT_EQ(
      se_constants2.test_update_default_func(io_block_read_name, new_value2),
      COST_CONSTANT_OK);
  EXPECT_EQ(se_constants2.io_block_read_cost(), new_value2);

  // Update with a engine specific value, first time
  EXPECT_EQ(se_constants2.test_update_func(io_block_read_name, new_value1),
            COST_CONSTANT_OK);
  EXPECT_EQ(se_constants2.io_block_read_cost(), new_value1);

  // Update with a engine specific value, second time
  EXPECT_EQ(se_constants2.test_update_func(io_block_read_name, new_value2),
            COST_CONSTANT_OK);
  EXPECT_EQ(se_constants2.io_block_read_cost(), new_value2);

  // Update the default value, this should not change the value
  EXPECT_EQ(
      se_constants2.test_update_default_func(io_block_read_name, new_value1),
      COST_CONSTANT_OK);
  EXPECT_EQ(se_constants2.io_block_read_cost(), new_value2);

  /*
    Test with upper, lower and mixed case for cost constant name.
  */
  const LEX_CSTRING io_name_upper = {STRING_WITH_LEN("IO_BLOCK_READ_COST")};
  const LEX_CSTRING io_name_lower = {STRING_WITH_LEN("io_block_read_cost")};
  const LEX_CSTRING io_name_mixed = {STRING_WITH_LEN("iO_bLoCk_ReAd_CoSt")};

  EXPECT_EQ(se_constants2.test_update_func(io_name_upper, new_value1),
            COST_CONSTANT_OK);
  EXPECT_EQ(se_constants2.io_block_read_cost(), new_value1);
  EXPECT_EQ(se_constants2.test_update_func(io_name_lower, new_value2),
            COST_CONSTANT_OK);
  EXPECT_EQ(se_constants2.io_block_read_cost(), new_value2);
  EXPECT_EQ(se_constants2.test_update_func(io_name_mixed, new_value1),
            COST_CONSTANT_OK);
  EXPECT_EQ(se_constants2.io_block_read_cost(), new_value1);

  /*
    Test with an unknown cost constant name.
  */
  const LEX_CSTRING unknown_name = {STRING_WITH_LEN("UNKNOWN_COST")};
  EXPECT_EQ(se_constants2.test_update_func(unknown_name, new_value2),
            UNKNOWN_COST_NAME);
  EXPECT_EQ(se_constants2.io_block_read_cost(), new_value1);

  /*
    Test with illegal cost values: negative and zero is illegal.
  */
  EXPECT_EQ(se_constants2.io_block_read_cost(), new_value1);
  EXPECT_EQ(se_constants2.test_update_func(io_block_read_name, -1.0),
            INVALID_COST_VALUE);
  EXPECT_EQ(se_constants2.io_block_read_cost(), new_value1);
  EXPECT_EQ(se_constants2.test_update_func(io_block_read_name, 0.0),
            INVALID_COST_VALUE);
  EXPECT_EQ(se_constants2.io_block_read_cost(), new_value1);

  /*
    Test with cost constants for a storage engine that has added a new
    storage engine specific cost constant named TAPE_IO_COST.
  */
  TapeEngine_cost_constants tape_constants;

  // Validate expected default values for cost constants
  EXPECT_EQ(tape_constants.io_block_read_cost(), default_io_block_read_cost);
  EXPECT_EQ(tape_constants.tape_io_cost(), 200.0);

  // change io_block_read_cost
  EXPECT_EQ(tape_constants.io_block_read_cost(), default_io_block_read_cost);
  EXPECT_EQ(tape_constants.test_update_func(io_block_read_name, new_value1),
            COST_CONSTANT_OK);
  EXPECT_EQ(tape_constants.io_block_read_cost(), new_value1);

  // change tape_io_cost
  const LEX_CSTRING tape_io_name = {STRING_WITH_LEN("TAPE_IO_COST")};
  EXPECT_EQ(tape_constants.tape_io_cost(), 200.0);

  // Change default value for tape_io_cost
  EXPECT_EQ(tape_constants.test_update_default_func(tape_io_name, new_value1),
            COST_CONSTANT_OK);
  EXPECT_EQ(tape_constants.tape_io_cost(), new_value1);

  // Change engine specific value for tape_io_cost
  EXPECT_EQ(tape_constants.test_update_func(tape_io_name, new_value2),
            COST_CONSTANT_OK);
  EXPECT_EQ(tape_constants.tape_io_cost(), new_value2);

  // Test with an unknown cost constant name.
  EXPECT_EQ(tape_constants.test_update_func(unknown_name, new_value1),
            UNKNOWN_COST_NAME);
}

/*
  Test the Cost model constants interface.
*/
TEST_F(CostConstantsTest, CostConstants) {
  /*
    Test ref counter.
  */
  {
    Testable_Cost_model_constants cost_constants_ref_test;

    cost_constants_ref_test.test_inc_ref_count();
    cost_constants_ref_test.test_inc_ref_count();
    EXPECT_EQ(cost_constants_ref_test.test_dec_ref_count(), 1U);
    EXPECT_EQ(cost_constants_ref_test.test_dec_ref_count(), 0U);
  }

  /*
    Test default server cost constants.
  */
  Cost_model_constants cost_constants;

  const Server_cost_constants *server_const =
      cost_constants.get_server_cost_constants();

  validate_default_server_cost_constants(server_const);

  /*
    Test default table cost constants.
  */
  Fake_TABLE table(1, false);

  const SE_cost_constants *se_const =
      cost_constants.get_se_cost_constants(&table);

  // Validate expected default values for cost constants
  EXPECT_EQ(se_const->memory_block_read_cost(), default_memory_block_read_cost);
  EXPECT_EQ(se_const->io_block_read_cost(), default_io_block_read_cost);

  /*
    Test updating server constants.
  */
  const double new_value1 = 2.74;
  const double new_value2 = 3.14;
  const double new_value3 = 5.00;

  const LEX_CSTRING row_evaluate_name = {STRING_WITH_LEN("ROW_EVALUATE_COST")};
  EXPECT_EQ(
      cost_constants.update_server_cost_constant(row_evaluate_name, new_value1),
      COST_CONSTANT_OK);
  server_const = cost_constants.get_server_cost_constants();
  EXPECT_EQ(server_const->row_evaluate_cost(), new_value1);

  const LEX_CSTRING unknown_name = {STRING_WITH_LEN("UNKNOWN_COST")};
  EXPECT_EQ(
      cost_constants.update_server_cost_constant(unknown_name, new_value2),
      UNKNOWN_COST_NAME);

  // Test with illegal cost constant values
  EXPECT_EQ(server_const->row_evaluate_cost(), new_value1);
  EXPECT_EQ(cost_constants.update_server_cost_constant(row_evaluate_name, -1.0),
            INVALID_COST_VALUE);
  EXPECT_EQ(server_const->row_evaluate_cost(), new_value1);
  EXPECT_EQ(cost_constants.update_server_cost_constant(row_evaluate_name, 0.0),
            INVALID_COST_VALUE);
  EXPECT_EQ(server_const->row_evaluate_cost(), new_value1);

  /*
    Test updating table cost constants.
  */
  const LEX_CSTRING default_name = {STRING_WITH_LEN("default")};
  const LEX_CSTRING io_block_read_name = {
      STRING_WITH_LEN("IO_BLOCK_READ_COST")};
  EXPECT_EQ(cost_constants.update_engine_cost_constant(
                nullptr, default_name, 0, io_block_read_name, new_value1),
            COST_CONSTANT_OK);

  // Verify that the cost constant is updated
  se_const = cost_constants.get_se_cost_constants(&table);
  EXPECT_EQ(se_const->io_block_read_cost(), new_value1);

  /*
    Do a second update of the same default value.
  */
  EXPECT_EQ(cost_constants.update_engine_cost_constant(
                nullptr, default_name, 0, io_block_read_name, new_value2),
            COST_CONSTANT_OK);

  // Verify that the cost constant is updated
  se_const = cost_constants.get_se_cost_constants(&table);
  EXPECT_EQ(se_const->io_block_read_cost(), new_value2);

  /*
    Test with illegal cost constant values.
  */
  EXPECT_EQ(cost_constants.update_engine_cost_constant(
                nullptr, default_name, 0, io_block_read_name, -1.0),
            INVALID_COST_VALUE);
  EXPECT_EQ(se_const->io_block_read_cost(), new_value2);
  EXPECT_EQ(cost_constants.update_engine_cost_constant(nullptr, default_name, 0,
                                                       io_block_read_name, 0.0),
            INVALID_COST_VALUE);
  EXPECT_EQ(se_const->io_block_read_cost(), new_value2);

  /*
    Test updating with a none existing cost constant.
  */
  const LEX_CSTRING lunch_cost_name = {STRING_WITH_LEN("LUNCH_COST")};
  EXPECT_EQ(cost_constants.update_engine_cost_constant(
                nullptr, default_name, 0, lunch_cost_name, new_value1),
            UNKNOWN_COST_NAME);

  /*
    Test handling of illegal storage category number.
  */
  EXPECT_EQ(cost_constants.update_engine_cost_constant(
                nullptr, default_name, 100, io_block_read_name, new_value1),
            INVALID_DEVICE_TYPE);

  // Verify that the cost constant is not updated
  se_const = cost_constants.get_se_cost_constants(&table);
  EXPECT_EQ(se_const->io_block_read_cost(), new_value2);

  /*
    Create two table objects that are stored in different storage engines.
  */
  Fake_TABLE table_se1(1, false);
  table_se1.file->ht->slot = 1;
  Fake_TABLE table_se2(1, false);
  table_se2.file->ht->slot = 2;

  Cost_model_constants cost_constants2;

  const SE_cost_constants *se_cost1 =
      cost_constants2.get_se_cost_constants(&table_se1);
  const SE_cost_constants *se_cost2 =
      cost_constants2.get_se_cost_constants(&table_se2);

  // Verify that both tables have the same default value
  EXPECT_EQ(se_cost1->io_block_read_cost(), default_io_block_read_cost);
  EXPECT_EQ(se_cost2->io_block_read_cost(), default_io_block_read_cost);

  // Update the default value and verify that the cost constant for both
  // tables are changed.
  EXPECT_EQ(cost_constants2.update_engine_cost_constant(
                nullptr, default_name, 0, io_block_read_name, new_value1),
            COST_CONSTANT_OK);
  EXPECT_EQ(se_cost1->io_block_read_cost(), new_value1);
  EXPECT_EQ(se_cost2->io_block_read_cost(), new_value1);

  /*
    Create a cost model constants set that has cost constants for the
    two storage engines named "Karius" and "Baktus". We use this instead
    of the real Cost_model_constant class since that relies on that the
    plugins for storage engines have been initialized.
  */
  Testable_Cost_model_constants cost_constants3;

  const LEX_CSTRING karius_name = {STRING_WITH_LEN("Karius")};

  // Create two tables that use these storage engines
  Fake_TABLE table_karius(1, false);
  table_karius.file->ht->slot = 4;  // Karius is in ht->slot 4
  Fake_TABLE table_baktus(1, false);
  table_baktus.file->ht->slot = 7;  // Baktus is in ht->slot 7

  // Get cost constants to use for these tables
  const SE_cost_constants *se_cost_karius =
      cost_constants3.get_se_cost_constants(&table_karius);
  const SE_cost_constants *se_cost_baktus =
      cost_constants3.get_se_cost_constants(&table_baktus);

  // Verify that both tables have the same default value
  EXPECT_EQ(se_cost_karius->io_block_read_cost(), default_io_block_read_cost);
  EXPECT_EQ(se_cost_baktus->io_block_read_cost(), default_io_block_read_cost);

  // Update the default value and verify that the cost constant for both
  // tables are changed.
  EXPECT_EQ(cost_constants3.update_engine_cost_constant(
                nullptr, default_name, 0, io_block_read_name, new_value1),
            COST_CONSTANT_OK);
  EXPECT_EQ(se_cost_karius->io_block_read_cost(), new_value1);
  EXPECT_EQ(se_cost_baktus->io_block_read_cost(), new_value1);

  // Update one of the storage engines with a new cost value and verify that
  // only this engine got the new cost value
  EXPECT_EQ(cost_constants3.update_engine_cost_constant(
                nullptr, karius_name, 0, io_block_read_name, new_value2),
            COST_CONSTANT_OK);
  EXPECT_EQ(se_cost_karius->io_block_read_cost(), new_value2);
  EXPECT_EQ(se_cost_baktus->io_block_read_cost(), new_value1);

  // Do a new update of the default value and verify that only the storage
  // engine that still is using the default value is changed.
  EXPECT_EQ(cost_constants3.update_engine_cost_constant(
                nullptr, default_name, 0, io_block_read_name, new_value3),
            COST_CONSTANT_OK);
  EXPECT_EQ(se_cost_karius->io_block_read_cost(), new_value2);
  EXPECT_EQ(se_cost_baktus->io_block_read_cost(), new_value3);
}

}  // namespace costconstants_unittest