forked from haitianchen/LEON
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpg_executor.py
164 lines (136 loc) · 5.09 KB
/
pg_executor.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
import collections
import contextlib
import socket
import psycopg2
import psycopg2.extensions
import ray
from psycopg2.extensions import POLL_OK, POLL_READ, POLL_WRITE
from select import select
# Change these strings so that psycopg2.connect(dsn=dsn_val) works correctly
# for local & remote Postgres.
# JOB/IMDB.
# LOCAL_DSN = "postgres://psycopg:psycopg@localhost/imdb"
database = ""
user = ""
password = ""
host = ""
port = ""
LOCAL_DSN = ""
REMOTE_DSN = ""
# TPC-H.
# LOCAL_DSN = "postgres://psycopg:psycopg@localhost/tpch-sf10"
# REMOTE_DSN = "postgres://psycopg:psycopg@localhost/tpch-sf10"
# A simple class holding an execution result.
# result: a list, outputs from cursor.fetchall(). E.g., the textual outputs
# from EXPLAIN ANALYZE.
# has_timeout: bool, set to True iff an execution has run outside of its
# allocated timeouts; False otherwise (e.g., for non-execution statements
# such as EXPLAIN).
# server_ip: str, the private IP address of the Postgres server that
# generated this Result.
Result = collections.namedtuple(
'Result',
['result', 'has_timeout', 'server_ip'],
)
# ----------------------------------------
# Psycopg setup
# ----------------------------------------
def wait_select_inter(conn):
while 1:
try:
state = conn.poll()
if state == POLL_OK:
break
elif state == POLL_READ:
select([conn.fileno()], [], [])
elif state == POLL_WRITE:
select([], [conn.fileno()], [])
else:
raise conn.OperationalError("bad state from poll: %s" % state)
except KeyboardInterrupt:
conn.cancel()
# the loop will be broken by a server error
continue
psycopg2.extensions.set_wait_callback(wait_select_inter)
@contextlib.contextmanager
def Cursor():
"""Get a cursor to local Postgres database."""
# TODO: create the cursor once per worker node.
conn = psycopg2.connect(database=database, user=user,
password=password, host=host, port=port)
conn.set_session(autocommit=True)
try:
with conn.cursor() as cursor:
cursor.execute("load 'pg_hint_plan';")
yield cursor
finally:
conn.close()
# ----------------------------------------
# Postgres execution
# ----------------------------------------
def _SetGeneticOptimizer(flag, cursor):
# NOTE: DISCARD would erase settings specified via SET commands. Make sure
# no DISCARD ALL is called unexpectedly.
assert cursor is not None
assert flag in ['on', 'off', 'default'], flag
cursor.execute('set geqo = {};'.format(flag))
assert cursor.statusmessage == 'SET'
def ExecuteRemote(sql, verbose=False, geqo_off=False, timeout_ms=None):
return _ExecuteRemoteImpl.remote(sql, verbose, geqo_off, timeout_ms)
@ray.remote(resources={'pg': 1})
def _ExecuteRemoteImpl(sql, verbose, geqo_off, timeout_ms):
with Cursor(dsn=REMOTE_DSN) as cursor:
return Execute(sql, verbose, geqo_off, timeout_ms, cursor)
def Execute(sql, verbose=False, geqo_off=False, timeout_ms=None, cursor=None):
"""Executes a sql statement.
Returns:
A pg_executor.Result.
"""
# if verbose:
# print(sql)
_SetGeneticOptimizer('off' if geqo_off else 'on', cursor)
if timeout_ms is not None:
cursor.execute('SET statement_timeout to {}'.format(int(timeout_ms)))
else:
# Passing None / setting to 0 means disabling timeout.
cursor.execute('SET statement_timeout to 0')
try:
cursor.execute(sql)
result = cursor.fetchall()
has_timeout = False
except Exception as e:
if isinstance(e, psycopg2.errors.QueryCanceled):
assert 'canceling statement due to statement timeout' \
in str(e).strip(), e
result = []
has_timeout = True
elif isinstance(e, psycopg2.errors.InternalError_):
print(
'psycopg2.errors.InternalError_, treating as a' \
' timeout'
)
print(e)
result = []
has_timeout = True
elif isinstance(e, psycopg2.OperationalError):
if 'SSL SYSCALL error: EOF detected' in str(e).strip():
# This usually indicates an expensive query, putting the server
# into recovery mode. 'cursor' may get closed too.
print('Treating as a timeout:', e)
result = []
has_timeout = True
else:
# E.g., psycopg2.OperationalError: FATAL: the database system
# is in recovery mode
raise e
else:
raise e
try:
_SetGeneticOptimizer('default', cursor)
except psycopg2.InterfaceError as e:
# This could happen if the server is in recovery, due to some expensive
# queries just crashing the server (see the above exceptions).
assert 'cursor already closed' in str(e), e
pass
ip = socket.gethostbyname(socket.gethostname())
return Result(result, has_timeout, ip)