Skip to content

Commit 3fd5d89

Browse files
authored
Write logs to file, split by database (bakwc#9)
1 parent b78ed91 commit 3fd5d89

File tree

2 files changed

+64
-5
lines changed

2 files changed

+64
-5
lines changed

mysql_ch_replicator/db_replicator.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ class Statistics:
8282
insert_records_count: int = 0
8383
erase_events_count: int = 0
8484
erase_records_count: int = 0
85+
no_events_count: int = 0
8586

8687

8788
class DbReplicator:
@@ -124,6 +125,7 @@ def __init__(self, config: Settings, database: str, target_database: str = None,
124125
self.last_touch_time = 0
125126

126127
def run(self):
128+
logger.info('launched db_replicator')
127129
if self.state.status == Status.RUNNING_REALTIME_REPLICATION:
128130
self.run_realtime_replication()
129131
return
@@ -226,6 +228,9 @@ def perform_initial_replication_table(self, table_name):
226228
primary_key_index = field_names.index(primary_key)
227229
primary_key_type = field_types[primary_key_index]
228230

231+
stats_number_of_records = 0
232+
last_stats_dump_time = time.time()
233+
229234
while True:
230235

231236
query_start_value = max_primary_key
@@ -258,6 +263,14 @@ def perform_initial_replication_table(self, table_name):
258263
self.save_state_if_required()
259264
self.prevent_binlog_removal()
260265

266+
stats_number_of_records += len(records)
267+
curr_time = time.time()
268+
if curr_time - last_stats_dump_time >= 60.0:
269+
last_stats_dump_time = curr_time
270+
logger.info(
271+
f'replicating {table_name}, replicated {stats_number_of_records}, primary key: {max_primary_key}',
272+
)
273+
261274
def run_realtime_replication(self):
262275
if self.initial_only:
263276
logger.info('skip running realtime replication, only initial replication was requested')
@@ -277,6 +290,8 @@ def run_realtime_replication(self):
277290
if event is None:
278291
time.sleep(DbReplicator.READ_LOG_INTERVAL)
279292
self.upload_records_if_required(table_name=None)
293+
self.stats.no_events_count += 1
294+
self.log_stats_if_required()
280295
continue
281296
assert event.db_name == self.database
282297
if self.database != self.target_database:
@@ -402,7 +417,7 @@ def log_stats_if_required(self):
402417
if curr_time - self.last_dump_stats_time < DbReplicator.STATS_DUMP_INTERVAL:
403418
return
404419
self.last_dump_stats_time = curr_time
405-
logger.info(f'statistics:\n{json.dumps(self.stats.__dict__, indent=3)}')
420+
logger.info(f'statistics:\n{json.dumps(self.stats.__dict__)}')
406421
self.stats = Statistics()
407422

408423
def upload_records_if_required(self, table_name):

mysql_ch_replicator/main.py

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import argparse
44
import logging
5+
from logging.handlers import RotatingFileHandler
6+
import sys
7+
import os
58

69
from .config import Settings
710
from .db_replicator import DbReplicator
@@ -10,15 +13,38 @@
1013
from .runner import Runner
1114

1215

13-
def set_logging_config(tags):
16+
def set_logging_config(tags, log_file=None):
17+
18+
handlers = []
19+
handlers.append(logging.StreamHandler(sys.stderr))
20+
if log_file is not None:
21+
handlers.append(
22+
RotatingFileHandler(
23+
filename=log_file,
24+
maxBytes=50*1024*1024, # 50 Mb
25+
backupCount=3,
26+
encoding='utf-8',
27+
delay=False,
28+
)
29+
)
30+
1431
logging.basicConfig(
1532
level=logging.INFO,
1633
format=f'[{tags} %(asctime)s %(levelname)8s] %(message)s',
34+
handlers=handlers,
1735
)
1836

1937

2038
def run_binlog_replicator(args, config: Settings):
21-
set_logging_config('binlogrepl')
39+
if not os.path.exists(config.binlog_replicator.data_dir):
40+
os.mkdir(config.binlog_replicator.data_dir)
41+
42+
log_file = os.path.join(
43+
config.binlog_replicator.data_dir,
44+
'binlog_replicator.log',
45+
)
46+
47+
set_logging_config('binlogrepl', log_file=log_file)
2248
binlog_replicator = BinlogReplicator(
2349
settings=config,
2450
)
@@ -29,11 +55,29 @@ def run_db_replicator(args, config: Settings):
2955
if not args.db:
3056
raise Exception("need to pass --db argument")
3157

32-
set_logging_config(f'dbrepl {args.db}')
58+
db_name = args.db
59+
60+
if not os.path.exists(config.binlog_replicator.data_dir):
61+
os.mkdir(config.binlog_replicator.data_dir)
62+
63+
db_dir = os.path.join(
64+
config.binlog_replicator.data_dir,
65+
db_name,
66+
)
67+
68+
if not os.path.exists(db_dir):
69+
os.mkdir(db_dir)
70+
71+
log_file = os.path.join(
72+
db_dir,
73+
'db_replicator.log',
74+
)
75+
76+
set_logging_config(f'dbrepl {args.db}', log_file=log_file)
3377

3478
db_replicator = DbReplicator(
3579
config=config,
36-
database=args.db,
80+
database=db_name,
3781
target_database=getattr(args, 'target_db', None),
3882
initial_only=args.initial_only,
3983
)

0 commit comments

Comments
 (0)