Skip to content

Commit 417cf72

Browse files
authored
Merge pull request ClickHouse#77774 from ClickHouse/notach
Fix refreshable materialized view not working on newly added replicas
2 parents 9fa5b62 + 8aa5421 commit 417cf72

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

src/Storages/MaterializedView/RefreshTask.cpp

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ namespace ErrorCodes
6363
}
6464

6565
RefreshTask::RefreshTask(
66-
StorageMaterializedView * view_, ContextPtr context, const DB::ASTRefreshStrategy & strategy, bool attach, bool coordinated, bool empty)
66+
StorageMaterializedView * view_, ContextPtr context, const DB::ASTRefreshStrategy & strategy, bool /* attach */, bool coordinated, bool empty)
6767
: log(getLogger("RefreshTask"))
6868
, view(view_)
6969
, refresh_schedule(strategy)
@@ -88,29 +88,22 @@ RefreshTask::RefreshTask(
8888

8989
auto zookeeper = context->getZooKeeper();
9090
String replica_path = coordination.path + "/replicas/" + coordination.replica_name;
91+
bool replica_path_existed = zookeeper->exists(replica_path);
9192

92-
if (server_settings[ServerSetting::disable_insertion_and_mutation])
93-
coordination.read_only = true;
94-
95-
if (attach)
96-
{
97-
/// Check that this replica is registered in keeper.
98-
if (!zookeeper->exists(replica_path))
99-
{
100-
LOG_ERROR(log, "Attaching refreshable materialized view {} as read-only because znode {} is missing", view->getStorageID().getFullTableName(), replica_path);
101-
coordination.read_only = true;
102-
}
103-
}
104-
else
93+
/// Create znodes even if it's ATTACH query. This seems weird, possibly incorrect, but
94+
/// currently both DatabaseReplicated and DatabaseShared seem to require this behavior.
95+
if (!replica_path_existed)
10596
{
10697
zookeeper->createAncestors(coordination.path);
107-
/// Create coordination znodes if they don't exist. Register this replica, throwing if already exists.
10898
Coordination::Requests ops;
10999
ops.emplace_back(zkutil::makeCreateRequest(coordination.path, coordination.root_znode.toString(), zkutil::CreateMode::Persistent, true));
110100
ops.emplace_back(zkutil::makeCreateRequest(coordination.path + "/replicas", "", zkutil::CreateMode::Persistent, true));
111101
ops.emplace_back(zkutil::makeCreateRequest(replica_path, "", zkutil::CreateMode::Persistent));
112102
zookeeper->multi(ops);
113103
}
104+
105+
if (server_settings[ServerSetting::disable_insertion_and_mutation])
106+
coordination.read_only = true;
114107
}
115108
}
116109

tests/integration/test_refreshable_mv/test.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,30 @@ def test_refresh_vs_shutdown_smoke(started_cluster):
338338
node1.query("drop database re sync")
339339
node2.query("drop database re sync")
340340

341+
def test_adding_replica(started_cluster):
342+
node1.query(
343+
"create database re engine = Replicated('/test/re', 'shard1', 'r1');"
344+
"create materialized view re.a refresh every 1 second (x Int64) engine ReplicatedMergeTree order by x as select number*10 as x from numbers(2);"
345+
"system wait view re.a")
346+
assert node1.query("select * from re.a order by all") == "0\n10\n"
347+
assert node1.query("select last_refresh_replica from system.view_refreshes") == "1\n"
348+
349+
r = node2.query(
350+
"create database re engine = Replicated('/test/re', 'shard1', 'r2');"
351+
"system sync database replica re")
352+
assert node2.query("select * from re.a order by all") == "0\n10\n"
353+
354+
node1.query("system stop view re.a")
355+
r = node2.query(
356+
"system wait view re.a;"
357+
"system refresh view re.a;"
358+
"system wait view re.a;"
359+
"select last_refresh_replica from system.view_refreshes")
360+
assert r == "2\n"
361+
362+
node1.query("drop database re sync")
363+
node2.query("drop database re sync")
364+
341365
def test_replicated_db_startup_race(started_cluster):
342366
for node in nodes:
343367
node.query(

0 commit comments

Comments
 (0)