-
Notifications
You must be signed in to change notification settings - Fork 168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add defer_connect config to allow eagerly verifying connection #394
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,8 @@ | |
from typing import Any, Dict, List, NamedTuple, Optional # NOQA for mypy types | ||
from urllib.parse import urlparse | ||
|
||
from requests.exceptions import RequestException | ||
|
||
try: | ||
from zoneinfo import ZoneInfo | ||
except ModuleNotFoundError: | ||
|
@@ -157,6 +159,7 @@ def __init__( | |
legacy_prepared_statements=None, | ||
roles=None, | ||
timezone=None, | ||
defer_connect=False, | ||
): | ||
# Automatically assign http_schema, port based on hostname | ||
parsed_host = urlparse(host, allow_fragments=False) | ||
|
@@ -201,6 +204,31 @@ def __init__( | |
self.legacy_primitive_types = legacy_primitive_types | ||
self.legacy_prepared_statements = legacy_prepared_statements | ||
|
||
if not defer_connect: | ||
self.connect() | ||
|
||
def connect(self) -> None: | ||
connection_test_request = trino.client.TrinoRequest( | ||
self.host, | ||
self.port, | ||
self._client_session, | ||
self._http_session, | ||
self.http_scheme, | ||
self.auth, | ||
self.max_attempts, | ||
self.request_timeout, | ||
verify=self._http_session.verify, | ||
) | ||
try: | ||
test_response = connection_test_request.post("<not-going-to-be-executed>") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @electrum The server doesn't seem to "execute" the query until the first Is this expected from the protocol? Asking because this change relies on this behaviour to allow verifying that connection is indeed correct - e.g. proper auth instead of failing when the first query is submitted. This is a UX issue which people complain about when using the Trino CLI or JDBC as well for example. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I discussed this with David offline and he said that's it's expected behaviour. However if we don't send a DELETE the query might occupy resources on the server. So in this change we should send a subsequent DELETE as well for the query. Additionally we also discussed about making the protocol more "synchronous" in future so that connection verification happens eagerly always. @Shaheer-rossoneri14 Can you adjust this code to send a DELETE for the fake query we executed as well. |
||
response_content = test_response.content if test_response.content else "" | ||
if not test_response.ok: | ||
raise trino.exceptions.TrinoConnectionError( | ||
"error {}: {}".format(test_response.status_code, response_content)) | ||
|
||
except RequestException as e: | ||
raise trino.exceptions.TrinoConnectionError("connection failed: {}".format(e)) | ||
|
||
@property | ||
def isolation_level(self): | ||
return self._isolation_level | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hovaesco This would mean we'll only do eager connection if some authentication is actually provided. WDYT? Or do you think existing code is better (simpler and we don't assume when someone might want eager connections)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see any production use cases when auth is
constants.DEFAULT_AUTH
but might be helpful in some local testing.