Skip to content

Commit 6df5da8

Browse files
authored
Auto restart db_replicator (#42)
1 parent 624c750 commit 6df5da8

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,11 @@ tables: '*'
140140
exclude_databases: ['database_10', 'database_*_42'] # optional
141141
exclude_tables: ['meta_table_*'] # optional
142142

143-
log_level: 'info' # optional
144-
optimize_interval: 86400 # optional
143+
log_level: 'info' # optional
144+
optimize_interval: 86400 # optional
145+
auto_restart_interval: 3600 # optional
145146

146-
indexes: # optional
147+
indexes: # optional
147148
- databases: '*'
148149
tables: ['test_table']
149150
index: 'INDEX name_idx name TYPE ngrambf_v1(5, 65536, 4, 0) GRANULARITY 1'
@@ -163,6 +164,7 @@ indexes: # optional
163164
- `exclude_tables` - databases to __exclude__, string or list. If same table matches `tables` and `exclude_tables`, exclude has higher priority.
164165
- `log_level` - log level, default is `info`, you can set to `debug` to get maximum information (allowed values are `debug`, `info`, `warning`, `error`, `critical`)
165166
- `optimize_interval` - interval (seconds) between automatic `OPTIMIZE table FINAL` calls. Default 86400 (1 day). This is required to perform all merges guaranteed and avoid increasing of used storage and decreasing performance.
167+
- `auto_restart_interval` - interval (seconds) between automatic db_replicator restart. Default 3600 (1 hour). This is done to reduce memory usage.
166168
- `indexes` - you may want to add some indexes to accelerate performance, eg. ngram index for full-test search, etc. To apply indexes you need to start replication from scratch.
167169

168170
Few more tables / dbs examples:

mysql_ch_replicator/config.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ class Settings:
9191
DEFAULT_LOG_LEVEL = 'info'
9292
DEFAULT_OPTIMIZE_INTERVAL = 86400
9393
DEFAULT_CHECK_DB_UPDATED_INTERVAL = 120
94+
DEFAULT_AUTO_RESTART_INTERVAL = 3600
9495

9596
def __init__(self):
9697
self.mysql = MysqlSettings()
@@ -106,6 +107,7 @@ def __init__(self):
106107
self.optimize_interval = 0
107108
self.check_db_updated_interval = 0
108109
self.indexes: list[Index] = []
110+
self.auto_restart_interval = 0
109111

110112
def load(self, settings_file):
111113
data = open(settings_file, 'r').read()
@@ -123,6 +125,9 @@ def load(self, settings_file):
123125
self.check_db_updated_interval = data.pop(
124126
'check_db_updated_interval', Settings.DEFAULT_CHECK_DB_UPDATED_INTERVAL,
125127
)
128+
self.auto_restart_interval = data.pop(
129+
'auto_restart_interval', Settings.DEFAULT_AUTO_RESTART_INTERVAL,
130+
)
126131
indexes = data.pop('indexes', [])
127132
for index in indexes:
128133
self.indexes.append(

mysql_ch_replicator/db_replicator.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ def __init__(self, config: Settings, database: str, target_database: str = None,
132132
self.records_to_delete = defaultdict(set) # table_name => {record_id, ...}
133133
self.last_records_upload_time = 0
134134
self.last_touch_time = 0
135+
self.start_time = time.time()
135136

136137
def create_state(self):
137138
return State(os.path.join(self.config.binlog_replicator.data_dir, self.database, 'state.pckl'))
@@ -359,6 +360,12 @@ def run_realtime_replication(self):
359360
killer = GracefulKiller()
360361

361362
while not killer.kill_now:
363+
if self.config.auto_restart_interval:
364+
curr_time = time.time()
365+
if curr_time - self.start_time >= self.config.auto_restart_interval:
366+
logger.info('process restart (check auto_restart_interval config option)')
367+
break
368+
362369
event = self.data_reader.read_next_event()
363370
if event is None:
364371
time.sleep(DbReplicator.READ_LOG_INTERVAL)

0 commit comments

Comments
 (0)