Review fixes and better tests

This commit is contained in:
alesapin 2019-07-09 12:02:52 +03:00
parent 6a3b543e10
commit 9a17a461ea
5 changed files with 69 additions and 14 deletions

View File

@ -1141,19 +1141,24 @@ CheckResults StorageMergeTree::checkData(const ASTPtr & query, const Context & c
String full_part_path = part->getFullPath();
/// If the checksums file is not present, calculate the checksums and write them to disk.
String checksums_path = full_part_path + "checksums.txt";
String tmp_checksums_path = full_part_path + "checksums.txt.tmp";
if (!Poco::File(checksums_path).exists())
{
try
{
auto counted_checksums = checkDataPart(part, false, primary_key_data_types, skip_indices);
counted_checksums.checkEqual(part->checksums, true);
WriteBufferFromFile out(full_part_path + "checksums.txt.tmp", 4096);
auto calculated_checksums = checkDataPart(part, false, primary_key_data_types, skip_indices);
calculated_checksums.checkEqual(part->checksums, true);
WriteBufferFromFile out(tmp_checksums_path, 4096);
part->checksums.write(out);
Poco::File(full_part_path + "checksums.txt.tmp").renameTo(full_part_path + "checksums.txt");
Poco::File(tmp_checksums_path).renameTo(checksums_path);
results.emplace_back(part->name, true, "Checksums recounted and written to disk.");
}
catch (Exception & ex)
catch (const Exception & ex)
{
Poco::File tmp_file(tmp_checksums_path);
if (tmp_file.exists())
tmp_file.remove();
results.emplace_back(part->name, false,
"Check of part finished with error: '" + ex.message() + "'");
}
@ -1165,7 +1170,7 @@ CheckResults StorageMergeTree::checkData(const ASTPtr & query, const Context & c
checkDataPart(part, true, primary_key_data_types, skip_indices);
results.emplace_back(part->name, true, "");
}
catch (Exception & ex)
catch (const Exception & ex)
{
results.emplace_back(part->name, false, ex.message());
}

View File

@ -5127,9 +5127,9 @@ CheckResults StorageReplicatedMergeTree::checkData(const ASTPtr & query, const C
{
results.push_back(part_check_thread.checkPart(part->name));
}
catch (Exception & ex)
catch (const Exception & ex)
{
results.emplace_back(part->name, false, "Error during check:" + ex.message());
results.emplace_back(part->name, false, "Check of part finished with error: '" + ex.message() + "'");
}
}
return results;

View File

@ -112,16 +112,18 @@ def test_check_replicated_table_corruption(started_cluster):
assert node1.query("SELECT count() from replicated_mt") == "4\n"
assert node2.query("SELECT count() from replicated_mt") == "4\n"
corrupt_data_part_on_disk(node1, "replicated_mt", "201901_0_0_0")
assert node1.query("CHECK TABLE replicated_mt PARTITION 201901") == "201901_0_0_0\t0\tPart 201901_0_0_0 looks broken. Removing it and queueing a fetch.\n"
part_name = node1.query("SELECT name from system.parts where table = 'replicated_mt' and partition_id = '201901' and active = 1").strip()
corrupt_data_part_on_disk(node1, "replicated_mt", part_name)
assert node1.query("CHECK TABLE replicated_mt PARTITION 201901") == "{p}\t0\tPart {p} looks broken. Removing it and queueing a fetch.\n".format(p=part_name)
node1.query("SYSTEM SYNC REPLICA replicated_mt")
assert node1.query("CHECK TABLE replicated_mt PARTITION 201901") == "201901_0_0_0\t1\t\n"
assert node1.query("CHECK TABLE replicated_mt PARTITION 201901") == "{}\t1\t\n".format(part_name)
assert node1.query("SELECT count() from replicated_mt") == "4\n"
remove_part_from_disk(node2, "replicated_mt", "201901_0_0_0")
assert node2.query("CHECK TABLE replicated_mt PARTITION 201901") == "201901_0_0_0\t0\tPart 201901_0_0_0 looks broken. Removing it and queueing a fetch.\n"
remove_part_from_disk(node2, "replicated_mt", part_name)
assert node2.query("CHECK TABLE replicated_mt PARTITION 201901") == "{p}\t0\tPart {p} looks broken. Removing it and queueing a fetch.\n".format(p=part_name)
node1.query("SYSTEM SYNC REPLICA replicated_mt")
assert node1.query("CHECK TABLE replicated_mt PARTITION 201901") == "201901_0_0_0\t1\t\n"
assert node1.query("CHECK TABLE replicated_mt PARTITION 201901") == "{}\t1\t\n".format(part_name)
assert node1.query("SELECT count() from replicated_mt") == "4\n"

View File

@ -0,0 +1,11 @@
201901_1_1_0 1
========
201901_1_1_0 1
201901_2_2_0 1
========
201901_1_2_1 1
========
201901_1_2_1 1
201902_3_3_0 1
========
201902_3_4_1 1

View File

@ -0,0 +1,37 @@
DROP TABLE IF EXISTS mt_table;
CREATE TABLE mt_table (d Date, key UInt64, data String) ENGINE = MergeTree() PARTITION BY toYYYYMM(d) ORDER BY key;
CHECK TABLE mt_table;
INSERT INTO mt_table VALUES (toDate('2019-01-02'), 1, 'Hello'), (toDate('2019-01-02'), 2, 'World');
CHECK TABLE mt_table;
INSERT INTO mt_table VALUES (toDate('2019-01-02'), 3, 'quick'), (toDate('2019-01-02'), 4, 'brown');
SELECT '========';
CHECK TABLE mt_table;
OPTIMIZE TABLE mt_table FINAL;
SELECT '========';
CHECK TABLE mt_table;
SELECT '========';
INSERT INTO mt_table VALUES (toDate('2019-02-03'), 5, '!'), (toDate('2019-02-03'), 6, '?');
CHECK TABLE mt_table;
SELECT '========';
INSERT INTO mt_table VALUES (toDate('2019-02-03'), 7, 'jump'), (toDate('2019-02-03'), 8, 'around');
OPTIMIZE TABLE mt_table FINAL;
CHECK TABLE mt_table PARTITION 201902;
DROP TABLE IF EXISTS mt_table;