| 102 | } |
| 103 | |
| 104 | void MaterializeMySQLSyncThread::synchronization() |
| 105 | { |
| 106 | setThreadName(MYSQL_BACKGROUND_THREAD_NAME); |
| 107 | |
| 108 | try |
| 109 | { |
| 110 | /// Before it will be read from persistent file each time; so if data is flushed, it will be updated |
| 111 | MaterializeMetadata metadata(binlog_info, getContext()->getSettingsRef()); |
| 112 | bool need_reconnect = true; |
| 113 | const auto thread_schedule_interval_ms = settings->sync_thread_schedule_interval_ms.value; |
| 114 | UInt64 max_flush_time = settings->max_flush_data_time; |
| 115 | |
| 116 | Buffers buffers(database_name, thread_key); |
| 117 | UInt64 sum{0}, last{0}; |
| 118 | bool got_event{false}; |
| 119 | Stopwatch watch; |
| 120 | |
| 121 | while (!isCancelled()) |
| 122 | { |
| 123 | if (need_reconnect) |
| 124 | { |
| 125 | if (!prepareSynchronized(metadata)) |
| 126 | break; |
| 127 | need_reconnect = false; |
| 128 | } |
| 129 | |
| 130 | try |
| 131 | { |
| 132 | got_event = false; |
| 133 | BinlogEventPtr binlog_event = client.readOneBinlogEvent(std::max(UInt64(1), max_flush_time - watch.elapsedMilliseconds())); |
| 134 | if (binlog_event) |
| 135 | { |
| 136 | got_event = true; |
| 137 | ++sum; |
| 138 | onEvent(buffers, binlog_event, metadata); |
| 139 | } |
| 140 | |
| 141 | exception_occur_times = 0; |
| 142 | /// Do NOT catch exception of `flushBuffersData` here, |
| 143 | /// or the data will be lost if exception occurs and skip_error_count enabled |
| 144 | } |
| 145 | catch (const Exception & e) |
| 146 | { |
| 147 | exception_occur_times = std::min(10ul, exception_occur_times + 1); |
| 148 | |
| 149 | if (e.code() == ErrorCodes::CANNOT_READ_ALL_DATA && settings->max_wait_time_when_mysql_unavailable >= 0) |
| 150 | { |
| 151 | flushBuffersData(buffers, metadata); |
| 152 | recordException(); |
| 153 | need_reconnect = true; |
| 154 | tryLogCurrentException(log, "Lost connect to MySQL and we will retry later"); |
| 155 | std::this_thread::sleep_for(std::chrono::milliseconds(settings->max_wait_time_when_mysql_unavailable)); |
| 156 | continue; |
| 157 | } |
| 158 | else if (settings->skip_error_count < 0 || settings->skip_error_count > already_skip_errors) |
| 159 | { |
| 160 | /// Print stack of the exception for debug |
| 161 | tryLogCurrentException(log, "Skip this error due to MaterializedMySQLSetting [skip_error_count = " + settings->skip_error_count.toString() |
nothing calls this directly
no test coverage detected