Repository navigation
Add dedicated InfluxDB 3 sensor #73522
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
Merged
+592
−4
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4c36e6a
Add deferrable InfluxDB 3 sensor
subhramit 11c47a0
Update provider info
subhramit 0a1ccaf
Template InfluxDB sensor connection ID
subhramit 1388108
Document defaults for conn_id and fail_on_empty
subhramit f46dc38
Remove redundant timeout normalization
subhramit 36d1b4f
Align InfluxDB sensor trigger serialization test
subhramit 7577c83
Test InfluxDB sensor pre-deferral empty failure
subhramit 2ce4be1
Test sensor failure after polling
subhramit 19ea095
Align InfluxDB sensor empty-result failure behavior
subhramit 1aaaab9
Simplify sensor scalar validation
subhramit 6a901b5
Asset sensor does not defer on empty failure
subhramit b3238f8
Add seealso
subhramit 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 hidden or 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 hidden or 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,40 @@ | ||
| .. 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. | ||
|
|
||
| .. _howto/sensor:InfluxDB3Sensor: | ||
|
|
||
| InfluxDB3Sensor | ||
| =============== | ||
|
|
||
| Use :class:`~airflow.providers.influxdb.sensors.influxdb3.InfluxDB3Sensor` to wait until an | ||
| InfluxDB 3.x SQL query returns a truthy first cell. Prefer an efficient existence query that returns | ||
| one value and limits the result to one row. | ||
|
|
||
| .. exampleinclude:: /../../influxdb/tests/system/influxdb/example_influxdb3.py | ||
| :language: python | ||
| :start-after: [START howto_sensor_influxdb3] | ||
| :end-before: [END howto_sensor_influxdb3] | ||
|
|
||
| An empty result, a missing value, numeric or string zero, and an empty string are treated as false. | ||
| Set ``fail_on_empty=True`` to fail immediately when the query returns no rows. | ||
|
|
||
| Deferrable mode | ||
| ^^^^^^^^^^^^^^^ | ||
|
|
||
| Set ``deferrable=True`` to release the worker slot between queries. The | ||
| :class:`~airflow.providers.influxdb.triggers.influxdb3.InfluxDB3SensorTrigger` repeats the query | ||
| at the configured ``poke_interval`` until the condition is met or Airflow reaches the sensor timeout. |
This file contains hidden or 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 hidden or 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
16 changes: 16 additions & 0 deletions
16
providers/influxdb/src/airflow/providers/influxdb/sensors/__init__.py
This file contains hidden or 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. |
108 changes: 108 additions & 0 deletions
108
providers/influxdb/src/airflow/providers/influxdb/sensors/influxdb3.py
This file contains hidden or 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,108 @@ | ||
| # 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. | ||
| """Sensor waiting for a SQL query to return a truthy first cell in InfluxDB 3.x.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Sequence | ||
| from datetime import timedelta | ||
| from typing import TYPE_CHECKING, Any | ||
|
|
||
| from airflow.providers.common.compat.sdk import AirflowFailException, BaseSensorOperator, conf | ||
| from airflow.providers.influxdb.hooks.influxdb3 import InfluxDB3Hook | ||
| from airflow.providers.influxdb.triggers.influxdb3 import InfluxDB3SensorTrigger | ||
| from airflow.providers.influxdb.utils import _first_cell_is_truthy | ||
|
|
||
| if TYPE_CHECKING: | ||
| from airflow.sdk.definitions.context import Context | ||
|
|
||
|
|
||
| class InfluxDB3Sensor(BaseSensorOperator): | ||
| """ | ||
| Wait until an InfluxDB 3.x SQL query returns a truthy first cell. | ||
|
|
||
| .. seealso:: | ||
| For more information on how to use this sensor, take a look at the guide: | ||
| :ref:`howto/sensor:InfluxDB3Sensor` | ||
|
|
||
| :param sql: The SQL query to poll. | ||
| :param influxdb3_conn_id: Reference to :ref:`InfluxDB 3 connection id <howto/connection:influxdb3>`. | ||
| Defaults to ``influxdb3_default``. | ||
| :param fail_on_empty: Fail instead of waiting when the query returns no rows. Defaults to ``False``. | ||
| :param deferrable: Run polling in the triggerer. Defaults to the | ||
| ``operators.default_deferrable`` configuration (``False`` if unset). | ||
| """ | ||
|
|
||
| template_fields: Sequence[str] = ("sql", "influxdb3_conn_id") | ||
| template_ext: Sequence[str] = (".sql",) | ||
|
|
||
| def __init__( | ||
| self, | ||
| *, | ||
| sql: str, | ||
| influxdb3_conn_id: str = "influxdb3_default", | ||
| fail_on_empty: bool = False, | ||
| deferrable: bool = conf.getboolean("operators", "default_deferrable", fallback=False), | ||
| **kwargs, | ||
| ) -> None: | ||
| super().__init__(**kwargs) | ||
| self.sql = sql | ||
| self.influxdb3_conn_id = influxdb3_conn_id | ||
| self.fail_on_empty = fail_on_empty | ||
| self.deferrable = deferrable | ||
|
|
||
| def poke(self, context: Context) -> bool: | ||
| """Return whether the query result meets the sensor condition.""" | ||
| self.log.info("Poking with SQL query: %s", self.sql) | ||
| dataframe = InfluxDB3Hook(conn_id=self.influxdb3_conn_id).query(self.sql) | ||
| if dataframe.empty and self.fail_on_empty: | ||
| raise AirflowFailException("No rows returned, raising as per fail_on_empty flag") | ||
|
subhramit marked this conversation as resolved.
|
||
| return _first_cell_is_truthy(dataframe) | ||
|
|
||
| def execute(self, context: Context) -> None: | ||
| if not self.deferrable: | ||
| super().execute(context) | ||
| return | ||
|
|
||
| if self.poke(context): | ||
| return | ||
|
|
||
| self.defer( | ||
| timeout=timedelta(seconds=self.timeout), | ||
| trigger=InfluxDB3SensorTrigger( | ||
| sql=self.sql, | ||
| influxdb3_conn_id=self.influxdb3_conn_id, | ||
| poll_interval=self.poke_interval, | ||
| fail_on_empty=self.fail_on_empty, | ||
| ), | ||
| method_name="execute_complete", | ||
| ) | ||
|
|
||
| def execute_complete(self, context: Context, event: dict[str, Any] | None = None) -> None: | ||
| """Complete after the trigger reports that the condition was met.""" | ||
| if event is None: | ||
| raise RuntimeError("InfluxDB 3 sensor did not return an event") | ||
|
|
||
| status = event.get("status") | ||
| if status == "fail": | ||
| raise AirflowFailException(event.get("message", "InfluxDB 3 sensor failed")) | ||
| if status == "error": | ||
| raise RuntimeError(event.get("message", "InfluxDB 3 sensor failed")) | ||
| if status != "success": | ||
| raise RuntimeError(f"InfluxDB 3 sensor returned unexpected status: {status!r}") | ||
|
subhramit marked this conversation as resolved.
|
||
|
|
||
| self.log.info("InfluxDB 3 sensor condition met") | ||
This file contains hidden or 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 hidden or 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 hidden or 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
16 changes: 16 additions & 0 deletions
16
providers/influxdb/tests/unit/influxdb/sensors/__init__.py
This file contains hidden or 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. |
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.
Uh oh!
There was an error while loading. Please reload this page.