| 1 | /* Copyright (c) 2013, 2026, 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 | #include <gtest/gtest.h> |
| 25 | #include <sys/types.h> |
| 26 | |
| 27 | #include "mysql/psi/mysql_mutex.h" |
| 28 | #include "sql/log.h" |
| 29 | #include "unittest/gunit/test_utils.h" |
| 30 | |
| 31 | namespace log_throttle_unittest { |
| 32 | |
| 33 | using my_testing::Server_initializer; |
| 34 | |
| 35 | int summary_count = 0; |
| 36 | char last_query[10]; |
| 37 | |
| 38 | static bool slow_logger(THD *, const char *query, size_t, bool, ulonglong, |
| 39 | ulonglong) { |
| 40 | summary_count++; |
| 41 | strcpy(last_query, query); |
| 42 | return false; |
| 43 | } |
| 44 | |
| 45 | class LogThrottleTest : public ::testing::Test { |
| 46 | protected: |
| 47 | void SetUp() override { |
| 48 | initializer.SetUp(); |
| 49 | mysql_mutex_init(0, &m_mutex, MY_MUTEX_INIT_FAST); |
| 50 | summary_count = 0; |
| 51 | } |
| 52 | |
| 53 | void TearDown() override { |
| 54 | initializer.TearDown(); |
| 55 | mysql_mutex_destroy(&m_mutex); |
| 56 | } |
| 57 | |
| 58 | THD *thd() { return initializer.thd(); } |
| 59 | |
| 60 | Server_initializer initializer; |
| 61 | mysql_mutex_t m_mutex; |
| 62 | }; |
| 63 | |
| 64 | // Slow_log_throttles test cases starts from here. |
| 65 | |
| 66 | /* |
| 67 | Test basic functionality - throttling, eligibility, printing of summary of |
| 68 | Slow_log_throttle. |
| 69 | */ |
| 70 | TEST_F(LogThrottleTest, SlowLogBasic) { |
| 71 | ulong threshold = 2; |
| 72 | ulong window = 1000000; |
| 73 | Slow_log_throttle throttle(&threshold, &m_mutex, window, slow_logger, "%lu"); |
| 74 | |
| 75 | // Should not be throttled |
| 76 | EXPECT_FALSE(throttle.log(thd(), true)); |
| 77 | EXPECT_FALSE(throttle.log(thd(), true)); |
| 78 | |
| 79 | // Should not be throttled, not eligible |
| 80 | EXPECT_FALSE(throttle.log(thd(), false)); |
| 81 | |
| 82 | // Flush and check that summary was not printed |
| 83 | EXPECT_FALSE(throttle.flush(thd())); |
| 84 | EXPECT_EQ(0, summary_count); |
| 85 | |
| 86 | // Should be throttled |
| 87 | EXPECT_TRUE(throttle.log(thd(), true)); |
| 88 | |
| 89 | // Flush and check that summary was printed |
| 90 | EXPECT_TRUE(throttle.flush(thd())); |
| 91 | EXPECT_EQ(1, summary_count); |
| 92 | |
| 93 | // Flush and check that summary was not printed again |
| 94 | EXPECT_FALSE(throttle.flush(thd())); |
| 95 | EXPECT_EQ(1, summary_count); |
| 96 | |
| 97 | // Get another summary printed |
| 98 | EXPECT_FALSE(throttle.log(thd(), true)); |
| 99 | EXPECT_FALSE(throttle.log(thd(), true)); |
| 100 | EXPECT_TRUE(throttle.log(thd(), true)); |
| 101 | EXPECT_TRUE(throttle.flush(thd())); |
| 102 | EXPECT_EQ(2, summary_count); |
| 103 | } |
| 104 | |
| 105 | // Test changes to the threshold level of slow logger |
| 106 | TEST_F(LogThrottleTest, SlowLogThresholdChange) { |
| 107 | ulong threshold = 2; |
| 108 | ulong window = 1000000; |
| 109 | Slow_log_throttle throttle(&threshold, &m_mutex, window, slow_logger, "%lu"); |
| 110 | |
| 111 | // Reach threshold |
| 112 | EXPECT_FALSE(throttle.log(thd(), true)); |
| 113 | EXPECT_FALSE(throttle.log(thd(), true)); |
| 114 | |
| 115 | // Flush and check that summary was not printed |
| 116 | EXPECT_FALSE(throttle.flush(thd())); |
| 117 | EXPECT_EQ(0, summary_count); |
| 118 | |
| 119 | // Reduce threshold, flush and check that summary was printed |
| 120 | threshold = 1; |
| 121 | EXPECT_TRUE(throttle.flush(thd())); |
| 122 | EXPECT_EQ(1, summary_count); |
| 123 | |
| 124 | // Increase threshold and reach it |
| 125 | threshold = 3; |
| 126 | EXPECT_FALSE(throttle.log(thd(), true)); |
| 127 | EXPECT_FALSE(throttle.log(thd(), true)); |
| 128 | EXPECT_FALSE(throttle.log(thd(), true)); |
| 129 | |
| 130 | // Flush and check that summary was not printed |
| 131 | EXPECT_FALSE(throttle.flush(thd())); |
| 132 | EXPECT_EQ(1, summary_count); |
| 133 | } |
| 134 | |
| 135 | // Test number of suppressed messages written by slow logger |
| 136 | TEST_F(LogThrottleTest, SlowLogSuppressCount) { |
| 137 | ulong threshold = 1; |
| 138 | ulong window = 1000000; |
| 139 | Slow_log_throttle throttle(&threshold, &m_mutex, window, slow_logger, "%lu"); |
| 140 | |
| 141 | // Suppress 3 events |
| 142 | EXPECT_FALSE(throttle.log(thd(), true)); |
| 143 | EXPECT_TRUE(throttle.log(thd(), true)); |
| 144 | EXPECT_TRUE(throttle.log(thd(), true)); |
| 145 | EXPECT_TRUE(throttle.log(thd(), true)); |
| 146 | EXPECT_TRUE(throttle.flush(thd())); |
| 147 | EXPECT_EQ(1, summary_count); |
| 148 | EXPECT_STREQ("3", last_query); |
| 149 | |
| 150 | // Suppress 2 events |
| 151 | EXPECT_FALSE(throttle.log(thd(), true)); |
| 152 | EXPECT_FALSE(throttle.log(thd(), false)); |
| 153 | EXPECT_TRUE(throttle.log(thd(), true)); |
| 154 | EXPECT_TRUE(throttle.log(thd(), true)); |
| 155 | EXPECT_FALSE(throttle.log(thd(), false)); |
| 156 | EXPECT_TRUE(throttle.flush(thd())); |
| 157 | EXPECT_EQ(2, summary_count); |
| 158 | EXPECT_STREQ("2", last_query); |
| 159 | } |
| 160 | // End of Slow_log_throttles test cases. |
| 161 | |
| 162 | } // namespace log_throttle_unittest |