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-1418523 Make Session thread safe #2312

Merged

Conversation

sfc-gh-aalam
Copy link
Contributor

@sfc-gh-aalam sfc-gh-aalam commented Sep 17, 2024

  1. Which Jira issue is this PR addressing? Make sure that there is an accompanying issue to your PR.

    Fixes SNOW-1418523

  2. Fill out the following pre-review checklist:

    • I am adding a new automated test(s) to verify correctness of my new code
      • If this test skips Local Testing mode, I'm requesting review from @snowflakedb/local-testing
    • I am adding new logging messages
    • I am adding a new telemetry message
    • I am adding new credentials
    • I am adding a new dependency
    • If this is a new feature/behavior, I'm adding the Local Testing parity changes.
    • I acknowledge that I have ensured my changes to be thread-safe.
  3. Please describe how your code solves the related issue.

    This PR is merging back add updates from feature branch to main branch. It includes the following changes:

    1. Core session variables to enable concurrent df operations and queries: SNOW-1418523 make analyzer server connection thread safe #2282
    2. Concurrent Files I/O: SNOW-1418523: concurrent file operations #2288
    3. UDxF/Sproc updates: SNOW-1418523: make udf and sproc registration thread safe #2289
    4. Session parameters and configs: SNOW-1663726 make session config updates thread safe #2302
    5. Temp table cleaner updates: SNOW-1663726 make temp table cleaner thread safe #2309
    6. Telemetry collection: SNOW-1642189: collect telemetry about concurrency usage #2316
    7. Add merge-gate: SNOW-1546090 add merge gate for future thread safe updates #2323

Copy link

Seems like your changes contain some Local Testing changes, please request review from @snowflakedb/local-testing

Copy link

Seems like your changes contain some Local Testing changes, please request review from @snowflakedb/local-testing

Copy link

Seems like your changes contain some Local Testing changes, please request review from @snowflakedb/local-testing

Copy link

github-actions bot commented Oct 2, 2024

Seems like your changes contain some Local Testing changes, please request review from @snowflakedb/local-testing

Copy link

github-actions bot commented Oct 3, 2024

Seems like your changes contain some Local Testing changes, please request review from @snowflakedb/local-testing

Copy link

github-actions bot commented Oct 4, 2024

Seems like your changes contain some Local Testing changes, please request review from @snowflakedb/local-testing

Copy link

github-actions bot commented Oct 4, 2024

Seems like your changes contain some Local Testing changes, please request review from @snowflakedb/local-testing

Copy link

github-actions bot commented Oct 4, 2024

Seems like your changes contain some Local Testing changes, please request review from @snowflakedb/local-testing

Copy link

github-actions bot commented Oct 4, 2024

Seems like your changes contain some Local Testing changes, please request review from @snowflakedb/local-testing

Copy link

github-actions bot commented Oct 4, 2024

Seems like your changes contain some Local Testing changes, please request review from @snowflakedb/local-testing

@sfc-gh-aalam sfc-gh-aalam marked this pull request as ready for review October 4, 2024 21:41
@sfc-gh-aalam sfc-gh-aalam requested review from a team as code owners October 4, 2024 21:41
Copy link

github-actions bot commented Oct 4, 2024

Seems like your changes contain some Local Testing changes, please request review from @snowflakedb/local-testing

Copy link
Contributor

@sfc-gh-jrose sfc-gh-jrose left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For other reviewers: Turning off whitespace comparison simplifies quite a bit.

.github/pull_request_template.md Outdated Show resolved Hide resolved
Copy link

github-actions bot commented Oct 5, 2024

Seems like your changes contain some Local Testing changes, please request review from @snowflakedb/local-testing

Copy link

github-actions bot commented Oct 8, 2024

Seems like your changes contain some Local Testing changes, please request review from @snowflakedb/local-testing

CHANGELOG.md Outdated
@@ -6,6 +6,8 @@

#### New Features

- Updated `Session` class to be thread-safe. This allows concurrent query submission, dataframe operations, UDF and store procedure registration, and concurret file uploads.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: concurret

CHANGELOG.md Outdated
@@ -6,6 +6,8 @@

#### New Features

- Updated `Session` class to be thread-safe. This allows concurrent query submission, dataframe operations, UDF and store procedure registration, and concurret file uploads.
- One limitation is that updating `Session` configurations inside multiple-threads may cause other active thread to break. Please update `Session` configurations before starting multiple threads.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This details isn't needed in the changelog. It should be somewhere else in the doc.

@@ -184,6 +185,15 @@ def __init__(
"_skip_upload_on_content_match" in signature.parameters
)

@property
def _cursor(self) -> SnowflakeCursor:
if not hasattr(self._thread_store, "cursor"):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is hasattr() itself thread-safe?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, hasattr is read-only operation on thread local object.

@@ -38,6 +39,7 @@ class TelemetryField(Enum):
TYPE_PERFORMANCE_DATA = "snowpark_performance_data"
TYPE_FUNCTION_USAGE = "snowpark_function_usage"
TYPE_SESSION_CREATED = "snowpark_session_created"
TYPE_CURSOR_CREATED = "snowpark_cursor_created"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the purpose to know whether multiple cursor is used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this addition is part of collecting telemtry. It will track if a cursor is used in a new thread.

Comment on lines 1241 to 1245
runtime_version = (
f"{sys.version_info[0]}.{sys.version_info[1]}"
if not runtime_version
else runtime_version
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: no need to change. Just for your reference:
runtime_version = runtime_version or f"{sys.version_info[0]}.{sys.version_info[1]}"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep. that's much simpler. I'll update the code.

)
with self._package_lock:
result_dict = (
existing_packages_dict if existing_packages_dict is not None else {}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the .copy() is removed here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original version of this code had this pattern.

result_dict = existing_packages_dict if existing_pacakges_dict else {}
...
for package in dependency_packages:
    name = package.name
    if name in result_dict:
        ...
    else:
        result["name"] = name

I refactored it the wrong way using this pattern

result_dict = existing_packages_dict.copy() if existing_pacakges_dict else {}
...
for package in dependency_packages:
    name = package.name
    if name in result_dict:
        ...
    else:
        result["name"] = name

if existing_packages_dict:
    existing_pacakges_dict.update(result_dict)

I noticed this refactor is not correct as the update step can overwrite updates from a different thread.

So in this change, I undid the refactor and updated the code as follows:

with self._package_lock:
    result_dict = existing_packages_dict if existing_pacakges_dict else {}
    ...
    for package in dependency_packages:
        name = package.name
        if name in result_dict:
            ...
        else:
            result["name"] = name

@sfc-gh-aalam
Copy link
Contributor Author

Copy link

Seems like your changes contain some Local Testing changes, please request review from @snowflakedb/local-testing

Copy link

Seems like your changes contain some Local Testing changes, please request review from @snowflakedb/local-testing

@sfc-gh-aalam sfc-gh-aalam enabled auto-merge (squash) October 14, 2024 21:01
@sfc-gh-aalam sfc-gh-aalam merged commit 7c22750 into main Oct 14, 2024
36 checks passed
@sfc-gh-aalam sfc-gh-aalam deleted the aalam-SNOW-1418523-make-internal-session-variables-thread-safe branch October 14, 2024 21:32
@github-actions github-actions bot locked and limited conversation to collaborators Oct 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants