Skip to content

Commit 67049ee

Browse files
authored
Merge pull request ClickHouse#89566 from ClickHouse/backport/25.8/89496
Backport ClickHouse#89496 to 25.8: Fix changelog load in Keeper if rename failed
2 parents 832b636 + 8c6fcbf commit 67049ee

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

src/Coordination/Changelog.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1881,7 +1881,17 @@ try
18811881
}
18821882

18831883
ChangelogReader reader(changelog_description_ptr);
1884-
last_log_read_result = reader.readChangelog(entry_storage, start_to_read_from, log);
1884+
auto log_read_result = reader.readChangelog(entry_storage, start_to_read_from, log);
1885+
1886+
/// We didn't find the first required log in this changelog so we move to the next changelog
1887+
/// This can happen in case we failed to rename changelog to a name with correct first and last log index
1888+
if (log_read_result.first_read_index == 0)
1889+
{
1890+
LOG_TRACE(log, "Changelog contains only logs before {}", start_to_read_from);
1891+
continue;
1892+
}
1893+
1894+
last_log_read_result = std::move(log_read_result);
18851895

18861896
if (last_log_read_result->last_read_index != 0)
18871897
last_read_index = last_log_read_result->last_read_index;

src/Coordination/tests/gtest_coordination_changelog.cpp

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1149,7 +1149,6 @@ TYPED_TEST(CoordinationChangelogTest, TestRotateIntervalChanges)
11491149

11501150
TYPED_TEST(CoordinationChangelogTest, ChangelogTestMaxLogSize)
11511151
{
1152-
11531152
ChangelogDirTest test("./logs");
11541153
this->setLogDirectory("./logs");
11551154

@@ -1524,4 +1523,59 @@ TYPED_TEST(CoordinationChangelogTest, ChangelogTestBrokenWriteAt)
15241523
}
15251524
}
15261525

1526+
TYPED_TEST(CoordinationChangelogTest, ChangelogLoadingFromInvalidName)
1527+
{
1528+
if (this->enable_compression)
1529+
return;
1530+
1531+
ChangelogDirTest test("./logs");
1532+
this->setLogDirectory("./logs");
1533+
1534+
{
1535+
DB::KeeperLogStore changelog(
1536+
DB::LogFileSettings{
1537+
.force_sync = true, .compress_logs = this->enable_compression, .rotate_interval = 100'000, .max_size = 500},
1538+
DB::FlushSettings(),
1539+
this->keeper_context);
1540+
changelog.init(1, 0);
1541+
1542+
EXPECT_TRUE(fs::exists("./logs/changelog_1_100000.bin"));
1543+
for (size_t i = 0; i < 500; ++i)
1544+
{
1545+
auto entry = getLogEntry(std::to_string(i) + "_hello_world", 1);
1546+
changelog.append(entry);
1547+
}
1548+
changelog.end_of_append_batch(0, 0);
1549+
1550+
waitDurableLogs(changelog);
1551+
}
1552+
1553+
// Find file starting with "changelog_1_" (renamed because of file size limit)
1554+
fs::path new_changelog_path;
1555+
for (const auto & entry : fs::directory_iterator("./logs"))
1556+
{
1557+
if (entry.is_regular_file())
1558+
{
1559+
const auto filename = entry.path().filename().string();
1560+
if (filename.starts_with("changelog_1_"))
1561+
new_changelog_path = entry.path();
1562+
}
1563+
}
1564+
1565+
ASSERT_NE(new_changelog_path, fs::path{});
1566+
1567+
fs::rename(new_changelog_path, "./logs/changelog_1_100000.bin");
1568+
1569+
std::cout << new_changelog_path << std::endl;
1570+
1571+
DB::KeeperLogStore changelog(
1572+
DB::LogFileSettings{
1573+
.force_sync = true, .compress_logs = this->enable_compression, .rotate_interval = 100'000, .max_size = 500},
1574+
DB::FlushSettings(),
1575+
this->keeper_context);
1576+
changelog.init(15, 0);
1577+
1578+
ASSERT_EQ(changelog.next_slot(), 501);
1579+
}
1580+
15271581
#endif

0 commit comments

Comments
 (0)