Skip to content
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 temp table cleaner thread safe #2309

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
56fb566
init
sfc-gh-aalam Sep 11, 2024
66003d1
make udf/sproc related files thread-safe
sfc-gh-aalam Sep 11, 2024
0e58205
Merge branch 'main' into aalam-SNOW-1418523-make-udf-sproc-thread-safe
sfc-gh-aalam Sep 11, 2024
e75dde1
init
sfc-gh-aalam Sep 11, 2024
68a8c1c
make query listener thread-safe
sfc-gh-aalam Sep 11, 2024
31a5734
Fix query_tag and last_action_id
sfc-gh-aalam Sep 11, 2024
b4dadda
core updates done
sfc-gh-aalam Sep 11, 2024
b8c6496
Add tests
sfc-gh-aalam Sep 12, 2024
f39837e
Fix local tests
sfc-gh-aalam Sep 12, 2024
31a196f
Merge branch 'main' into aalam-SNOW-1418523-make-analyzer-server_conn…
sfc-gh-aalam Sep 12, 2024
723bdf7
Merge branch 'aalam-SNOW-1418523-make-internal-session-variables-thre…
sfc-gh-aalam Sep 12, 2024
37c0419
add file IO tests
sfc-gh-aalam Sep 12, 2024
8a2d433
Merge branch 'aalam-SNOW-1418523-concurrent-file-operations' into aal…
sfc-gh-aalam Sep 12, 2024
a083989
make session._runtime_version_from_requirement safe
sfc-gh-aalam Sep 13, 2024
947d384
add sp/udf concurrent tests
sfc-gh-aalam Sep 13, 2024
fd51720
fix broken test
sfc-gh-aalam Sep 13, 2024
3077853
add udtf/udaf tests
sfc-gh-aalam Sep 13, 2024
65c3186
fix broken test
sfc-gh-aalam Sep 13, 2024
94412cf
sql_simplifier, cte_optimization, eliminate_numeric, query_compilatio…
sfc-gh-aalam Sep 13, 2024
638dd09
cover more configs
sfc-gh-aalam Sep 17, 2024
7ae2c33
fix SnowflakePlan copy
sfc-gh-aalam Sep 17, 2024
1689ebf
minor update
sfc-gh-aalam Sep 17, 2024
5e8a2d2
add description
sfc-gh-aalam Sep 17, 2024
e5b3f83
init
sfc-gh-aalam Sep 17, 2024
1c83ef2
use _package_lock to protect Session._packages
sfc-gh-aalam Sep 17, 2024
a649761
undo refactor
sfc-gh-aalam Sep 17, 2024
f03d618
undo refactor
sfc-gh-aalam Sep 17, 2024
5f398d5
fix test
sfc-gh-aalam Sep 17, 2024
3807087
fix test
sfc-gh-aalam Sep 17, 2024
4eef3e9
Merge branch 'aalam-SNOW-1418523-make-internal-session-variables-thre…
sfc-gh-aalam Sep 17, 2024
df3263c
add file IO tests
sfc-gh-aalam Sep 12, 2024
6769c54
merge with base
sfc-gh-aalam Sep 17, 2024
af86f67
merge with base
sfc-gh-aalam Sep 17, 2024
a737f33
fix test
sfc-gh-aalam Sep 17, 2024
9f2c707
merge with base
sfc-gh-aalam Sep 17, 2024
8ca2730
protect complexity bounds setter with lock
sfc-gh-aalam Sep 17, 2024
5c8389b
Merge branch 'aalam-SNOW-1663726-make-session-config-updates-thread-s…
sfc-gh-aalam Sep 18, 2024
39ea350
add tests
sfc-gh-aalam Sep 18, 2024
b616424
fix test
sfc-gh-aalam Sep 18, 2024
c10daf6
fix test
sfc-gh-aalam Sep 18, 2024
81417a3
add config context
sfc-gh-aalam Sep 19, 2024
e340567
add tests
sfc-gh-aalam Sep 19, 2024
30952bb
update documentation
sfc-gh-aalam Sep 20, 2024
03f25b5
use config context in plan compiler
sfc-gh-aalam Sep 20, 2024
6deb402
add comments
sfc-gh-aalam Sep 20, 2024
8e1dfe0
minor refactor
sfc-gh-aalam Sep 20, 2024
10bfeb4
fix test
sfc-gh-aalam Sep 20, 2024
879940a
update documentation
sfc-gh-aalam Sep 20, 2024
5aad2d9
simplify context config
sfc-gh-aalam Sep 25, 2024
669eb91
merge with base
sfc-gh-aalam Sep 25, 2024
a85a144
add config context to repeated subquery elimination resolution stage
sfc-gh-aalam Sep 25, 2024
a79ffb4
fix tests
sfc-gh-aalam Sep 26, 2024
4420350
refactor
sfc-gh-aalam Sep 26, 2024
5f1eaa6
remove do_analyze
sfc-gh-aalam Sep 27, 2024
9d62017
fix
sfc-gh-aalam Sep 27, 2024
b58aa8b
fix
sfc-gh-aalam Sep 27, 2024
db37033
fix
sfc-gh-aalam Sep 27, 2024
dddd15f
fix unit tests
sfc-gh-aalam Sep 27, 2024
57ee9e8
simplify
sfc-gh-aalam Sep 27, 2024
809a86e
simplify
sfc-gh-aalam Sep 27, 2024
6021ab8
simplify
sfc-gh-aalam Sep 27, 2024
43986f6
simplify
sfc-gh-aalam Sep 27, 2024
0430e92
simplify
sfc-gh-aalam Sep 27, 2024
095b04e
remove config context
sfc-gh-aalam Sep 30, 2024
32707f9
min-diff
sfc-gh-aalam Sep 30, 2024
3bf678d
min-diff
sfc-gh-aalam Sep 30, 2024
3eade1a
min-diff
sfc-gh-aalam Sep 30, 2024
1850d5d
Merge branch 'aalam-SNOW-1418523-make-internal-session-variables-thre…
sfc-gh-aalam Oct 2, 2024
1fa6ad2
add warnings
sfc-gh-aalam Oct 2, 2024
095708c
Merge branch 'aalam-SNOW-1663726-make-session-config-updates-thread-s…
sfc-gh-aalam Oct 2, 2024
f2ce7b7
less flaky changes
sfc-gh-aalam Oct 2, 2024
ee79515
Merge branch 'aalam-SNOW-1418523-make-internal-session-variables-thre…
sfc-gh-aalam Oct 4, 2024
20fc937
fix
sfc-gh-aalam Oct 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions src/snowflake/snowpark/_internal/temp_table_auto_cleaner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Copyright (c) 2012-2024 Snowflake Computing Inc. All rights reserved.
#
import logging
import threading
import weakref
from collections import defaultdict
from typing import TYPE_CHECKING, Dict
Expand Down Expand Up @@ -31,9 +32,12 @@ def __init__(self, session: "Session") -> None:
# to its reference count for later temp table management
# this dict will still be maintained even if the cleaner is stopped (`stop()` is called)
self.ref_count_map: Dict[str, int] = defaultdict(int)
# Lock to protect the ref_count_map
self.lock = threading.RLock()

def add(self, table: SnowflakeTable) -> None:
self.ref_count_map[table.name] += 1
with self.lock:
self.ref_count_map[table.name] += 1
# the finalizer will be triggered when it gets garbage collected
# and this table will be dropped finally
_ = weakref.finalize(table, self._delete_ref_count, table.name)
Expand All @@ -43,13 +47,15 @@ def _delete_ref_count(self, name: str) -> None: # pragma: no cover
Decrements the reference count of a temporary table,
and if the count reaches zero, puts this table in the queue for cleanup.
"""
self.ref_count_map[name] -= 1
if self.ref_count_map[name] == 0:
with self.lock:
self.ref_count_map[name] -= 1
current_ref_count = self.ref_count_map[name]
if current_ref_count == 0:
if self.session.auto_clean_up_temp_table_enabled:
self.drop_table(name)
elif self.ref_count_map[name] < 0:
elif current_ref_count < 0:
logging.debug(
f"Unexpected reference count {self.ref_count_map[name]} for table {name}"
f"Unexpected reference count {current_ref_count} for table {name}"
)

def drop_table(self, name: str) -> None: # pragma: no cover
Expand Down Expand Up @@ -89,9 +95,11 @@ def stop(self) -> None:

@property
def num_temp_tables_created(self) -> int:
return len(self.ref_count_map)
with self.lock:
return len(self.ref_count_map)

@property
def num_temp_tables_cleaned(self) -> int:
# TODO SNOW-1662536: we may need a separate counter for the number of tables cleaned when parameter is enabled
return sum(v == 0 for v in self.ref_count_map.values())
with self.lock:
return sum(v == 0 for v in self.ref_count_map.values())
9 changes: 5 additions & 4 deletions src/snowflake/snowpark/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -831,10 +831,11 @@ def auto_clean_up_temp_table_enabled(self, value: bool) -> None:
)

if value in [True, False]:
self._conn._telemetry_client.send_auto_clean_up_temp_table_telemetry(
self._session_id, value
)
self._auto_clean_up_temp_table_enabled = value
with self._lock:
self._conn._telemetry_client.send_auto_clean_up_temp_table_telemetry(
self._session_id, value
)
self._auto_clean_up_temp_table_enabled = value
else:
raise ValueError(
"value for auto_clean_up_temp_table_enabled must be True or False!"
Expand Down
41 changes: 41 additions & 0 deletions tests/integ/test_multithreading.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Copyright (c) 2012-2024 Snowflake Computing Inc. All rights reserved.
#

import gc
import hashlib
import logging
import os
Expand All @@ -14,6 +15,7 @@

from snowflake.snowpark.session import Session
from snowflake.snowpark.types import IntegerType
from tests.integ.test_temp_table_cleanup import wait_for_drop_table_sql_done

try:
import dateutil
Expand Down Expand Up @@ -505,6 +507,45 @@ def finish(self):
executor.submit(register_and_test_udaf, session, i)


@pytest.mark.xfail(
"config.getoption('local_testing_mode', default=False)",
reason="session.sql is not supported in local testing mode",
run=False,
)
def test_auto_temp_table_cleaner(session, caplog):
session._temp_table_auto_cleaner.ref_count_map.clear()
original_auto_clean_up_temp_table_enabled = session.auto_clean_up_temp_table_enabled
session.auto_clean_up_temp_table_enabled = True

def create_temp_table(session_, thread_id):
df = session.sql(f"select {thread_id} as A").cache_result()
table_name = df.table_name
del df
return table_name

with ThreadPoolExecutor(max_workers=10) as executor:
futures = []
table_names = []
for i in range(10):
futures.append(executor.submit(create_temp_table, session, i))

for future in as_completed(futures):
table_names.append(future.result())

gc.collect()
wait_for_drop_table_sql_done(session, caplog, expect_drop=True)

try:
for table_name in table_names:
assert session._temp_table_auto_cleaner.ref_count_map[table_name] == 0
assert session._temp_table_auto_cleaner.num_temp_tables_created == 10
assert session._temp_table_auto_cleaner.num_temp_tables_cleaned == 10
finally:
session.auto_clean_up_temp_table_enabled = (
original_auto_clean_up_temp_table_enabled
)


@pytest.mark.skipif(
IS_LINUX or IS_WINDOWS,
reason="Linux and Windows test show multiple active threads when no threadpool is enabled",
Expand Down
13 changes: 8 additions & 5 deletions tests/integ/test_temp_table_cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,25 @@ def setup(session):
def wait_for_drop_table_sql_done(session: Session, caplog, expect_drop: bool) -> None:
# Loop through captured logs and search for the pattern
pattern = r"Dropping .* with query id ([0-9a-f\-]+)"
matches = []
for record in caplog.records:
match = re.search(pattern, record.message)
if match:
query_id = match.group(1)
break
else:
matches.append(query_id)

if len(matches) == 0:
if expect_drop:
pytest.fail("No drop table sql found in logs")
else:
caplog.clear()
return

caplog.clear()
async_job = session.create_async_job(query_id)
# Wait for the async job to finish
_ = async_job.result()
for query_id in matches:
async_job = session.create_async_job(query_id)
# Wait for the async job to finish
_ = async_job.result()


def test_basic(session, caplog):
Expand Down
Loading