-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpg_tuner.py
570 lines (448 loc) · 16.3 KB
/
pg_tuner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
#!/usr/bin/env python3
"""
pg_tuner.py
Usage:
pg_tuner.py show --conf conf.toml
pg_tuner.py check --conf conf.toml
pg_tuner.py run --conf conf.toml
pg_tuner.py create --conf conf.toml
pg_tuner.py drop --conf conf.toml
pg_tuner.py backup --conf conf.toml
pg_tuner.py analyze --db PATH/study.db
Copyright (c) 2024-2025, Hironobu Suzuki @ interdb.jp
"""
import argparse
import time, sys, csv, os
import optuna
from utils import Common, Log, PG, Repository
from conf import Conf
from benchmark import Sysbench, SysbenchScenario
from benchmark import Pgbench, PgbenchScenario
try:
import sqlite3
except:
pass
class PgTuner:
def __init__(self):
self.conf = None
self.pg = None
self.sc = None
self.repo = None
self.max_connections = None
def _objective(self, trial):
conf_params = self.conf.extract_params_from(trial)
# 0. restore database cluster
if self.conf.restore_everytime:
self.pg.restore_backup()
# 1. set conf
if self.pg.set_conf(conf_params, self.max_connections) != True:
sys.exit(1)
# 2. pg start
self.pg.start()
time.sleep(5) # Wait until Postgres is operational.
# 3. benchmark run
_log_dir = self.repo.get_log_dir(trial.number)
cmd = "mv {} {}".format(Common.ADDITIONAL_CONF, _log_dir)
self.pg.exec_local(cmd)
score, ret = self.sc.play(
self.conf.bench_scenario,
_log_dir,
self.conf.monitoring_time,
self.conf.linux_monitoring,
self.conf.additional_monitor_items,
)
ret_file = "{}{}".format(_log_dir, Common.RESULT_FILE)
with open(ret_file, "w") as f:
writer = csv.writer(f, quotechar="'", quoting=csv.QUOTE_NONNUMERIC)
writer.writerow(["no"] + self.sc.get_col_name())
for [no, scenario, result] in ret:
writer.writerow([no] + result)
score_file = "{}{}".format(_log_dir, Common.SCORE_FILE)
with open(score_file, "w") as f:
f.write(str(score) + "\n")
# 4. pg stop
self.pg.stop()
return float(score)
def _confirm_duration(self, duration, n_trials, show_only=False):
estimated_time = duration * n_trials
if estimated_time > 10 * 60: # over 10 [min]
estimated_time, unit = Common.pretty_time_format(estimated_time)
print(
"Estimated Time: This task is estimated to take approximately {:.1f} {} to complete.".format(
estimated_time, unit
)
)
if show_only == True:
return True
print("Confirmation: Do you want to proceed with this task?")
while True:
answer = input("Enter yes or no: ").lower()
if answer in ("yes"):
return True
else:
return False
"""
# Checks the max connections
"""
def _check_max_connections(self, connections, required_max_connections):
[
max_connections,
reserved_connections,
superuser_reserved_connections,
] = connections
if required_max_connections <= (
max_connections - reserved_connections - superuser_reserved_connections
):
return True
else:
new_max_connections = (
required_max_connections
+ reserved_connections
+ superuser_reserved_connections
+ 2
) # 1 is for monitoring, and another is margin.
print(
"Error: Although max_connections is set to {}, your scenario requires {} connections maximum.".format(
max_connections, new_max_connections
)
)
print(
"Hint: Consider adjusting the number of threads in your scenario to stay within the connection limit."
)
return False
def _check_conf_file(self, args):
conf_file = args.conf
if os.path.isfile(conf_file) == False:
print("Error: configuration file '{}' not found".format(conf_file))
sys.exit(1)
return conf_file
"""
Public methods
"""
"""
# show command:
"""
def show(self):
conf_file = self._check_conf_file(args)
self.conf = Conf(conf_file)
self.sc = self.conf.create_bench_scenario()
(
total_duration,
required_max_connections,
total_connections,
) = self.sc.check_scenario(self.conf.get_scenario())
self.conf.print_conf(
total_duration, required_max_connections, total_connections
)
del self.conf, self.sc
"""
# check command:
"""
def check(self):
conf_file = self._check_conf_file(args)
self.conf = Conf(conf_file)
self.conf.input_server_passwd()
self.pg = self.conf.create_pg()
self.sc = self.conf.create_bench_scenario()
[
max_connections,
reserved_connections,
superuser_reserved_connections,
] = self.conf.check()
(
total_duration,
required_max_connections,
total_connections,
) = self.sc.check_scenario(self.conf.get_scenario())
self._confirm_duration(total_duration, self.conf.n_trials, show_only=True)
self.conf.print_connection_info(required_max_connections, total_connections)
del self.conf, self.pg, self.sc
"""
# create command:
"""
def create(self):
conf_file = self._check_conf_file(args)
self.conf = Conf(conf_file)
self.sb = self.conf.create_bench()
self.sb.create_bench()
if self.conf.restore_everytime:
print("Notice: *** Create a backup using the 'pg_tuner.py backup' command before running the task. ***")
del self.sb, self.conf
"""
# drop command:
"""
def drop(self):
conf_file = self._check_conf_file(args)
self.conf = Conf(conf_file)
self.sb = self.conf.create_bench()
print("Confirmation: Do you want to drop benchmark tables?")
answer = input("Enter yes or no: ").lower()
if answer in ("yes"):
self.sb.drop_bench()
del self.sb, self.conf
"""
# backup command:
"""
def backup(self):
conf_file = self._check_conf_file(args)
self.conf = Conf(conf_file)
if self.conf.restore_everytime == False:
print("Error: parameter 'restore_everytime' is False.")
sys.exit(1)
self.conf.input_server_passwd()
self.pg = self.conf.create_pg()
# Gets pg_ctl status
pg_status = self.pg.is_running()
# Stops PostgreSQL server if running.
if pg_status == True:
self.pg.stop()
# pg.make_backup
self.pg.make_backup()
# Restart if pg_status == True
if pg_status == True:
self.pg.start()
"""
# run command:
"""
def run(self):
conf_file = self._check_conf_file(args)
self.conf = Conf(conf_file)
# Check repository
self.repo = self.conf.create_repository()
if self.repo.check_repo() == False:
sys.exit(0)
#
self.conf.input_server_passwd()
self.pg = self.conf.create_pg()
self.sc = self.conf.create_bench_scenario()
# check
[
max_connections,
reserved_connections,
superuser_reserved_connections,
] = self.conf.check()
(
total_duration,
required_max_connections,
total_connections,
) = self.sc.check_scenario(self.conf.get_scenario())
# Confirm duration
if self._confirm_duration(total_duration, self.conf.n_trials) == False:
del self.sc, self.pg, self.conf
sys.exit(0)
# Check max_connection
if (
self._check_max_connections(
[max_connections, reserved_connections, superuser_reserved_connections],
required_max_connections,
)
== False
):
del self.sc, self.pg, self.conf
sys.exit(0)
self.conf.print_conf(total_duration, required_max_connections, total_connections)
self.conf.dump_conf()
# Cold start
self.pg.stop()
# Optimize parameters
sampler = self.conf.get_sampler()
if "sqlite3" in sys.modules:
storage = "sqlite:///{}{}{}".format(
self.conf.base_dir, self.conf.log_dir, Common.STUDY_DB
)
study = optuna.create_study(
direction="maximize", study_name=self.conf.log_dir, storage=storage, sampler=sampler,
)
else:
study = optuna.create_study(direction="maximize", sampler=sampler)
study.optimize(self._objective, n_trials=self.conf.n_trials)
# Store result
self.conf.dump_best_result(study.best_value, study.best_params, study.best_trial, study.best_trials)
print("Best objective value: {}".format(study.best_value))
print("Best parameter: {}".format(study.best_params))
#
self.pg.start()
del self.conf, self.pg, self.sc, self.repo
"""
# restart command:
"""
def restart(self):
if "sqlite3" not in sys.modules:
print("Error: sqlite3 module not found.")
sys.exit(1)
conf_file = args.conf
if os.path.isfile(conf_file) == False:
print("Error: configuration file '{}' not found".format(conf_file))
sys.exit(1)
# How to restart pg_tuner.py
#
# (1) Get latest trial_id from trials in study.db.
#
# sqlite> SELECT max(number) FROM trials WHERE state = 'RUNNING' OR state = 'FAIL';
# 42 (e.g. max_number := 42)
#
# (2) Delete latest trial data.
#
# sqlite> DELETE FROM trials WHERE number >= max_number;
# sqlite> DELETE FROM trial_values WHERE trial_id > max_number;
# sqlite> DELETE FROM trial_params WHERE trial_id > max_number;
#
# (3) Remove latest trial data directory.
#
# $ rm -rf data_repo/self.conf.log_dir/00XX (e.g. 00XX = max_number.zfill(4))
#
# (4) Set remaining number of trails
#
# study.optimize(self._objective, n_trials=(self.conf.n_trials - max_number - 1))
self.conf = Conf(conf_file)
self.repo = self.conf.create_repository()
self.conf.input_server_passwd()
self.pg = self.conf.create_pg()
self.sc = self.conf.create_bench_scenario()
# check
[
max_connections,
reserved_connections,
superuser_reserved_connections,
] = self.conf.check()
(
total_duration,
required_max_connections,
total_connections,
) = self.sc.check_scenario(self.conf.get_scenario())
# Cold start
self.pg.stop()
# Optimize parameters
sampler = self.conf.get_sampler()
storage = "sqlite:///{}{}{}".format(self.conf.base_dir, self.conf.log_dir, Common.STUDY_DB)
study = optuna.load_study(storage=storage, study_name=self.conf.log_dir, sampler=sampler)
# Set remaining number of trails
study.optimize(self._objective, n_trials=(self.conf.n_trials - 42))
# Store result
self.conf.dump_best_result(study.best_value, study.best_params, study.best_trial, study.best_trials)
print("Best objective value: {}".format(study.best_value))
print("Best parameter: {}".format(study.best_params))
#
self.pg.start()
del self.conf, self.pg, self.sc, self.repo
#
# Creates the script "important_params.py" that executes optuna.visualization.plot_param_importances()
# to visualize the importance of configuration parameters using the specified study.db.
#
# Reference: link to optuna.visualization.plot_param_importances:
# https://optuna.readthedocs.io/en/stable/reference/visualization/generated/optuna.visualization.plot_param_importances.html
#
def analyze_study_db(atgs):
if "sqlite3" not in sys.modules:
print("Error: analyze command requires 'sqlite3' module, however, this script did not import it.")
sys.exit(1)
db = args.db
if os.path.isfile(db) == False:
print("Error: study db '{}' not found".format(db))
sys.exit(1)
filename = Common.DEFAULT_IMPORTANT_PARAMS_FILE
con = sqlite3.connect(db)
cur = con.cursor()
res = cur.execute("SELECT study_name FROM studies")
study_name = res.fetchone()
res = cur.execute("SELECT param_name FROM trial_params WHERE trial_id = 1")
param_name = res.fetchall()
with open(filename, "w") as f:
print("import optuna\n", file=f)
print(
'study = optuna.load_study(storage="sqlite:///{}", study_name="{}")'.format(
db, study_name[0]
),
file=f,
)
print("\nret = optuna.importance.get_param_importances(study=study,params=[", file=f)
for key in param_name:
print('\t"{}",'.format(key[0]), file=f)
print("])", file=f)
print("print(ret)\n", file=f)
print("\noptuna.visualization.plot_param_importances(study=study,params=[", file=f)
for key in param_name:
print('\t"{}",'.format(key[0]), file=f)
print("]).show()", file=f)
print("Created: '{}'.".format(filename))
if __name__ == "__main__":
def show(args):
pgt = PgTuner()
pgt.show()
del pgt
def check(args):
pgt = PgTuner()
pgt.check()
del pgt
def run(args):
pgt = PgTuner()
pgt.run()
del pgt
# Hidden command
def restart(args):
pgt = PgTuner()
pgt.restart()
del pgt
def create(args):
pgt = PgTuner()
pgt.create()
del pgt
def drop(args):
pgt = PgTuner()
pgt.drop()
del pgt
def backup(args):
pgt = PgTuner()
pgt.backup()
del pgt
# Create command parser.
parser = argparse.ArgumentParser(
description="This is another automated PostgreSQL database tuning tool."
)
subparsers = parser.add_subparsers()
# show command.
parser_show = subparsers.add_parser("show", help="show configuration")
parser_show.add_argument("--conf", nargs="?")
parser_show.set_defaults(handler=show)
# check command.
parser_check = subparsers.add_parser("check", help="check configuration")
parser_check.add_argument("--conf", nargs="?")
parser_check.set_defaults(handler=check)
# run command.
parser_run = subparsers.add_parser("run", help="run")
parser_run.add_argument("--conf", nargs="?")
parser_run.set_defaults(handler=run)
"""
# Hidden command
# restart command.
parser_restart = subparsers.add_parser("restart", help="restart study")
parser_restart.add_argument("--conf", nargs="?")
parser_restart.set_defaults(handler=restart)
"""
# create benchmark.
parser_create = subparsers.add_parser("create", help="create benchmark")
parser_create.add_argument("--conf", nargs="?")
parser_create.set_defaults(handler=create)
# drop benchmark.
parser_drop = subparsers.add_parser("drop", help="drop benchmark")
parser_drop.add_argument("--conf", nargs="?")
parser_drop.set_defaults(handler=drop)
# backup data cluster.
parser_backup = subparsers.add_parser("backup", help="backup data cluster")
parser_backup.add_argument("--conf", nargs="?")
parser_backup.set_defaults(handler=backup)
# analyze study.db.
parser_analyze = subparsers.add_parser("analyze", help="analyze study.db")
parser_analyze.add_argument(
"--db",
nargs="?",
default="./study.db",
help='Analyze the specified study.db and create the script "important_params.py" that visualizes the importance of configuration parameters',
)
parser_analyze.set_defaults(handler=analyze_study_db)
# Main procedure.
args = parser.parse_args()
if hasattr(args, "handler"):
args.handler(args)
else:
parser.print_help()