-
Notifications
You must be signed in to change notification settings - Fork 14.1k
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 Deferrable switch to SnowflakeSqlApiOperator #31596
Merged
potiuk
merged 22 commits into
apache:main
from
utkarsharma2:DeferrableSnowflakeSqlApiOperator
Jul 4, 2023
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
34a1be2
Add Deferrable switch to SnowflakeSqlApiOperator
utkarsharma2 52f73e0
Merge branch 'main' into DeferrableSnowflakeSqlApiOperator
utkarsharma2 0063f65
Fix python 3.8 typing issue
utkarsharma2 a6add08
Merge branch 'main' into DeferrableSnowflakeSqlApiOperator
utkarsharma2 9ffd1ef
Update airflow/providers/snowflake/hooks/snowflake_sql_api.py
utkarsharma2 96baa30
Fix static check
utkarsharma2 fc40089
Fix static checks
utkarsharma2 566ffe8
Fix static issue
utkarsharma2 cb6a7c1
Merge branch 'main' into DeferrableSnowflakeSqlApiOperator
utkarsharma2 1149913
Update airflow/providers/snowflake/triggers/snowflake_trigger.py
utkarsharma2 d1f7cf2
Update airflow/providers/snowflake/triggers/snowflake_trigger.py
utkarsharma2 4b5037e
Update airflow/providers/snowflake/triggers/snowflake_trigger.py
utkarsharma2 e8fc37a
Update airflow/providers/snowflake/triggers/snowflake_trigger.py
utkarsharma2 64c72e6
Sync param list
utkarsharma2 f3b13fc
Optimise status polling
utkarsharma2 c5626f3
Merge branch 'main' into DeferrableSnowflakeSqlApiOperator
utkarsharma2 b39b57b
Fix tests
utkarsharma2 f201020
Add return statement post yield
utkarsharma2 bd50387
Merge branch 'main' into DeferrableSnowflakeSqlApiOperator
utkarsharma2 cc3787f
Merge branch 'main' into DeferrableSnowflakeSqlApiOperator
utkarsharma2 23033b1
Update airflow/providers/snowflake/hooks/snowflake_sql_api.py
utkarsharma2 d70a22d
Merge branch 'main' into DeferrableSnowflakeSqlApiOperator
utkarsharma2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you 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. |
109 changes: 109 additions & 0 deletions
109
airflow/providers/snowflake/triggers/snowflake_trigger.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you 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 asyncio | ||
from datetime import timedelta | ||
from typing import Any, AsyncIterator | ||
|
||
from airflow.providers.snowflake.hooks.snowflake_sql_api import SnowflakeSqlApiHook | ||
from airflow.triggers.base import BaseTrigger, TriggerEvent | ||
|
||
|
||
class SnowflakeSqlApiTrigger(BaseTrigger): | ||
""" | ||
Fetch the status for the query ids passed. | ||
|
||
:param poll_interval: polling period in seconds to check for the status | ||
:param query_ids: List of Query ids to run and poll for the status | ||
:param snowflake_conn_id: Reference to Snowflake connection id | ||
:param token_life_time: lifetime of the JWT Token in timedelta | ||
:param token_renewal_delta: Renewal time of the JWT Token in timedelta | ||
""" | ||
|
||
def __init__( | ||
self, | ||
poll_interval: float, | ||
query_ids: list[str], | ||
snowflake_conn_id: str, | ||
token_life_time: timedelta, | ||
token_renewal_delta: timedelta, | ||
): | ||
super().__init__() | ||
self.poll_interval = poll_interval | ||
self.query_ids = query_ids | ||
self.snowflake_conn_id = snowflake_conn_id | ||
self.token_life_time = token_life_time | ||
self.token_renewal_delta = token_renewal_delta | ||
|
||
def serialize(self) -> tuple[str, dict[str, Any]]: | ||
"""Serializes SnowflakeSqlApiTrigger arguments and classpath.""" | ||
return ( | ||
"airflow.providers.snowflake.triggers.snowflake_trigger.SnowflakeSqlApiTrigger", | ||
{ | ||
"poll_interval": self.poll_interval, | ||
"query_ids": self.query_ids, | ||
"snowflake_conn_id": self.snowflake_conn_id, | ||
"token_life_time": self.token_life_time, | ||
"token_renewal_delta": self.token_renewal_delta, | ||
}, | ||
) | ||
|
||
async def run(self) -> AsyncIterator[TriggerEvent]: | ||
"""Wait for the query the snowflake query to complete.""" | ||
SnowflakeSqlApiHook( | ||
self.snowflake_conn_id, | ||
self.token_life_time, | ||
self.token_renewal_delta, | ||
) | ||
try: | ||
statement_query_ids: list[str] = [] | ||
for query_id in self.query_ids: | ||
while True: | ||
statement_status = await self.get_query_status(query_id) | ||
if statement_status["status"] not in ["running"]: | ||
break | ||
await asyncio.sleep(self.poll_interval) | ||
if statement_status["status"] == "error": | ||
yield TriggerEvent(statement_status) | ||
return | ||
if statement_status["status"] == "success": | ||
statement_query_ids.extend(statement_status["statement_handles"]) | ||
yield TriggerEvent( | ||
{ | ||
"status": "success", | ||
"statement_query_ids": statement_query_ids, | ||
} | ||
) | ||
except Exception as e: | ||
yield TriggerEvent({"status": "error", "message": str(e)}) | ||
|
||
async def get_query_status(self, query_id: str) -> dict[str, Any]: | ||
""" | ||
Async function to check whether the query statement submitted via SQL API is still | ||
running state and returns True if it is still running else | ||
return False. | ||
""" | ||
hook = SnowflakeSqlApiHook( | ||
self.snowflake_conn_id, | ||
self.token_life_time, | ||
self.token_renewal_delta, | ||
) | ||
return await hook.get_sql_api_query_status_async(query_id) | ||
|
||
def _set_context(self, context): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 wonder if we should make the response body be fetched lazily, what could take a while (and might actually be the part worth deferring—not sure)
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.
@uranusjr I'm not sure I understand you completely, but based on what I got. The function
get_sql_api_query_status_async
is called indirectly when we are polling for the status of the query within triggered. So deferring it would not be ideal, right?