-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.py
125 lines (105 loc) · 4.18 KB
/
Database.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
from __future__ import division
from __future__ import print_function
import json
import numpy as np
import configparser
import psycopg2
import pymysql
import pymysql.cursors as pycursor
import time
# obtain and normalize configuration knobs
class DictParser(configparser.ConfigParser):
def read_dict(self):
d = dict(self._sections)
for k in d:
d[k] = dict(d[k])
return d
def parse_knob_config():
from performance_graphembedding_checkpoint import config_dict
_knob_config = config_dict["knob_config"]
for key in _knob_config:
_knob_config[key] = json.loads(str(_knob_config[key]).replace("\'", "\""))
return _knob_config
class Database:
def __init__(self, server_name='postgresql'):
knob_config = parse_knob_config()
self.knob_names = [knob for knob in knob_config]
self.knob_config = knob_config
self.server_name = server_name
# print("knob_names:", self.knob_names)
try:
conn = self._get_conn()
cursor = conn.cursor()
sql = "SELECT count FROM INFORMATION_SCHEMA.INNODB_METRICS where status='enabled'"
cursor.execute(sql)
result = cursor.fetchall()
self.internal_metric_num = len(result)
cursor.close()
conn.close()
except Exception as err:
print("execute sql error:", err)
def _get_conn(self):
if self.server_name == 'mysql':
sucess = 0
conn = -1
count = 0
while not sucess and count < 3:
try:
conn = pymysql.connect(host="166.111.121.62",
port=3306,
user="feng",
password="db10204",
db='INFORMATION_SCHEMA',
connect_timeout=36000,
cursorclass=pycursor.DictCursor)
sucess = 1
except Exception as result:
count += 1
time.sleep(10)
if conn == -1:
raise Exception
return conn
elif self.server_name == 'postgresql':
sucess = 0
conn = -1
count = 0
while not sucess and count < 3:
try:
db_name = "INFORMATION_SCHEMA" # zxn Modified.
conn = psycopg2.connect(database="INFORMATION_SCHEMA", user="lixizhang", password="xi10261026zhang",
host="166.111.5.177", port="5433")
sucess = 1
except Exception as result:
count += 1
time.sleep(10)
if conn == -1:
raise Exception
return conn
else:
print('数据库连接不上...')
return
def fetch_knob(self):
state_list = np.append([], [])
try:
conn = self._get_conn()
cursor = conn.cursor()
sql = "select"
for i, knob in enumerate(self.knob_names):
sql = sql + ' @@' + knob
if i < len(self.knob_names) - 1:
sql = sql + ', '
# state metrics
cursor.execute(sql)
result = cursor.fetchall()
for i in range(len(self.knob_names)):
value = result[0]["@@%s" % self.knob_names[i]] if result[0]["@@%s" % self.knob_names[i]] != 0 else \
self.knob_config[self.knob_names[i]]["max_value"] # not limit if value equals 0
# print(value, self.knob_config[self.knob_names[i]]["max_value"], self.knob_config[self.knob_names[i]]["min_value"])
state_list = np.append(state_list, value / (
self.knob_config[self.knob_names[i]]["max_value"] - self.knob_config[self.knob_names[i]][
"min_value"]))
cursor.close()
conn.close()
except Exception as error:
print("fetch_knob Error:", error)
return state_list