-
Notifications
You must be signed in to change notification settings - Fork 116
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
SNOW-1663726 make session config updates thread safe #2302
Changes from 66 commits
56fb566
66003d1
0e58205
e75dde1
68a8c1c
31a5734
b4dadda
b8c6496
f39837e
31a196f
723bdf7
37c0419
8a2d433
a083989
947d384
fd51720
3077853
65c3186
94412cf
638dd09
7ae2c33
1689ebf
5e8a2d2
1c83ef2
a649761
f03d618
5f398d5
3807087
4eef3e9
df3263c
6769c54
af86f67
a737f33
8ca2730
81417a3
e340567
30952bb
03f25b5
6deb402
8e1dfe0
10bfeb4
879940a
5aad2d9
669eb91
a85a144
a79ffb4
4420350
5f1eaa6
9d62017
b58aa8b
db37033
dddd15f
57ee9e8
809a86e
6021ab8
43986f6
0430e92
095b04e
32707f9
3bf678d
3eade1a
1850d5d
1fa6ad2
7c85432
f994842
4621836
e1c68f3
e5b48dd
496e2be
980d3b7
54a6b5d
67609e8
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 |
---|---|---|
|
@@ -114,6 +114,7 @@ | |
unwrap_single_quote, | ||
unwrap_stage_location_single_quote, | ||
validate_object_name, | ||
warn_session_config_update_in_multithreaded_mode, | ||
warning, | ||
zip_file_or_directory_to_stream, | ||
) | ||
|
@@ -339,38 +340,44 @@ def __init__(self, session: "Session", conf: Dict[str, Any]) -> None: | |
"use_constant_subquery_alias": True, | ||
"flatten_select_after_filter_and_orderby": True, | ||
} # For config that's temporary/to be removed soon | ||
self._lock = self._session._lock | ||
for key, val in conf.items(): | ||
if self.is_mutable(key): | ||
self.set(key, val) | ||
|
||
def get(self, key: str, default=None) -> Any: | ||
if hasattr(Session, key): | ||
return getattr(self._session, key) | ||
if hasattr(self._session._conn._conn, key): | ||
return getattr(self._session._conn._conn, key) | ||
return self._conf.get(key, default) | ||
with self._lock: | ||
if hasattr(Session, key): | ||
return getattr(self._session, key) | ||
if hasattr(self._session._conn._conn, key): | ||
return getattr(self._session._conn._conn, key) | ||
return self._conf.get(key, default) | ||
|
||
def is_mutable(self, key: str) -> bool: | ||
if hasattr(Session, key) and isinstance(getattr(Session, key), property): | ||
return getattr(Session, key).fset is not None | ||
if hasattr(SnowflakeConnection, key) and isinstance( | ||
getattr(SnowflakeConnection, key), property | ||
): | ||
return getattr(SnowflakeConnection, key).fset is not None | ||
return key in self._conf | ||
with self._lock: | ||
if hasattr(Session, key) and isinstance( | ||
getattr(Session, key), property | ||
): | ||
return getattr(Session, key).fset is not None | ||
if hasattr(SnowflakeConnection, key) and isinstance( | ||
getattr(SnowflakeConnection, key), property | ||
): | ||
return getattr(SnowflakeConnection, key).fset is not None | ||
return key in self._conf | ||
|
||
def set(self, key: str, value: Any) -> None: | ||
if self.is_mutable(key): | ||
if hasattr(Session, key): | ||
setattr(self._session, key, value) | ||
if hasattr(SnowflakeConnection, key): | ||
setattr(self._session._conn._conn, key, value) | ||
if key in self._conf: | ||
self._conf[key] = value | ||
else: | ||
raise AttributeError( | ||
f'Configuration "{key}" does not exist or is not mutable in runtime' | ||
) | ||
with self._lock: | ||
if self.is_mutable(key): | ||
if hasattr(Session, key): | ||
setattr(self._session, key, value) | ||
if hasattr(SnowflakeConnection, key): | ||
setattr(self._session._conn._conn, key, value) | ||
if key in self._conf: | ||
self._conf[key] = value | ||
else: | ||
raise AttributeError( | ||
f'Configuration "{key}" does not exist or is not mutable in runtime' | ||
) | ||
|
||
class SessionBuilder: | ||
""" | ||
|
@@ -538,11 +545,6 @@ def __init__( | |
self._udtf_registration = UDTFRegistration(self) | ||
self._udaf_registration = UDAFRegistration(self) | ||
|
||
self._plan_builder = ( | ||
SnowflakePlanBuilder(self) | ||
if isinstance(self._conn, ServerConnection) | ||
else MockSnowflakePlanBuilder(self) | ||
) | ||
self._last_action_id = 0 | ||
self._last_canceled_id = 0 | ||
self._use_scoped_temp_objects: bool = ( | ||
|
@@ -637,6 +639,16 @@ def _analyzer(self) -> Analyzer: | |
) | ||
return self._thread_store.analyzer | ||
|
||
@property | ||
def _plan_builder(self): | ||
if not hasattr(self._thread_store, "plan_builder"): | ||
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. oh, i didn't notice we have plan builder here, i thought plan builder is just part of the analyzer, what is the plan builder used here?, and what is the reason that we moved it to a property 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. and the plan builder seems thread local to me 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. This plan builder is different from analyzer's plan builder. Only used by
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 added it as part of old changes. reverted back to original now since plan builder should be thread-safe with restricted config updates |
||
self._thread_store.plan_builder = ( | ||
SnowflakePlanBuilder(self) | ||
if isinstance(self._conn, ServerConnection) | ||
else MockSnowflakePlanBuilder(self) | ||
) | ||
return self._thread_store.plan_builder | ||
|
||
def close(self) -> None: | ||
"""Close this session.""" | ||
if is_in_stored_procedure(): | ||
|
@@ -770,36 +782,46 @@ def custom_package_usage_config(self) -> Dict: | |
|
||
@sql_simplifier_enabled.setter | ||
def sql_simplifier_enabled(self, value: bool) -> None: | ||
self._conn._telemetry_client.send_sql_simplifier_telemetry( | ||
self._session_id, value | ||
) | ||
try: | ||
self._conn._cursor.execute( | ||
f"alter session set {_PYTHON_SNOWPARK_USE_SQL_SIMPLIFIER_STRING} = {value}" | ||
warn_session_config_update_in_multithreaded_mode("sql_simplifier_enabled") | ||
|
||
with self._lock: | ||
self._conn._telemetry_client.send_sql_simplifier_telemetry( | ||
self._session_id, value | ||
) | ||
except Exception: | ||
pass | ||
self._sql_simplifier_enabled = value | ||
try: | ||
self._conn._cursor.execute( | ||
f"alter session set {_PYTHON_SNOWPARK_USE_SQL_SIMPLIFIER_STRING} = {value}" | ||
) | ||
except Exception: | ||
pass | ||
self._sql_simplifier_enabled = value | ||
|
||
@cte_optimization_enabled.setter | ||
@experimental_parameter(version="1.15.0") | ||
def cte_optimization_enabled(self, value: bool) -> None: | ||
if value: | ||
self._conn._telemetry_client.send_cte_optimization_telemetry( | ||
self._session_id | ||
) | ||
self._cte_optimization_enabled = value | ||
warn_session_config_update_in_multithreaded_mode("cte_optimization_enabled") | ||
|
||
with self._lock: | ||
if value: | ||
self._conn._telemetry_client.send_cte_optimization_telemetry( | ||
self._session_id | ||
) | ||
self._cte_optimization_enabled = value | ||
|
||
@eliminate_numeric_sql_value_cast_enabled.setter | ||
@experimental_parameter(version="1.20.0") | ||
def eliminate_numeric_sql_value_cast_enabled(self, value: bool) -> None: | ||
"""Set the value for eliminate_numeric_sql_value_cast_enabled""" | ||
warn_session_config_update_in_multithreaded_mode( | ||
"eliminate_numeric_sql_value_cast_enabled" | ||
) | ||
|
||
if value in [True, False]: | ||
self._conn._telemetry_client.send_eliminate_numeric_sql_value_cast_telemetry( | ||
self._session_id, value | ||
) | ||
self._eliminate_numeric_sql_value_cast_enabled = value | ||
with self._lock: | ||
self._conn._telemetry_client.send_eliminate_numeric_sql_value_cast_telemetry( | ||
self._session_id, value | ||
) | ||
self._eliminate_numeric_sql_value_cast_enabled = value | ||
else: | ||
raise ValueError( | ||
"value for eliminate_numeric_sql_value_cast_enabled must be True or False!" | ||
|
@@ -809,6 +831,10 @@ def eliminate_numeric_sql_value_cast_enabled(self, value: bool) -> None: | |
@experimental_parameter(version="1.21.0") | ||
def auto_clean_up_temp_table_enabled(self, value: bool) -> None: | ||
"""Set the value for auto_clean_up_temp_table_enabled""" | ||
warn_session_config_update_in_multithreaded_mode( | ||
"auto_clean_up_temp_table_enabled" | ||
) | ||
|
||
if value in [True, False]: | ||
self._conn._telemetry_client.send_auto_clean_up_temp_table_telemetry( | ||
self._session_id, value | ||
|
@@ -827,12 +853,16 @@ def large_query_breakdown_enabled(self, value: bool) -> None: | |
materialize the partitions, and then combine them to execute the query to improve | ||
overall performance. | ||
""" | ||
warn_session_config_update_in_multithreaded_mode( | ||
"large_query_breakdown_enabled" | ||
) | ||
|
||
if value in [True, False]: | ||
self._conn._telemetry_client.send_large_query_breakdown_telemetry( | ||
self._session_id, value | ||
) | ||
self._large_query_breakdown_enabled = value | ||
with self._lock: | ||
self._conn._telemetry_client.send_large_query_breakdown_telemetry( | ||
self._session_id, value | ||
) | ||
self._large_query_breakdown_enabled = value | ||
else: | ||
raise ValueError( | ||
"value for large_query_breakdown_enabled must be True or False!" | ||
|
@@ -841,6 +871,9 @@ def large_query_breakdown_enabled(self, value: bool) -> None: | |
@large_query_breakdown_complexity_bounds.setter | ||
def large_query_breakdown_complexity_bounds(self, value: Tuple[int, int]) -> None: | ||
"""Set the lower and upper bounds for the complexity score used in large query breakdown optimization.""" | ||
warn_session_config_update_in_multithreaded_mode( | ||
"large_query_breakdown_complexity_bounds" | ||
) | ||
|
||
if len(value) != 2: | ||
raise ValueError( | ||
|
@@ -850,16 +883,20 @@ def large_query_breakdown_complexity_bounds(self, value: Tuple[int, int]) -> Non | |
raise ValueError( | ||
f"Expecting a tuple of lower and upper bound with the lower bound less than the upper bound. Got (lower, upper) = ({value[0], value[1]})" | ||
) | ||
self._conn._telemetry_client.send_large_query_breakdown_update_complexity_bounds( | ||
self._session_id, value[0], value[1] | ||
) | ||
with self._lock: | ||
self._conn._telemetry_client.send_large_query_breakdown_update_complexity_bounds( | ||
self._session_id, value[0], value[1] | ||
) | ||
|
||
self._large_query_breakdown_complexity_bounds = value | ||
self._large_query_breakdown_complexity_bounds = value | ||
|
||
@custom_package_usage_config.setter | ||
@experimental_parameter(version="1.6.0") | ||
def custom_package_usage_config(self, config: Dict) -> None: | ||
self._custom_package_usage_config = {k.lower(): v for k, v in config.items()} | ||
with self._lock: | ||
self._custom_package_usage_config = { | ||
k.lower(): v for k, v in config.items() | ||
} | ||
|
||
def cancel_all(self) -> None: | ||
""" | ||
|
@@ -1458,7 +1495,8 @@ def _get_dependency_packages( | |
statement_params=statement_params, | ||
) | ||
|
||
custom_package_usage_config = self._custom_package_usage_config.copy() | ||
with self._lock: | ||
custom_package_usage_config = self._custom_package_usage_config.copy() | ||
|
||
unsupported_packages: List[str] = [] | ||
for package, package_info in package_dict.items(): | ||
|
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.
is that intended? i think we still need the condition here
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.
yes.
replace_repeated_subquery_with_cte()
does this check automatically.