| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2017, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify |
| 6 |
it under the terms of the GNU General Public License, version 2.0, |
| 7 |
as published by the Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, |
| 18 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 |
GNU General Public License, version 2.0, for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License |
| 23 |
along with this program; if not, write to the Free Software |
| 24 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/**************************************************/ /** |
| 29 |
@file log/log0test.cc |
| 30 |
|
| 31 |
Tools used to test redo log in gtests (log0log-t.cc). |
| 32 |
|
| 33 |
*******************************************************/ |
| 34 |
|
| 35 |
#ifndef UNIV_HOTBACKUP |
| 36 |
|
| 37 |
#include "log0test.h" |
| 38 |
#include "log0log.h" |
| 39 |
|
| 40 |
/** Maximum size of payload put inside each MLOG_TEST record. */ |
| 41 |
static constexpr size_t MLOG_TEST_PAYLOAD_MAX_LEN = 50; |
| 42 |
|
| 43 |
/** Space id used for pages modified during tests of redo log. */ |
| 44 |
static constexpr size_t MLOG_TEST_PAGE_SPACE_ID = 1; |
| 45 |
|
| 46 |
/** Represents currently running test of redo log, nullptr otherwise. */ |
| 47 |
std::unique_ptr<Log_test> log_test; |
| 48 |
|
| 49 |
lsn_t Log_test::oldest_modification_approx() const { |
| 50 |
std::lock_guard<std::mutex> lock{m_mutex}; |
| 51 |
|
| 52 |
return m_buf.empty() ? 0 : m_buf.begin()->first; |
| 53 |
} |
| 54 |
|
| 55 |
void Log_test::add_dirty_page(const Page &page) { |
| 56 |
#ifndef UNIV_HOTBACKUP |
| 57 |
ut_a(log_is_data_lsn(page.oldest_modification)); |
| 58 |
ut_a(log_is_data_lsn(page.newest_modification)); |
| 59 |
#endif /* !UNIV_HOTBACKUP */ |
| 60 |
|
| 61 |
std::lock_guard<std::mutex> lock{m_mutex}; |
| 62 |
|
| 63 |
m_buf.insert(std::make_pair(page.oldest_modification, page)); |
| 64 |
} |
| 65 |
|
| 66 |
void Log_test::fsync_written_pages() { |
| 67 |
std::lock_guard<std::mutex> lock{m_mutex}; |
| 68 |
|
| 69 |
for (auto written : m_written) { |
| 70 |
m_flushed[written.first] = written.second; |
| 71 |
} |
| 72 |
|
| 73 |
m_written.clear(); |
| 74 |
} |
| 75 |
|
| 76 |
void Log_test::purge(lsn_t max_dirty_page_age) { |
| 77 |
std::lock_guard<std::mutex> purge_lock{m_purge_mutex}; |
| 78 |
|
| 79 |
std::unique_lock<std::mutex> lock{m_mutex}; |
| 80 |
|
| 81 |
if (m_buf.empty()) { |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
const lsn_t max_lsn = m_buf.rbegin()->first; |
| 86 |
|
| 87 |
while (!m_buf.empty() && |
| 88 |
max_lsn - m_buf.begin()->first > max_dirty_page_age) { |
| 89 |
auto it = m_buf.begin(); |
| 90 |
|
| 91 |
const auto &page = it->second; |
| 92 |
|
| 93 |
/* Fragment below would make it more similar to real env. |
| 94 |
However there is some issue now. */ |
| 95 |
#if 0 |
| 96 |
/* We need to avoid deadlock when resizing log |
| 97 |
buffer in background ... (because of m_mutex). */ |
| 98 |
if (page.newest_modification > log_sys->write_lsn.load()) { |
| 99 |
|
| 100 |
lock.unlock(); |
| 101 |
|
| 102 |
log_write_up_to( |
| 103 |
*log_sys, |
| 104 |
page.newest_modification, |
| 105 |
true); |
| 106 |
|
| 107 |
lock.lock(); |
| 108 |
continue; |
| 109 |
} |
| 110 |
#endif |
| 111 |
|
| 112 |
m_written[page.key] = page; |
| 113 |
|
| 114 |
m_buf.erase(it); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
byte *Log_test::create_mlog_rec(byte *rec, Key key, Value value) { |
| 119 |
const size_t payload = ut::random_from_interval(0, MLOG_TEST_PAYLOAD_MAX_LEN); |
| 120 |
return create_mlog_rec(rec, key, value, payload); |
| 121 |
} |
| 122 |
|
| 123 |
byte *Log_test::create_mlog_rec(byte *rec, Key key, Value value, |
| 124 |
size_t payload) { |
| 125 |
const space_id_t space_id = MLOG_TEST_PAGE_SPACE_ID; |
| 126 |
const page_no_t page_no = key; |
| 127 |
|
| 128 |
uchar *ptr; |
| 129 |
|
| 130 |
ptr = rec; |
| 131 |
|
| 132 |
mach_write_to_1(ptr, MLOG_TEST); |
| 133 |
ptr++; |
| 134 |
|
| 135 |
ptr += mach_write_compressed(ptr, space_id); |
| 136 |
|
| 137 |
ptr += mach_write_compressed(ptr, page_no); |
| 138 |
|
| 139 |
mach_write_to_8(ptr, key); |
| 140 |
ptr += 8; |
| 141 |
|
| 142 |
mach_write_to_8(ptr, value); |
| 143 |
ptr += 8; |
| 144 |
|
| 145 |
mach_write_to_2(ptr, payload); |
| 146 |
ptr += 2; |
| 147 |
|
| 148 |
std::memset(ptr, 0x00, payload); |
| 149 |
ptr += payload; |
| 150 |
|
| 151 |
/* Place for oldest_modification */ |
| 152 |
mach_write_to_8(ptr, 0); |
| 153 |
ptr += 8; |
| 154 |
|
| 155 |
/* Place for newest_modification */ |
| 156 |
mach_write_to_8(ptr, 0); |
| 157 |
ptr += 8; |
| 158 |
|
| 159 |
return ptr; |
| 160 |
} |
| 161 |
|
| 162 |
byte *Log_test::parse_mlog_rec(byte *begin, byte *end, Key &key, Value &value, |
| 163 |
lsn_t &start_lsn, lsn_t &end_lsn) { |
| 164 |
if (begin + 2 * 8 + 2 > end) { |
| 165 |
return nullptr; |
| 166 |
} |
| 167 |
|
| 168 |
key = static_cast<Key>(mach_read_from_8(begin)); |
| 169 |
begin += 8; |
| 170 |
|
| 171 |
value = static_cast<Value>(mach_read_from_8(begin)); |
| 172 |
begin += 8; |
| 173 |
|
| 174 |
const size_t payload = mach_read_from_2(begin); |
| 175 |
begin += 2; |
| 176 |
|
| 177 |
if (begin + payload + 2 * 8 > end) { |
| 178 |
return nullptr; |
| 179 |
} |
| 180 |
|
| 181 |
begin += payload; |
| 182 |
|
| 183 |
start_lsn = mach_read_from_8(begin); |
| 184 |
begin += 8; |
| 185 |
|
| 186 |
end_lsn = mach_read_from_8(begin); |
| 187 |
begin += 8; |
| 188 |
|
| 189 |
return begin; |
| 190 |
} |
| 191 |
|
| 192 |
byte *Log_test::parse_mlog_rec(byte *begin, byte *end) { |
| 193 |
Key key; |
| 194 |
Value value; |
| 195 |
lsn_t start_lsn, end_lsn; |
| 196 |
|
| 197 |
byte *ptr = parse_mlog_rec(begin, end, key, value, start_lsn, end_lsn); |
| 198 |
|
| 199 |
if (ptr == nullptr) { |
| 200 |
return nullptr; |
| 201 |
} |
| 202 |
|
| 203 |
if (value == MLOG_TEST_VALUE) { |
| 204 |
recovered_reset(key, start_lsn, end_lsn); |
| 205 |
|
| 206 |
recovered_add(key, value, start_lsn, end_lsn); |
| 207 |
|
| 208 |
} else if (value == 0) { |
| 209 |
recovered_reset(key, start_lsn, end_lsn); |
| 210 |
|
| 211 |
} else { |
| 212 |
recovered_add(key, value, start_lsn, end_lsn); |
| 213 |
} |
| 214 |
|
| 215 |
return ptr; |
| 216 |
} |
| 217 |
|
| 218 |
void Log_test::recovered_reset(Key key, lsn_t oldest_modification, |
| 219 |
lsn_t newest_modification) { |
| 220 |
Page page; |
| 221 |
page.key = key; |
| 222 |
page.value = 0; |
| 223 |
page.oldest_modification = oldest_modification; |
| 224 |
page.newest_modification = newest_modification; |
| 225 |
|
| 226 |
m_recovered[key] = page; |
| 227 |
} |
| 228 |
|
| 229 |
void Log_test::recovered_add(Key key, Value value, lsn_t oldest_modification, |
| 230 |
lsn_t newest_modification) { |
| 231 |
auto it = m_recovered.find(key); |
| 232 |
ut_a(it != m_recovered.end()); |
| 233 |
|
| 234 |
ut_a(it->second.oldest_modification == oldest_modification); |
| 235 |
ut_a(it->second.newest_modification == newest_modification); |
| 236 |
|
| 237 |
it->second.value += value; |
| 238 |
ut_a(it->second.value <= MLOG_TEST_VALUE); |
| 239 |
} |
| 240 |
|
| 241 |
const Log_test::Pages &Log_test::flushed() const { return m_flushed; } |
| 242 |
|
| 243 |
const Log_test::Pages &Log_test::recovered() const { return m_recovered; } |
| 244 |
|
| 245 |
void Log_test::sync_point(const std::string &sync_point_name) { |
| 246 |
auto it = m_sync_points.find(sync_point_name); |
| 247 |
|
| 248 |
if (it == m_sync_points.end()) { |
| 249 |
return; |
| 250 |
} |
| 251 |
|
| 252 |
it->second->sync(); |
| 253 |
} |
| 254 |
|
| 255 |
void Log_test::register_sync_point_handler( |
| 256 |
const std::string &sync_point_name, |
| 257 |
std::unique_ptr<Sync_point> &&sync_point_handler) { |
| 258 |
m_sync_points[sync_point_name] = std::move(sync_point_handler); |
| 259 |
} |
| 260 |
|
| 261 |
bool Log_test::enabled(Options option) const { |
| 262 |
return (m_options_enabled & uint64_t(option)) != 0; |
| 263 |
} |
| 264 |
|
| 265 |
void Log_test::set_enabled(Options option, bool enabled) { |
| 266 |
if (enabled) { |
| 267 |
m_options_enabled |= uint64_t(option); |
| 268 |
} else { |
| 269 |
m_options_enabled &= ~uint64_t(option); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
int Log_test::flush_every() const { return m_flush_every; } |
| 274 |
|
| 275 |
void Log_test::set_flush_every(int flush_every) { m_flush_every = flush_every; } |
| 276 |
|
| 277 |
int Log_test::verbosity() const { return m_verbosity; } |
| 278 |
|
| 279 |
void Log_test::set_verbosity(int level) { |
| 280 |
ut_a(level >= 0); |
| 281 |
m_verbosity = level; |
| 282 |
} |
| 283 |
|
| 284 |
#endif /* !UNIV_HOTBACKUP */ |