Fix replicated tables startup when updating from old version

This commit is contained in:
alesapin 2020-04-07 18:28:29 +03:00
parent 385e8839dc
commit 82a87bc0d2
3 changed files with 69 additions and 1 deletions

View File

@ -256,6 +256,15 @@ StorageReplicatedMergeTree::StorageReplicatedMergeTree(
}
else
{
/// In old tables this node may missing
String replica_metadata;
bool replica_metadata_exists = current_zookeeper->tryGet(replica_path + "/metadata", replica_metadata);
if (!replica_metadata_exists || replica_metadata.empty())
{
ReplicatedMergeTreeTableMetadata current_metadata(*this);
current_zookeeper->createOrUpdate(replica_path + "/metadata", current_metadata.toString(), zkutil::CreateMode::Persistent);
}
checkTableStructure(replica_path);
checkParts(skip_sanity_checks);
@ -263,8 +272,13 @@ StorageReplicatedMergeTree::StorageReplicatedMergeTree(
{
metadata_version = parse<int>(current_zookeeper->get(replica_path + "/metadata_version"));
}
else /// This replica was created on old version, so we have to take version of global node
else
{
/// This replica was created with old clickhouse version, so we have
/// to take version of global node. If somebody will alter our
/// table, than we will fill /metadata_version node in zookeeper.
/// Otherwise on the next restart we can again use version from
/// shared metadata node because it was not changed.
Coordination::Stat metadata_stat;
current_zookeeper->get(zookeeper_path + "/metadata", &metadata_stat);
metadata_version = metadata_stat.version;

View File

@ -0,0 +1,54 @@
import time
import pytest
from helpers.cluster import ClickHouseCluster
cluster = ClickHouseCluster(__file__)
node1 = cluster.add_instance('node1', with_zookeeper=True)
@pytest.fixture(scope="module")
def start_cluster():
try:
cluster.start()
yield cluster
finally:
cluster.shutdown()
def test_table_start_without_metadata(start_cluster):
node1.query("""
CREATE TABLE test (date Date)
ENGINE = ReplicatedMergeTree('/clickhouse/table/test_table', '1')
ORDER BY tuple()
""")
node1.query("INSERT INTO test VALUES(toDate('2019-12-01'))")
assert node1.query("SELECT date FROM test") == "2019-12-01\n"
# some fake alter
node1.query("ALTER TABLE test MODIFY COLUMN date Date DEFAULT toDate('2019-10-01')")
assert node1.query("SELECT date FROM test") == "2019-12-01\n"
node1.query("DETACH TABLE test")
zk_cli = cluster.get_kazoo_client('zoo1')
# simulate update from old version
zk_cli.delete("/clickhouse/table/test_table/replicas/1/metadata")
zk_cli.delete("/clickhouse/table/test_table/replicas/1/metadata_version")
node1.query("ATTACH TABLE test")
assert node1.query("SELECT date FROM test") == "2019-12-01\n"
node1.query("ALTER TABLE test MODIFY COLUMN date Date DEFAULT toDate('2019-09-01')")
node1.query("DETACH TABLE test")
zk_cli.set("/clickhouse/table/test_table/replicas/1/metadata", "")
node1.query("ATTACH TABLE test")
assert node1.query("SELECT date FROM test") == "2019-12-01\n"