|
13 | 13 | from .converter import MysqlToClickhouseConverter, strip_sql_name, strip_sql_comments |
14 | 14 | from .table_structure import TableStructure |
15 | 15 | from .binlog_replicator import DataReader, LogEvent, EventType |
16 | | -from .utils import GracefulKiller |
| 16 | +from .utils import GracefulKiller, touch_all_files |
17 | 17 |
|
18 | 18 |
|
19 | 19 | logger = getLogger(__name__) |
@@ -89,6 +89,7 @@ class DbReplicator: |
89 | 89 | INITIAL_REPLICATION_BATCH_SIZE = 50000 |
90 | 90 | SAVE_STATE_INTERVAL = 10 |
91 | 91 | STATS_DUMP_INTERVAL = 60 |
| 92 | + BINLOG_TOUCH_INTERVAL = 120 |
92 | 93 |
|
93 | 94 | DATA_DUMP_INTERVAL = 1 |
94 | 95 | DATA_DUMP_BATCH_SIZE = 10000 |
@@ -120,6 +121,7 @@ def __init__(self, config: Settings, database: str, target_database: str = None, |
120 | 121 | self.records_to_insert = defaultdict(dict) # table_name => {record_id=>record, ...} |
121 | 122 | self.records_to_delete = defaultdict(set) # table_name => {record_id, ...} |
122 | 123 | self.last_records_upload_time = 0 |
| 124 | + self.last_touch_time = 0 |
123 | 125 |
|
124 | 126 | def run(self): |
125 | 127 | if self.state.status == Status.RUNNING_REALTIME_REPLICATION: |
@@ -156,6 +158,16 @@ def create_initial_structure_table(self, table_name): |
156 | 158 | self.state.tables_structure[table_name] = (mysql_structure, clickhouse_structure) |
157 | 159 | self.clickhouse_api.create_table(clickhouse_structure) |
158 | 160 |
|
| 161 | + def prevent_binlog_removal(self): |
| 162 | + if time.time() - self.last_touch_time < self.BINLOG_TOUCH_INTERVAL: |
| 163 | + return |
| 164 | + binlog_directory = os.path.join(self.config.binlog_replicator.data_dir, self.database) |
| 165 | + logger.info(f'touch binlog {binlog_directory}') |
| 166 | + if not os.path.exists(binlog_directory): |
| 167 | + return |
| 168 | + self.last_touch_time = time.time() |
| 169 | + touch_all_files(binlog_directory) |
| 170 | + |
159 | 171 | def perform_initial_replication(self): |
160 | 172 | self.clickhouse_api.database = self.target_database_tmp |
161 | 173 | logger.info('running initial replication') |
@@ -236,6 +248,7 @@ def perform_initial_replication_table(self, table_name): |
236 | 248 |
|
237 | 249 | self.state.initial_replication_max_primary_key = max_primary_key |
238 | 250 | self.save_state_if_required() |
| 251 | + self.prevent_binlog_removal() |
239 | 252 |
|
240 | 253 | def run_realtime_replication(self): |
241 | 254 | if self.initial_only: |
|
0 commit comments