Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid deadlock when starting table in attach thread of ReplicatedMergeTree #50026

Merged
merged 4 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/Storages/MergeTree/ReplicatedMergeTreeRestartingThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class ReplicatedMergeTreeRestartingThread

void shutdown(bool part_of_full_shutdown);

void run();
private:
StorageReplicatedMergeTree & storage;
String log_name;
Expand All @@ -43,8 +44,6 @@ class ReplicatedMergeTreeRestartingThread
UInt32 consecutive_check_failures = 0; /// How many consecutive checks have failed
bool first_time = true; /// Activate replica for the first time.

void run();

/// Restarts table if needed, returns false if it failed to restart replica.
bool runImpl();

Expand Down
25 changes: 17 additions & 8 deletions src/Storages/StorageReplicatedMergeTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4416,21 +4416,30 @@ void StorageReplicatedMergeTree::startupImpl(bool from_attach_thread)

startBeingLeader();

/// In this thread replica will be activated.
restarting_thread.start();
if (from_attach_thread)
{
/// Try activating replica in current thread.
restarting_thread.run();
antonio2368 marked this conversation as resolved.
Show resolved Hide resolved
}
else
{
/// Activate replica in a separate thread.
restarting_thread.start();

/// Wait while restarting_thread finishing initialization.
/// NOTE It does not mean that replication is actually started after receiving this event.
/// It only means that an attempt to startup replication was made.
/// Table may be still in readonly mode if this attempt failed for any reason.
startup_event.wait();
}

/// And this is just a callback
session_expired_callback_handler = EventNotifier::instance().subscribe(Coordination::Error::ZSESSIONEXPIRED, [this]()
{
LOG_TEST(log, "Received event for expired session. Waking up restarting thread");
restarting_thread.start();
});

/// Wait while restarting_thread finishing initialization.
/// NOTE It does not mean that replication is actually started after receiving this event.
/// It only means that an attempt to startup replication was made.
/// Table may be still in readonly mode if this attempt failed for any reason.
startup_event.wait();

startBackgroundMovesIfNeeded();

part_moves_between_shards_orchestrator.start();
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<clickhouse>
<background_schedule_pool_size>1</background_schedule_pool_size>
</clickhouse>
60 changes: 60 additions & 0 deletions tests/integration/test_replicated_table_attach/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import pytest

from helpers.cluster import ClickHouseCluster
from helpers.network import PartitionManager


cluster = ClickHouseCluster(__file__)

node = cluster.add_instance(
"node",
main_configs=["configs/config.xml"],
with_zookeeper=True,
stay_alive=True,
)


@pytest.fixture(scope="module")
def started_cluster():
try:
cluster.start()
yield cluster

finally:
cluster.shutdown()


def test_startup_with_small_bg_pool(started_cluster):
node.query(
"CREATE TABLE replicated_table (k UInt64, i32 Int32) ENGINE=ReplicatedMergeTree('/clickhouse/replicated_table', 'r1') ORDER BY k"
)

node.query("INSERT INTO replicated_table VALUES(20, 30)")

def assert_values():
assert node.query("SELECT * FROM replicated_table") == "20\t30\n"

assert_values()
node.restart_clickhouse(stop_start_wait_sec=10)
assert_values()

node.query("DROP TABLE replicated_table SYNC")


def test_startup_with_small_bg_pool_partitioned(started_cluster):
node.query(
"CREATE TABLE replicated_table_partitioned (k UInt64, i32 Int32) ENGINE=ReplicatedMergeTree('/clickhouse/replicated_table_partitioned', 'r1') ORDER BY k"
)

node.query("INSERT INTO replicated_table_partitioned VALUES(20, 30)")

def assert_values():
assert node.query("SELECT * FROM replicated_table_partitioned") == "20\t30\n"

assert_values()
with PartitionManager() as pm:
pm.drop_instance_zk_connections(node)
node.restart_clickhouse(stop_start_wait_sec=20)
assert_values()

node.query("DROP TABLE replicated_table_partitioned SYNC")