MCPcopy Create free account
hub / github.com/ClickHouse/ClickHouse / initializeMainThread

Method initializeMainThread

src/Databases/DatabaseReplicatedWorker.cpp:78–142  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

76}
77
78bool DatabaseReplicatedDDLWorker::initializeMainThread()
79{
80 {
81 std::lock_guard lock(initialization_duration_timer_mutex);
82 initialization_duration_timer.emplace();
83 initialization_duration_timer->start();
84 }
85
86 while (!stop_flag)
87 {
88 try
89 {
90 chassert(!database->is_probably_dropped);
91 auto zookeeper = getAndSetZooKeeper();
92 if (database->is_readonly)
93 database->tryConnectToZooKeeperAndInitDatabase(LoadingStrictnessLevel::ATTACH);
94 if (database->is_probably_dropped)
95 {
96 /// The flag was set in tryConnectToZooKeeperAndInitDatabase
97 LOG_WARNING(log, "Exiting main thread, because the database was probably dropped");
98 /// NOTE It will not stop cleanup thread until DDLWorker::shutdown() call (cleanup thread will just do nothing)
99 break;
100 }
101
102 if (database->db_settings[DatabaseReplicatedSetting::max_retries_before_automatic_recovery]
103 && database->db_settings[DatabaseReplicatedSetting::max_retries_before_automatic_recovery] <= subsequent_errors_count)
104 {
105 String current_task_name;
106 {
107 std::unique_lock lock{mutex};
108 current_task_name = current_task;
109 }
110 LOG_WARNING(log, "Database got stuck at processing task {}: it failed {} times in a row with the same error. "
111 "Will reset digest to mark our replica as lost, and trigger recovery from the most up-to-date metadata "
112 "from ZooKeeper. See max_retries_before_automatic_recovery setting. The error: {}",
113 current_task, subsequent_errors_count.load(), last_unexpected_error);
114
115 String digest_str;
116 zookeeper->tryGet(database->replica_path + "/digest", digest_str);
117 LOG_WARNING(log, "Resetting digest from {} to {}", digest_str, FORCE_AUTO_RECOVERY_DIGEST);
118 zookeeper->trySet(database->replica_path + "/digest", FORCE_AUTO_RECOVERY_DIGEST);
119 }
120
121 initializeReplication();
122 initialized = true;
123 {
124 std::lock_guard lock(initialization_duration_timer_mutex);
125 initialization_duration_timer.reset();
126 }
127 return true;
128 }
129 catch (...)
130 {
131 tryLogCurrentException(log, fmt::format("Error on initialization of {}", database->getDatabaseName()));
132 queue_updated_event->tryWait(5000);
133 }
134 }
135

Callers

nothing calls this directly

Calls 11

tryLogCurrentExceptionFunction · 0.50
formatFunction · 0.50
emplaceMethod · 0.45
startMethod · 0.45
loadMethod · 0.45
tryGetMethod · 0.45
trySetMethod · 0.45
resetMethod · 0.45
getDatabaseNameMethod · 0.45
tryWaitMethod · 0.45

Tested by

no test coverage detected