-
Notifications
You must be signed in to change notification settings - Fork 1
/
OtterTuneEnv.py
172 lines (136 loc) · 4.71 KB
/
OtterTuneEnv.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
import numpy as np
import copy
from fabric.api import (env, local, task, lcd)
import json
from multiprocessing import Process
import time
from Parser import Parser
with open('../driver/driver_config.json', 'r') as f:
CONF = json.load(f)
@task
def free_cache():
cmd = 'sync; sudo bash -c "echo 1 > /proc/sys/vm/drop_caches"'
local(cmd)
@task
def run_tpch():
cmd = 'PGPASSWORD=bohan psql --cluster 10/main -U bohan -d tpch40 -a -f ./q13.sql -o tpch.log > ./query.log' #2>&1 &'
local(cmd)
@task
def run_controller():
cmd = 'sudo gradle run -PappArgs="-c {} -d output/" --no-daemon'.\
format(CONF['controller_config'])
with lcd("../controller"): # pylint: disable=not-context-manager
local(cmd)
@task
def stop_controller():
pid = int(open('../controller/pid.txt').read())
cmd = 'sudo kill -2 {}'.format(pid)
with lcd("../controller"): # pylint: disable=not-context-manager
local(cmd)
@task
def get_latency():
with open('tpch.log', 'r') as f:
lines = f.readlines()
line = lines[-3]
latency = line.split(':')[1].split('.')[0].strip() # ms
print(line)
print(latency)
return latency
@task
def restart_database():
cmd = 'sudo service postgresql restart'
local(cmd)
@task
def save_dbms_result():
t = int(time.time())
files = ['knobs.json', 'metrics_after.json', 'metrics_before.json', 'summary.json']
for f_ in files:
f_prefix = f_.split('.')[0]
cmd = 'cp ../controller/output/{} {}/{}__{}.json'.\
format(f_, CONF['save_path'], t, f_prefix)
local(cmd)
cmd = 'cp {} {}/{}__{}.txt'.\
format('tpch.log', CONF['save_path'], t, 'queryplan')
local(cmd)
@task
def upload_result():
cmd = 'python3 ../../server/website/script/upload/upload.py \
../controller/output/ {} {}/new_result/'.format(CONF['upload_code'],
CONF['upload_url'])
local(cmd)
@task
def add_udf():
cmd = 'sudo python3 ../driver/LatencyUDF.py ../controller/output/ {}'.format('tpch.log')
local(cmd)
class OtterTuneEnv(object):
def __init__(self, min_vals, max_vals, default_vals, knob_names):
self.min_vals = np.array(min_vals)
self.max_vals = np.array(max_vals)
self.default_vals = np.array(default_vals)
self.knob_names = np.array(knob_names)
self.N = len(knob_names)
self.knob_id = 0
assert(self.N > 0)
assert(len(min_vals) == self.N)
assert(len(max_vals) == self.N)
def reset(self):
self.knob_id = 0
# initial state is default config
initial_state = np.zeros(self.N + 1)
scaled_vals = Parser().scaled(self.min_vals, self.max_vals, self.default_vals)
initial_state[:self.N] = scaled_vals
return initial_state
def step(self, action, state):
'''
action: (0, 1)
'''
knob_id = self.knob_id
#print(state)
nextstate = copy.copy(state)
nextstate[knob_id] = action
nextstate[self.N] = knob_id + 1
#print('action', action)
#print('nextstate', nextstate)
debug_info = {}
reward = 0
if knob_id < self.N - 1 and knob_id >= 0:
is_terminal = False
elif knob_id == self.N - 1:
is_terminal = True
else:
raise Exception("Invalid Knob ID {}. ".format(knob_id))
self.knob_id += 1
return (nextstate, reward, is_terminal, debug_info)
def run_experiment(self):
# free cache
free_cache()
# restart database
restart_database()
p = Process(target=run_controller, args=())
p.start()
# sleep some time to wait for the contorller.
time.sleep(5)
run_tpch()
stop_controller()
p.join()
reward = int(get_latency())
print (reward)
return reward
def change_conf(self, config_vals):
conf_path = '/etc/postgresql/10/main/postgresql.conf'
with open(conf_path, "r+") as postgresqlconf:
lines = postgresqlconf.readlines()
settings_idx = lines.index("# Add settings for extensions here\n")
postgresqlconf.seek(0)
postgresqlconf.truncate(0)
lines = lines[0:(settings_idx + 1)]
for line in lines:
postgresqlconf.write(line)
for i in range(len(self.knob_names)):
s = str(self.knob_names[i]) + ' = ' + str(config_vals[i]) + "\n"
print (s)
postgresqlconf.write(s)
def save_and_upload(self):
add_udf()
save_dbms_result()
upload_result()