-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathpg_driver_dialect.py
More file actions
223 lines (182 loc) · 9.38 KB
/
Copy pathpg_driver_dialect.py
File metadata and controls
223 lines (182 loc) · 9.38 KB
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
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
import socket
from typing import TYPE_CHECKING, Any, Callable, Set
import psycopg
if TYPE_CHECKING:
from aws_advanced_python_wrapper.hostinfo import HostInfo
from aws_advanced_python_wrapper.pep249 import Connection
from inspect import signature
from aws_advanced_python_wrapper.driver_dialect import DriverDialect
from aws_advanced_python_wrapper.driver_dialect_codes import DriverDialectCodes
from aws_advanced_python_wrapper.errors import UnsupportedOperationError
from aws_advanced_python_wrapper.pep249_methods import DbApiMethod
from aws_advanced_python_wrapper.utils.log import Logger
from aws_advanced_python_wrapper.utils.messages import Messages
from aws_advanced_python_wrapper.utils.properties import (Properties,
PropertiesUtils,
WrapperProperties)
logger = Logger(__name__)
class PgDriverDialect(DriverDialect):
_driver_name: str = "Psycopg"
TARGET_DRIVER_CODE: str = "psycopg"
# https://www.psycopg.org/psycopg3/docs/api/pq.html#psycopg.pq.TransactionStatus
PSYCOPG_IDLE_TRANSACTION_STATUS = 0
_dialect_code: str = DriverDialectCodes.PSYCOPG
_network_bound_methods: Set[str] = {
DbApiMethod.CONNECTION_COMMIT.method_name,
DbApiMethod.CONNECTION_AUTOCOMMIT.method_name,
DbApiMethod.CONNECTION_AUTOCOMMIT_SETTER.method_name,
DbApiMethod.CONNECTION_IS_READ_ONLY.method_name,
DbApiMethod.CONNECTION_SET_READ_ONLY.method_name,
DbApiMethod.CONNECTION_ROLLBACK.method_name,
DbApiMethod.CONNECTION_CLOSE.method_name,
DbApiMethod.CONNECTION_CURSOR.method_name,
DbApiMethod.CURSOR_CLOSE.method_name,
DbApiMethod.CURSOR_CALLPROC.method_name,
DbApiMethod.CURSOR_EXECUTE.method_name,
DbApiMethod.CURSOR_FETCHONE.method_name,
DbApiMethod.CURSOR_FETCHMANY.method_name,
DbApiMethod.CURSOR_FETCHALL.method_name
}
def is_dialect(self, connect_func: Callable) -> bool:
if PgDriverDialect.TARGET_DRIVER_CODE not in str(signature(connect_func)):
return PgDriverDialect.TARGET_DRIVER_CODE.lower() in (connect_func.__module__ + connect_func.__qualname__).lower()
return True
def is_closed(self, conn: Connection) -> bool:
if isinstance(conn, psycopg.Connection):
return conn.closed
raise UnsupportedOperationError(
Messages.get_formatted("DriverDialect.UnsupportedOperationError", self._driver_name, "closed"))
def abort_connection(self, conn: Connection):
if isinstance(conn, psycopg.Connection):
# abort_connection is invoked from the host-monitoring (EFM) monitor
# thread to interrupt an in-flight operation when the host becomes
# unreachable, so the owning thread's blocked recv returns promptly.
#
# It must NOT call close(): psycopg close() is PQfinish, which frees
# the libpq connection struct and tears down the SSL session
# (SSL_free) with no lock. Freeing that struct while the owning thread
# is mid-SSL-op on the same connection races a use-after-free in
# libpq/OpenSSL and crashes the whole process with SIGSEGV.
#
# cancel()/cancel_safe() avoids the crash but cannot interrupt a query
# when the host is unreachable (the CancelRequest can't be delivered),
# which defeats the EFM's purpose on exactly the network-failure case
# it exists for.
#
# Shutting the underlying socket down is the thread-safe equivalent of
# JDBC's Connection.abort(): it unblocks the owning thread's recv
# immediately (even on a dead host) WITHOUT freeing any struct, so
# there is no SSL_free to race. The owning thread observes the broken
# connection and closes it on its own thread (the only safe place for
# PQfinish).
if conn.closed:
return
try:
fd = conn.fileno()
except Exception:
return
if fd is None or fd < 0:
return
try:
sock = socket.socket(fileno=fd)
except OSError as e:
logger.debug("PgDriverDialect.AbortConnectionShutdownFailed", e)
return
try:
sock.shutdown(socket.SHUT_RDWR)
except OSError as e:
logger.debug("PgDriverDialect.AbortConnectionShutdownFailed", e)
finally:
# release the fd WITHOUT closing it; the connection still owns it
# and will PQfinish on its owning thread.
sock.detach()
return
raise UnsupportedOperationError(
Messages.get_formatted("DriverDialect.UnsupportedOperationError", self._driver_name, "abort_connection"))
def is_in_transaction(self, conn: Connection) -> bool:
if isinstance(conn, psycopg.Connection):
status: int = conn.info.transaction_status
return status != self.PSYCOPG_IDLE_TRANSACTION_STATUS
raise UnsupportedOperationError(Messages.get_formatted(
"DriverDialect.UnsupportedOperationError",
self._driver_name,
"transaction_status"))
def is_read_only(self, conn: Connection) -> bool:
if isinstance(conn, psycopg.Connection):
if conn.read_only is None:
return False
return conn.read_only
raise UnsupportedOperationError(
Messages.get_formatted("DriverDialect.UnsupportedOperationError", self._driver_name, "read_only"))
def set_read_only(self, conn: Connection, read_only: bool):
if isinstance(conn, psycopg.Connection):
conn.read_only = read_only
return
raise UnsupportedOperationError(
Messages.get_formatted("DriverDialect.UnsupportedOperationError", self._driver_name, "read_only"))
def get_autocommit(self, conn: Connection) -> bool:
if isinstance(conn, psycopg.Connection):
return conn.autocommit
raise UnsupportedOperationError(
Messages.get_formatted("DriverDialect.UnsupportedOperationError", self._driver_name, "autocommit"))
def set_autocommit(self, conn: Connection, autocommit: bool):
if isinstance(conn, psycopg.Connection):
conn.autocommit = autocommit
return
raise UnsupportedOperationError(
Messages.get_formatted("DriverDialect.UnsupportedOperationError", self._driver_name, "autocommit"))
def transfer_session_state(self, from_conn: Connection, to_conn: Connection):
if isinstance(from_conn, psycopg.Connection) and isinstance(to_conn, psycopg.Connection):
to_conn.read_only = from_conn.read_only
to_conn.autocommit = from_conn.autocommit
to_conn.isolation_level = from_conn.isolation_level
def get_connection_from_obj(self, obj: object) -> Any:
if isinstance(obj, psycopg.Connection):
return obj
if isinstance(obj, psycopg.Cursor):
return obj.connection
def prepare_connect_info(self, host_info: HostInfo, original_props: Properties) -> Properties:
driver_props: Properties = Properties(original_props.copy())
PropertiesUtils.remove_wrapper_props(driver_props)
driver_props["host"] = host_info.host
if host_info.is_port_specified():
driver_props["port"] = str(host_info.port)
db = WrapperProperties.DATABASE.get(original_props)
if db is not None:
driver_props["dbname"] = db
connect_timeout = WrapperProperties.CONNECT_TIMEOUT_SEC.get(original_props)
if connect_timeout is not None:
driver_props["connect_timeout"] = connect_timeout
keepalive = WrapperProperties.TCP_KEEPALIVE.get(original_props)
if keepalive is not None:
driver_props["keepalives"] = keepalive
keepalive_time = WrapperProperties.TCP_KEEPALIVE_TIME_SEC.get(original_props)
if keepalive_time is not None:
driver_props["keepalives_idle"] = keepalive_time
keepalive_interval = WrapperProperties.TCP_KEEPALIVE_INTERVAL_SEC.get(original_props)
if keepalive_interval is not None:
driver_props["keepalives_interval"] = keepalive_interval
keepalive_probes = WrapperProperties.TCP_KEEPALIVE_PROBES.get(original_props)
if keepalive_probes is not None:
driver_props["keepalives_count"] = keepalive_probes
return driver_props
def supports_connect_timeout(self) -> bool:
return True
def supports_tcp_keepalive(self) -> bool:
return True
def supports_abort_connection(self) -> bool:
return True